hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r349036 - in /jakarta/httpcomponents/trunk/http-core/src: java/org/apache/http/ java/org/apache/http/impl/ test/org/apache/http/
Date Fri, 25 Nov 2005 23:25:35 GMT
Author: olegk
Date: Fri Nov 25 15:25:13 2005
New Revision: 349036

URL: http://svn.apache.org/viewcvs?rev=349036&view=rev
Log:
StatusLine, RequestLine and HttpVersion parsing code refactored to make use of CharArrayBuffer

Modified:
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HeaderElement.java
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpVersion.java
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/RequestLine.java
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/StatusLine.java
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpClientConnection.java
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpServerConnection.java
    jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHttpVersion.java
    jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestRequestLine.java
    jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestStatusLine.java

Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HeaderElement.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HeaderElement.java?rev=349036&r1=349035&r2=349036&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HeaderElement.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HeaderElement.java Fri
Nov 25 15:25:13 2005
@@ -163,7 +163,8 @@
      * 
      * @since 3.0
      */
-    public static final HeaderElement[] parseElements(final CharArrayBuffer buffer, int indexFrom,
int indexTo) {
+    public static final HeaderElement[] parseElements(
+            final CharArrayBuffer buffer, final int indexFrom, final int indexTo) {
         if (buffer == null) {
             throw new IllegalArgumentException("Char array buffer may not be null");
         }
@@ -177,24 +178,25 @@
             throw new IndexOutOfBoundsException();
         }
         List elements = new ArrayList(); 
-        int i = indexFrom;
+        int cur = indexFrom;
+        int from = indexFrom;
         boolean qouted = false;
-        while (i < indexTo) {
-            char ch = buffer.charAt(i);
+        while (cur < indexTo) {
+            char ch = buffer.charAt(cur);
             if (ch == '"') {
                 qouted = !qouted;
             }
             HeaderElement element = null;
             if ((!qouted) && (ch == ',')) {
-                element = parse(buffer, indexFrom, i);
-                indexFrom = i + 1;
-            } else if (i == indexTo - 1) {
-                element = parse(buffer, indexFrom, indexTo);
+                element = parse(buffer, from, cur);
+                from = cur + 1;
+            } else if (cur == indexTo - 1) {
+                element = parse(buffer, from, indexTo);
             }
             if (element != null && !element.getName().equals("")) {
                 elements.add(element);
             }
-            i++;
+            cur++;
         }
         return (HeaderElement[])
             elements.toArray(new HeaderElement[elements.size()]);
@@ -219,7 +221,8 @@
         return parseElements(buffer, 0, buffer.length());
     }
 
-    public static HeaderElement parse(final CharArrayBuffer buffer, int indexFrom, int indexTo)
{
+    public static HeaderElement parse(
+            final CharArrayBuffer buffer, final int indexFrom, final int indexTo) {
         if (buffer == null) {
             throw new IllegalArgumentException("Char array buffer may not be null");
         }

Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpVersion.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpVersion.java?rev=349036&r1=349035&r2=349036&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpVersion.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpVersion.java Fri Nov
25 15:25:13 2005
@@ -218,33 +218,70 @@
      * 
      * @throws ProtocolException if the string is not a valid HTTP protocol version. 
      */
-    public static HttpVersion parse(final String s) throws ProtocolException {
-        if (s == null) {
-            throw new IllegalArgumentException("String may not be null");
+    public static HttpVersion 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 (!s.startsWith("HTTP/")) {
-            throw new ProtocolException("Invalid HTTP version string: " + s);
+        if (indexFrom < 0) {
+            throw new IndexOutOfBoundsException();
         }
-        int major, minor;
-        
-        int i1 = "HTTP/".length();
-        int i2 = s.indexOf(".", i1);
-        if (i2 == -1) {
-            throw new ProtocolException("Invalid HTTP version number: " + s);
+        if (indexTo > buffer.length()) {
+            throw new IndexOutOfBoundsException();
         }
-        try {
-            major = Integer.parseInt(s.substring(i1, i2)); 
-        } catch (NumberFormatException e) {
-            throw new ProtocolException("Invalid HTTP major version number: " + s);
+        if (indexFrom > indexTo) {
+            throw new IndexOutOfBoundsException();
         }
-        i1 = i2 + 1;
-        i2 = s.length();
         try {
-            minor = Integer.parseInt(s.substring(i1, i2)); 
-        } catch (NumberFormatException e) {
-            throw new ProtocolException("Invalid HTTP minor version number: " + s);
+            int major, minor;
+
+            int i = indexFrom;
+            while (Character.isWhitespace(buffer.charAt(i))) {
+                i++;
+            }            
+            if (buffer.charAt(i    ) != 'H' 
+             || buffer.charAt(i + 1) != 'T'
+             || buffer.charAt(i + 2) != 'T'
+             || buffer.charAt(i + 3) != 'P'
+             || buffer.charAt(i + 4) != '/') {
+                throw new ProtocolException("Not a valid HTTP version string: " + 
+                        buffer.substring(indexFrom, indexTo));
+            }
+            i += 5;
+            int period = buffer.indexOf('.', i);
+            if (period == -1) {
+                throw new ProtocolException("Invalid HTTP version number: " + 
+                        buffer.substring(indexFrom, indexTo));
+            }
+            try {
+                major = Integer.parseInt(buffer.substringTrimmed(i, period)); 
+            } catch (NumberFormatException e) {
+                throw new ProtocolException("Invalid HTTP major version number: " + 
+                        buffer.substring(indexFrom, indexTo));
+            }
+            try {
+                minor = Integer.parseInt(buffer.substringTrimmed(period + 1, indexTo)); 
+            } catch (NumberFormatException e) {
+                throw new ProtocolException("Invalid HTTP minor version number: " + 
+                        buffer.substring(indexFrom, indexTo));
+            }
+            return new HttpVersion(major, minor);
+            
+        } catch (IndexOutOfBoundsException e) {
+            throw new ProtocolException("Invalid HTTP version string: " + 
+                    buffer.substring(indexFrom, indexTo)); 
         }
-        return new HttpVersion(major, minor);
     }
 
+    public static final HttpVersion 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());
+    }
+    
 }

Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/RequestLine.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/RequestLine.java?rev=349036&r1=349035&r2=349036&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/RequestLine.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/RequestLine.java Fri Nov
25 15:25:13 2005
@@ -29,9 +29,6 @@
 
 package org.apache.http;
 
-import java.util.NoSuchElementException;
-import java.util.StringTokenizer;
-
 import org.apache.http.io.CharArrayBuffer;
 
 /**
@@ -87,23 +84,58 @@
         return buffer.toString();
     }
     
-    public static RequestLine parse(final String requestLine) 
-            throws HttpException {
-        if (requestLine == null) {
-            throw new IllegalArgumentException("Request line string may not be null");
-        }
-        String method = null;
-        String uri = null;
-        String protocol = null;
+    public static RequestLine 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 {
-            StringTokenizer st = new StringTokenizer(requestLine, " ");
-            method = st.nextToken().trim();
-            uri = st.nextToken().trim();
-            protocol = st.nextToken();
-        } catch (NoSuchElementException e) {
-            throw new ProtocolException("Invalid request line: " + requestLine);
+            int i = indexFrom;
+            while (Character.isWhitespace(buffer.charAt(i))) {
+                i++;
+            }
+            int blank = buffer.indexOf(' ', i);
+            if (blank < 0) {
+                throw new ProtocolException("Invalid request line: " + 
+                        buffer.substring(indexFrom, indexTo));
+            }
+            String method = buffer.substringTrimmed(i, blank);
+            i = blank;
+            while (Character.isWhitespace(buffer.charAt(i))) {
+                i++;
+            }
+            blank = buffer.indexOf(' ', i);
+            if (blank < 0) {
+                throw new ProtocolException("Invalid request line: " + 
+                        buffer.substring(indexFrom, indexTo));
+            }
+            String uri = buffer.substringTrimmed(i, blank);
+            HttpVersion ver = HttpVersion.parse(buffer, blank, indexTo);
+            return new RequestLine(method, uri, ver);
+        } catch (IndexOutOfBoundsException e) {
+            throw new ProtocolException("Invalid request line: " + 
+                    buffer.substring(indexFrom, indexTo)); 
         }
-        return new RequestLine(method, uri, HttpVersion.parse(protocol));
     }
 
+    public static final RequestLine 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());
+    }
+    
 }

Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/StatusLine.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/StatusLine.java?rev=349036&r1=349035&r2=349036&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/StatusLine.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/StatusLine.java Fri Nov
25 15:25:13 2005
@@ -99,62 +99,77 @@
      * 
      * @since 4.0 
      */
-    public static StatusLine parse(final String statusLine) throws HttpException {
-        if (statusLine == null) {
-            throw new IllegalArgumentException("Status line string may not be null");
-        }
-        HttpVersion httpVersion = null;
-        int statusCode = 0;
-        String reasonPhrase = null;
-        int length = statusLine.length();
-        int at = 0;
-        int start = 0;
+    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 {
-            while (Character.isWhitespace(statusLine.charAt(at))) {
-                ++at;
-                ++start;
-            }
-            if (!"HTTP".equals(statusLine.substring(at, at += 4))) {
-                throw new HttpException("Status-Line '" + statusLine 
-                    + "' does not start with HTTP");
-            }
+            int i = indexFrom;
             //handle the HTTP-Version
-            at = statusLine.indexOf(" ", at);
-            if (at <= 0) {
+            while (Character.isWhitespace(buffer.charAt(i))) {
+                i++;
+            }            
+            int blank = buffer.indexOf(' ', i);
+            if (blank <= 0) {
                 throw new ProtocolException(
-                        "Unable to parse HTTP-Version from the status line: '"
-                        + statusLine + "'");
+                        "Unable to parse HTTP-Version from the status line: "
+                        + buffer.substring(indexFrom, indexTo));
             }
-            httpVersion = HttpVersion.parse(statusLine.substring(start, at));
+            HttpVersion ver = HttpVersion.parse(buffer, i, blank);
 
+            i = blank;
             //advance through spaces
-            while (statusLine.charAt(at) == ' ') {
-                at++;
-            }
+            while (Character.isWhitespace(buffer.charAt(i))) {
+                i++;
+            }            
 
             //handle the Status-Code
-            int to = statusLine.indexOf(" ", at);
-            if (to < 0) {
-                to = length;
+            blank = buffer.indexOf(' ', i);
+            if (blank < 0) {
+                blank = indexTo;
             }
+            int statusCode = 0;
             try {
-                statusCode = Integer.parseInt(statusLine.substring(at, to));
+                statusCode = Integer.parseInt(buffer.substringTrimmed(i, blank));
             } catch (NumberFormatException e) {
                 throw new ProtocolException(
-                    "Unable to parse status code from status line: '" 
-                    + statusLine + "'");
+                    "Unable to parse status code from status line: " 
+                    + buffer.substring(indexFrom, indexTo));
             }
             //handle the Reason-Phrase
-            at = to + 1;
-            if (at < length) {
-                reasonPhrase = statusLine.substring(at).trim();
+            i = blank;
+            String reasonPhrase = null;
+            if (i < indexTo) {
+                reasonPhrase = buffer.substringTrimmed(i, indexTo);
             } else {
                 reasonPhrase = "";
             }
-        } catch (StringIndexOutOfBoundsException e) {
-            throw new HttpException("Status-Line '" + statusLine + "' is not valid"); 
+            return new StatusLine(ver, statusCode, reasonPhrase);
+        } catch (IndexOutOfBoundsException e) {
+            throw new ProtocolException("Invalid status line: " + 
+                    buffer.substring(indexFrom, indexTo)); 
         }
-        return new StatusLine(httpVersion, statusCode, reasonPhrase);
+    }
+
+    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
@@ -191,22 +206,5 @@
         }
         return buffer.toString();
     }
-
-    /**
-     * Tests if the string starts with 'HTTP' signature.
-     * @param s string to test
-     * @return <tt>true</tt> if the line starts with 'HTTP' 
-     *   signature, <tt>false</tt> otherwise.
-     */
-    public static boolean startsWithHTTP(final String s) {
-        try {
-            int at = 0;
-            while (Character.isWhitespace(s.charAt(at))) {
-                ++at;
-            }
-            return ("HTTP".equals(s.substring(at, at + 4)));
-        } catch (StringIndexOutOfBoundsException e) {
-            return false;
-        }
-    }
+    
 }

Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpClientConnection.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpClientConnection.java?rev=349036&r1=349035&r2=349036&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpClientConnection.java
(original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpClientConnection.java
Fri Nov 25 15:25:13 2005
@@ -53,6 +53,7 @@
 import org.apache.http.impl.entity.DefaultEntityGenerator;
 import org.apache.http.impl.entity.EntityGenerator;
 import org.apache.http.impl.entity.EntityWriter;
+import org.apache.http.io.CharArrayBuffer;
 import org.apache.http.io.SocketFactory;
 import org.apache.http.params.HttpParams;
 import org.apache.http.params.HttpProtocolParams;
@@ -77,6 +78,8 @@
     private HttpHost targethost = null;
     private InetAddress localAddress = null;
 
+    private final CharArrayBuffer buffer; 
+    
     /*
      * Dependent interfaces
      */
@@ -86,6 +89,7 @@
         super();
         this.targethost = targethost;
         this.localAddress = localAddress;
+        this.buffer = new CharArrayBuffer(64);
         this.responsefactory = new DefaultHttpResponseFactory();
     }
     
@@ -266,38 +270,60 @@
         }
         return response;
     }
-
+    
+    /**
+     * Tests if the string starts with 'HTTP' signature.
+     * @param buffer buffer to test
+     * @return <tt>true</tt> if the line starts with 'HTTP' 
+     *   signature, <tt>false</tt> otherwise.
+     */
+    private static boolean startsWithHTTP(final CharArrayBuffer buffer) {
+        try {
+            int i = 0;
+            while (Character.isWhitespace(buffer.charAt(i))) {
+                ++i;
+            }
+            return buffer.charAt(i) == 'H' 
+                && buffer.charAt(i + 1) == 'T'
+                && buffer.charAt(i + 2) == 'T'
+                && buffer.charAt(i + 3) == 'P';
+        } catch (IndexOutOfBoundsException e) {
+            return false;
+        }
+    }
+    
     protected HttpMutableResponse readResponseStatusLine(final HttpParams params) 
                 throws HttpException, IOException {
+        // clear the buffer
+        this.buffer.clear();
         //read out the HTTP status string
         int maxGarbageLines = params.getIntParameter(
                 HttpProtocolParams.STATUS_LINE_GARBAGE_LIMIT, Integer.MAX_VALUE);
         int count = 0;
-        String s;
         do {
-            s = this.datareceiver.readLine();
-            if (s == null && count == 0) {
+            int i = this.datareceiver.readLine(this.buffer);
+            if (i == -1 && count == 0) {
                 // The server just dropped connection on us
                 throw new NoHttpResponseException("The server " + 
                         this.targethost.getHostName() + " failed to respond");
             }
-            if (s != null && StatusLine.startsWithHTTP(s)) {
+            if (startsWithHTTP(this.buffer)) {
                 // Got one
                 break;
-            } else if (s == null || count >= maxGarbageLines) {
+            } else if (i == -1 || count >= maxGarbageLines) {
                 // Giving up
                 throw new ProtocolException("The server " + this.targethost.getHostName()
+ 
                         " failed to respond with a valid HTTP response");
             }
             count++;
             if (isWirelogEnabled()) {
-                wirelog("<< " + s + "[\\r][\\n]");
+                wirelog("<< " + this.buffer.toString() + "[\\r][\\n]");
             }
         } while(true);
         //create the status line from the status string
-        StatusLine statusline = StatusLine.parse(s);
+        StatusLine statusline = StatusLine.parse(this.buffer, 0, this.buffer.length());
         if (isWirelogEnabled()) {
-            wirelog("<< " + s + "[\\r][\\n]");
+            wirelog("<< " + this.buffer.toString() + "[\\r][\\n]");
         }
         HttpMutableResponse response = this.responsefactory.newHttpResponse(statusline);
         response.getParams().setDefaults(params);

Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpServerConnection.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpServerConnection.java?rev=349036&r1=349035&r2=349036&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpServerConnection.java
(original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpServerConnection.java
Fri Nov 25 15:25:13 2005
@@ -47,6 +47,7 @@
 import org.apache.http.impl.entity.DefaultServerEntityWriter;
 import org.apache.http.impl.entity.EntityGenerator;
 import org.apache.http.impl.entity.EntityWriter;
+import org.apache.http.io.CharArrayBuffer;
 import org.apache.http.params.HttpParams;
 import org.apache.http.util.HeaderParser;
 
@@ -67,9 +68,12 @@
      */
     private HttpRequestFactory requestfactory = null; 
 
+    private final CharArrayBuffer buffer; 
+        
     public DefaultHttpServerConnection() {
         super();
         this.requestfactory = new DefaultHttpRequestFactory();
+        this.buffer = new CharArrayBuffer(128);
     }
     
     public void setRequestFactory(final HttpRequestFactory requestfactory) {
@@ -103,13 +107,14 @@
     
     protected HttpMutableRequest receiveRequestLine(final HttpParams params)
             throws HttpException, IOException {
-        String line = this.datareceiver.readLine();
-        if (line == null) {
+        this.buffer.clear();
+        int i = this.datareceiver.readLine(this.buffer);
+        if (i == -1) {
             throw new ConnectionClosedException("Client closed connection"); 
         }
-        RequestLine requestline = RequestLine.parse(line);
+        RequestLine requestline = RequestLine.parse(this.buffer, 0, this.buffer.length());
         if (isWirelogEnabled()) {
-            wirelog(">> " + line + "[\\r][\\n]");
+            wirelog(">> " + this.buffer.toString() + "[\\r][\\n]");
         }
         HttpMutableRequest request = this.requestfactory.newHttpRequest(requestline);
         request.getParams().setDefaults(params);

Modified: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHttpVersion.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHttpVersion.java?rev=349036&r1=349035&r2=349036&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHttpVersion.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHttpVersion.java Fri
Nov 25 15:25:13 2005
@@ -29,6 +29,8 @@
 
 package org.apache.http;
 
+import org.apache.http.io.CharArrayBuffer;
+
 import junit.framework.Test;
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
@@ -91,6 +93,18 @@
             //expected
         }
         try {
+            HttpVersion.parse("    ");
+            fail("ProtocolException should have been thrown");
+        } catch (ProtocolException e) {
+            //expected
+        }
+        try {
+            HttpVersion.parse("HTT");
+            fail("ProtocolException should have been thrown");
+        } catch (ProtocolException e) {
+            //expected
+        }
+        try {
             HttpVersion.parse("crap");
             fail("ProtocolException should have been thrown");
         } catch (ProtocolException e) {
@@ -137,6 +151,41 @@
             fail("ProtocolException should have been thrown");
         } catch (ProtocolException e) {
             //expected
+        }
+    }
+
+    public void testInvalidInput() throws Exception {
+        CharArrayBuffer buffer = new CharArrayBuffer(32);
+        buffer.append("HTTP/1.1");
+        try {
+            HttpVersion.parse(null, 0, 0);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+        try {
+            HttpVersion.parse(null);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+        try {
+            HttpVersion.parse(buffer, -1, 0);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+        try {
+            HttpVersion.parse(buffer, 0, 1000);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+        try {
+            HttpVersion.parse(buffer, 2, 1);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
         }
     }
 

Modified: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestRequestLine.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestRequestLine.java?rev=349036&r1=349035&r2=349036&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestRequestLine.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestRequestLine.java Fri
Nov 25 15:25:13 2005
@@ -28,6 +28,8 @@
 
 package org.apache.http;
 
+import org.apache.http.io.CharArrayBuffer;
+
 import junit.framework.*;
 
 /**
@@ -102,6 +104,16 @@
 
     public void testFailure() throws Exception {
         try {
+            RequestLine.parse("    ");
+            fail();
+        } catch (HttpException e) { /* expected */ }
+
+        try {
+            RequestLine.parse("  GET");
+            fail();
+        } catch (HttpException e) { /* expected */ }
+
+        try {
             RequestLine.parse("GET /stuff");
             fail();
         } catch (HttpException e) { /* expected */ }
@@ -112,11 +124,39 @@
         } catch (HttpException e) { /* expected */ }
     }
 
-    public void testNullInput() throws Exception {
+    public void testInvalidInput() throws Exception {
+        CharArrayBuffer buffer = new CharArrayBuffer(32);
+        buffer.append("GET /stuff HTTP/1.1");
+        try {
+            RequestLine.parse(null, 0, 0);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
         try {
             RequestLine.parse(null);
             fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException e) { /* expected */ }
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+        try {
+            RequestLine.parse(buffer, -1, 0);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+        try {
+            RequestLine.parse(buffer, 0, 1000);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+        try {
+            RequestLine.parse(buffer, 2, 1);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
     }
 
 }

Modified: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestStatusLine.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestStatusLine.java?rev=349036&r1=349035&r2=349036&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestStatusLine.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestStatusLine.java Fri
Nov 25 15:25:13 2005
@@ -28,6 +28,8 @@
 
 package org.apache.http;
 
+import org.apache.http.io.CharArrayBuffer;
+
 import junit.framework.*;
 
 /**
@@ -84,16 +86,6 @@
         } catch (IllegalArgumentException e) { /* expected */ }
     }
         
-    public void testIfStatusLine() throws Exception {
-        assertTrue(StatusLine.startsWithHTTP("HTTP"));
-        assertTrue(StatusLine.startsWithHTTP("         HTTP"));
-        assertTrue(StatusLine.startsWithHTTP("\rHTTP"));
-        assertTrue(StatusLine.startsWithHTTP("\tHTTP"));
-        assertFalse(StatusLine.startsWithHTTP("crap"));
-        assertFalse(StatusLine.startsWithHTTP("HTT"));
-        assertFalse(StatusLine.startsWithHTTP("http"));
-    }
-
     public void testSuccess() throws Exception {
         //typical status line
         StatusLine statusLine = StatusLine.parse("HTTP/1.1 200 OK");
@@ -164,13 +156,41 @@
         } catch (HttpException e) { /* expected */ }
     }
 
-    public void testNullInput() throws Exception {
+    public void testInvalidInput() throws Exception {
+        CharArrayBuffer buffer = new CharArrayBuffer(32);
+        buffer.append("HTTP/1.1 200 OK");
+        try {
+            StatusLine.parse(null, 0, 0);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
         try {
             StatusLine.parse(null);
             fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException e) { /* expected */ }
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+        try {
+            StatusLine.parse(buffer, -1, 0);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+        try {
+            StatusLine.parse(buffer, 0, 1000);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+        try {
+            StatusLine.parse(buffer, 2, 1);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
     }
-    
+
     public void testToString() throws Exception {
         StatusLine statusline = new StatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
         assertEquals("HTTP/1.1 200 OK", statusline.toString());



Mime
View raw message