Comparison

net/http/parser.lua @ 11184:2ede7f43ccfe

net.http.parser: Expose 'partial', 'chunked' and 'body_length' on packets
author Matthew Wild <mwild1@gmail.com>
date Wed, 21 Oct 2020 10:34:16 +0100
parent 11029:5550fc5e83f3
child 11727:f3aee8a825cc
comparison
equal deleted inserted replaced
11183:2ac63715ef6f 11184:2ede7f43ccfe
45 if error then return nil, "parse has failed"; end 45 if error then return nil, "parse has failed"; end
46 if not data then -- EOF 46 if not data then -- EOF
47 if state and client and not len then -- reading client body until EOF 47 if state and client and not len then -- reading client body until EOF
48 buffer:collapse(); 48 buffer:collapse();
49 packet.body = buffer:read_chunk() or ""; 49 packet.body = buffer:read_chunk() or "";
50 packet.partial = nil;
50 success_cb(packet); 51 success_cb(packet);
51 state = nil; 52 state = nil;
52 elseif buffer:length() ~= 0 then -- unexpected EOF 53 elseif buffer:length() ~= 0 then -- unexpected EOF
53 error = true; return error_cb("unexpected-eof"); 54 error = true; return error_cb("unexpected-eof");
54 end 55 end
94 packet = { 95 packet = {
95 code = status_code; 96 code = status_code;
96 httpversion = httpversion; 97 httpversion = httpversion;
97 headers = headers; 98 headers = headers;
98 body = false; 99 body = false;
100 body_length = len;
101 chunked = chunked;
102 partial = true;
99 -- COMPAT the properties below are deprecated 103 -- COMPAT the properties below are deprecated
100 responseversion = httpversion; 104 responseversion = httpversion;
101 responseheaders = headers; 105 responseheaders = headers;
102 }; 106 };
103 else 107 else
120 path = path; 124 path = path;
121 httpversion = httpversion; 125 httpversion = httpversion;
122 headers = headers; 126 headers = headers;
123 body = false; 127 body = false;
124 body_sink = nil; 128 body_sink = nil;
129 chunked = chunked;
130 partial = true;
125 }; 131 };
126 end 132 end
127 if len and len > bodylimit then 133 if len and len > bodylimit then
128 -- Early notification, for redirection 134 -- Early notification, for redirection
129 success_cb(packet); 135 success_cb(packet);
155 buffer:collapse(); 161 buffer:collapse();
156 local buf = buffer:read_chunk(); 162 local buf = buffer:read_chunk();
157 buf = buf:gsub("^.-\r\n\r\n", ""); -- This ensure extensions and trailers are stripped 163 buf = buf:gsub("^.-\r\n\r\n", ""); -- This ensure extensions and trailers are stripped
158 buffer:write(buf); 164 buffer:write(buf);
159 state, chunked = nil, nil; 165 state, chunked = nil, nil;
166 packet.partial = nil;
160 success_cb(packet); 167 success_cb(packet);
161 elseif buffer:length() - chunk_start - 2 >= chunk_size then -- we have a chunk 168 elseif buffer:length() - chunk_start - 2 >= chunk_size then -- we have a chunk
162 buffer:discard(chunk_start - 1); -- TODO verify that it's not off-by-one 169 buffer:discard(chunk_start - 1); -- TODO verify that it's not off-by-one
163 (packet.body_sink or packet.body_buffer):write(buffer:read(chunk_size)); 170 (packet.body_sink or packet.body_buffer):write(buffer:read(chunk_size));
164 buffer:discard(2); -- CRLF 171 buffer:discard(2); -- CRLF
174 else 181 else
175 error = true; 182 error = true;
176 return error_cb("body-sink-write-failure"); 183 return error_cb("body-sink-write-failure");
177 end 184 end
178 end 185 end
179 if len == 0 then state = nil; success_cb(packet); end 186 if len == 0 then
187 state = nil;
188 packet.partial = nil;
189 success_cb(packet);
190 end
180 elseif buffer:length() >= len then 191 elseif buffer:length() >= len then
181 assert(not chunked) 192 assert(not chunked)
182 packet.body = buffer:read(len) or ""; 193 packet.body = buffer:read(len) or "";
183 state = nil; success_cb(packet); 194 state = nil;
195 packet.partial = nil;
196 success_cb(packet);
184 else 197 else
185 break; 198 break;
186 end 199 end
187 else 200 else
188 break; 201 break;