Software /
code /
prosody
Comparison
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 |
comparison
equal
deleted
inserted
replaced
12881:91baddaeea84 | 12882:9ed628635dc6 |
---|---|
128 body_sink = nil; | 128 body_sink = nil; |
129 chunked = chunked; | 129 chunked = chunked; |
130 partial = true; | 130 partial = true; |
131 }; | 131 }; |
132 end | 132 end |
133 if len and len > bodylimit then | 133 if not len or len > bodylimit then |
134 -- Early notification, for redirection | 134 -- Early notification, for redirection |
135 success_cb(packet); | 135 success_cb(packet); |
136 if not packet.body_sink then error = true; return error_cb("content-length-limit-exceeded"); end | 136 if not packet.body_sink and (len and len > bodylimit) then |
137 error = true; | |
138 return error_cb("content-length-limit-exceeded"); | |
139 end | |
137 end | 140 end |
138 if chunked and not packet.body_sink then | 141 if chunked and not packet.body_sink then |
139 success_cb(packet); | 142 success_cb(packet); |
140 if not packet.body_sink then | 143 if not packet.body_sink then |
141 packet.body_buffer = dbuffer.new(buflimit); | 144 packet.body_buffer = dbuffer.new(buflimit); |
172 else -- Partial chunk remaining | 175 else -- Partial chunk remaining |
173 break; | 176 break; |
174 end | 177 end |
175 elseif packet.body_sink then | 178 elseif packet.body_sink then |
176 local chunk = buffer:read_chunk(len); | 179 local chunk = buffer:read_chunk(len); |
177 while chunk and len > 0 do | 180 while chunk and (not len or len > 0) do |
178 if packet.body_sink:write(chunk) then | 181 if packet.body_sink:write(chunk) then |
179 len = len - #chunk; | 182 if len then |
183 len = len - #chunk; | |
184 end | |
180 chunk = buffer:read_chunk(len); | 185 chunk = buffer:read_chunk(len); |
181 else | 186 else |
182 error = true; | 187 error = true; |
183 return error_cb("body-sink-write-failure"); | 188 return error_cb("body-sink-write-failure"); |
184 end | 189 end |
186 if len == 0 then | 191 if len == 0 then |
187 state = nil; | 192 state = nil; |
188 packet.partial = nil; | 193 packet.partial = nil; |
189 success_cb(packet); | 194 success_cb(packet); |
190 end | 195 end |
191 elseif buffer:length() >= len then | 196 elseif not len or buffer:length() >= len then -- or not len |
192 assert(not chunked) | 197 assert(not chunked) |
193 packet.body = buffer:read(len) or ""; | 198 packet.body = len and buffer:read(len) or buffer:read_chunk() or ""; |
194 state = nil; | 199 state = nil; |
195 packet.partial = nil; | 200 packet.partial = nil; |
196 success_cb(packet); | 201 success_cb(packet); |
197 else | 202 else |
198 break; | 203 break; |