You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by John Sheehy <jo...@voxer.com> on 2012/06/07 19:00:33 UTC

Detecting an aborted GET request w/ chunked encoding

Hi all,

I'm using Http client 4.1.3 on android (I replaced the built-in version
with the version from http://hc.apache.org/httpcomponents-client-ga/ using
a different namespace). I'm not quite sure how to detect an aborted GET
request. The server response is streamed, and chunked etc., and my code to
read it (in an HttpResponseHandler, with response status 200) looks like so:

          HttpEntity entity = response.getEntity();

    if (entity != null) {
        InputStream inputStream = null;
        try {
            int n = 0;
            inputStream = entity.getContent();
            while((n =  inputStream.read(chunkBuf)) != -1) {
                // do something with the chunkBuf...
            };
        } catch (Exception e) {
            // If the connection is aborted, should a Chunk or IO exception
be thrown?
        } finally {
            if(inputStream != null) {
                inputStream.close();
            }
        }

When I abort the connection on the server (mid-stream so the chunk length
would not match the content), the response ends without throwing an
exception. Using curl to do the GET, I get something along the lines of:

curl: (18) transfer closed with outstanding read data remaining

Is there some special type I have to use to wrap the input stream (I note
in 3.x that there's a ChunkedInputStream, but can't figure out how to use
it in 4.1.x), or something I should set on the request? I note the
inputStream is of type EOFSensorInputStream.. I'm not going through any
proxies or anything like that.

I would really appreciate any insight into how to detect this condition!

Thanks,
John Sheehy

Re: Detecting an aborted GET request w/ chunked encoding

Posted by John Sheehy <jo...@voxer.com>.
Got it, thanks.


> John
>
> If you do not have a problem with changing the source and recompiling
> HttpClient all you need is to make #getChunkSize() method throw an
> exception if a read operation returns -1 (end of stream).
>
> Hope this helps
>
> Oleg
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>

Re: Detecting an aborted GET request w/ chunked encoding

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Wed, 2012-06-13 at 20:00 +0100, John Sheehy wrote:
> >
> >
> > John
> >
> > I looked at the source code and found no way to override the default
> > behavior other than copying the entire ChunkedInputStream class and
> > overriding the entity deserializer used by the default HTTP connection
> > implementation.
> >
> > Oleg
> >
> >
> Thanks for checking Oleg.
> 
> I'm a little unsure of what you mean here, I have ChunkedInputStream.java
> from
> httpcore/src/main/java/org/apache/http/impl/io/ChunkedInputStream.java (v
> 4.2) -
> is there some field I need to override in this class, or modify it
> otherwise?
> 
> Thanks,
> John.

John

If you do not have a problem with changing the source and recompiling
HttpClient all you need is to make #getChunkSize() method throw an
exception if a read operation returns -1 (end of stream).

Hope this helps

Oleg


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: Detecting an aborted GET request w/ chunked encoding

Posted by John Sheehy <jo...@voxer.com>.
>
>
> John
>
> I looked at the source code and found no way to override the default
> behavior other than copying the entire ChunkedInputStream class and
> overriding the entity deserializer used by the default HTTP connection
> implementation.
>
> Oleg
>
>
Thanks for checking Oleg.

I'm a little unsure of what you mean here, I have ChunkedInputStream.java
from
httpcore/src/main/java/org/apache/http/impl/io/ChunkedInputStream.java (v
4.2) -
is there some field I need to override in this class, or modify it
otherwise?

Thanks,
John.

Re: Detecting an aborted GET request w/ chunked encoding

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Wed, 2012-06-13 at 13:09 +0100, John Sheehy wrote:
> >
> > Please re-ran the test with wire logging activates to see whether or not
> >> the last content chunk gets truncated.
> >>
> >> Oleg
> >>
> >>
> > Hi Oleg,
> 
> I'm having a lot of trouble so far getting wire logging out on android (I'm
> using a repackaged 4.2, to get past the built-in 4.0beta android bundled
> version, using this package: http://code.google.com/p/httpclientandroidlib/).
> In the meantime, is there anyway to get HttpClient to throw an exception if
> the closing "0" chunk is not present? Perhaps with a custom Chunk Decoder?
> 
> Thanks
> John.

John 

I looked at the source code and found no way to override the default
behavior other than copying the entire ChunkedInputStream class and
overriding the entity deserializer used by the default HTTP connection
implementation.

Oleg 


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: Detecting an aborted GET request w/ chunked encoding

Posted by John Sheehy <jo...@voxer.com>.
>
> Please re-ran the test with wire logging activates to see whether or not
>> the last content chunk gets truncated.
>>
>> Oleg
>>
>>
> Hi Oleg,

I'm having a lot of trouble so far getting wire logging out on android (I'm
using a repackaged 4.2, to get past the built-in 4.0beta android bundled
version, using this package: http://code.google.com/p/httpclientandroidlib/).
In the meantime, is there anyway to get HttpClient to throw an exception if
the closing "0" chunk is not present? Perhaps with a custom Chunk Decoder?

Thanks
John.

Re: Detecting an aborted GET request w/ chunked encoding

Posted by John Sheehy <jo...@voxer.com>.
> HttpClient should throw TruncatedChunkException in case it encounters an
> incomplete content chunk. It will, however, silently ignore missing
> closing chunk.
>
> Please re-ran the test with wire logging activates to see whether or not
> the last content chunk gets truncated.
>
> Oleg
>
>
Hi Oleg, I'll re-do the test with logging - I'll do header wire + context
logging. Please let me know if you need some other configuration.

John

Re: Detecting an aborted GET request w/ chunked encoding

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Thu, 2012-06-07 at 10:00 -0700, John Sheehy wrote:
> Hi all,
> 
> I'm using Http client 4.1.3 on android (I replaced the built-in version
> with the version from http://hc.apache.org/httpcomponents-client-ga/ using
> a different namespace). I'm not quite sure how to detect an aborted GET
> request. The server response is streamed, and chunked etc., and my code to
> read it (in an HttpResponseHandler, with response status 200) looks like so:
> 
>           HttpEntity entity = response.getEntity();
> 
>     if (entity != null) {
>         InputStream inputStream = null;
>         try {
>             int n = 0;
>             inputStream = entity.getContent();
>             while((n =  inputStream.read(chunkBuf)) != -1) {
>                 // do something with the chunkBuf...
>             };
>         } catch (Exception e) {
>             // If the connection is aborted, should a Chunk or IO exception
> be thrown?
>         } finally {
>             if(inputStream != null) {
>                 inputStream.close();
>             }
>         }
> 
> When I abort the connection on the server (mid-stream so the chunk length
> would not match the content), the response ends without throwing an
> exception. Using curl to do the GET, I get something along the lines of:
> 
> curl: (18) transfer closed with outstanding read data remaining
> 
> Is there some special type I have to use to wrap the input stream (I note
> in 3.x that there's a ChunkedInputStream, but can't figure out how to use
> it in 4.1.x), or something I should set on the request? I note the
> inputStream is of type EOFSensorInputStream.. I'm not going through any
> proxies or anything like that.
> 
> I would really appreciate any insight into how to detect this condition!
> 
> Thanks,
> John Sheehy

John

HttpClient should throw TruncatedChunkException in case it encounters an
incomplete content chunk. It will, however, silently ignore missing
closing chunk.

Please re-ran the test with wire logging activates to see whether or not
the last content chunk gets truncated.

Oleg



---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org