Diff

net/http/parser.lua @ 12882:9ed628635dc6 0.12

net.http.parser: Improve handling of responses without content-length This ensures that we support responses without a content-length header, and allow streaming them through the streaming handler interface. An example of such a response would be Server-Sent Events streams.
author Matthew Wild <mwild1@gmail.com>
date Thu, 09 Feb 2023 22:57:32 +0000
parent 11727:f3aee8a825cc
child 12889:94a99330ce87
line wrap: on
line diff
--- a/net/http/parser.lua	Thu Feb 09 22:37:54 2023 +0000
+++ b/net/http/parser.lua	Thu Feb 09 22:57:32 2023 +0000
@@ -130,10 +130,13 @@
 							partial = true;
 						};
 					end
-					if len and len > bodylimit then
+					if not len or len > bodylimit then
 						-- Early notification, for redirection
 						success_cb(packet);
-						if not packet.body_sink then error = true; return error_cb("content-length-limit-exceeded"); end
+						if not packet.body_sink and (len and len > bodylimit) then
+							error = true;
+							return error_cb("content-length-limit-exceeded");
+						end
 					end
 					if chunked and not packet.body_sink then
 						success_cb(packet);
@@ -174,9 +177,11 @@
 						end
 					elseif packet.body_sink then
 						local chunk = buffer:read_chunk(len);
-						while chunk and len > 0 do
+						while chunk and (not len or len > 0) do
 							if packet.body_sink:write(chunk) then
-								len = len - #chunk;
+								if len then
+									len = len - #chunk;
+								end
 								chunk = buffer:read_chunk(len);
 							else
 								error = true;
@@ -188,9 +193,9 @@
 							packet.partial = nil;
 							success_cb(packet);
 						end
-					elseif buffer:length() >= len then
+					elseif not len or buffer:length() >= len then -- or not len
 						assert(not chunked)
-						packet.body = buffer:read(len) or "";
+						packet.body = len and buffer:read(len) or buffer:read_chunk() or "";
 						state = nil;
 						packet.partial = nil;
 						success_cb(packet);