Handling HEAD requests in Chrome

242 views
Skip to first unread message

Ross Bates

unread,
Dec 6, 2011, 3:27:47 PM12/6/11
to cherryp...@googlegroups.com
Hi all - when making a request using Google Chrome the browser issues a GET followed by a HEAD.

My page is database driven and this second HEAD request is essentially running through all the code just as the GET does.

I'm trying to put a before_handler tool in to grab the request and return headers only using something simple like this

if cherrypy.request.method.upper() == 'HEAD':
return cherrypy.response.headers

Alas, it continues to process the body of the page. This custom tool can capture and handle HEAD requests when I issue a redirect or raise an error, but then the browser fails to fully load the page.

Any thoughts/suggestions would be much appreciated.

Best Regards,
Ross

Eric Larson

unread,
Dec 7, 2011, 2:58:01 AM12/7/11
to cherryp...@googlegroups.com
I would think a cache in some shape or form is the best thing to do. On the browser side an etag might work with the head request to let you recognize the content didn't change. Likewise I'd hope a cache-control header would tell chrome to avoid the head request and use the cache.

On the server side you could also cache the output of the handler. When the HEAD request is made, you can see if the GET request happened already and return the same response without the body.

Sorry if that isn't terribly specific. That is just where I would start. 

HTH, 

Eric

Best Regards,
Ross

--
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/cherrypy-users/-/_v-W79ey1oEJ.
To post to this group, send email to cherryp...@googlegroups.com.
To unsubscribe from this group, send email to cherrypy-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cherrypy-users?hl=en.

Robert Brewer

unread,
Dec 7, 2011, 10:06:05 AM12/7/11
to cherryp...@googlegroups.com
Ross Bates wrote:
> I'm trying to put a before_handler tool in to grab the
> request and return headers only using something simple
> like this:
>
> if cherrypy.request.method.upper() == 'HEAD':
> return cherrypy.response.headers
>
> Alas, it continues to process the body of the page.

First, try caching as Eric suggested. But to make a Tool that overrides
the normal handler, just remove it. Try this instead:

if cherrypy.request.method.upper() == 'HEAD':
cherrypy.response.headers['Content-Length'] = 3182
...other headers as needed to reproduce the GET headers...
cherrypy.request.handler = None


Robert Brewer
fuma...@aminus.org

Ross Bates

unread,
Dec 7, 2011, 11:47:29 AM12/7/11
to cherryp...@googlegroups.com
Thanks all for the responses. Much appreciated.

Something which I discovered was that the 2nd HEAD request was coming from a 3rd party extension used for syntax highlighting. It ignored cache-control but the suggestion to set the cherrypy.request.handler = None worked well.

Returning cherrypy.HTTPRedirect([], 304) also worked, but setting the handler to None gave me more control over setting the headers I needed to.

Not sure how prevalent HEAD requests are out in the wild but this was a good learning exercise. Thanks again.

-Ross


Reply all
Reply to author
Forward
0 new messages