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