Comparison

net/http/parser.lua @ 12889:94a99330ce87 0.12

net.http.parser: Fix off-by-one error in chunk parser
author Matthew Wild <mwild1@gmail.com>
date Fri, 17 Feb 2023 17:01:19 +0000
parent 12882:9ed628635dc6
child 12974:ba409c67353b
child 13378:db30ffbf2090
comparison
equal deleted inserted replaced
12887:68df46926c26 12889:94a99330ce87
151 local chunk_header = buffer:sub(1, 512); -- XXX How large do chunk headers grow? 151 local chunk_header = buffer:sub(1, 512); -- XXX How large do chunk headers grow?
152 local chunk_size, chunk_start = chunk_header:match("^(%x+)[^\r\n]*\r\n()"); 152 local chunk_size, chunk_start = chunk_header:match("^(%x+)[^\r\n]*\r\n()");
153 if not chunk_size then return; end 153 if not chunk_size then return; end
154 chunk_size = chunk_size and tonumber(chunk_size, 16); 154 chunk_size = chunk_size and tonumber(chunk_size, 16);
155 if not chunk_size then error = true; return error_cb("invalid-chunk-size"); end 155 if not chunk_size then error = true; return error_cb("invalid-chunk-size"); end
156
156 if chunk_size == 0 and chunk_header:find("\r\n\r\n", chunk_start-2, true) then 157 if chunk_size == 0 and chunk_header:find("\r\n\r\n", chunk_start-2, true) then
157 local body_buffer = packet.body_buffer; 158 local body_buffer = packet.body_buffer;
158 if body_buffer then 159 if body_buffer then
159 packet.body_buffer = nil; 160 packet.body_buffer = nil;
160 body_buffer:collapse(); 161 body_buffer:collapse();
166 buf = buf:gsub("^.-\r\n\r\n", ""); -- This ensure extensions and trailers are stripped 167 buf = buf:gsub("^.-\r\n\r\n", ""); -- This ensure extensions and trailers are stripped
167 buffer:write(buf); 168 buffer:write(buf);
168 state, chunked = nil, nil; 169 state, chunked = nil, nil;
169 packet.partial = nil; 170 packet.partial = nil;
170 success_cb(packet); 171 success_cb(packet);
171 elseif buffer:length() - chunk_start - 2 >= chunk_size then -- we have a chunk 172 elseif buffer:length() - chunk_start - 1 >= chunk_size then -- we have a chunk
172 buffer:discard(chunk_start - 1); -- TODO verify that it's not off-by-one 173 buffer:discard(chunk_start - 1);
173 (packet.body_sink or packet.body_buffer):write(buffer:read(chunk_size)); 174 (packet.body_sink or packet.body_buffer):write(buffer:read(chunk_size));
174 buffer:discard(2); -- CRLF 175 buffer:discard(2); -- CRLF
175 else -- Partial chunk remaining 176 else -- Partial chunk remaining
176 break; 177 break;
177 end 178 end