Skip to content

Commit

Permalink
Prepare release 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tmattio committed Apr 9, 2022
1 parent 5465b23 commit 0085ff4
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 250 deletions.
21 changes: 3 additions & 18 deletions .ocamlformat
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
version = 0.18.0
profile = sparse
break-cases = nested
break-fun-decl = smart
cases-exp-indent = 2
if-then-else = fit-or-vertical
parens-tuple = multi-line-only
parens-tuple-patterns = multi-line-only
parens-ite = false
infix-precedence = parens
break-infix-before-func = false
sequence-style = terminator
sequence-blank-line = compact
indicate-multiline-delimiters = no
ocp-indent-compat = true
wrap-comments = true
version = 0.21.0
profile = default
parse-docstrings = true
let-binding-spacing = compact
type-decl = sparse
wrap-comments = true
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 0.2.0

- Support for dream.1.0.0~alpha3
# 0.1.0

- Initial release
9 changes: 2 additions & 7 deletions dream-encoding.opam
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ homepage: "https://github.com/tmattio/dream-encoding"
doc: "https://tmattio.github.io/dream-encoding/"
bug-reports: "https://github.com/tmattio/dream-encoding/issues"
depends: [
"ocaml" {>= "4.08.0"}
"dune" {>= "2.7"}
"dream"
"ocaml" {>= "4.08.0"}
"dream" {>= "1.0.0~alpha3"}
"decompress" {>= "1.4.1"}
"lwt_ppx"
"odoc" {with-doc}
Expand All @@ -34,8 +34,3 @@ dev-repo: "git+https://github.com/tmattio/dream-encoding.git"
conflicts: [
"result" {< "1.5"} # Might use result through lwt and explicitly uses Result.bind
]
pin-depends: [
["dream.dev" "git+https://github.com/aantron/dream#master"]
["dream-pure.dev" "git+https://github.com/aantron/dream#master"]
["dream-httpaf.dev" "git+https://github.com/aantron/dream#master"]
]
5 changes: 0 additions & 5 deletions dream-encoding.opam.template
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
conflicts: [
"result" {< "1.5"} # Might use result through lwt and explicitly uses Result.bind
]
pin-depends: [
["dream.dev" "git+https://github.com/aantron/dream#master"]
["dream-pure.dev" "git+https://github.com/aantron/dream#master"]
["dream-httpaf.dev" "git+https://github.com/aantron/dream#master"]
]
7 changes: 3 additions & 4 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
(depends
(ocaml
(>= 4.08.0))
dune
dream
(dream
(>= 1.0.0~alpha3))
(decompress
(>= 1.4.1))
lwt_ppx
(odoc :with-doc)))
lwt_ppx))
5 changes: 1 addition & 4 deletions example/server.ml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
(** Main entry point for our application. *)

let () =
Dream.run
@@ Dream.logger
@@ Dream_encoding.compress
Dream.run @@ Dream.logger @@ Dream_encoding.compress
@@ Dream.router [ Dream.get "/" (fun _ -> Dream.html "Hello World!") ]
@@ Dream.not_found
123 changes: 36 additions & 87 deletions lib/accept.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,59 +27,33 @@ type encoding =
| Any

type p = string * string

type q = int

type 'a qlist = (q * 'a) list

(** Lexer *)
let is_space = function ' ' | '\t' -> true | _ -> false

let is_token = function
| '\000' .. '\031'
| '\127'
| ')'
| '('
| '<'
| '>'
| '@'
| ','
| ';'
| ':'
| '"'
| '/'
| '['
| ']'
| '?'
| '='
| '{'
| '}'
| ' ' ->
false
| _s ->
true
| '\127' | ')' | '(' | '<' | '>' | '@' | ',' | ';' | ':' | '"' | '/' | '['
| ']' | '?' | '=' | '{' | '}' | ' ' ->
false
| _s -> true

let ows = skip is_space <|> return ()

let token = take_while1 is_token

let sep_by1_comma value_parser = sep_by1 (char ',') value_parser <* end_of_input

let eval_parser parser default_value = function
| None ->
[ 1000, default_value ]
| Some str ->
(match parse_string ~consume:Angstrom.Consume.All parser str with
| Ok v ->
v
| Error msg ->
failwith msg)
| None -> [ (1000, default_value) ]
| Some str -> (
match parse_string ~consume:Angstrom.Consume.All parser str with
| Ok v -> v
| Error msg -> failwith msg)

(** Parser for header parameters like defined in rfc
https://tools.ietf.org/html/rfc7231#section-5.3.2 *)
type param =
| Q of int
| Kv of p
type param = Q of int | Kv of p

let q_of_string s = truncate (1000. *. float_of_string s)

Expand All @@ -89,9 +63,7 @@ let qs = char '"' *> token <* char '"'
(* a header parameter can be : OWS ; OWS q=[value] OWS ; OWS [name]=[value] OWS
; OWS [name]="[value]" *)
let param : param t =
ows
*> char ';'
*> ows
ows *> char ';' *> ows
*> (* OWS ; OWS q=[value] OWS ; OWS [name]=[value]*)
(lift2
(fun n v -> if n = "q" then Q (q_of_string v) else Kv (n, v))
Expand All @@ -115,75 +87,52 @@ let encoding_value_parser =
<|> lift
(fun s ->
match String.lowercase_ascii s with
| "gzip" ->
Gzip
| "compress" ->
Compress
| "deflate" ->
Deflate
| "identity" ->
Identity
| enc ->
Encoding enc)
| "gzip" -> Gzip
| "compress" -> Compress
| "deflate" -> Deflate
| "identity" -> Identity
| enc -> Encoding enc)
token)

let encoding_parser =
lift2 (fun value q -> q, value) encoding_value_parser (lift get_q params)
lift2 (fun value q -> (q, value)) encoding_value_parser (lift get_q params)

let encodings_parser = sep_by1_comma encoding_parser

let encodings = eval_parser encodings_parser Any

(** Other functions (from Cohttp.Accept) *)
let rec string_of_pl = function
| [] ->
""
| [] -> ""
| (k, v) :: r ->
let e = Stringext.quote v in
if v = e then
sprintf ";%s=%s%s" k v (string_of_pl r)
else
sprintf ";%s=\"%s\"%s" k e (string_of_pl r)
let e = Stringext.quote v in
if v = e then sprintf ";%s=%s%s" k v (string_of_pl r)
else sprintf ";%s=\"%s\"%s" k e (string_of_pl r)

let string_of_q = function
| q when q < 0 ->
invalid_arg (Printf.sprintf "qvalue %d must be positive" q)
| q when q < 0 -> invalid_arg (Printf.sprintf "qvalue %d must be positive" q)
| q when q > 1000 ->
invalid_arg (Printf.sprintf "qvalue %d must be less than 1000" q)
| 1000 ->
"1"
| q ->
Printf.sprintf "0.%03d" q
invalid_arg (Printf.sprintf "qvalue %d must be less than 1000" q)
| 1000 -> "1"
| q -> Printf.sprintf "0.%03d" q

let accept_el ?q el pl =
match q with
| Some q ->
sprintf "%s;q=%s%s" el (string_of_q q) (string_of_pl pl)
| None ->
el
| Some q -> sprintf "%s;q=%s%s" el (string_of_q q) (string_of_pl pl)
| None -> el

let string_of_encoding ?q = function
| Encoding e ->
accept_el ?q e []
| Gzip ->
accept_el ?q "gzip" []
| Compress ->
accept_el ?q "compress" []
| Deflate ->
accept_el ?q "deflate" []
| Identity ->
accept_el ?q "identity" []
| Any ->
accept_el ?q "*" []
| Encoding e -> accept_el ?q e []
| Gzip -> accept_el ?q "gzip" []
| Compress -> accept_el ?q "compress" []
| Deflate -> accept_el ?q "deflate" []
| Identity -> accept_el ?q "identity" []
| Any -> accept_el ?q "*" []

let string_of_list s_of_el =
let rec aux s = function
| [ (q, el) ] ->
s ^ s_of_el el q
| [] ->
s
| (q, el) :: r ->
aux (s ^ s_of_el el q ^ ",") r
| [ (q, el) ] -> s ^ s_of_el el q
| [] -> s
| (q, el) :: r -> aux (s ^ s_of_el el q ^ ",") r
in
aux ""

Expand Down
6 changes: 2 additions & 4 deletions lib/accept.mli
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,16 @@ type encoding =

(** Accept-Encoding HTTP header parsing and generation *)

type q = int
(** Qualities are integers between 0 and 1000. A header with ["q=0.7"]
corresponds to a quality of [700]. *)
type q = int

(** Lists, annotated with qualities. *)
type 'a qlist = (q * 'a) list
(** Lists, annotated with qualities. *)

val qsort : 'a qlist -> 'a qlist
(** Sort by quality, biggest first. Respect the initial ordering. *)

val encodings : string option -> encoding qlist

val string_of_encoding : ?q:q -> encoding -> string

val string_of_encodings : encoding qlist -> string
Loading

0 comments on commit 0085ff4

Please sign in to comment.