hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rola...@apache.org
Subject svn commit: r571582 - in /jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message: BasicLineParser.java LineParser.java
Date Fri, 31 Aug 2007 21:05:10 GMT
Author: rolandw
Date: Fri Aug 31 14:05:09 2007
New Revision: 571582

URL: http://svn.apache.org/viewvc?rev=571582&view=rev
Log:
moved startsWithHTTP to LineParser, introduced skipWhitespace in BasicLineParser

Modified:
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicLineParser.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/LineParser.java

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicLineParser.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicLineParser.java?rev=571582&r1=571581&r2=571582&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicLineParser.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicLineParser.java
Fri Aug 31 14:05:09 2007
@@ -118,10 +118,8 @@
         try {
             int major, minor;
 
-            int i = indexFrom;
-            while (HTTP.isWhitespace(buffer.charAt(i))) {
-                i++;
-            }            
+            int i = skipWhitespace(buffer, indexFrom);
+
             if (buffer.charAt(i    ) != 'H' 
              || buffer.charAt(i + 1) != 'T'
              || buffer.charAt(i + 2) != 'T'
@@ -174,6 +172,42 @@
 
 
 
+    // non-javadoc, see interface LineParser
+    public boolean hasProtocolVersion(final CharArrayBuffer buffer,
+                                      int index) {
+        if (buffer == null) {
+            throw new IllegalArgumentException
+                ("Char array buffer may not be null");
+        }
+        if (index >= buffer.length()) {
+            throw new IndexOutOfBoundsException();
+        }
+
+        if (buffer.length() < 8)
+            return false; // not long enough for "HTTP/1.1"
+
+        if (index < 0) {
+            // end of line, no tolerance for trailing whitespace
+            index = buffer.length()-8;
+        } else if (index == 0) {
+            // beginning of line, tolerate leading whitespace
+            index = skipWhitespace(buffer, index);
+
+        } // else within line, don't tolerate whitespace
+
+
+        if (index + 8 > buffer.length())
+            return false;
+
+        // just check for the protocol name, no need to analyse the version
+        return buffer.charAt(index    ) == 'H' 
+            && buffer.charAt(index + 1) == 'T'
+            && buffer.charAt(index + 2) == 'T'
+            && buffer.charAt(index + 3) == 'P';
+    }
+
+
+
     public final static
         RequestLine parseRequestLine(String value,
                                      LineParser parser)
@@ -222,20 +256,15 @@
         }
 
         try {
-            int i = indexFrom;
-            while (HTTP.isWhitespace(buffer.charAt(i))) {
-                i++;
-            }
+            int i = skipWhitespace(buffer, indexFrom);
             int blank = buffer.indexOf(' ', i, indexTo);
             if (blank < 0) {
                 throw new ParseException("Invalid request line: " + 
                         buffer.substring(indexFrom, indexTo));
             }
             String method = buffer.substringTrimmed(i, blank);
-            i = blank;
-            while (HTTP.isWhitespace(buffer.charAt(i))) {
-                i++;
-            }
+
+            i = skipWhitespace(buffer, blank);
             blank = buffer.indexOf(' ', i, indexTo);
             if (blank < 0) {
                 throw new ParseException("Invalid request line: " + 
@@ -309,11 +338,8 @@
         }
 
         try {
-            int i = indexFrom;
-            //handle the HTTP-Version
-            while (HTTP.isWhitespace(buffer.charAt(i))) {
-                i++;
-            }            
+            // handle the HTTP-Version
+            int i = skipWhitespace(buffer, indexFrom);
             int blank = buffer.indexOf(' ', i, indexTo);
             if (blank <= 0) {
                 throw new ParseException(
@@ -322,13 +348,8 @@
             }
             HttpVersion ver = parseProtocolVersion(buffer, i, blank);
 
-            i = blank;
-            //advance through spaces
-            while (HTTP.isWhitespace(buffer.charAt(i))) {
-                i++;
-            }            
-
-            //handle the Status-Code
+            // handle the Status-Code
+            i = skipWhitespace(buffer, blank);
             blank = buffer.indexOf(' ', i, indexTo);
             if (blank < 0) {
                 blank = indexTo;
@@ -413,6 +434,24 @@
      */
     protected HeaderValueParser getHeaderValueParser() {
         return null;
+    }
+
+
+    /**
+     * Helper to skip whitespace.
+     *
+     * @param buffer    the buffer in which to skip whitespace
+     * @param index     the index at which to start skipping
+     *
+     * @return  the index after the whitespace. This is the argument index
+     *          if there was no whitespace. It is the end of the buffer if
+     *          the rest of the line is whitespace.
+     */
+    protected int skipWhitespace(CharArrayBuffer buffer, int index) {
+        while (HTTP.isWhitespace(buffer.charAt(index))) {
+            index++;
+        }
+        return index;
     }
 
 

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/LineParser.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/LineParser.java?rev=571582&r1=571581&r2=571582&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/LineParser.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/LineParser.java
Fri Aug 31 14:05:09 2007
@@ -78,6 +78,29 @@
 
 
     /**
+     * Checks whether there likely is a protocol version in a line.
+     * This method implements a <i>heuristic</i> to check for a
+     * likely protocol version specification. It does <i>not</i>
+     * guarantee that {@link #parseProtocolVersion} would not
+     * detect a parse error.
+     * This can be used to detect garbage lines before a request
+     * or status line.
+     *
+     * @param buffer    a buffer holding the line to inspect
+     * @param index     the index at which to check for a protocol version, or
+     *                  negative for "end of line". Whether the check tolerates
+     *                  whitespace before or after the protocol version is
+     *                  implementation dependent.
+     *
+     * @return  <code>true</code> if there is a protocol version at the
+     *          argument index (possibly ignoring whitespace),
+     *          <code>false</code> otherwise
+     */
+    boolean hasProtocolVersion(CharArrayBuffer buffer, int index)
+        ;
+
+
+    /**
      * Parses a request line.
      *
      * @param buffer    a buffer holding the line to parse



Mime
View raw message