httpclientif-modified-since

Using client generated time in the "If-Modified-Since" HTTP header


We are using the "Last-Modified" response header from the server and the "If-Modified-Since" request header for the resource validation. HTTP header definitions states that the "If-Modified-Since" SHOULD contain value received from the server (it does not prohibit usage of another value). At some point we started using a "last updated time" as value for the "If-Modified-Since" sent from the client. The "last updated time" is the time when client last time received an update from server (either new version of the resource or 304 error). We were told that we must not use any client generated time because of possible time synchronization issues.

Of course all times are expressed in GMT.

I could not find confirmation that this is a requirement. It would be nice to hear espert's opinion on it is a MUST to use a value returned from server as a value for the "If-Modified-Since" or there is some degree of flexibility? In what circumstances it is possible to use client generated time?

Thanks a lot


Solution

  • If the client's clock is ahead of the server's clock, it is invalid to specify a date/time that is after the server's current clock (the HTTP spec even says so).

    If the client's clock is behind the server clock's, at the very least you might end up with a 200 reply when you might be expecting a 304 reply instead, which is not an error but would waste bandwidth sending a really-unmodified resource.

    Either way, you are better off using the server's date/time values. In fact, the HTTP spec does says so in section 14.25:

    Note: When handling an If-Modified-Since header field, some servers will use an exact date comparison function, rather than a less-than function, for deciding whether to send a 304 (Not Modified) response. To get best results when sending an If- Modified-Since header field for cache validation, clients are advised to use the exact date string received in a previous Last- Modified header field whenever possible.

    Note: If a client uses an arbitrary date in the If-Modified-Since header instead of a date taken from the Last-Modified header for the same request, the client should be aware of the fact that this date is interpreted in the server's understanding of time. The client should consider unsynchronized clocks and rounding problems due to the different encodings of time between the client and server. This includes the possibility of race conditions if the document has changed between the time it was first requested and the If-Modified-Since date of a subsequent request, and the possibility of clock-skew-related problems if the If-Modified- Since date is derived from the client's clock without correction to the server's clock. Corrections for different time bases between client and server are at best approximate due to network latency.