rstudio / httpuv

HTTP and WebSocket server package for R
Other
229 stars 86 forks source link

Implement Last-Modified response header and use If-Modified-Since request header for static files #180

Closed wch closed 5 years ago

wch commented 5 years ago

When serving static files, this adds a Last-Modified header to responses. It also adds the ability to use the If-Modified-Since header from HTTP requests, and sends back a 304 response if appropriate.

Note that in my testing, it's hard to get a browser to actually send the If-Modified-Since header in real-world usage. In Chrome, if you uncheck "Disable Cache" in the network panel (of the dev tools), then reload the page, it will simply retrieve the files from the browser's memory cache. I think if you wait for a long enough period of time (at least several minutes), it will make a request with If-Modified-Since and then receive a 304 response.

For better caching, I think there needs to be Cache-Control and ETag headers, as described here: https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching#validating_cached_responses_with_etags

This PR also changes all instances of DataSource* to a shared_ptr<DataSource> (and same for FileDataSource* and InMemoryDataSource*) because there were some places where managing the lifetime was getting pretty complicated. Switching to a shared_ptr simplifies it greatly.