comparison net/http/server.lua @ 14076:a558af9e8c35

net.http.server: Allow sending a file slice To enable implementing Range requests
author Kim Alvefur <zash@zash.se>
date Fri, 21 Nov 2025 21:41:35 +0100
parents 7a31775f1d7f
children
comparison
equal deleted inserted replaced
14075:7a31775f1d7f 14076:a558af9e8c35
363 local output = prepare_header(response); 363 local output = prepare_header(response);
364 t_insert(output, body); 364 t_insert(output, body);
365 response.conn:write(t_concat(output)); 365 response.conn:write(t_concat(output));
366 response:done(); 366 response:done();
367 end 367 end
368 function _M.send_file(response, f) 368 function _M.send_file(response, f, length)
369 if response.is_head_request then 369 if response.is_head_request then
370 if f.close then f:close(); end 370 if f.close then f:close(); end
371 return _M.send_head_response(response); 371 return _M.send_head_response(response);
372 end 372 end
373 if response.finished then return; end 373 if response.finished then return; end
377 response._send_more = function () 377 response._send_more = function ()
378 if response.finished then 378 if response.finished then
379 incomplete[response.conn] = nil; 379 incomplete[response.conn] = nil;
380 return; 380 return;
381 end 381 end
382 local chunk = f:read(blocksize); 382 local chunksize = blocksize;
383 if chunk then 383 if length and length < chunksize then chunksize = length; end
384 if chunked then 384
385 chunk = ("%x\r\n%s\r\n"):format(#chunk, chunk); 385 local chunk = chunksize > 0 and f:read(chunksize);
386 end 386 if not chunk then
387 -- io.write("."); io.flush();
388 response.conn:write(chunk);
389 else
390 incomplete[response.conn] = nil; 387 incomplete[response.conn] = nil;
391 if f.close then f:close(); end 388 if f.close then f:close(); end
392 if chunked then 389 if chunked then
393 response.conn:write("0\r\n\r\n"); 390 response.conn:write("0\r\n\r\n");
394 end 391 end
395 -- io.write("\n"); 392 -- io.write("\n");
396 return response:done(); 393 return response:done();
394 else
395 if length then length = length - #chunk; end
396 if chunked then chunk = ("%x\r\n%s\r\n"):format(#chunk, chunk); end
397 -- io.write("."); io.flush();
398 response.conn:write(chunk);
397 end 399 end
398 end 400 end
399 _M.write_headers(response); 401 _M.write_headers(response);
400 return true; 402 return true;
401 end 403 end