hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r589374 - in /jakarta/httpcomponents/httpcore/trunk: ./ module-main/src/main/java/org/apache/http/impl/io/ module-main/src/main/java/org/apache/http/message/ module-main/src/test/java/org/apache/http/message/ module-nio/src/main/java/org/ap...
Date Sun, 28 Oct 2007 16:25:08 GMT
Author: olegk
Date: Sun Oct 28 09:25:07 2007
New Revision: 589374

URL: http://svn.apache.org/viewvc?rev=589374&view=rev
Log:
HTTPCORE-126: Improved HTTP message parsing
* Ported ProtocolVersion, RequestLine and StatusLine parsing code to the new API

Modified:
    jakarta/httpcomponents/httpcore/trunk/RELEASE_NOTES.txt
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpRequestParser.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpResponseParser.java
    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
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/ParserCursor.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicLineParser.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/HttpRequestParser.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/HttpResponseParser.java

Modified: jakarta/httpcomponents/httpcore/trunk/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/RELEASE_NOTES.txt?rev=589374&r1=589373&r2=589374&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/RELEASE_NOTES.txt (original)
+++ jakarta/httpcomponents/httpcore/trunk/RELEASE_NOTES.txt Sun Oct 28 09:25:07 2007
@@ -1,5 +1,9 @@
 Changes since 4.0 Alpha 6
 
+* [HTTPCORE-126] Improved HTTP message parsing API and optimized parser
+  implementations. 
+  Contributed by Oleg Kalnichevski <olegk at apache.org>
+
 * Do not include "Connection: close" to 500 responses per default. 
   Contributed by Oleg Kalnichevski <olegk at apache.org>
 

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpRequestParser.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpRequestParser.java?rev=589374&r1=589373&r2=589374&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpRequestParser.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpRequestParser.java
Sun Oct 28 09:25:07 2007
@@ -41,6 +41,7 @@
 import org.apache.http.ParseException;
 import org.apache.http.io.SessionInputBuffer;
 import org.apache.http.message.LineParser;
+import org.apache.http.message.ParserCursor;
 import org.apache.http.params.HttpParams;
 import org.apache.http.util.CharArrayBuffer;
 
@@ -71,7 +72,8 @@
         if (i == -1) {
             throw new ConnectionClosedException("Client closed connection"); 
         }
-        RequestLine requestline = lineParser.parseRequestLine(this.lineBuf, 0, this.lineBuf.length());
+        ParserCursor cursor = new ParserCursor(0, this.lineBuf.length());
+        RequestLine requestline = this.lineParser.parseRequestLine(this.lineBuf, cursor);
         return this.requestFactory.newHttpRequest(requestline);
     }
     

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpResponseParser.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpResponseParser.java?rev=589374&r1=589373&r2=589374&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpResponseParser.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpResponseParser.java
Sun Oct 28 09:25:07 2007
@@ -41,6 +41,7 @@
 import org.apache.http.ParseException;
 import org.apache.http.io.SessionInputBuffer;
 import org.apache.http.message.LineParser;
+import org.apache.http.message.ParserCursor;
 import org.apache.http.params.HttpParams;
 import org.apache.http.util.CharArrayBuffer;
 
@@ -72,7 +73,8 @@
             throw new NoHttpResponseException("The target server failed to respond");
         }
         //create the status line from the status string
-        StatusLine statusline = lineParser.parseStatusLine(this.lineBuf, 0, this.lineBuf.length());
+        ParserCursor cursor = new ParserCursor(0, this.lineBuf.length());
+        StatusLine statusline = lineParser.parseStatusLine(this.lineBuf, cursor);
         return this.responseFactory.newHttpResponse(statusline, null);
     }
 

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=589374&r1=589373&r2=589374&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
Sun Oct 28 09:25:07 2007
@@ -41,7 +41,6 @@
 import org.apache.http.util.CharArrayBuffer;
 
 
-
 /**
  * Basic parser for lines in the head section of an HTTP message.
  * There are individual methods for parsing a request line, a
@@ -120,35 +119,33 @@
 
         CharArrayBuffer buffer = new CharArrayBuffer(value.length());
         buffer.append(value);
-        return parser.parseProtocolVersion(buffer, 0, buffer.length());
+        ParserCursor cursor = new ParserCursor(0, value.length());
+        return parser.parseProtocolVersion(buffer, cursor);
     }
 
 
     // non-javadoc, see interface LineParser
     public ProtocolVersion parseProtocolVersion(final CharArrayBuffer buffer,
-                                                final int indexFrom,
-                                                final int indexTo) 
+                                                final ParserCursor cursor) 
         throws ParseException {
 
         if (buffer == null) {
-            throw new IllegalArgumentException
-                ("Char array buffer may not be null");
-        }
-        if (indexFrom < 0) {
-            throw new IndexOutOfBoundsException();
-        }
-        if (indexTo > buffer.length()) {
-            throw new IndexOutOfBoundsException();
+            throw new IllegalArgumentException("Char array buffer may not be null");
         }
-        if (indexFrom > indexTo) {
-            throw new IndexOutOfBoundsException();
+        if (cursor == null) {
+            throw new IllegalArgumentException("Parser cursor may not be null");
         }
 
-
         final String protoname = this.protocol.getProtocol();
         final int protolength  = protoname.length();
 
-        int i = skipWhitespace(buffer, indexFrom);
+        int indexFrom = cursor.getPos();
+        int indexTo = cursor.getUpperBound();
+        
+        skipWhitespace(buffer, cursor);
+
+        int i = cursor.getPos();
+        
         // long enough for "HTTP/1.1"?
         if (i + protolength + 4 > indexTo) {
             throw new ParseException
@@ -172,13 +169,12 @@
 
         i += protolength+1;
 
-        final int period = buffer.indexOf('.', i, indexTo);
+        int period = buffer.indexOf('.', i, indexTo);
         if (period == -1) {
             throw new ParseException
                 ("Invalid protocol version number: " + 
                  buffer.substring(indexFrom, indexTo));
         }
-
         int major;
         try {
             major = Integer.parseInt(buffer.substringTrimmed(i, period)); 
@@ -187,16 +183,22 @@
                 ("Invalid protocol major version number: " + 
                  buffer.substring(indexFrom, indexTo));
         }
-
+        i = period + 1;
+        
+        int blank = buffer.indexOf(' ', i, indexTo);
+        if (blank == -1) {
+            blank = indexTo;
+        }
         int minor;
         try {
-            minor = Integer.parseInt(buffer.substringTrimmed(period + 1,
-                                                             indexTo)); 
+            minor = Integer.parseInt(buffer.substringTrimmed(i, blank)); 
         } catch (NumberFormatException e) {
             throw new ParseException(
                 "Invalid protocol minor version number: " + 
                 buffer.substring(indexFrom, indexTo));
         }
+        
+        cursor.updatePos(blank);
 
         return createProtocolVersion(major, minor);
 
@@ -220,15 +222,15 @@
 
     // non-javadoc, see interface LineParser
     public boolean hasProtocolVersion(final CharArrayBuffer buffer,
-                                      int index) {
+                                      final ParserCursor cursor) {
+
         if (buffer == null) {
-            throw new IllegalArgumentException
-                ("Char array buffer may not be null");
+            throw new IllegalArgumentException("Char array buffer may not be null");
         }
-        if (index >= buffer.length()) {
-            throw new IndexOutOfBoundsException();
+        if (cursor == null) {
+            throw new IllegalArgumentException("Parser cursor may not be null");
         }
-
+        int index = cursor.getPos();
 
         final String protoname = this.protocol.getProtocol();
         final int  protolength = protoname.length();
@@ -242,8 +244,10 @@
             index = buffer.length() -4 -protolength;
         } else if (index == 0) {
             // beginning of line, tolerate leading whitespace
-            index = skipWhitespace(buffer, index);
-
+            while ((index < buffer.length()) &&
+                    HTTP.isWhitespace(buffer.charAt(index))) {
+                 index++;
+             }
         } // else within line, don't tolerate whitespace
 
 
@@ -280,7 +284,8 @@
 
         CharArrayBuffer buffer = new CharArrayBuffer(value.length());
         buffer.append(value);
-        return parser.parseRequestLine(buffer, 0, buffer.length());
+        ParserCursor cursor = new ParserCursor(0, value.length());
+        return parser.parseRequestLine(buffer, cursor);
     }
 
 
@@ -294,41 +299,50 @@
      * @throws ParseException        in case of a parse error
      */
     public RequestLine parseRequestLine(final CharArrayBuffer buffer,
-                                        final int indexFrom,
-                                        final int indexTo)
+                                        final ParserCursor cursor)
         throws ParseException {
 
         if (buffer == null) {
-            throw new IllegalArgumentException
-                ("Char array buffer may not be null");
-        }
-        if (indexFrom < 0) {
-            throw new IndexOutOfBoundsException();
-        }
-        if (indexTo > buffer.length()) {
-            throw new IndexOutOfBoundsException();
+            throw new IllegalArgumentException("Char array buffer may not be null");
         }
-        if (indexFrom > indexTo) {
-            throw new IndexOutOfBoundsException();
+        if (cursor == null) {
+            throw new IllegalArgumentException("Parser cursor may not be null");
         }
 
+        int indexFrom = cursor.getPos();
+        int indexTo = cursor.getUpperBound();
+        
         try {
-            int i = skipWhitespace(buffer, indexFrom);
+            skipWhitespace(buffer, cursor);
+            int i = cursor.getPos();
+            
             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);
+            cursor.updatePos(blank);
+
+            skipWhitespace(buffer, cursor);
+            i = cursor.getPos();
 
-            i = skipWhitespace(buffer, blank);
             blank = buffer.indexOf(' ', i, indexTo);
             if (blank < 0) {
                 throw new ParseException("Invalid request line: " + 
                         buffer.substring(indexFrom, indexTo));
             }
             String uri = buffer.substringTrimmed(i, blank);
-            ProtocolVersion ver = parseProtocolVersion(buffer, blank, indexTo);
+            cursor.updatePos(blank);
+
+            ProtocolVersion ver = parseProtocolVersion(buffer, cursor);
+            
+            skipWhitespace(buffer, cursor);
+            if (!cursor.atEnd()) {
+                throw new ParseException("Invalid request line: " + 
+                        buffer.substring(indexFrom, indexTo));
+            }
+            
             return createRequestLine(method, uri, ver);
         } catch (IndexOutOfBoundsException e) {
             throw new ParseException("Invalid request line: " + 
@@ -370,44 +384,35 @@
 
         CharArrayBuffer buffer = new CharArrayBuffer(value.length());
         buffer.append(value);
-        return parser.parseStatusLine(buffer, 0, buffer.length());
+        ParserCursor cursor = new ParserCursor(0, value.length());
+        return parser.parseStatusLine(buffer, cursor);
     }
 
 
     // non-javadoc, see interface LineParser
     public StatusLine parseStatusLine(final CharArrayBuffer buffer,
-                                      final int indexFrom,
-                                      final int indexTo) 
+                                      final ParserCursor cursor) 
         throws ParseException {
 
         if (buffer == null) {
-            throw new IllegalArgumentException
-                ("Char array buffer may not be null");
-        }
-        if (indexFrom < 0) {
-            throw new IndexOutOfBoundsException();
-        }
-        if (indexTo > buffer.length()) {
-            throw new IndexOutOfBoundsException();
+            throw new IllegalArgumentException("Char array buffer may not be null");
         }
-        if (indexFrom > indexTo) {
-            throw new IndexOutOfBoundsException();
+        if (cursor == null) {
+            throw new IllegalArgumentException("Parser cursor may not be null");
         }
 
+        int indexFrom = cursor.getPos();
+        int indexTo = cursor.getUpperBound();
+        
         try {
             // handle the HTTP-Version
-            int i = skipWhitespace(buffer, indexFrom);
-            int blank = buffer.indexOf(' ', i, indexTo);
-            if (blank <= 0) {
-                throw new ParseException(
-                        "Unable to parse HTTP-Version from the status line: "
-                        + buffer.substring(indexFrom, indexTo));
-            }
-            ProtocolVersion ver = parseProtocolVersion(buffer, i, blank);
+            ProtocolVersion ver = parseProtocolVersion(buffer, cursor);
 
             // handle the Status-Code
-            i = skipWhitespace(buffer, blank);
-            blank = buffer.indexOf(' ', i, indexTo);
+            skipWhitespace(buffer, cursor);
+            int i = cursor.getPos();
+            
+            int blank = buffer.indexOf(' ', i, indexTo);
             if (blank < 0) {
                 blank = indexTo;
             }
@@ -497,21 +502,15 @@
 
     /**
      * 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(final CharArrayBuffer buffer, int index) {
-        while ((index < buffer.length()) &&
-               HTTP.isWhitespace(buffer.charAt(index))) {
-            index++;
+    protected void skipWhitespace(final CharArrayBuffer buffer, final ParserCursor cursor)
{
+        int pos = cursor.getPos();
+        int indexTo = cursor.getUpperBound();
+        while ((pos < indexTo) &&
+               HTTP.isWhitespace(buffer.charAt(pos))) {
+            pos++;
         }
-        return index;
+        cursor.updatePos(pos);
     }
-
 
 } // class BasicLineParser

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=589374&r1=589373&r2=589374&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
Sun Oct 28 09:25:07 2007
@@ -65,16 +65,16 @@
      * as well as status lines (first element).
      *
      * @param buffer    a buffer holding the protocol version to parse
+     * @param cursor    the parser cursor containing the current position and 
+     *                  the bounds within the buffer for the parsing operation
      * 
      * @return  the parsed protocol version
      *
      * @throws ParseException        in case of a parse error
      */
-    ProtocolVersion parseProtocolVersion(CharArrayBuffer buffer,
-                                         int indexFrom,
-                                         int indexTo) 
-        throws ParseException
-        ;
+    ProtocolVersion parseProtocolVersion(
+            CharArrayBuffer buffer,
+            ParserCursor cursor) throws ParseException;
 
 
     /**
@@ -87,7 +87,7 @@
      * 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
+     * @param cursor    the cursor 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.
@@ -96,40 +96,41 @@
      *          argument index (possibly ignoring whitespace),
      *          <code>false</code> otherwise
      */
-    boolean hasProtocolVersion(CharArrayBuffer buffer, int index)
-        ;
+    boolean hasProtocolVersion(
+            CharArrayBuffer buffer, 
+            ParserCursor cursor);
 
 
     /**
      * Parses a request line.
      *
      * @param buffer    a buffer holding the line to parse
+     * @param cursor    the parser cursor containing the current position and 
+     *                  the bounds within the buffer for the parsing operation
      *
      * @return  the parsed request line
      *
      * @throws ParseException        in case of a parse error
      */
-    RequestLine parseRequestLine(CharArrayBuffer buffer,
-                                 int indexFrom,
-                                 int indexTo)
-        throws ParseException
-        ;
+    RequestLine parseRequestLine(
+            CharArrayBuffer buffer,
+            ParserCursor cursor) throws ParseException;
 
 
     /**
      * Parses a status line.
      *
      * @param buffer    a buffer holding the line to parse
+     * @param cursor    the parser cursor containing the current position and 
+     *                  the bounds within the buffer for the parsing operation
      *
      * @return  the parsed status line
      *
      * @throws ParseException        in case of a parse error
      */
-    StatusLine parseStatusLine(CharArrayBuffer buffer,
-                               int indexFrom,
-                               int indexTo) 
-        throws ParseException
-        ;
+    StatusLine parseStatusLine(
+            CharArrayBuffer buffer,
+            ParserCursor cursor) throws ParseException;
 
 
     /**

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/ParserCursor.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/ParserCursor.java?rev=589374&r1=589373&r2=589374&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/ParserCursor.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/ParserCursor.java
Sun Oct 28 09:25:07 2007
@@ -31,6 +31,8 @@
 
 package org.apache.http.message;
 
+import org.apache.http.util.CharArrayBuffer;
+
 /**
  * This class represents a context of a parsing operation: 
  * <ul>
@@ -83,6 +85,18 @@
     
     public boolean atEnd() {
         return this.pos >= this.upperBound;
+    }
+
+    public String toString() {
+        CharArrayBuffer buffer = new CharArrayBuffer(16);
+        buffer.append('[');
+        buffer.append(Integer.toString(this.lowerBound));
+        buffer.append('>');
+        buffer.append(Integer.toString(this.pos));
+        buffer.append('>');
+        buffer.append(Integer.toString(this.upperBound));
+        buffer.append(']');
+        return buffer.toString();
     }
     
 }

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicLineParser.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicLineParser.java?rev=589374&r1=589373&r2=589374&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicLineParser.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicLineParser.java
Sun Oct 28 09:25:07 2007
@@ -120,45 +120,15 @@
         } catch (ParseException e) {
             // expected
         }
-    }
 
-    public void testRLParseInvalidInput() throws Exception {
-        CharArrayBuffer buffer = new CharArrayBuffer(32);
-        buffer.append("GET /stuff HTTP/1.1");
-        try {
-            BasicLineParser.parseRequestLine(null, null);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        try {
-            BasicLineParser.DEFAULT.parseRequestLine(null, 0, 0);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        try {
-            BasicLineParser.DEFAULT.parseRequestLine(buffer, -1, 0);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-        try {
-            BasicLineParser.DEFAULT.parseRequestLine(buffer, 0, 1000);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
         try {
-            BasicLineParser.DEFAULT.parseRequestLine(buffer, 2, 1);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
+            BasicLineParser.parseRequestLine("GET /stuff HTTP/1.1 Oooooooooooppsie", null);
+            fail();
+        } catch (ParseException e) {
             // expected
         }
     }
 
-
-        
     public void testSLParseSuccess() throws Exception {
         //typical status line
         StatusLine statusLine = BasicLineParser.parseStatusLine
@@ -246,43 +216,6 @@
         }
     }
 
-    public void testSLParseInvalidInput() throws Exception {
-        CharArrayBuffer buffer = new CharArrayBuffer(32);
-        buffer.append("HTTP/1.1 200 OK");
-        try {
-            BasicLineParser.parseStatusLine(null, null);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        try {
-            BasicLineParser.DEFAULT.parseStatusLine(null, 0, 0);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        try {
-            BasicLineParser.DEFAULT.parseStatusLine(buffer, -1, 0);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-        try {
-            BasicLineParser.DEFAULT.parseStatusLine(buffer, 0, 1000);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-        try {
-            BasicLineParser.DEFAULT.parseStatusLine(buffer, 2, 1);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-    }
-
-
-    
     public void testHttpVersionParsing() throws Exception {
 
         String s = "HTTP/1.1";
@@ -302,9 +235,41 @@
         assertEquals("HTTP version number", s, version.toString());
     }
 
+    public void testHttpVersionParsingUsingCursor() throws Exception {
+
+        String s = "HTTP/1.1";
+        CharArrayBuffer buffer = new CharArrayBuffer(16);
+        buffer.append(s);
+        ParserCursor cursor = new ParserCursor(0, s.length());
+        
+        LineParser parser = BasicLineParser.DEFAULT;
+        
+        HttpVersion version = (HttpVersion) parser.parseProtocolVersion(buffer, cursor);
+        assertEquals("HTTP protocol name", "HTTP", version.getProtocol());
+        assertEquals("HTTP major version number", 1, version.getMajor());
+        assertEquals("HTTP minor version number", 1, version.getMinor());
+        assertEquals("HTTP version number", "HTTP/1.1", version.toString());
+        assertEquals(s.length(), cursor.getPos());
+        assertTrue(cursor.atEnd());
+        
+        s = "HTTP/1.123 123";
+        buffer = new CharArrayBuffer(16);
+        buffer.append(s);
+        cursor = new ParserCursor(0, s.length());
+        
+        version = (HttpVersion) parser.parseProtocolVersion(buffer, cursor);
+        assertEquals("HTTP protocol name", "HTTP", version.getProtocol());
+        assertEquals("HTTP major version number", 1, version.getMajor());
+        assertEquals("HTTP minor version number", 123, version.getMinor());
+        assertEquals("HTTP version number", "HTTP/1.123", version.toString());
+        assertEquals(' ', buffer.charAt(cursor.getPos()));
+        assertEquals(s.length() - 4, cursor.getPos());
+        assertFalse(cursor.atEnd());
+    }
+
     public void testInvalidHttpVersionParsing() throws Exception {
         try {
-            BasicLineParser.parseProtocolVersion(null, null);
+            BasicLineParser.parseProtocolVersion((String)null, null);
             fail("IllegalArgumentException should have been thrown");
         } catch (IllegalArgumentException e) {
             //expected
@@ -360,13 +325,6 @@
         }
         try {
             BasicLineParser.parseProtocolVersion
-                ("HTTP/1.1 crap", null);
-            fail("ParseException should have been thrown");
-        } catch (ParseException e) {
-            //expected
-        }
-        try {
-            BasicLineParser.parseProtocolVersion
                 ("HTTP/whatever.whatever whatever", null);
             fail("ParseException should have been thrown");
         } catch (ParseException e) {
@@ -381,39 +339,4 @@
         }
     }
 
-    public void testHttpVersionParsingInvalidInput() throws Exception {
-        CharArrayBuffer buffer = new CharArrayBuffer(32);
-        buffer.append("HTTP/1.1");
-        try {
-            BasicLineParser.parseProtocolVersion(null, null);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        try {
-            BasicLineParser.DEFAULT.parseProtocolVersion(null, 0, 0);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        try {
-            BasicLineParser.DEFAULT.parseProtocolVersion(buffer, -1, 0);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-        try {
-            BasicLineParser.DEFAULT.parseProtocolVersion(buffer, 0, 1000);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-        try {
-            BasicLineParser.DEFAULT.parseProtocolVersion(buffer, 2, 1);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-    }
-    
 }

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/HttpRequestParser.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/HttpRequestParser.java?rev=589374&r1=589373&r2=589374&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/HttpRequestParser.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/HttpRequestParser.java
Sun Oct 28 09:25:07 2007
@@ -37,6 +37,7 @@
 import org.apache.http.RequestLine;
 import org.apache.http.ParseException;
 import org.apache.http.message.LineParser;
+import org.apache.http.message.ParserCursor;
 import org.apache.http.nio.reactor.SessionInputBuffer;
 import org.apache.http.params.HttpParams;
 import org.apache.http.util.CharArrayBuffer;
@@ -59,7 +60,8 @@
 
     protected HttpMessage createMessage(final CharArrayBuffer buffer) 
             throws HttpException, ParseException {
-        RequestLine requestLine = lineParser.parseRequestLine(buffer, 0, buffer.length());
+        ParserCursor cursor = new ParserCursor(0, buffer.length());
+        RequestLine requestLine = lineParser.parseRequestLine(buffer, cursor);
         return this.requestFactory.newHttpRequest(requestLine);
     }
 

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/HttpResponseParser.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/HttpResponseParser.java?rev=589374&r1=589373&r2=589374&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/HttpResponseParser.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/HttpResponseParser.java
Sun Oct 28 09:25:07 2007
@@ -37,6 +37,7 @@
 import org.apache.http.StatusLine;
 import org.apache.http.ParseException;
 import org.apache.http.message.LineParser;
+import org.apache.http.message.ParserCursor;
 import org.apache.http.nio.reactor.SessionInputBuffer;
 import org.apache.http.params.HttpParams;
 import org.apache.http.util.CharArrayBuffer;
@@ -59,7 +60,8 @@
 
     protected HttpMessage createMessage(final CharArrayBuffer buffer) 
             throws HttpException, ParseException {
-        StatusLine statusline = lineParser.parseStatusLine(buffer, 0, buffer.length());
+        ParserCursor cursor = new ParserCursor(0, buffer.length());
+        StatusLine statusline = lineParser.parseStatusLine(buffer, cursor);
         return this.responseFactory.newHttpResponse(statusline, null);
     }
 



Mime
View raw message