Diff

mod_http_debug/mod_http_debug.lua @ 5490:91564b57e595

mod_http_debug: Handle more HTTP methods Often you might want to see what POST data was sent, or such.
author Kim Alvefur <zash@zash.se>
date Fri, 26 May 2023 15:36:04 +0200
parent 5333:10fcfa7e62a1
child 5491:7842502c1157
line wrap: on
line diff
--- a/mod_http_debug/mod_http_debug.lua	Fri May 26 15:20:04 2023 +0200
+++ b/mod_http_debug/mod_http_debug.lua	Fri May 26 15:36:04 2023 +0200
@@ -1,26 +1,32 @@
 local json = require "util.json"
 
 module:depends("http")
+local function handle_request(event)
+	local request = event.request;
+	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;
+end
+
 module:provides("http", {
-		route = {
-			GET = function(event)
-				local request = event.request;
-				return {
-					status_code = 200;
-					headers = {
-						content_type = "application/json",
-					},
-					body = json.encode {
-						body = request.body;
-						headers = request.headers;
-						httpversion = request.httpversion;
-						ip = request.ip;
-						method = request.method;
-						path = request.path;
-						secure = request.secure;
-						url = request.url;
-					}
-				}
-			end;
-		}
-	})
+	route = route;
+})