You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ro...@apache.org on 2007/08/21 12:41:17 UTC

svn commit: r568052 - in /jakarta/httpcomponents/httpcore/trunk/module-main/src: main/java/org/apache/http/message/BasicStatusLine.java test/java/org/apache/http/message/TestBasicLineParser.java test/java/org/apache/http/message/TestStatusLine.java

Author: rolandw
Date: Tue Aug 21 03:41:05 2007
New Revision: 568052

URL: http://svn.apache.org/viewvc?rev=568052&view=rev
Log:
removed static parser code from BasicStatusLine, adjusted test cases

Modified:
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicStatusLine.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicLineParser.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestStatusLine.java

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicStatusLine.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicStatusLine.java?rev=568052&r1=568051&r2=568052&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicStatusLine.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicStatusLine.java Tue Aug 21 03:41:05 2007
@@ -37,7 +37,7 @@
 import org.apache.http.StatusLine;
 import org.apache.http.protocol.HTTP;
 import org.apache.http.util.CharArrayBuffer;
-//import org.apache.http.impl.EnglishReasonPhraseCatalog;
+
 
 
 /**
@@ -89,88 +89,6 @@
         this.httpVersion = httpVersion;
         this.statusCode = statusCode;
         this.reasonPhrase = reasonPhrase;
-    }
-
-    /**
-     * Parses the status line returned from the HTTP server.
-     *
-     * @param buffer    the buffer from which to parse
-     * @param indexFrom where to start parsing in the buffer
-     * @param indexTo   where to stop parsing in the buffer
-     * 
-     * @throws HttpException if the status line is invalid
-     */
-    public static StatusLine parse(
-            final CharArrayBuffer buffer, final int indexFrom, final int indexTo) 
-            throws ProtocolException {
-        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();
-        }
-        if (indexFrom > indexTo) {
-            throw new IndexOutOfBoundsException();
-        }
-        try {
-            int i = indexFrom;
-            //handle the HTTP-Version
-            while (HTTP.isWhitespace(buffer.charAt(i))) {
-                i++;
-            }            
-            int blank = buffer.indexOf(' ', i, indexTo);
-            if (blank <= 0) {
-                throw new ProtocolException(
-                        "Unable to parse HTTP-Version from the status line: "
-                        + buffer.substring(indexFrom, indexTo));
-            }
-            HttpVersion ver = BasicHttpVersionFormat.parse(buffer, i, blank);
-
-            i = blank;
-            //advance through spaces
-            while (HTTP.isWhitespace(buffer.charAt(i))) {
-                i++;
-            }            
-
-            //handle the Status-Code
-            blank = buffer.indexOf(' ', i, indexTo);
-            if (blank < 0) {
-                blank = indexTo;
-            }
-            int statusCode = 0;
-            try {
-                statusCode = Integer.parseInt(buffer.substringTrimmed(i, blank));
-            } catch (NumberFormatException e) {
-                throw new ProtocolException(
-                    "Unable to parse status code from status line: " 
-                    + buffer.substring(indexFrom, indexTo));
-            }
-            //handle the Reason-Phrase
-            i = blank;
-            String reasonPhrase = null;
-            if (i < indexTo) {
-                reasonPhrase = buffer.substringTrimmed(i, indexTo);
-            } else {
-                reasonPhrase = "";
-            }
-            return new BasicStatusLine(ver, statusCode, reasonPhrase);
-        } catch (IndexOutOfBoundsException e) {
-            throw new ProtocolException("Invalid status line: " + 
-                    buffer.substring(indexFrom, indexTo)); 
-        }
-    }
-
-    public static final StatusLine parse(final String s)
-            throws ProtocolException {
-        if (s == null) {
-            throw new IllegalArgumentException("String may not be null");
-        }
-        CharArrayBuffer buffer = new CharArrayBuffer(s.length()); 
-        buffer.append(s);
-        return parse(buffer, 0, buffer.length());
     }
 
     // --------------------------------------------------------- Public Methods

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=568052&r1=568051&r2=568052&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 Tue Aug 21 03:41:05 2007
@@ -33,6 +33,7 @@
 import org.apache.http.HttpException;
 import org.apache.http.HttpVersion;
 import org.apache.http.RequestLine;
+import org.apache.http.StatusLine;
 import org.apache.http.message.BasicRequestLine;
 import org.apache.http.util.CharArrayBuffer;
 
@@ -82,7 +83,7 @@
         assertEquals("/stuff", requestline.getUri());
         assertEquals(HttpVersion.HTTP_1_1, requestline.getHttpVersion());
 
-        //this is not strictly valid, but is lienent
+        //this is not strictly valid, but is lenient
         requestline = BasicLineParser.parseRequestLine
             ("\rGET /stuff HTTP/1.1", null);
         assertEquals("GET", requestline.getMethod());
@@ -94,22 +95,30 @@
         try {
             BasicLineParser.parseRequestLine("    ", null);
             fail();
-        } catch (HttpException e) { /* expected */ }
+        } catch (HttpException e) {
+            // expected
+        }
 
         try {
             BasicLineParser.parseRequestLine("  GET", null);
             fail();
-        } catch (HttpException e) { /* expected */ }
+        } catch (HttpException e) {
+            // expected
+        }
 
         try {
             BasicLineParser.parseRequestLine("GET /stuff", null);
             fail();
-        } catch (HttpException e) { /* expected */ }
+        } catch (HttpException e) {
+            // expected
+        }
 
         try {
             BasicLineParser.parseRequestLine("GET/stuff HTTP/1.1", null);
             fail();
-        } catch (HttpException e) { /* expected */ }
+        } catch (HttpException e) {
+            // expected
+        }
     }
 
     public void testRLParseInvalidInput() throws Exception {
@@ -141,6 +150,130 @@
         }
         try {
             BasicLineParser.DEFAULT.parseRequestLine(buffer, 2, 1);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+    }
+
+
+        
+    public void testSLParseSuccess() throws Exception {
+        //typical status line
+        StatusLine statusLine = BasicLineParser.parseStatusLine
+            ("HTTP/1.1 200 OK", null);
+        assertEquals("HTTP/1.1 200 OK", statusLine.toString());
+        assertEquals(HttpVersion.HTTP_1_1, statusLine.getHttpVersion());
+        assertEquals(200, statusLine.getStatusCode());
+        assertEquals("OK", statusLine.getReasonPhrase());
+
+        //status line with multi word reason phrase
+        statusLine = BasicLineParser.parseStatusLine
+            ("HTTP/1.1 404 Not Found", null);
+        assertEquals(404, statusLine.getStatusCode());
+        assertEquals("Not Found", statusLine.getReasonPhrase());
+
+        //reason phrase can be anyting
+        statusLine = BasicLineParser.parseStatusLine
+            ("HTTP/1.1 404 Non Trouve", null);
+        assertEquals("Non Trouve", statusLine.getReasonPhrase());
+
+        //its ok to end with a \n\r
+        statusLine = BasicLineParser.parseStatusLine
+            ("HTTP/1.1 404 Not Found\r\n", null);
+        assertEquals("Not Found", statusLine.getReasonPhrase());
+
+        //this is valid according to the Status-Line BNF
+        statusLine = BasicLineParser.parseStatusLine
+            ("HTTP/1.1 200 ", null);
+        assertEquals(200, statusLine.getStatusCode());
+        assertEquals("", statusLine.getReasonPhrase());
+
+        //this is not strictly valid, but is lenient
+        statusLine = BasicLineParser.parseStatusLine
+            ("HTTP/1.1 200", null);
+        assertEquals(200, statusLine.getStatusCode());
+        assertEquals("", statusLine.getReasonPhrase());
+
+        //this is not strictly valid, but is lenient
+        statusLine = BasicLineParser.parseStatusLine
+            ("HTTP/1.1     200 OK", null);
+        assertEquals(200, statusLine.getStatusCode());
+        assertEquals("OK", statusLine.getReasonPhrase());
+
+        //this is not strictly valid, but is lenient
+        statusLine = BasicLineParser.parseStatusLine
+            ("\rHTTP/1.1 200 OK", null);
+        assertEquals(200, statusLine.getStatusCode());
+        assertEquals("OK", statusLine.getReasonPhrase());
+        assertEquals(HttpVersion.HTTP_1_1, statusLine.getHttpVersion());
+
+        //this is not strictly valid, but is lenient
+        statusLine = BasicLineParser.parseStatusLine
+            ("  HTTP/1.1 200 OK", null);
+        assertEquals(200, statusLine.getStatusCode());
+        assertEquals("OK", statusLine.getReasonPhrase());
+        assertEquals(HttpVersion.HTTP_1_1, statusLine.getHttpVersion());
+    }
+
+    public void testSLParseFailure() throws Exception {
+        try {
+            BasicLineParser.parseStatusLine("xxx 200 OK", null);
+            fail();
+        } catch (HttpException e) {
+            // expected
+        }
+
+        try {
+            BasicLineParser.parseStatusLine("HTTP/1.1 xxx OK", null);
+            fail();
+        } catch (HttpException e) {
+            // expected
+        }
+
+        try {
+            BasicLineParser.parseStatusLine("HTTP/1.1    ", null);
+            fail();
+        } catch (HttpException e) {
+            // expected
+        }
+        try {
+            BasicLineParser.parseStatusLine("HTTP/1.1", null);
+            fail();
+        } catch (HttpException e) {
+            // expected
+        }
+    }
+
+    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

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestStatusLine.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestStatusLine.java?rev=568052&r1=568051&r2=568052&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestStatusLine.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestStatusLine.java Tue Aug 21 03:41:05 2007
@@ -87,111 +87,6 @@
             fail("IllegalArgumentException should have been thrown");
         } catch (IllegalArgumentException e) { /* expected */ }
     }
-        
-    public void testParseSuccess() throws Exception {
-        //typical status line
-        StatusLine statusLine = BasicStatusLine.parse("HTTP/1.1 200 OK");
-        assertEquals("HTTP/1.1 200 OK", statusLine.toString());
-        assertEquals(HttpVersion.HTTP_1_1, statusLine.getHttpVersion());
-        assertEquals(200, statusLine.getStatusCode());
-        assertEquals("OK", statusLine.getReasonPhrase());
-
-        //status line with multi word reason phrase
-        statusLine = BasicStatusLine.parse("HTTP/1.1 404 Not Found");
-        assertEquals(404, statusLine.getStatusCode());
-        assertEquals("Not Found", statusLine.getReasonPhrase());
-
-        //reason phrase can be anyting
-        statusLine = BasicStatusLine.parse("HTTP/1.1 404 Non Trouve");
-        assertEquals("Non Trouve", statusLine.getReasonPhrase());
-
-        //its ok to end with a \n\r
-        statusLine = BasicStatusLine.parse("HTTP/1.1 404 Not Found\r\n");
-        assertEquals("Not Found", statusLine.getReasonPhrase());
-
-        //this is valid according to the Status-Line BNF
-        statusLine = BasicStatusLine.parse("HTTP/1.1 200 ");
-        assertEquals(200, statusLine.getStatusCode());
-        assertEquals("", statusLine.getReasonPhrase());
-
-        //this is not strictly valid, but is lienent
-        statusLine = BasicStatusLine.parse("HTTP/1.1 200");
-        assertEquals(200, statusLine.getStatusCode());
-        assertEquals("", statusLine.getReasonPhrase());
-
-        //this is not strictly valid, but is lienent
-        statusLine = BasicStatusLine.parse("HTTP/1.1     200 OK");
-        assertEquals(200, statusLine.getStatusCode());
-        assertEquals("OK", statusLine.getReasonPhrase());
-
-        //this is not strictly valid, but is lienent
-        statusLine = BasicStatusLine.parse("\rHTTP/1.1 200 OK");
-        assertEquals(200, statusLine.getStatusCode());
-        assertEquals("OK", statusLine.getReasonPhrase());
-        assertEquals(HttpVersion.HTTP_1_1, statusLine.getHttpVersion());
-
-        //this is not strictly valid, but is lienent
-        statusLine = BasicStatusLine.parse("  HTTP/1.1 200 OK");
-        assertEquals(200, statusLine.getStatusCode());
-        assertEquals("OK", statusLine.getReasonPhrase());
-        assertEquals(HttpVersion.HTTP_1_1, statusLine.getHttpVersion());
-    }
-
-    public void testParseFailure() throws Exception {
-        try {
-            BasicStatusLine.parse("xxx 200 OK");
-            fail();
-        } catch (HttpException e) { /* expected */ }
-
-        try {
-            BasicStatusLine.parse("HTTP/1.1 xxx OK");
-            fail();
-        } catch (HttpException e) { /* expected */ }
-
-        try {
-            BasicStatusLine.parse("HTTP/1.1    ");
-            fail();
-        } catch (HttpException e) { /* expected */ }
-        try {
-            BasicStatusLine.parse("HTTP/1.1");
-            fail();
-        } catch (HttpException e) { /* expected */ }
-    }
-
-    public void testParseInvalidInput() throws Exception {
-        CharArrayBuffer buffer = new CharArrayBuffer(32);
-        buffer.append("HTTP/1.1 200 OK");
-        try {
-            BasicStatusLine.parse(null, 0, 0);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        try {
-            BasicStatusLine.parse(null);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        try {
-            BasicStatusLine.parse(buffer, -1, 0);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-        try {
-            BasicStatusLine.parse(buffer, 0, 1000);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-        try {
-            BasicStatusLine.parse(buffer, 2, 1);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-    }
 
     public void testToString() throws Exception {
         StatusLine statusline = new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");