changeset 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
parents a7188eb4ded4
children 7842502c1157
files mod_http_debug/README.md mod_http_debug/mod_http_debug.lua
diffstat 2 files changed, 37 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/mod_http_debug/README.md	Fri May 26 15:20:04 2023 +0200
+++ b/mod_http_debug/README.md	Fri May 26 15:36:04 2023 +0200
@@ -29,3 +29,12 @@
    }
 }
 ```
+
+# Configuration
+
+HTTP Methods handled can be configured via the `http_debug_methods`
+setting. By default, the most common methods are already enabled.
+
+```lua
+http_debug_methods = { "GET"; "HEAD"; "DELETE"; "OPTIONS"; "PATCH"; "POST"; "PUT" };
+```
--- 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;
+})