httpd: handle httpModules

This commit is contained in:
Jeremy Baxter 2024-08-07 14:19:24 +12:00
parent 9d23d3454c
commit 0f6e0a4951

16
httpd.d
View file

@ -1,6 +1,7 @@
module httpd; module httpd;
import http; import http;
import httpmodules;
import std.algorithm; import std.algorithm;
import std.array; import std.array;
@ -55,15 +56,18 @@ string
tryRequest(char[] requestBody) tryRequest(char[] requestBody)
{ {
HTTPRequest rq; HTTPRequest rq;
string lastErr;
try { try {
rq = parseHTTPRequest(requestBody); rq = parseHTTPRequest(requestBody);
if (rq.method != HTTPMethod.GET) foreach (mod; httpModules) {
return failurePage("405 Method Not Allowed"); try
return rq.resource == "/" return makeHTTPResponse(mod(rq));
|| rq.resource == "/index.html" catch (HTTPException e)
? respondWithPage(import("index.html")) lastErr = e.msg;
: failurePage("404 Not Found"); }
/* jump to catch block */
throw new HTTPException(lastErr);
} catch (HTTPException e) } catch (HTTPException e)
return failurePage(e.msg); return failurePage(e.msg);
} }