commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From oglu...@apache.org
Subject cvs commit: jakarta-commons/httpclient/src/test/org/apache/commons/httpclient TestStreams.java
Date Thu, 26 Sep 2002 12:40:33 GMT
oglueck     2002/09/26 05:40:33

  Modified:    httpclient/src/java/org/apache/commons/httpclient
                        ChunkedInputStream.java
               httpclient/src/test/org/apache/commons/httpclient
                        TestStreams.java
  Log:
  Patch by Martin Elwin fixes bug #12607. Test case included.
  Parsing chunk extensions correctly.
  
  Revision  Changes    Path
  1.5       +57 -30    jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/ChunkedInputStream.java
  
  Index: ChunkedInputStream.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/ChunkedInputStream.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ChunkedInputStream.java	23 Sep 2002 14:01:40 -0000	1.4
  +++ ChunkedInputStream.java	26 Sep 2002 12:40:33 -0000	1.5
  @@ -68,14 +68,15 @@
   /**
    * <p>Transparently coalesces chunks of a HTTP stream that uses Transfer-Encoding
    * chunked.</p>
  - * 
  + *
    * @see ResponseInputStream
  - * 
  + *
    * @author Ortwin Glück
    * @author Sean C. Sullivan
  - * 
  + * @author Martin Elwin
  + *
    * @since 2.0
  - * 
  + *
    */
   
   public class ChunkedInputStream extends InputStream {
  @@ -86,22 +87,22 @@
       private HttpMethod method;
   
       /**
  -     *  
  -     * 
  +     *
  +     *
        * @param in must be non-null
        * @param method must be non-null
  -     * 
  -     * @throws java.io.IOException 
  -     * @throws java.lang.NullPointerException 
  -     * 
  +     *
  +     * @throws java.io.IOException
  +     * @throws java.lang.NullPointerException
  +     *
        */
       public ChunkedInputStream(final InputStream in, final HttpMethod method) throws IOException
{
  -    	if (null == in) {
  -    		throw new NullPointerException("InputStream parameter");
  -    	}
  -    	if (null == method) {
  -    		throw new NullPointerException("HttpMethod parameter");
  -    	}
  +      if (null == in) {
  +        throw new NullPointerException("InputStream parameter");
  +      }
  +      if (null == method) {
  +        throw new NullPointerException("HttpMethod parameter");
  +      }
           this.in = in;
           this.method = method;
           this.chunkSize = getChunkSizeFromInputStream(in);
  @@ -164,7 +165,7 @@
        * Positions the stream at the start of the next line.
        *
        * @return the chunk size as integer
  -     * 
  +     *
        * @throws IOException when the chunk size could not be parsed
        * @throws java.lang.RuntimeException
        *
  @@ -172,34 +173,60 @@
        */
       private static int getChunkSizeFromInputStream(final InputStream in) throws IOException
{
           ByteArrayOutputStream baos = new ByteArrayOutputStream();
  +        int state = 0; //0: normal, 1: \r was scanned, 2: inside quoted string, -1: end
  +        while (state != -1) {
           int b = in.read();
  -        int state = 0;
  -        while (state != 2) {
               if (b == -1) throw new IOException("chunked stream ended unexpectedly");
               switch (state) {
                   case 0:
  -                    if (b == '\r') state = 1;
  +                    switch (b) {
  +                        case '\r':
  +                            state = 1;
                       break;
  +                        case '\"':
  +                            state = 2;
  +                            /* fall through */
  +                        default:
  +                            baos.write(b);
  +                    }
  +                    break;
  +
                   case 1:
  -                    if (b == '\n') state = 2;
  -                    else state = 0;
  +                    if (b == '\n') {
  +                        state = -1;
  +                    } else {
  +                        // this was not CRLF
  +                        throw new IOException("Protocol violation: Unexpected single newline
character in chunk size");
  +                    }
  +                    break;
  +
  +                case 2:
  +                    switch (b) {
  +                        case '\\':
  +                            b = in.read();
  +                            baos.write(b);
  +                            break;
  +                        case '\"':
  +                            state = 0;
  +                            /* fall through */
  +                        default:
  +                            baos.write(b);
  +                    }
                       break;
                   default: throw new RuntimeException("assertion failed");
               }
  -            if (state == 0) baos.write(b);
  -            if (state != 2) b = in.read();
           }
   
           //parse data
           String dataString = new String(baos.toByteArray(), HTTP_ENC);
           int separator = dataString.indexOf(';');
  -        dataString = (separator > 0) 
  -            ? dataString.substring(0, separator).trim() 
  +        dataString = (separator > 0)
  +            ? dataString.substring(0, separator).trim()
               : dataString.trim();
   
           int result;
           try {
  -            result = Integer.parseInt(dataString, 16);
  +            result = Integer.parseInt(dataString.trim(), 16);
           } catch(NumberFormatException e) {
               throw new IOException("Bad chunk size: "+dataString);
           }
  
  
  
  1.3       +20 -1     jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestStreams.java
  
  Index: TestStreams.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestStreams.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestStreams.java	23 Sep 2002 14:01:40 -0000	1.2
  +++ TestStreams.java	26 Sep 2002 12:40:33 -0000	1.3
  @@ -13,7 +13,7 @@
       }
   
       public void testChunkedInputStream() throws IOException {
  -        String correctInput = "10; this is comments\r\n1234567890123456\r\n5\r\n12345\r\n0\r\nFooter1:
abcde\r\nFooter2: fghij\r\n";
  +        String correctInput = "10;key=\"value\r\nnewline\"\r\n1234567890123456\r\n5\r\n12345\r\n0\r\nFooter1:
abcde\r\nFooter2: fghij\r\n";
           String correctResult = "123456789012345612345";
           HttpMethod method = new SimpleHttpMethod();
   
  @@ -46,6 +46,25 @@
           assertEquals(footer.getValue(), "abcde");
           footer = method.getResponseFooter("footer2");
           assertEquals(footer.getValue(), "fghij");
  +    }
  +
  +    public void testCorruptChunkedInputStream1() throws IOException {
  +        //missing \r\n at the end of the first chunk
  +        String corrupInput = "10;key=\"value\"\r\n123456789012345\r\n5\r\n12345\r\n0\r\nFooter1:
abcde\r\nFooter2: fghij\r\n";
  +        HttpMethod method = new SimpleHttpMethod();
  +
  +        InputStream in = new ChunkedInputStream(new ByteArrayInputStream(corrupInput.getBytes()),
method);
  +        byte[] buffer = new byte[300];
  +        ByteArrayOutputStream out = new ByteArrayOutputStream();
  +        int len;
  +        try {
  +            while ((len = in.read(buffer)) > 0) {
  +                out.write(buffer, 0, len);
  +            }
  +            fail("Should have thrown exception");
  +        } catch(IOException e) {
  +            /* expected exception */
  +        }
       }
   
       public void testContentLengthInputStream() throws IOException {
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message