HttpComponents HttpCore
  1. HttpComponents HttpCore
  2. HTTPCORE-190

ChunkedInputStream does not handle sockets with timeouts

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 4.0
    • Fix Version/s: 4.1-alpha1
    • Component/s: None
    • Labels:
      None

      Description

      ChunkedInputStream does not handle socket timeouts when reading data. If a socket timeout is received after finishing reading a chunk (including it's CRLF combination) and before reading the next chunk's length, the next call to read will attempt to re-read the CRLF, causing an IOException with the message "CRLF expected at end of chunk" to be thrown. I have only tested this with 3.0 but it doesn't appear to be fixed in 3.1 or any 4.0 from looking at the code.

      1. HTTPCLIENT-829.patch
        2 kB
        Adam Bryzak
      2. HttpClient_829.java
        4 kB
        Adam Bryzak

        Issue Links

          Activity

          Adam Bryzak created issue -
          Hide
          Adam Bryzak added a comment -

          Created a patch which stores some local variables in fields instead.

          Show
          Adam Bryzak added a comment - Created a patch which stores some local variables in fields instead.
          Adam Bryzak made changes -
          Field Original Value New Value
          Attachment HTTPCLIENT-829.patch [ 12400928 ]
          Hide
          Oleg Kalnichevski added a comment -

          Adam,

          Socket timeouts are signalled as SocketTimeoutException. In case of a socket timeout there will be no more attempts to read from the input stream. Probably you mean end of stream condition, not a timeout?

          There is basically no point fixing HttpClient 3.x, as it is nearing its end of life.

          By looking at the code I do not think HttpClient 4.0 is affected. I believe the end of stream conditions are handled correctly. If you are able to reproduce the problem with HttpClient 4.0, feel free to submit a patch or a test case. Otherwise I'll close this issue as won't fix.

          Oleg

          Show
          Oleg Kalnichevski added a comment - Adam, Socket timeouts are signalled as SocketTimeoutException. In case of a socket timeout there will be no more attempts to read from the input stream. Probably you mean end of stream condition, not a timeout? There is basically no point fixing HttpClient 3.x, as it is nearing its end of life. By looking at the code I do not think HttpClient 4.0 is affected. I believe the end of stream conditions are handled correctly. If you are able to reproduce the problem with HttpClient 4.0, feel free to submit a patch or a test case. Otherwise I'll close this issue as won't fix. Oleg
          Hide
          Adam Bryzak added a comment -

          When getting a SocketTimeoutException, the connection is still valid. In my project, I'm using the timeout as an opportunity to check if the connection should be terminated (for potentially various reasons such as how many active connections there are, how long the connection has been invalid or even if the service should no longer be running). The code for ChunkedInputStream in 4.0 looked to be similar or the same as in 3.1 which is what led me to believe it would still be a problem in it. The main issue is that it doesn't track all state as it's reading.

          Show
          Adam Bryzak added a comment - When getting a SocketTimeoutException, the connection is still valid. In my project, I'm using the timeout as an opportunity to check if the connection should be terminated (for potentially various reasons such as how many active connections there are, how long the connection has been invalid or even if the service should no longer be running). The code for ChunkedInputStream in 4.0 looked to be similar or the same as in 3.1 which is what led me to believe it would still be a problem in it. The main issue is that it doesn't track all state as it's reading.
          Hide
          Oleg Kalnichevski added a comment -

          Adam,

          I am sorry but I do not see how SocketTimeoutException is related to ChunkedInputStream. ChunkedInputStream, like all other stream codecs, simply propagate IOExceptions to the caller.

          Could you please demonstrate the problem with 4.0 code using a test case?

          Oleg

          Show
          Oleg Kalnichevski added a comment - Adam, I am sorry but I do not see how SocketTimeoutException is related to ChunkedInputStream. ChunkedInputStream, like all other stream codecs, simply propagate IOExceptions to the caller. Could you please demonstrate the problem with 4.0 code using a test case? Oleg
          Hide
          Adam Bryzak added a comment -

          Attached a simple test case. Simply run the attached file with httpcore-4.0.jar in your classpath.

          The test starts a simple http server which will respond with "012" chunked with 2 seconds between each chunk. It then connects a client with a timeout of 1 second and reads the data from the response 1 character at a time. After the first character the exception is thrown.

          Note that this is just a Java class with a main method, and not a JUnit test case.

          Show
          Adam Bryzak added a comment - Attached a simple test case. Simply run the attached file with httpcore-4.0.jar in your classpath. The test starts a simple http server which will respond with "012" chunked with 2 seconds between each chunk. It then connects a client with a timeout of 1 second and reads the data from the response 1 character at a time. After the first character the exception is thrown. Note that this is just a Java class with a main method, and not a JUnit test case.
          Adam Bryzak made changes -
          Attachment HttpClient_829.java [ 12400969 ]
          Oleg Kalnichevski made changes -
          Key HTTPCLIENT-829 HTTPCORE-190
          Affects Version/s 3.1 Final [ 12312326 ]
          Project HttpComponents HttpClient [ 12310360 ] HttpComponents HttpCore [ 12310340 ]
          Affects Version/s 4.0 [ 12313121 ]
          Hide
          Oleg Kalnichevski added a comment -

          I can confirm the bug. Thanks for the good test case.

          Oleg

          Show
          Oleg Kalnichevski added a comment - I can confirm the bug. Thanks for the good test case. Oleg
          Oleg Kalnichevski made changes -
          Fix Version/s 4.1 [ 12313548 ]
          Priority Major [ 3 ] Minor [ 4 ]
          Hide
          Oleg Kalnichevski added a comment -

          Adam

          I fixed the problem in SVN trunk. Would it be a big deal for you to re-test your application with the latest snapshot from SVN trunk?

          Oleg

          Show
          Oleg Kalnichevski added a comment - Adam I fixed the problem in SVN trunk. Would it be a big deal for you to re-test your application with the latest snapshot from SVN trunk? Oleg
          Oleg Kalnichevski made changes -
          Status Open [ 1 ] Resolved [ 5 ]
          Resolution Fixed [ 1 ]
          Hide
          Adam Bryzak added a comment -

          I'll try and get around to testing it later today.

          Show
          Adam Bryzak added a comment - I'll try and get around to testing it later today.
          Hide
          Adam Bryzak added a comment -

          ChunkedInputStream now behaves correctly when receiving a SocketTimeoutException in all my tests.

          Show
          Adam Bryzak added a comment - ChunkedInputStream now behaves correctly when receiving a SocketTimeoutException in all my tests.
          Oleg Kalnichevski made changes -
          Status Resolved [ 5 ] Closed [ 6 ]
          Oleg Kalnichevski made changes -
          Fix Version/s 4.1-alpha1 [ 12314094 ]
          Fix Version/s 4.1 [ 12313548 ]
          Anton Khitrenovich made changes -
          Link This issue is cloned as HTTPCORE-333 [ HTTPCORE-333 ]

            People

            • Assignee:
              Unassigned
              Reporter:
              Adam Bryzak
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development