users@jersey.java.net

Re: Resource Lifecycle, Providers and Content-Length

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 10 Apr 2008 13:20:33 +0200

Jean Aurambault wrote:
>>> When I make a GET on a resource (200) the Content-Length is not set.
>>> When I make a HEAD on the same resource Content-Length is 0.
>>
>> Yes, you have found a bug :-) Would you mind logging an issue?
>>
>>
>>> If I make a HEAD an resource which doesn't exist (404) Content-Length
>>> is 952. I have specific content for 404 with variable size. 952 is
>>> not the size of the returned content, because when the content
>>> changes Content-Length stays at 952.
>>>
>>
>> Is that because the the Web container is handling the HEAD (and the
>> GET) ? I suspect the Web container is returning some HTML as the 404
>> response.
>>
> I don't think so. For 404 I build a specific xml response with jaxb
> bean. And if I understand your following explanation, HEAD request
> should return an unknown content length.
>
> Here is some logs:
>
> *> GET* /V1/product/ids_test_qsdfff/ HTTP/1.1
> > User-Agent: curl/7.15.5 (i686-redhat-linux-gnu) libcurl/7.15.5
> OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5
> > Host: xxxxxxxxxxxxxx
> > Accept: */*
> >
> < HTTP/1.1 404 Not Found
> < Server: Apache-Coyote/1.1
> < Content-Type: application/xml
> < Date: Wed, 09 Apr 2008 11:32:41 GMT
> < Connection: close
> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
> <xxxx>
> ...
> </xxxx>
>
>
> *> *HEAD /V1/product/ids_test_qsdfff/ HTTP/1.1
> > User-Agent: curl/7.15.5 (i686-redhat-linux-gnu) libcurl/7.15.5
> OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5
> > Host: xxxxxxxxxxxxxx
> > Accept: */*
> >
> < HTTP/1.1 404 Not Found
> < Server: Apache-Coyote/1.1
> < Content-Type: text/html;charset=utf-8
> *< Content-Length: 952*
> < Date: Wed, 09 Apr 2008 11:33:31 GMT
> < Connection: close
> * transfer closed with 952 bytes remaining to read
> * Closing connection #0
> curl: (18) transfer closed with 952 bytes remaining to read
>
> (note that 952 doesn't match the size of the content return by the GET
> neither, without more analysis it seems to be a magic number :-D)
>

I think 952 corresponds to the size of the entity that would have been
returned by GF/Tomcat if you do not return your own entity for the 404
response you created. Try returning a 404 without an entity and count
the bytes returned from the GET respone.

I just verified that was the case for a simple example i created using
GF. Basically the container is getting in the way. If i set the HEAD
response header to "Transfer-Encoding: chunked" the container still sets
the "Content-Length" header for the size of data what would be sent if
the container returned the entity. This is a Tomcat/GF bug.


>>
>>> HEAD is supposed to return the Content-Length that a GET request
>>> would have returned. Doesn't it? Is jersey supposed to take in
>>> charge the Content-Length?
>>>
>>
>> It should, when the content length is known upfront (e.g. for File or
>> for byte[]). For efficient Jersey will use chunked transfer encoding
>> when the content length is not known (and one would have to buffer to
>> determine it, e.g. for JAXB beans). In such cases a HEAD response
>> should return information consistent with the GET with an unknown
>> content length.
> Not sure to fully understand what's going on. Can you confirm that GET
> will still return Content-Length even with Jaxb beans?

No, Jersey will not set the size for JAXB beans because to determine
that it will have to buffer the serialized bytes of JAXB beans. Jersey
will defer to the container, which should hopefully use the chunked
encoding or if the client does not use keep-alive close the connection,
so that the bytes are written directly out in a streaming manner.

Paul.

> It is only for
> HEAD, that the size cannot be determined (without marshalling) and so is
> not set.
>
> Jean.
>
>
>>
>> Paul.
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>

-- 
| ? + ? = To question
----------------\
    Paul Sandoz
         x38109
+33-4-76188109