dream
dream copied to clipboard
Provide a helper to add Etag headers to responses
It would be really useful to have a helper val with_etag : request -> response -> response
(or similar) that computes a hash of the response body and compare it with the request's Etag
. It would transform the response to a 304 Not modified
if they match, or add the Etag
header with the computed hash if they don't
Thanks. I plan to look at ETag
(and other cache control topics) in a few days, after finishing the last of the bulk doc writing. I also want to expose non-cryptographic hash functions that Dream already uses internally, IIRC MD5 and SHA-1, which could be useful for writing this, or similar helpers in user code.
@tmattio It seems that this will work only for small files (so typical assets and pages, which is fine). Is that right? So the helper would check that the response body is not a stream. If it is a stream, it would leave the request alone. Otherwise, it seems that it would have to buffer the entire stream, which could be disastrous.
That sounds good, for larger files, if they are embedded with ocaml-crunch
, the hash could be computed at compile time and the header could be added even for Stream responses. I created an issue for that: https://github.com/mirage/ocaml-crunch/issues/53
But having Etag for small files and pages would be great already!