Comparison

net/http/server.lua @ 10323:73938168681c

net.http.server: Ensure HEAD requests are sent with empty body
author Kim Alvefur <zash@zash.se>
date Sat, 12 Oct 2019 18:27:02 +0200
parent 9624:cc9dff0212f4
child 10324:3f4c25425589
comparison
equal deleted inserted replaced
10322:b54c94f25947 10323:73938168681c
192 response_conn_header = "Keep-Alive"; 192 response_conn_header = "Keep-Alive";
193 else 193 else
194 response_conn_header = httpversion == "1.1" and "close" or nil 194 response_conn_header = httpversion == "1.1" and "close" or nil
195 end 195 end
196 196
197 local is_head_request = request.method == "HEAD";
198
197 local response = { 199 local response = {
198 request = request; 200 request = request;
201 is_head_request = is_head_request;
199 status_code = 200; 202 status_code = 200;
200 headers = { date = date_header, connection = response_conn_header }; 203 headers = { date = date_header, connection = response_conn_header };
201 persistent = persistent; 204 persistent = persistent;
202 conn = conn; 205 conn = conn;
203 send = _M.send_response; 206 send = _M.send_response;
289 end 292 end
290 t_insert(output, "\r\n\r\n"); 293 t_insert(output, "\r\n\r\n");
291 return output; 294 return output;
292 end 295 end
293 _M.prepare_header = prepare_header; 296 _M.prepare_header = prepare_header;
297 function _M.send_head_response(response)
298 if response.finished then return; end
299 local output = prepare_header(response);
300 response.conn:write(t_concat(output));
301 response:done();
302 end
294 function _M.send_response(response, body) 303 function _M.send_response(response, body)
295 if response.finished then return; end 304 if response.finished then return; end
296 body = body or response.body or ""; 305 body = body or response.body or "";
297 response.headers.content_length = #body; 306 response.headers.content_length = #body;
307 if response.is_head_request then
308 return _M.send_head_response(response)
309 end
298 local output = prepare_header(response); 310 local output = prepare_header(response);
299 t_insert(output, body); 311 t_insert(output, body);
300 response.conn:write(t_concat(output)); 312 response.conn:write(t_concat(output));
301 response:done(); 313 response:done();
302 end 314 end
303 function _M.send_file(response, f) 315 function _M.send_file(response, f)
316 if response.is_head_request then
317 if f.close then f:close(); end
318 return _M.send_head_response(response);
319 end
304 if response.finished then return; end 320 if response.finished then return; end
305 local chunked = not response.headers.content_length; 321 local chunked = not response.headers.content_length;
306 if chunked then response.headers.transfer_encoding = "chunked"; end 322 if chunked then response.headers.transfer_encoding = "chunked"; end
307 incomplete[response.conn] = response; 323 incomplete[response.conn] = response;
308 response._send_more = function () 324 response._send_more = function ()