dream icon indicating copy to clipboard operation
dream copied to clipboard

Provide a helper to add Etag headers to responses

Open tmattio opened this issue 3 years ago • 3 comments

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

tmattio avatar Apr 21 '21 14:04 tmattio

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.

aantron avatar Apr 21 '21 14:04 aantron

@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.

aantron avatar Apr 21 '21 15:04 aantron

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!

tmattio avatar Apr 21 '21 15:04 tmattio