@@ -52,48 +52,48 @@ module Response = struct
52
52
let rec read_body ~buffer ~headers ~body_remaining reader =
53
53
match Http.Header. get_transfer_encoding headers with
54
54
| Http.Transfer. Chunked -> (
55
- debug (fun f -> f " Reading chunked body" );
56
- debug (fun f -> f " -> body_remaining: %d" body_remaining);
55
+ trace (fun f -> f " Reading chunked body" );
56
+ trace (fun f -> f " -> body_remaining: %d" body_remaining);
57
57
match read_chunked_body ~buffer reader with
58
58
| Ok `no_more_chunks -> `finished Bytestring. empty
59
59
| Ok (`chunk (body , buffer )) -> `continue (body, buffer)
60
60
| Error reason -> `error reason)
61
61
| _ -> (
62
- debug (fun f -> f " reading content-length body" );
62
+ trace (fun f -> f " reading content-length body" );
63
63
match read_content_length_body reader buffer body_remaining with
64
64
| Ok (body , buffer , body_remaining ) ->
65
- debug (fun f ->
65
+ trace (fun f ->
66
66
f " read content_length body: body_remaning=%d buffer=%d"
67
67
body_remaining (Bytestring. length buffer));
68
68
if body_remaining = 0 && Bytestring. length buffer = 0 then (
69
- debug (fun f -> f " read content_length body: ok" );
69
+ trace (fun f -> f " read content_length body: ok" );
70
70
`finished body)
71
71
else (
72
- debug (fun f -> f " read content_length body: more" );
72
+ trace (fun f -> f " read content_length body: more" );
73
73
`continue (body, buffer))
74
74
| Error reason -> `error reason)
75
75
76
76
and read_chunked_body ~buffer reader =
77
77
match split buffer with
78
78
| [ zero; _ ] when String. equal (Bytestring. to_string zero) " 0" ->
79
- debug (fun f -> f " read_chunked_body: last chunk!" );
79
+ trace (fun f -> f " read_chunked_body: last chunk!" );
80
80
Ok `no_more_chunks
81
81
| [ chunk_size; chunk_data ] -> (
82
82
let chunk_size =
83
83
Int64. (of_string (" 0x" ^ Bytestring. to_string chunk_size) |> to_int)
84
84
in
85
- debug (fun f -> f " read_chunked_body: chunk_size=%d" chunk_size);
85
+ trace (fun f -> f " read_chunked_body: chunk_size=%d" chunk_size);
86
86
let binstr_data = Bytestring. to_string chunk_data in
87
- debug (fun f ->
87
+ trace (fun f ->
88
88
f " read_chunked_body: (%d bytes)" (String. length binstr_data));
89
89
let binstr_data = binstr_data |> Bitstring. bitstring_of_string in
90
90
match % bitstring binstr_data with
91
91
| {| full_chunk : (chunk_size * 8 ) : string ;
92
92
" \r\n " : 2 * 8 : string ;
93
93
rest : - 1 : bitstring |}
94
94
->
95
- debug (fun f -> f " read_chunked_body: read full chunk" );
96
- debug (fun f ->
95
+ trace (fun f -> f " read_chunked_body: read full chunk" );
96
+ trace (fun f ->
97
97
f " read_chunked_body: rest=%d" (Bitstring. bitstring_length rest));
98
98
let rest =
99
99
Bytestring. of_string (Bitstring. string_of_bitstring rest)
@@ -102,38 +102,38 @@ module Response = struct
102
102
Ok (`chunk (full_chunk, rest))
103
103
| {| _ |} ->
104
104
let left_to_read = chunk_size - Bytestring. length chunk_data + 2 in
105
- debug (fun f ->
105
+ trace (fun f ->
106
106
f " read_chunked_body: reading more data left_to_read=%d"
107
107
left_to_read);
108
108
let * chunk = read ~to_read: left_to_read reader in
109
109
let buffer = Bytestring. join buffer chunk in
110
110
read_chunked_body ~buffer reader)
111
111
| _ ->
112
- debug (fun f -> f " read_chunked_body: need more data" );
112
+ trace (fun f -> f " read_chunked_body: need more data" );
113
113
let * chunk = Bytestring. with_bytes (fun buf -> IO. read reader buf) in
114
114
let buffer = Bytestring. join buffer chunk in
115
115
read_chunked_body ~buffer reader
116
116
117
117
and read_content_length_body reader buffer body_remaining =
118
118
let limit = body_remaining in
119
119
let to_read = limit - Bytestring. length buffer in
120
- debug (fun f ->
120
+ trace (fun f ->
121
121
f " read_content_length_body: up to limit=%d with preread_buffer=%d"
122
122
limit (Bytestring. length buffer));
123
123
match body_remaining with
124
124
| 0 when Bytestring. length buffer > = limit ->
125
- debug (fun f -> f " read_content_length_body: can answer with buffer" );
125
+ trace (fun f -> f " read_content_length_body: can answer with buffer" );
126
126
(* let len = Int.min limit (Bytestring.length buffer) in *)
127
127
(* let body = Bytestring.sub ~off:0 ~len buffer in *)
128
128
Ok (buffer, Bytestring. empty, 0 )
129
129
| n when n < 0 || to_read < 0 ->
130
- debug (fun f -> f " read_content_length_body: excess body" );
130
+ trace (fun f -> f " read_content_length_body: excess body" );
131
131
Error `Excess_body_read
132
132
| remaining_bytes ->
133
133
let to_read =
134
134
Int. min (limit - Bytestring. length buffer) remaining_bytes
135
135
in
136
- debug (fun f -> f " read_content_length_body: need to read %d" to_read);
136
+ trace (fun f -> f " read_content_length_body: need to read %d" to_read);
137
137
let * chunk = read ~to_read reader in
138
138
let body = Bytestring. join buffer chunk in
139
139
let body_remaining = remaining_bytes - Bytestring. length body in
@@ -143,15 +143,15 @@ module Response = struct
143
143
if to_read = 0 then Ok buffer
144
144
else
145
145
let buf = Bytes. create to_read in
146
- debug (fun f -> f " reading to_read=%d" to_read);
146
+ trace (fun f -> f " reading to_read=%d" to_read);
147
147
let * len = IO. read reader buf in
148
148
let chunk =
149
149
Bytestring. of_string (Bytes. unsafe_to_string (Bytes. sub buf 0 len))
150
150
in
151
151
let remaining_bytes = to_read - Bytestring. length chunk in
152
152
let buffer = Bytestring. join buffer chunk in
153
- debug (fun f -> f " read: remaining_bytes %d" remaining_bytes);
154
- debug (fun f -> f " read: buffer=%d" (Bytestring. length buffer));
153
+ trace (fun f -> f " read: remaining_bytes %d" remaining_bytes);
154
+ trace (fun f -> f " read: buffer=%d" (Bytestring. length buffer));
155
155
if remaining_bytes > 0 then read ~to_read: remaining_bytes ~buffer reader
156
156
else Ok buffer
157
157
0 commit comments