@@ -27,6 +27,7 @@ type st = {
2727 boundary : string ;
2828 ic : Iostream.In .t ;
2929 buf : buf ; (* * Used to split on the boundary *)
30+ mutable first : bool ; (* * Are we parsing the first boundary? *)
3031 mutable eof_split : bool ;
3132 buf_out : buf ; (* * Used to return output slices *)
3233 mutable st_out : out_state ;
@@ -37,6 +38,7 @@ let create ?(buf_size = 64 * 1024) ?(out_buf_size = 8 * 1024) ~boundary ic : st
3738 let ic = (ic : #Iostream.In.t :> Iostream.In.t ) in
3839 {
3940 boundary;
41+ first = true ;
4042 ic;
4143 buf = { bs = Bytes. create buf_size; len = 0 };
4244 eof_split = false ;
@@ -46,7 +48,14 @@ let create ?(buf_size = 64 * 1024) ?(out_buf_size = 8 * 1024) ~boundary ic : st
4648
4749type chunk = Delim | Eof | Read of int
4850
49- let [@ inline] min_len_ (self : st ) : int = 2 + String. length self.boundary
51+ let [@ inline] prefix_size_ (self : st ) : int =
52+ if self.first then
53+ 2
54+ else
55+ 4
56+
57+ let [@ inline] min_len_ (self : st ) : int =
58+ prefix_size_ self + String. length self.boundary
5059
5160exception Found_boundary of int
5261
@@ -74,15 +83,27 @@ let rec read_chunk_ (self : st) buf i_buf len : chunk =
7483 ) else (
7584 try
7685 let i = ref 0 in
77- let end_pos = min len self.buf.len - 2 - String. length self.boundary in
86+ let end_pos =
87+ min len self.buf.len - prefix_size_ self - String. length self.boundary
88+ in
7889 while ! i < = end_pos do
7990 if
80- Bytes. unsafe_get self.buf.bs ! i = '-'
91+ self.first
92+ && Bytes. unsafe_get self.buf.bs ! i = '-'
8193 && Bytes. unsafe_get self.buf.bs (! i + 1 ) = '-'
8294 && Utils_. string_eq
8395 ~a: (Bytes. unsafe_to_string self.buf.bs)
8496 ~a_start: (! i + 2 ) ~b: self.boundary
8597 ~len: (String. length self.boundary)
98+ || (not self.first)
99+ && Bytes. unsafe_get self.buf.bs ! i = '\r'
100+ && Bytes. unsafe_get self.buf.bs (! i + 1 ) = '\n'
101+ && Bytes. unsafe_get self.buf.bs (! i + 2 ) = '-'
102+ && Bytes. unsafe_get self.buf.bs (! i + 3 ) = '-'
103+ && Utils_. string_eq
104+ ~a: (Bytes. unsafe_to_string self.buf.bs)
105+ ~a_start: (! i + 4 ) ~b: self.boundary
106+ ~len: (String. length self.boundary)
86107 then
87108 raise_notrace (Found_boundary ! i);
88109 incr i
@@ -93,7 +114,8 @@ let rec read_chunk_ (self : st) buf i_buf len : chunk =
93114 Read n_read
94115 with
95116 | Found_boundary 0 ->
96- shift_left_ self.buf (2 + String. length self.boundary);
117+ shift_left_ self.buf (prefix_size_ self + String. length self.boundary);
118+ self.first < - false ;
97119 Delim
98120 | Found_boundary n ->
99121 let n_read = min n len in
@@ -189,6 +211,7 @@ and parse_headers_rec (self : st) acc : Headers.t =
189211 )
190212 | i ->
191213 let line = Bytes. sub_string self.buf_out.bs 0 i in
214+ Printf. eprintf " parse header line %S\n %!" line;
192215 shift_left_ self.buf_out (i + 2 );
193216 if line = " " then
194217 List. rev acc
0 commit comments