Page 2 of 2 FirstFirst 12
Results 11 to 13 of 13

Thread: Forcing 304 instead of 200 HTTP response?

  1. #11
    Join Date
    Oct 2005
    Location
    UK
    Posts
    11

    Default

    Quote Originally Posted by gmatthews
    Checking for If-Modified-Since and sending back 304 or 200 works fine. The only thing is when parsing the date, it's in GMT so you need to set a TimeZone on your DateFormat instance to parse the request time back into local time.
    You can use request.getDateHeader() / response.setDateHeader() to avoid having to faff with TimeZones. Confusingly they take a long, not a Date, but the conversion is easy (theDate.getTime() / new Date(longValue)

    A couple of other things to watch out for:
    - Firefox will only send conditional GET requests (i.e. include an if-modified-since) if you've previously set a last-modified date on the response;
    - If you send a content-length header make sure it's zero for a 304 response else Safari will hang for about 30 seconds waiting for you to finish the response.

    The LiveHttpHeaders firefox extension is invaluable for debugging / understanding what's going on here, as is Ethereal. Oh, and the spec (http://www.w3.org/Protocols/rfc2616/rfc2616.html )is surprisingly accessible.

  2. #12
    Join Date
    Aug 2004
    Location
    Sydney
    Posts
    503

    Default

    Thanks very much for the info.

    I am setting last-modified every time, but only out of luck I guess. I didn't know about the firefox issue.

    I think I'll use the getDateHeader/setDateHeader + set the content length as you suggest.

    Thanks also for the spec reference.

    I wonder if this might be useful for those people doing AJAX or even Lazslo (sp?), and being able to cache generated content.

    I vaguely remember working on some project in about 1997 where we were using XMLHttpRequest (pretty much AJAX) for an IE only app, and we had loads of big generated .js files that would have been good to cache.

  3. #13
    Join Date
    Jul 2012
    Posts
    1

    Default

    Quote Originally Posted by gmatthews View Post
    Checking for If-Modified-Since and sending back 304 or 200 works fine. The only thing is when parsing the date, it's in GMT so you need to set a TimeZone on your DateFormat instance to parse the request time back into local time.

    I also noticed that Spring provides a org.springframework.web.servlet.mvc.LastModified interface that is supposed to be implemented by your controller.

    DispatcherServlet and SimpleControllerHandlerAdapter co-ordinate to eventually call LastModifier.getLastModified

    It might be interesting to see if support for sending back a 304 with a null ModelAndView (to indicate that the controller has already handled the request) could be put into Spring.
    buy fans

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •