mod_http_debug/mod_http_debug.lua
author Kim Alvefur <zash@zash.se>
Sun, 03 Mar 2024 11:23:40 +0100
changeset 5857 97c9b76867ca
parent 5496 b6af4d1ff8c1
permissions -rw-r--r--
mod_log_ringbuffer: Detach event handlers on logging reload (thanks Menel) Otherwise the global event handlers accumulate, one added each time logging is reoladed, and each invocation of the signal or event triggers one dump of each created ringbuffer.

local json = require "util.json"

module:depends("http")
local function handle_request(event)
	local request = event.request;
	(request.log or module._log)("debug", "%s -- %s %q HTTP/%s -- %q -- %s", request.ip, request.method, request.url, request.httpversion, request.headers, request.body);
	return {
		status_code = 200;
		headers = { content_type = "application/json" };
		host = module.host;
		body = json.encode {
			body = request.body;
			headers = request.headers;
			httpversion = request.httpversion;
			id = request.id;
			ip = request.ip;
			method = request.method;
			path = request.path;
			secure = request.secure;
			url = request.url;
		};
	}
end

local methods = module:get_option_set("http_debug_methods", { "GET"; "HEAD"; "DELETE"; "OPTIONS"; "PATCH"; "POST"; "PUT" });
local route = {};
for method in methods do
	route[method] = handle_request;
	route[method .. " /*"] = handle_request;
end

module:provides("http", {
	route = route;
})