+ accept-encoding
This commit is contained in:
parent
2828e171aa
commit
5342a1281f
2 changed files with 45 additions and 21 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
let default_lang = "en"
|
let default_lang = "en"
|
||||||
|
let default_encoding : [< `Identity | `DEFLATE | `Gzip ] = `Identity
|
||||||
|
|
||||||
(* TODO
|
(* TODO
|
||||||
generate `config.ml` from config file (virtual module)?
|
generate `config.ml` from config file (virtual module)?
|
||||||
|
|
|
||||||
65
src/mte.ml
65
src/mte.ml
|
|
@ -19,9 +19,7 @@ module Header_util = struct
|
||||||
fun headers ->
|
fun headers ->
|
||||||
let accept = Vif.Headers.get headers "accept" in
|
let accept = Vif.Headers.get headers "accept" in
|
||||||
Cohttp.Accept.media_ranges accept
|
Cohttp.Accept.media_ranges accept
|
||||||
|>
|
|> Cohttp.Accept.qsort
|
||||||
(* TODO check that its not in reverse order *)
|
|
||||||
Cohttp.Accept.qsort
|
|
||||||
|> List.filter_map (fun (_q, (m, _p)) -> Util.media_to_extension m)
|
|> List.filter_map (fun (_q, (m, _p)) -> Util.media_to_extension m)
|
||||||
|
|
||||||
let parse_accept_language_header headers =
|
let parse_accept_language_header headers =
|
||||||
|
|
@ -36,24 +34,50 @@ module Header_util = struct
|
||||||
match language_range with
|
match language_range with
|
||||||
| [] -> assert false
|
| [] -> assert false
|
||||||
| primary_tag :: _ -> primary_tag))
|
| primary_tag :: _ -> primary_tag))
|
||||||
|
|
||||||
|
let select_encoding headers =
|
||||||
|
Vif.Headers.get headers "accept-encoding"
|
||||||
|
|> Cohttp.Accept.encodings
|
||||||
|
|> Cohttp.Accept.qsort
|
||||||
|
|> List.map snd
|
||||||
|
|> List.filter_map (function
|
||||||
|
| Cohttp.Accept.Identity -> Some `Identity
|
||||||
|
| Deflate -> Some `DEFLATE
|
||||||
|
| Gzip -> Some `Gzip
|
||||||
|
| AnyEncoding -> Some Config.default_encoding
|
||||||
|
| Encoding _ | Compress -> (* unsupported *) None)
|
||||||
|
|> function
|
||||||
|
| [] -> assert false
|
||||||
|
| `Identity :: _ -> None
|
||||||
|
| `DEFLATE :: _ -> Some `DEFLATE
|
||||||
|
| `Gzip :: _ -> Some `Gzip
|
||||||
end
|
end
|
||||||
|
|
||||||
module Static = struct
|
module Static = struct
|
||||||
(* TODO handle errors:
|
(* - try to find a response with an acceptable mime-type,
|
||||||
- bad headers
|
- then pick the version in the most preferred language of the user,
|
||||||
- see what to do if usupported lang or ext *)
|
- and finally apply compression if that is allowed by the client
|
||||||
(* assumes accept_ext_l and accept_lang_l to be sorted by preference *)
|
|
||||||
let select_file ~accept_lang_l ~accept_ext_l kind =
|
TODO:
|
||||||
|
- set an ETAG,
|
||||||
|
- 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 not, a "304 Not Modified" response will be returned.
|
||||||
|
Note that the “304 Not Modified” will also be returned if the client changed
|
||||||
|
the "Accept-Language" or "Accept-Encoding" header.
|
||||||
|
- 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
|
||||||
|
|
||||||
|
*)
|
||||||
|
|
||||||
|
(* accept_ext_l and accept_lang_l are assumed to be sorted by preference *)
|
||||||
|
let select_file headers kind =
|
||||||
let open Syntax in
|
let open Syntax in
|
||||||
let* () =
|
let accept_ext_l = Header_util.parse_accept_header headers in
|
||||||
if List.is_empty accept_ext_l then Fmt.error "empty accept header"
|
let accept_lang_l = Header_util.parse_accept_language_header headers in
|
||||||
else Ok ()
|
|
||||||
in
|
|
||||||
let* () =
|
|
||||||
if List.is_empty accept_lang_l then
|
|
||||||
Fmt.error "empty accept language header"
|
|
||||||
else Ok ()
|
|
||||||
in
|
|
||||||
let* ext =
|
let* ext =
|
||||||
accept_ext_l
|
accept_ext_l
|
||||||
|> List.find_opt Assets.is_supported_ext
|
|> List.find_opt Assets.is_supported_ext
|
||||||
|
|
@ -72,14 +96,13 @@ module Static = struct
|
||||||
let static kind req _server _env =
|
let static kind req _server _env =
|
||||||
let open Vif.Response.Syntax in
|
let open Vif.Response.Syntax in
|
||||||
let headers = Vif.Request.headers req in
|
let headers = Vif.Request.headers req in
|
||||||
let accept_ext_l = Header_util.parse_accept_header headers in
|
|
||||||
let accept_lang_l = Header_util.parse_accept_language_header headers in
|
|
||||||
let data =
|
let data =
|
||||||
match select_file ~accept_lang_l ~accept_ext_l kind with
|
match select_file headers kind with
|
||||||
| Error e -> Fmt.failwith "%s" e
|
| Error e -> Fmt.failwith "%s" e
|
||||||
| Ok content -> content
|
| Ok content -> content
|
||||||
in
|
in
|
||||||
let* () = Vif.Response.with_string ~compression:`Gzip req data in
|
let compression = Header_util.select_encoding headers in
|
||||||
|
let* () = Vif.Response.with_string ?compression req data in
|
||||||
let field = "content-type" in
|
let field = "content-type" in
|
||||||
let* () = Vif.Response.add ~field "html; charset=utf-8" in
|
let* () = Vif.Response.add ~field "html; charset=utf-8" in
|
||||||
Vif.Response.respond `OK
|
Vif.Response.respond `OK
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue