views:

747

answers:

2

I'm trying to build a HEAD method to one of our services so that clients can peek at the content type and size before deciding whether to download it.

How can I set the Content Length of the response header? Using HttpContext exposes the ContentType, Encoding etc but I can't specify a length as I presume this value is normally added by the framework when some content is added to the response.

Is there another way of doing this or am I incorrect to be setting the length for a HEAD request - should I actually be adding a custom header to return the size of the resource?

+1  A: 

I think a Content-Length specifies the size of the body of the response - not the size of the resource. As such, it doesn't make sense in the context of a HEAD response.

Edit: The specs says:

The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request. This method can be used for obtaining metainformation about the entity implied by the request without transferring the entity-body itself. This method is often used for testing hypertext links for validity, accessibility, and recent modification.

The response to a HEAD request MAY be cacheable in the sense that the information contained in the response MAY be used to update a previously cached entity from that resource. If the new field values indicate that the cached entity differs from the current entity (as would be indicated by a change in Content-Length, Content-MD5, ETag or Last-Modified), then the cache MUST treat the cache entry as stale.

-- http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4

This suggests that my above statement is wrong. Especially the latter paragraph strongly suggests that a HEAD can have a Content-Length header.

troelskn
Thanks - mu reading of it was that headers are identical so i'd expect the length to be set to the size that would be returned on a GET. In most cases you'd check the headers to determine if a resource has changed. In our case we want to give people the option whether to download based on the size.
Chris W
And I believe that's the right interpretation. That is not a guarantee that all actors will actually implement it this way though ...
troelskn
A: 

HEAD method is not supposed to have a content, so it has no Content-Length/Content-Type or Content-"anything else".

Havenard