package dream-pure

  1. Overview
  2. Docs

Source file formats.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
(* This file is part of Dream, released under the MIT license. See LICENSE.md
   for details, or visit https://github.com/aantron/dream.

   Copyright 2021 Anton Bachin *)



let html_escape s =
  let buffer = Buffer.create (String.length s * 2) in
  s |> String.iter begin function
    | '&' -> Buffer.add_string buffer "&"
    | '<' -> Buffer.add_string buffer "&lt;"
    | '>' -> Buffer.add_string buffer "&gt;"
    | '"' -> Buffer.add_string buffer "&quot;"
    | '\'' -> Buffer.add_string buffer "&#x27;"
    | c -> Buffer.add_char buffer c
    end;
  Buffer.contents buffer



let to_base64url string =
  Base64.encode_string ~pad:false ~alphabet:Base64.uri_safe_alphabet string

let from_base64url string =
  match Base64.decode ~pad:false ~alphabet:Base64.uri_safe_alphabet string with
  | Error _ -> None
  | Ok result -> Some result



let from_cookie s =
  let pairs =
    s
    |> String.split_on_char ';'
    |> List.map (String.split_on_char '=')
  in

  pairs |> List.fold_left (fun pairs -> function
    | [name; value] -> (String.trim name, String.trim value)::pairs
    | _ -> pairs) []
(* Note: found ocaml-cookie and http-cookie libraries, but they appear to have
   equivalent code for parsing Cookie: headers, so there is no point in using
   them yet, especially as they have stringent OCaml version constraints for
   other parts of their code. *)
(* Note: this parser doesn't actually appear to comply with the RFC strictly. It
   accepts more characters than the spec allows. It doesn't treate DQUOTE
   specially. This might not be important, however, if user agents treat cookies
   as opaque, because then only Dream has to deal with its own cookies. *)

let to_set_cookie
    ?expires ?max_age ?domain ?path ?secure ?http_only ?same_site name value =

  let expires =
    match Option.bind expires Ptime.of_float_s with
    | None -> ""
    | Some time ->
      let weekday =
        match Ptime.weekday time with
        | `Sun -> "Sun" | `Mon -> "Mon" | `Tue -> "Tue" | `Wed -> "Wed"
        | `Thu -> "Thu" | `Fri -> "Fri" | `Sat -> "Sat"
      in
      let ((y, m, d), ((hh, mm, ss), _tz_offset_s)) = Ptime.to_date_time time in
      let month =
        match m with
        | 1 -> "Jan" | 2 -> "Feb" | 3 -> "Mar" | 4 -> "Apr" | 5 -> "May"
        | 6 -> "Jun" | 7 -> "Jul" | 8 -> "Aug" | 9 -> "Sep" | 10 -> "Oct"
        | 11 -> "Nov" | 12 -> "Dec"
        | _ -> assert false
      in
      (* [Ptime.to_date_time] docs give range 0..60 for [ss], accounting for
         leap seconds. However, RFC 6265 §5.1.1 states:

         5.  Abort these steps and fail to parse the cookie-date if:

           *  the second-value is greater than 59.

           (Note that leap seconds cannot be represented in this syntax.)

         See https://tools.ietf.org/html/rfc6265#section-5.1.1.

         Even though [Ptime.to_date_time] time does not return leap seconds, in
         case I misunderstood the gmtime API, of system differences, or future
         refactoring, make sure no leap seconds creep into the output. *)
      let seconds =
        if ss < 60 then ss else 59 [@coverage off]
      in
      Printf.sprintf "; Expires=%s, %02i %s %i %02i:%02i:%02i GMT"
        weekday d month y hh mm seconds
  in

  let max_age =
    match max_age with
    | None -> ""
    | Some seconds -> Printf.sprintf "; Max-Age=%.0f" (floor seconds)
  in

  let domain =
    match domain with
    | None -> ""
    | Some domain -> Printf.sprintf "; Domain=%s" domain
  in

  let path =
    match path with
    | None -> ""
    | Some path -> Printf.sprintf "; Path=%s" path
  in

  let secure =
    match secure with
    | Some true -> "; Secure"
    | _ -> ""
  in

  let http_only =
    match http_only with
    | Some true -> "; HttpOnly"
    | _ -> ""
  in

  let same_site =
    match same_site with
    | None -> ""
    | Some `Strict -> "; SameSite=Strict"
    | Some `Lax -> "; SameSite=Lax"
    | Some `None -> "; SameSite=None"
  in

  Printf.sprintf "%s=%s%s%s%s%s%s%s%s"
    name value expires max_age domain path secure http_only same_site



let iri_safe_octets =
  String.init 128 (fun i -> Char.chr (i + 128))

let iri_generic =
  `Custom (`Generic, iri_safe_octets, "")

let to_percent_encoded ?(international = true) string =
  let component =
    if international then iri_generic
    else `Generic
  in
  Uri.pct_encode ~component string

let from_percent_encoded string =
  Uri.pct_decode string



let to_form_urlencoded dictionary =
  dictionary
  |> List.map (fun (name, value) -> name, [value])
  |> Uri.encoded_of_query

let from_form_urlencoded string =
  if string = "" then
    []
  else
    string
    |> Uri.query_of_encoded
    |> List.map (fun (name, values) -> name, String.concat "," values)



let split_target string =
  let uri = Uri.of_string string in
  let query =
    match Uri.verbatim_query uri with
    | Some query -> query
    | None -> ""
  in
  Uri.path uri, query

let from_path =
  (* Not tail-recursive. *)
  let rec filter_components = function
    | [] -> []
    | [""] as components -> components
    | ""::components -> filter_components components
    | component::components -> component::(filter_components components)
  in

  fun string ->
    let components =
      if string = "" then
        []
      else
        String.split_on_char '/' string
        |> filter_components
        |> List.map Uri.pct_decode
    in

    components

(* Not tail-recursive. Only called on the site prefix and route fragments during
   app setup. *)
let rec drop_trailing_slash = function
  | [] -> []
  | [""] -> []
  | component::components ->
    component::(drop_trailing_slash components)

let to_path ?(relative = false) ?(international = true) components =
  let rec filter_empty_components = function
    | ""::(_::_ as path) -> filter_empty_components path
    | component::path -> component::(filter_empty_components path)
    | [] -> []
  in
  let components = filter_empty_components components in

  let components =
    match relative, components with
    | false, [] -> [""; ""]
    | false, _ -> ""::components
    | true, _ -> components
  in

  components
  |> List.map (to_percent_encoded ~international)
  |> String.concat "/"



let text_html =
  "text/html; charset=utf-8"

let application_json =
  "application/json"
OCaml

Innovation. Community. Security.