Source file multipart_form.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
let src = Logs.Src.create "multipart-form"
module Log = (val Logs.src_log src : Logs.LOG)
module Field_name = Field_name
module Field = Field
module Content_type = Content_type
module Content_encoding = Content_encoding
module Content_disposition = Content_disposition
module IOVec = struct
type 'a t = 'a Faraday.iovec = { buffer : 'a; off : int; len : int }
let make buffer ~off ~len = { Faraday.buffer; off; len }
let substring { Faraday.buffer; off; len } =
Bigstringaf.substring buffer ~off ~len
let copy buf ~off ~len =
let buffer = Bigstringaf.copy buf ~off ~len in
make buffer ~off:0 ~len
let with_push ?(end_of_line = "\n") push =
let eol_len = String.length end_of_line in
let write_data s =
let len = String.length s in
let buffer = Bigstringaf.create len in
Bigstringaf.blit_from_string s ~src_off:0 buffer ~dst_off:0 ~len ;
push (Some (make buffer ~off:0 ~len)) in
let write_line s =
let strlen = String.length s in
let len = strlen + eol_len in
let buffer = Bigstringaf.create len in
Bigstringaf.blit_from_string s ~src_off:0 buffer ~dst_off:0 ~len:strlen ;
Bigstringaf.blit_from_string end_of_line ~src_off:0 buffer ~dst_off:strlen
~len:eol_len ;
push (Some (make buffer ~off:0 ~len)) in
(write_data, write_line)
end
type 'a stream = unit -> 'a option
module B64 = struct
open Angstrom
let parser ~write_data end_of_body =
let dec = Base64_rfc2045.decoder `Manual in
let check_end_of_body =
let expected_len = String.length end_of_body in
Unsafe.peek expected_len (fun ba ~off ~len ->
let raw = Bigstringaf.substring ba ~off ~len in
String.equal raw end_of_body) in
let trailer () =
let rec finish () =
match Base64_rfc2045.decode dec with
| `Await -> assert false
| `Flush data ->
write_data data ;
commit >>= finish
| `Malformed err -> commit *> fail err
| `Wrong_padding -> commit *> fail "wrong padding"
| `End -> commit
and go () =
match Base64_rfc2045.decode dec with
| `Await ->
Base64_rfc2045.src dec Bytes.empty 0 0 ;
commit >>= finish
| `Flush data ->
write_data data ;
commit >>= go
| `Malformed err -> commit *> fail err
| `Wrong_padding -> commit *> fail "wrong padding"
| `End -> commit in
go () in
fix @@ fun m ->
let choose chunk = function
| true ->
let chunk = Bytes.sub chunk 0 (Bytes.length chunk - 1) in
Base64_rfc2045.src dec chunk 0 (Bytes.length chunk) ;
commit *> trailer ()
| false ->
Bytes.set chunk (Bytes.length chunk - 1) end_of_body.[0] ;
Base64_rfc2045.src dec chunk 0 (Bytes.length chunk) ;
advance 1 *> commit *> m in
Unsafe.take_while (( <> ) end_of_body.[0]) Bigstringaf.substring
>>= fun chunk ->
let rec go () =
match Base64_rfc2045.decode dec with
| `End -> commit
| `Await ->
let chunk' = Bytes.create (String.length chunk + 1) in
Bytes.blit_string chunk 0 chunk' 0 (String.length chunk) ;
check_end_of_body >>= choose chunk'
| `Flush data ->
write_data data ;
commit >>= go
| `Malformed err -> commit *> fail err
| `Wrong_padding -> commit *> fail "wrong padding" in
go ()
let with_emitter ~emitter end_of_body =
let write_data, _ = IOVec.with_push emitter in
parser ~write_data end_of_body
let to_end_of_input ~write_data =
let dec = Base64_rfc2045.decoder `Manual in
fix @@ fun m ->
match Base64_rfc2045.decode dec with
| `End -> commit
| `Await -> (
peek_char >>= function
| None ->
Base64_rfc2045.src dec Bytes.empty 0 0 ;
commit *> return ()
| Some _ ->
available >>= fun n ->
Unsafe.take n (fun ba ~off ~len ->
let chunk = Bytes.create len in
Bigstringaf.blit_to_bytes ba ~src_off:off chunk ~dst_off:0 ~len ;
Base64_rfc2045.src dec chunk 0 len)
>>= fun () -> commit *> m)
| `Flush data ->
write_data data ;
commit *> m
| `Malformed err -> commit *> fail err
| `Wrong_padding -> commit *> fail "wrong padding"
let to_end_of_input_with_push push =
let write_data, _ = IOVec.with_push push in
to_end_of_input ~write_data
end
module RAW = struct
open Angstrom
type chunks = {
length : int ref;
mutable chunks : Bigstringaf.t IOVec.t list;
}
let bounded_end_of_body ~max_chunk_size end_of_body_discriminant
current_chunk_size c =
let cur_size = !current_chunk_size in
let big_enough = cur_size + 1 >= max_chunk_size in
let result = Char.equal end_of_body_discriminant c in
let result = big_enough || result in
if not result then incr current_chunk_size ;
result
let iovec_from_chunks { chunks; length } =
let len = !length in
let result_buffer = Bigstringaf.create len in
let final_len =
List.fold_left
(fun prev_off { IOVec.buffer; off; len } ->
let cur_off = prev_off - len in
Bigstringaf.unsafe_blit buffer ~src_off:off result_buffer
~dst_off:cur_off ~len ;
cur_off)
len chunks in
assert (final_len = 0) ;
IOVec.make result_buffer ~off:0 ~len:(Bigstringaf.length result_buffer)
let parser ~max_chunk_size ~write_data ~pred ~check_end =
let current_chunks = { length = ref 0; chunks = [] } in
let pred = pred current_chunks.length in
fix (fun m ->
Unsafe.take_till pred IOVec.copy >>= fun chunk ->
check_end >>= function
| true ->
current_chunks.chunks <- chunk :: current_chunks.chunks ;
let iovec = iovec_from_chunks current_chunks in
current_chunks.length := 0 ;
current_chunks.chunks <- [] ;
if iovec.len <> 0 then write_data iovec ;
commit
| false ->
Unsafe.take 1 IOVec.copy >>= fun cr ->
incr current_chunks.length ;
current_chunks.chunks <- cr :: chunk :: current_chunks.chunks ;
if !(current_chunks.length) >= max_chunk_size
then (
let iovec = iovec_from_chunks current_chunks in
current_chunks.length := 0 ;
current_chunks.chunks <- [] ;
write_data iovec ;
commit *> m)
else
commit *> m)
let multipart_parser ~max_chunk_size ~write_data end_of_body =
let check_end_of_body =
let expected_len = String.length end_of_body in
Unsafe.peek expected_len (fun ba ~off ~len ->
let raw = Bigstringaf.substring ba ~off ~len in
String.equal raw end_of_body) in
let bounded_end_of_body =
bounded_end_of_body ~max_chunk_size end_of_body.[0] in
parser ~max_chunk_size ~write_data ~pred:bounded_end_of_body
~check_end:check_end_of_body
let with_emitter ~max_chunk_size ~emitter end_of_body =
let write_data x = emitter (Some x) in
multipart_parser ~max_chunk_size ~write_data end_of_body
let to_end_of_input ~max_chunk_size ~write_data =
parser ~max_chunk_size ~write_data ~check_end:at_end_of_input
~pred:(fun current_chunk_size _ ->
let cur_size = !current_chunk_size in
let big_enough = cur_size + 1 >= max_chunk_size in
if not big_enough then incr current_chunk_size ;
big_enough)
let to_end_of_input_with_push ~max_chunk_size push =
let write_data x = push (Some x) in
to_end_of_input ~max_chunk_size ~write_data
end
module QP = struct
open Angstrom
let parser ~write_data ~write_line end_of_body =
let dec = Pecu.decoder `Manual in
let check_end_of_body =
let expected_len = String.length end_of_body in
Unsafe.peek expected_len (fun ba ~off ~len ->
let raw = Bigstringaf.substring ba ~off ~len in
String.equal raw end_of_body) in
let trailer () =
let rec finish () =
match Pecu.decode dec with
| `Await -> assert false
| `Data data ->
write_data data ;
commit >>= finish
| `Line line ->
write_line line ;
commit >>= finish
| `End -> commit
| `Malformed err -> commit *> fail err
and go () =
match Pecu.decode dec with
| `Await ->
Pecu.src dec Bytes.empty 0 0 ;
commit >>= finish
| `Data data ->
write_data data ;
commit >>= go
| `Line line ->
write_line line ;
commit >>= go
| `End -> commit
| `Malformed err -> commit *> fail err in
go () in
fix @@ fun m ->
let choose chunk = function
| true ->
let chunk = Bytes.sub chunk 0 (Bytes.length chunk - 1) in
Pecu.src dec chunk 0 (Bytes.length chunk) ;
commit >>= trailer
| false ->
Bytes.set chunk (Bytes.length chunk - 1) end_of_body.[0] ;
Pecu.src dec chunk 0 (Bytes.length chunk) ;
advance 1 *> commit *> m in
Unsafe.take_while (( <> ) end_of_body.[0]) Bigstringaf.substring
>>= fun chunk ->
let rec go () =
match Pecu.decode dec with
| `End -> commit
| `Await ->
let chunk' = Bytes.create (String.length chunk + 1) in
Bytes.blit_string chunk 0 chunk' 0 (String.length chunk) ;
check_end_of_body <* commit >>= choose chunk'
| `Data data ->
write_data data ;
commit >>= go
| `Line line ->
write_line line ;
commit >>= go
| `Malformed err -> commit *> fail err in
go ()
let to_end_of_input ~write_data ~write_line =
let dec = Pecu.decoder `Manual in
fix @@ fun m ->
match Pecu.decode dec with
| `End -> commit
| `Await -> (
peek_char >>= function
| None ->
Pecu.src dec Bytes.empty 0 0 ;
commit
| Some _ ->
available >>= fun n ->
Unsafe.take n (fun ba ~off ~len ->
let chunk = Bytes.create len in
Bigstringaf.blit_to_bytes ba ~src_off:off chunk ~dst_off:0 ~len ;
Pecu.src dec chunk 0 len)
*> commit
*> m)
| `Data data ->
write_data data ;
commit *> m
| `Line line ->
write_line line ;
commit *> m
| `Malformed err -> commit *> fail err
let with_push ~push end_of_body =
let write_data, write_line = IOVec.with_push push in
parser ~write_data ~write_line end_of_body
let to_end_of_input_with_push ?end_of_line push =
let write_data, write_line = IOVec.with_push ?end_of_line push in
to_end_of_input ~write_data ~write_line
end
type 'a elt = { header : Header.t; body : 'a }
type 'a t = Leaf of 'a elt | Multipart of 'a t option list elt
let rec map f = function
| Leaf { ; body } -> Leaf { header; body = f body }
| Multipart { ; body } ->
Multipart { header; body = List.map (Option.map (map f)) body }
let rec flatten = function
| Leaf elt -> [ elt ]
| Multipart { header = _; body } ->
List.flatten @@ List.filter_map (Option.map flatten) body
let iter ~f buf ~off ~len =
for i = off to len - 1 do
f buf.[i]
done
let to_quoted_printable :
?length:int -> (string * int * int) stream -> (string * int * int) stream =
fun ?length:(chunk_length = 4096) stream ->
let chunk = Bytes.create chunk_length in
let encoder = Pecu.encoder `Manual in
let queue = Ke.Rke.create ~capacity:128 Bigarray.Int in
let rec emit () =
Ke.Rke.cons queue 256 ;
let len = chunk_length - Pecu.dst_rem encoder in
Some (Bytes.unsafe_to_string chunk, 0, len)
and pending = function
| `Ok -> go ()
| `Partial ->
let len = chunk_length - Pecu.dst_rem encoder in
Some (Bytes.unsafe_to_string chunk, 0, len)
and go () =
match Ke.Rke.pop_exn queue with
| 256 -> (
Pecu.dst encoder chunk 0 chunk_length ;
match Pecu.encode encoder `Await with
| `Ok -> (go [@tailcall]) ()
| `Partial -> (emit [@tailcall]) ())
| 257 -> (
Ke.Rke.cons queue 258 ;
match Pecu.encode encoder `Line_break with
| `Ok -> go ()
| `Partial -> (emit [@tailcall]) ())
| 258 ->
Ke.Rke.cons queue 259 ;
(pending [@tailcall]) (Pecu.encode encoder `End)
| 259 ->
assert (Pecu.encode encoder `Await = `Ok) ;
Ke.Rke.cons queue 259 ;
None
| chr -> (
match Pecu.encode encoder (`Char (Char.chr chr)) with
| `Ok -> (go [@tailcall]) ()
| `Partial -> (emit [@tailcall]) ())
| exception Ke.Rke.Empty ->
match stream () with
| Some (buf, off, len) ->
iter ~f:(fun chr -> Ke.Rke.push queue (Char.code chr)) buf ~off ~len ;
(go [@tailcall]) ()
| None ->
Ke.Rke.push queue 257 ;
(go [@tailcall]) () in
Pecu.dst encoder chunk 0 chunk_length ;
go
let to_base64 :
?length:int -> (string * int * int) stream -> (string * int * int) stream =
fun ?length:(chunk_length = 4096) stream ->
let chunk = Bytes.create chunk_length in
let encoder = Base64_rfc2045.encoder `Manual in
let queue = Ke.Rke.create ~capacity:128 Bigarray.Int in
let rec emit () =
Ke.Rke.cons queue 256 ;
let len = chunk_length - Base64_rfc2045.dst_rem encoder in
Some (Bytes.unsafe_to_string chunk, 0, len)
and pending = function
| `Ok -> (go [@tailcall]) ()
| `Partial ->
let len = chunk_length - Base64_rfc2045.dst_rem encoder in
Some (Bytes.unsafe_to_string chunk, 0, len)
and go () =
match Ke.Rke.pop_exn queue with
| 256 -> (
Base64_rfc2045.dst encoder chunk 0 chunk_length ;
match Base64_rfc2045.encode encoder `Await with
| `Ok -> (go [@tailcall]) ()
| `Partial -> (emit [@tailcall]) ())
| 257 ->
Ke.Rke.cons queue 258 ;
(pending [@tailcall]) (Base64_rfc2045.encode encoder `End)
| 258 ->
assert (Base64_rfc2045.encode encoder `Await = `Ok) ;
Ke.Rke.cons queue 258 ;
None
| chr -> (
match Base64_rfc2045.encode encoder (`Char (Char.chr chr)) with
| `Ok -> (go [@tailcall]) ()
| `Partial -> (emit [@tailcall]) ())
| exception Ke.Rke.Empty ->
match stream () with
| Some (buf, off, len) ->
iter ~f:(fun chr -> Ke.Rke.push queue (Char.code chr)) buf ~off ~len ;
(go [@tailcall]) ()
| None ->
Ke.Rke.push queue 257 ;
(go [@tailcall]) () in
Base64_rfc2045.dst encoder chunk 0 chunk_length ;
go
let content_encoding fields =
let encoding : Content_encoding.t ref = ref `Bit7 in
let exception Found in
try
List.iter
(function
| Field.Field (_, Content_encoding, v) ->
encoding := v ;
raise Found
| _ -> ())
fields ;
!encoding
with Found -> !encoding
let failf fmt = Fmt.kstr Angstrom.fail fmt
let octet ~max_chunk_size ~emitter boundary =
let open Angstrom in
match boundary with
| None ->
(match content_encoding header with
| `Quoted_printable ->
Log.debug (fun m -> m "Decode the quoted-printable final part.") ;
QP.to_end_of_input_with_push emitter
| `Base64 ->
Log.debug (fun m -> m "Decode the base64 final part.") ;
B64.to_end_of_input_with_push emitter
| `Bit7 | `Bit8 | `Binary ->
Log.debug (fun m -> m "Decode the 8-bit final part.") ;
RAW.to_end_of_input_with_push ~max_chunk_size emitter
| `Ietf_token v | `X_token v ->
failf "Invalid Content-Transfer-Encoding value (%s)" v)
>>= fun () ->
emitter None ;
return ()
| Some boundary ->
let end_of_body = Rfc2046.make_delimiter boundary in
(match content_encoding header with
| `Quoted_printable ->
Log.debug (fun m -> m "Decode a quoted-printable part.") ;
QP.with_push ~push:emitter end_of_body
| `Base64 ->
Log.debug (fun m -> m "Decode a base64 part.") ;
B64.with_emitter ~emitter end_of_body
| `Bit7 | `Bit8 | `Binary ->
Log.debug (fun m -> m "Decode a 8-bit part.") ;
RAW.with_emitter ~max_chunk_size ~emitter end_of_body
| `Ietf_token v | `X_token v ->
failf "Invalid Content-Transfer-Encoding value (%s)" v)
>>= fun () ->
emitter None ;
return ()
type 'id emitters =
Header.t -> (Bigstringaf.t Faraday.iovec option -> unit) * 'id
type discrete = [ `Text | `Image | `Audio | `Video | `Application ]
let boundary =
let content_type = Header.content_type header in
match List.assoc_opt "boundary" (Content_type.parameters content_type) with
| Some (Token boundary) | Some (String boundary) -> Some boundary
| None -> None
let parser :
max_chunk_size:int ->
emitters:'id emitters ->
Field.field list ->
'id t Angstrom.t =
fun ~max_chunk_size ~emitters ->
let open Angstrom in
let rec body parent =
match Content_type.ty (Header.content_type header) with
| `Ietf_token v | `X_token v ->
failf "Invalid Content-Transfer-Encoding value (%s)" v
| #discrete ->
let emitter, id = emitters header in
octet ~max_chunk_size ~emitter parent header >>| fun () ->
Leaf { header; body = id }
| `Multipart ->
match boundary header with
| Some boundary ->
Rfc2046.multipart_body ?parent boundary (body (Option.some boundary))
>>| List.map (fun (, contents) -> contents)
>>| fun parts -> Multipart { header; body = parts }
| None -> failf "Invalid Content-Type, missing boundary" in
body None header
let parser ~max_chunk_size ~emitters content_type =
parser ~max_chunk_size ~emitters
[ Field.Field (Field_name.content_type, Field.Content_type, content_type) ]
let blit src src_off dst dst_off len =
Bigstringaf.blit_from_string src ~src_off dst ~dst_off ~len
let parse :
max_chunk_size:int ->
emitters:'id emitters ->
Content_type.t ->
[ `String of string | `Eof ] ->
[ `Continue | `Done of 'id t | `Fail of string ] =
fun ~max_chunk_size ~emitters content_type ->
let parser = parser ~emitters ~max_chunk_size content_type in
let state = ref (Angstrom.Unbuffered.parse parser) in
let ke = Ke.Rke.create ~capacity:0x1000 Bigarray.char in
fun data ->
match !state with
| Angstrom.Unbuffered.Done (_, tree) -> `Done tree
| Fail (_, _, msg) -> `Fail msg
| Partial { committed; continue } ->
Ke.Rke.N.shift_exn ke committed ;
if committed = 0 then Ke.Rke.compress ke ;
Log.debug (fun m -> m "Partial state of the multipart/form stream.") ;
(match data with
| `String "" -> ()
| `String str ->
Log.debug (fun m ->
m "Capacity of the internal queue: %d byte(s)."
(Ke.Rke.capacity ke)) ;
Log.debug (fun m ->
m "Length of the internal queue: %d byte(s)." (Ke.Rke.length ke)) ;
Ke.Rke.N.push ke ~blit ~length:String.length ~off:0
~len:(String.length str) str ;
let[@warning "-8"] (slice :: _) = Ke.Rke.N.peek ke in
state :=
continue slice ~off:0 ~len:(Bigstringaf.length slice) Incomplete
| `Eof -> (
Log.debug (fun m -> m "End of input.") ;
match Ke.Rke.N.peek ke with
| [] ->
Log.debug (fun m -> m "No more payloads.") ;
state := continue Bigstringaf.empty ~off:0 ~len:0 Complete
| [ slice ] ->
Log.debug (fun m ->
m "Remain one payload: %S" (Bigstringaf.to_string slice)) ;
state :=
continue slice ~off:0 ~len:(Bigstringaf.length slice) Complete
| slice :: _ ->
Log.debug (fun m -> m "Remain multiple payloads") ;
state :=
continue slice ~off:0 ~len:(Bigstringaf.length slice)
Incomplete)) ;
`Continue
let of_stream_tbl stream content_type =
let gen =
let v = ref (-1) in
fun () ->
incr v ;
!v in
let tbl = Hashtbl.create 0x10 in
let emitters =
let idx = gen () in
let buf = Buffer.create 0x100 in
Hashtbl.add tbl idx buf ;
( (function
| Some iovec ->
let str = IOVec.substring iovec in
Buffer.add_string buf str
| None -> ()),
idx ) in
let parse = parse ~emitters ~max_chunk_size:0x100 content_type in
let rec go () =
let data = match stream () with None -> `Eof | Some str -> `String str in
match parse data with
| `Continue -> go ()
| `Done m -> Ok (m, tbl)
| `Fail _msg -> Error (`Msg "Invalid input") in
go ()
let of_stream_to_list stream content_type =
match of_stream_tbl stream content_type with
| Ok (m, tbl) ->
let assoc =
Hashtbl.fold (fun k b a -> (k, Buffer.contents b) :: a) tbl [] in
Ok (m, assoc)
| Error e -> Error e
let of_stream_to_tree stream content_type =
match of_stream_tbl stream content_type with
| Ok (m, tbl) ->
let m' = map (fun k -> Buffer.contents (Hashtbl.find tbl k)) m in
Ok m'
| Error e -> Error e
let stream_of_string str =
let consumed = ref false in
fun () ->
if !consumed
then None
else (
consumed := true ;
Some str)
let of_string_to_list str content_type =
of_stream_to_list (stream_of_string str) content_type
let of_string_to_tree str content_type =
of_stream_to_tree (stream_of_string str) content_type
type part = { header : Header.t; body : (string * int * int) stream }
type multipart = { header : Header.t; parts : part list }
let part ?( = Header.empty) ?disposition ?encoding stream =
let =
match disposition with
| Some v ->
Header.add Field_name.content_disposition
(Field.Content_disposition, v)
header
| None -> header in
let =
match encoding with
| Some v ->
Header.add Field_name.content_transfer_encoding
(Field.Content_encoding, v)
header
| None -> header in
let content_type = Header.content_type header in
let encoding = content_encoding header in
if not (Content_type.is_discrete content_type)
then Fmt.invalid_arg "Content-type MUST be discrete type to a make a part" ;
let stream =
match encoding with
| `Quoted_printable -> to_quoted_printable stream
| `Base64 -> to_base64 stream
| `Bit8 | `Binary | `Bit7 -> stream
| `Ietf_token _ | `X_token _ -> assert false in
{ header; body = stream }
let multipart_content_default =
let open Content_type in
make `Multipart (Subtype.v "form-data") Parameters.empty
let multipart ~rng ?g ?( = Header.empty) ?boundary parts =
let boundary =
match boundary with Some boundary -> boundary | None -> rng ?g 8 in
let boundary = Content_type.Parameters.v boundary in
let content_type =
if Header.exists Field_name.content_type header
then Header.content_type header
else multipart_content_default in
let content_type =
Content_type.with_parameter content_type ("boundary", boundary) in
let =
Header.replace Field_name.content_type
(Field.Content_type, content_type)
header in
{ header; parts }
module Stream = struct
let none () = None
let map f stream =
let go () = match stream () with Some v -> Some (f v) | None -> None in
go
let of_string x =
let once = ref false in
let go () =
if !once
then None
else (
once := true ;
Some (x, 0, String.length x)) in
go
let crlf () = of_string "\r\n"
let concat s0 s1 =
let c = ref s0 in
let rec go () =
match !c () with
| Some x -> Some x
| None ->
if !c == s0
then (
c := s1 ;
go ())
else None in
go
let ( @ ) a b = concat a b
let of_part { ; body } =
let content_stream =
map
(fun s -> (s, 0, String.length s))
(Prettym.to_stream Header.Encoder.header header) in
content_stream @ crlf () @ body
end
let to_stream : multipart -> Header.t * (string * int * int) stream =
fun { ; parts } ->
let boundary =
match Content_type.boundary (Header.content_type header) with
| Some v -> v
| None -> Fmt.failwith "Multipart MUST have a boundary"
in
let beginner = Rfc2046.make_dash_boundary boundary ^ "\r\n" in
let inner = Rfc2046.make_delimiter boundary ^ "\r\n" in
let closer = Rfc2046.make_close_delimiter boundary ^ "\r\n" in
let rec go stream = function
| [] -> Stream.none
| [ x ] -> Stream.(stream @ of_part x @ of_string closer)
| x :: r ->
let stream = Stream.(stream @ of_part x @ of_string inner) in
go stream r in
(header, go (Stream.of_string beginner) parts)