package piaf

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file content_disposition.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
type disposition_type =
  [ `Inline | `Attachment | `Ietf_token of string | `X_token of string ]

type t = {
  ty : disposition_type;
  filename : string option;
  creation : date option;
  modification : date option;
  read : date option;
  size : int option;
  parameters : (string * value) list;
}

and value = String of string | Token of string
and date = unit

let error_msgf fmt = Fmt.kstr (fun msg -> Error (`Msg msg)) fmt

let v ?filename ?(kind = `Ietf_token "form-data") ?size name =
  {
    ty = kind;
    filename;
    creation = None;
    modification = None;
    read = None;
    size;
    parameters = [ ("name", String name) ];
  }

let pp_disposition_type ppf = function
  | `Inline -> Fmt.string ppf "inline"
  | `Attachment -> Fmt.string ppf "attachment"
  | `Ietf_token v -> Fmt.pf ppf "<ietf:%s>" v
  | `X_token v -> Fmt.pf ppf "X-%s" v

let pp_value ppf = function
  | String v -> Fmt.pf ppf "%S" v
  | Token v -> Fmt.string ppf v

let pp ppf t =
  Fmt.pf ppf
    "{ @[<hov>type= %a;@ filename= %a;@ creation= %a;@ modification= %a;@ \
     read= %a;@ size= %a;@ parameters= @[<hov>%a@];@] }"
    pp_disposition_type t.ty
    Fmt.(option string)
    t.filename
    Fmt.(option (any "#date"))
    t.creation
    Fmt.(option (any "#date"))
    t.modification
    Fmt.(option (any "#date"))
    t.read
    Fmt.(option int)
    t.size
    Fmt.(
      Dump.iter_bindings
        (fun f -> List.iter (fun (k, v) -> f k v))
        (any "parameters") string pp_value)
    t.parameters

let name t =
  match List.assoc_opt "name" t.parameters with
  | Some (String v | Token v) -> Some v
  | None -> None

let filename { filename; _ } = filename
let size { size; _ } = size
let disposition_type { ty; _ } = ty

let of_escaped_character = function
  | '\x61' -> '\x07' (* "\a" *)
  | '\x62' -> '\x08' (* "\b" *)
  | '\x74' -> '\x09' (* "\t" *)
  | '\x6E' -> '\x0A' (* "\n" *)
  | '\x76' -> '\x0B' (* "\v" *)
  | '\x66' -> '\x0C' (* "\f" *)
  | '\x72' -> '\x0D' (* "\r" *)
  | c -> c

let is_obs_no_ws_ctl = function
  | '\001' .. '\008' | '\011' | '\012' | '\014' .. '\031' | '\127' -> true
  | _ -> false

let is_qtext = function
  | '\033' | '\035' .. '\091' | '\093' .. '\126' -> true
  | c -> is_obs_no_ws_ctl c

let is_wsp = function ' ' | '\t' -> true | _ -> false

module Decoder = struct
  type parameter =
    | Filename of value
    | Creation of unit
    | Modification of unit
    | Read of unit
    | Size of int
    | Parameter of (string * value)

  open Angstrom

  (* From RFC 2045

          tspecials :=  "(" / ")" / "<" / ">" / "@" /
                        "," / ";" / ":" / "\" / <">
                        "/" / "[" / "]" / "?" / "="
                        ; Must be in quoted-string,
                        ; to use within parameter values

        Note that the definition of "tspecials" is the same as the RFC 822
        definition of "specials" with the addition of the three characters
        "/", "?", and "=", and the removal of ".".
  *)
  let is_tspecials = function
    | '(' | ')' | '<' | '>' | '@' | ',' | ';' | ':' | '\\' | '"' | '/' | '['
    | ']' | '?' | '=' ->
        true
    | _ -> false

  let invalid_token token = Fmt.kstr fail "invalid token: %s" token
  let nothing_to_do = Fmt.kstr fail "nothing to do"

  (* / *)

  let is_ctl = function '\000' .. '\031' | '\127' -> true | _ -> false
  let is_space = ( = ) ' '

  (* From RFC 2045

          token := 1*<any (US-ASCII) CHAR except SPACE, CTLs,
                      or tspecials>
  *)
  let is_ascii = function '\000' .. '\127' -> true | _ -> false

  let is_token c =
    is_ascii c && (not (is_tspecials c)) && (not (is_ctl c)) && not (is_space c)

  let token = take_while1 is_token
  let is_digit = function '0' .. '9' -> true | _ -> false

  (* From RFC 2045

          attribute := token
                       ; Matching of attributes
                       ; is ALWAYS case-insensitive.
  *)
  let attribute = token >>| String.lowercase_ascii

  (* From RFC 2045

          ietf-token := <An extension token defined by a
                            standards-track RFC and registered
                            with IANA.>
          iana-token := <A publicly-defined extension token. Tokens
                            of this form must be registered with IANA
                            as specified in RFC 2048.>

     XXX(dinosaure): we don't check at this time if IETF/IANA token exists.
  *)
  let ietf_token = token

  (* From RFC 2045

          x-token := <The two characters "X-" or "x-" followed, with
                         no intervening white space, by any token>
  *)
  let x_token =
    satisfy (function 'x' | 'X' -> true | _ -> false) *> char '-' *> token

  (* From RFC 2045

          extension-token := ietf-token / x-token
  *)
  let extension_token =
    peek_char >>= function
    | Some 'X' | Some 'x' -> x_token >>| fun v -> `X_token v
    | _ -> ietf_token >>| fun v -> `Ietf_token v

  (* From RFC 2045

          value := token / quoted-string
  *)
  let value =
    Content_type.Decoder.quoted_string
    >>| (fun v -> String v)
    <|> (token >>| fun v -> Token v)

  let of_string s a =
    match parse_string ~consume:All a s with Ok v -> Some v | Error _ -> None

  let disposition_type =
    token >>= fun s ->
    match String.lowercase_ascii s with
    | "inline" -> return `Inline
    | "attachment" -> return `Attachment
    | _ ->
    match of_string s extension_token with
    | Some v -> return v
    | None -> invalid_token s

  (* From RFC 2045

          parameter := attribute "=" value
  *)
  let parameter =
    attribute >>= fun attribute ->
    skip_while is_wsp *> char '=' *> skip_while is_wsp *> value >>| fun value ->
    (attribute, value)

  let quoted_date_time = value >>| fun _ -> ()

  let parm parm value =
    string parm *> skip_while is_wsp *> char '=' *> skip_while is_wsp *> value

  let filename_parm = parm "filename" value
  let creation_date_parm = parm "creation-date" quoted_date_time
  let modification_date_parm = parm "modification-date" quoted_date_time
  let read_date_parm = parm "read-date" quoted_date_time
  let size_parm = parm "read-date" (take_while1 is_digit >>| int_of_string)

  let disposition_parm =
    filename_parm
    >>| (fun v -> Filename v)
    <|> (creation_date_parm >>| fun v -> Creation v)
    <|> (modification_date_parm >>| fun v -> Modification v)
    <|> (read_date_parm >>| fun v -> Read v)
    <|> (size_parm >>| fun v -> Size v)
    <|> (parameter >>| fun v -> Parameter v)

  let disposition =
    skip_while is_wsp *> disposition_type <* skip_while is_wsp >>= fun ty ->
    many (skip_while is_wsp *> char ';' *> skip_while is_wsp *> disposition_parm)
    >>= fun parameters ->
    let filename = ref None in
    let creation = ref None in
    let modification = ref None in
    let read = ref None in
    let size = ref None in
    let parameters =
      List.fold_left
        (fun a -> function
          | Filename (String v) | Filename (Token v) ->
              filename := Some v ;
              a
          | Creation v ->
              creation := Some v ;
              a
          | Modification v ->
              modification := Some v ;
              a
          | Read v ->
              read := Some v ;
              a
          | Size v ->
              size := Some v ;
              a
          | Parameter v -> v :: a)
        [] parameters in
    return
      {
        ty;
        filename = !filename;
        creation = !creation;
        modification = !modification;
        read = !read;
        size = !size;
        parameters;
      }
end

let of_string str =
  let ( >>= ) = Result.bind and ( >>| ) x f = Result.map f x in
  Unstrctrd.of_string str
  >>| (fun (_, v) -> Unstrctrd.fold_fws v)
  >>| Unstrctrd.to_utf_8_string
  >>= fun str ->
  match Angstrom.parse_string ~consume:All Decoder.disposition str with
  | Ok v -> Ok v
  | Error _ -> error_msgf "Invalid (unfolded) Content-Disposition value: %S" str

module Encoder = struct
  open Prettym

  let disposition_type ppf = function
    | `Attachment -> eval ppf [ string $ "attachment" ]
    | `Inline -> eval ppf [ string $ "inline" ]
    | `Ietf_token v -> eval ppf [ !!string ] v
    | `X_token v -> eval ppf [ string $ "X-"; !!string ] v

  let token ppf str = eval ppf [ !!string ] str

  (* TODO(dinosaure): unsafe token. *)

  let value ppf = function
    | Token v -> token ppf v
    | String v -> eval ppf [ char $ '"'; !!string; char $ '"' ] v

  type 'a param =
    | Filename : string param
    | Creation : date param
    | Modification : date param
    | Read : date param
    | Size : int param
    | Parameter : (string * value) param

  type v = V : ('a param * 'a option) -> v

  let disposition_parm : v Prettym.t =
   fun ppf (V v) ->
    match v with
    | Filename, Some v ->
        eval ppf
          [ string $ "filename"; cut; char $ '='; !!token; char $ ';'; fws ]
          v
    | Filename, None -> ppf
    | Creation, _ -> ppf
    | Modification, _ -> ppf
    | Read, _ -> ppf
    | Size, Some v ->
        eval ppf
          [ string $ "size"; cut; char $ '='; !!string; char $ ';'; fws ]
          (string_of_int v)
    | Size, None -> ppf
    | Parameter, Some (k, v) ->
        eval ppf [ !!string; cut; char $ '='; !!value; char $ ';'; fws ] k v
    | Parameter, None -> ppf

  let disposition ppf v =
    let sep ppf () = eval ppf [ cut ] in
    eval ppf
      [
        !!disposition_type;
        cut;
        char $ ';';
        fws;
        !!(list ~sep:(sep, ()) disposition_parm);
      ]
      v.ty
      ([
         V (Filename, v.filename);
         V (Creation, v.creation);
         V (Modification, v.modification);
         V (Read, v.read);
         V (Size, v.size);
       ]
      @ List.map (fun (k, v) -> V (Parameter, Some (k, v))) v.parameters)
end

let to_string v = Prettym.to_string Encoder.disposition v
OCaml

Innovation. Community. Security.