<commit_before>
local socket = require "socket"
local server = require "net.server"
local url_parse = require "socket.url".parse;

local connlisteners_start = require "net.connlisteners".start;
local connlisteners_get = require "net.connlisteners".get;
local listener;

local t_insert, t_concat = table.insert, table.concat;
local s_match, s_gmatch = string.match, string.gmatch;
local tonumber, tostring, pairs = tonumber, tostring, pairs;

local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end });
local urlencode = function (s) return s and (s:gsub("%W", function (c) return string.format("%%%x", c:byte()); end)); end

local log = require "util.logger".init("httpserver");

local http_servers = {};

module "httpserver"

local default_handler;

local function expectbody(reqt)
    return reqt.method == "POST";
end

local function send_response(request, response)
	-- Write status line
	local resp;
	if response.body then
		log("debug", "Sending response to %s", request.id);
		resp = { "HTTP/1.0 ", response.status or "200 OK", "\r\n"};
		local h = response.headers;
		if h then
			for k, v in pairs(h) do
				t_insert(resp, k);
				t_insert(resp, ": ");
				t_insert(resp, v);
				t_insert(resp, "\r\n");
			end
		end
		if response.body and not (h and h["Content-Length"]) then
			t_insert(resp, "Content-Length: ");
			t_insert(resp, #response.body);
			t_insert(resp, "\r\n");
		end
		t_insert(resp, "\r\n");
		
		if response.body and request.method ~= "HEAD" then
			t_insert(resp, response.body);
		end
	else
		-- Response we have is just a string (the body)
		log("debug", "Sending response to %s: %s", request.id, response);
		
		resp = { "HTTP/1.0 200 OK\r\n" };
		t_insert(resp, "Connection: close\r\n");
		t_insert(resp, "Content-Length: ");
		t_insert(resp, #response);
		t_insert(resp, "\r\n\r\n");
		
		t_insert(resp, response);
	end
	request.write(t_concat(resp));
	if not request.stayopen then
		request:destroy();
	end
end

local function call_callback(request, err)
	if request.handled then return; end
	request.handled = true;
	local callback = request.callback;
	if not callback and request.path then
		local path = request.url.path;
		local base = path:match("^/([^/?]+)");
		if not base then
			base = path:match("^http://[^/?]+/([^/?]+)");
		end
		
		callback = (request.server and request.server.handlers[base]) or default_handler;
		if callback == default_handler then
			log("debug", "Default callback for this request (base: "..tostring(base)..")")
		end
	end
	if callback then
		if err then
			log("debug", "Request error: "..err);
			if not callback(nil, err, request) then
				destroy_request(request);
			end
			return;
		end
		
		local response = callback(request.method, request.body and t_concat(request.body), request);
		if response then
			if response == true and not request.destroyed then
				-- Keep connection open, we will reply later
				log("warn", "Request %s left open, on_destroy is %s", request.id, tostring(request.on_destroy));
			else
				-- Assume response
				send_response(request, response);
				destroy_request(request);
			end
		else
			log("debug", "Request handler provided no response, destroying request...");
			-- No response, close connection
			destroy_request(request);
		end
	end
end
<commit_msg>net.httpserver: Fix traceback when sending response to a destroyed request<commit_after>
local socket = require "socket"
local server = require "net.server"
local url_parse = require "socket.url".parse;

local connlisteners_start = require "net.connlisteners".start;
local connlisteners_get = require "net.connlisteners".get;
local listener;

local t_insert, t_concat = table.insert, table.concat;
local s_match, s_gmatch = string.match, string.gmatch;
local tonumber, tostring, pairs = tonumber, tostring, pairs;

local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end });
local urlencode = function (s) return s and (s:gsub("%W", function (c) return string.format("%%%x", c:byte()); end)); end

local log = require "util.logger".init("httpserver");

local http_servers = {};

module "httpserver"

local default_handler;

local function expectbody(reqt)
    return reqt.method == "POST";
end

local function send_response(request, response)
	-- Write status line
	local resp;
	if response.body then
		log("debug", "Sending response to %s", request.id);
		resp = { "HTTP/1.0 ", response.status or "200 OK", "\r\n"};
		local h = response.headers;
		if h then
			for k, v in pairs(h) do
				t_insert(resp, k);
				t_insert(resp, ": ");
				t_insert(resp, v);
				t_insert(resp, "\r\n");
			end
		end
		if response.body and not (h and h["Content-Length"]) then
			t_insert(resp, "Content-Length: ");
			t_insert(resp, #response.body);
			t_insert(resp, "\r\n");
		end
		t_insert(resp, "\r\n");
		
		if response.body and request.method ~= "HEAD" then
			t_insert(resp, response.body);
		end
	else
		-- Response we have is just a string (the body)
		log("debug", "Sending response to %s: %s", request.id, response);
		
		resp = { "HTTP/1.0 200 OK\r\n" };
		t_insert(resp, "Connection: close\r\n");
		t_insert(resp, "Content-Length: ");
		t_insert(resp, #response);
		t_insert(resp, "\r\n\r\n");
		
		t_insert(resp, response);
	end
	request.write(t_concat(resp));
	if not request.stayopen then
		request:destroy();
	end
end

local function call_callback(request, err)
	if request.handled then return; end
	request.handled = true;
	local callback = request.callback;
	if not callback and request.path then
		local path = request.url.path;
		local base = path:match("^/([^/?]+)");
		if not base then
			base = path:match("^http://[^/?]+/([^/?]+)");
		end
		
		callback = (request.server and request.server.handlers[base]) or default_handler;
		if callback == default_handler then
			log("debug", "Default callback for this request (base: "..tostring(base)..")")
		end
	end
	if callback then
		if err then
			log("debug", "Request error: "..err);
			if not callback(nil, err, request) then
				destroy_request(request);
			end
			return;
		end
		
		local response = callback(request.method, request.body and t_concat(request.body), request);
		if response then
			if response == true and not request.destroyed then
				-- Keep connection open, we will reply later
				log("warn", "Request %s left open, on_destroy is %s", request.id, tostring(request.on_destroy));
			elseif response ~= true then
				-- Assume response
				send_response(request, response);
				destroy_request(request);
			end
		else
			log("debug", "Request handler provided no response, destroying request...");
			-- No response, close connection
			destroy_request(request);
		end
	end
end

{'fraction_non_alphanumeric': 0.14436358235556215, 'fraction_numerical': 0.003267488489529185, 'mean_word_length': 3.9844559585492227, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1732086', 'n_tokens_mistral': 2437, 'n_tokens_neox': 2192, 'n_words': 670}