handle etag header

This commit is contained in:
Swrup 2025-09-28 17:24:32 +02:00
parent b9dfee8d3a
commit bed6e3fec1
2 changed files with 77 additions and 56 deletions

View file

@ -14,24 +14,6 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. *)
module Static = struct
(* /terms + /privacy
- try to find a response with an acceptable mime-type,
- then pick the version in the most preferred language of the user,
- and finally apply compression if that is allowed by the client
- set ETAG header
TODO:
- subsequent requests of the client should provide the tag in an "If-None-Match" header
to detect if the terms of service have changed
- If it did not change, a "304 Not Modified" response will be returned
- The ETAG is encoded in Crockford base-32
- A "Taler-Terms-Version" header is generated to indicate the legal version of the terms
- When returning a full response (not a "304 Not Modified"), the server
should also include a "Avail-Languages" header which includes a
comma-separated list of the languages in which the terms of service are available in
*)
let select_file headers kind =
let open Syntax in
let* ext =
@ -45,25 +27,55 @@ module Static = struct
let content = Assets.get_content ~lang ~ext kind in
content
(* TODO add headers, handle errors etc
think on how to (not) mix error and vif monade well *)
(* /terms + /privacy
- try to find a response with an acceptable mime-type,
- then pick the version in the most preferred language of the user,
- and finally apply compression if that is allowed by the client
- set ETAG header
- If it did not change, a "304 Not Modified" response will be returned
TODO:
- The ETAG is encoded in Crockford base-32
- A "Taler-Terms-Version" header is generated to indicate the legal version of the terms
- When returning a full response (not a "304 Not Modified"), the server
should also include a "Avail-Languages" header which includes a
comma-separated list of the languages in which the terms of service are available in
*)
let static kind req _server _env =
let open Vif.Response.Syntax in
let headers = Vif.Request.headers req in
(* TODO(etag) *)
let _if_none_match = Vif.Headers.get headers "if-none-match" in
let data =
match select_file headers kind with
| Error e -> Fmt.failwith "%s" e
| Ok content -> content
let get_ok = function
| Error e -> Fmt.failwith "TODO handle me, %s" e
| Ok v -> v
in
let compression = Headers.select_encoding headers in
let* () = Vif.Response.with_string ?compression req data in
(* TODO(etag) *)
let* () = Vif.Response.add ~field:"etag" (Assets.etag kind) in
(* TODO content-type *)
let* () = Vif.Response.add ~field:"content-type" "html; charset=utf-8" in
Vif.Response.respond `OK
let etag =
(* TODO keep our static asset etag as Etag.t in Config? *)
Assets.etag kind |> Headers.Etag.of_crockford32 |> Result.get_ok
in
let headers = Vif.Request.headers req in
let has_matching_etag =
get_ok
@@
match Vif.Headers.get headers "if-none-match" with
| None -> Ok false
| Some s ->
Headers.Etag.parse s |> Result.map (Headers.Etag.evaluate etag)
in
let open Vif.Response.Syntax in
match has_matching_etag with
| true ->
let* () = Vif.Response.empty in
Vif.Response.respond `Not_modified
| false ->
let data = select_file headers kind |> get_ok in
let compression = Headers.select_encoding headers in
let* () = Vif.Response.with_string ?compression req data in
let etag_field_value = Headers.Etag.to_field_value etag in
let* () = Vif.Response.add ~field:"etag" etag_field_value in
(* TODO content-type *)
let* () =
Vif.Response.add ~field:"content-type" "html; charset=utf-8"
in
Vif.Response.respond `OK
let terms = static Assets.Terms
let privacy = static Assets.Privacy