From 91f4167a45f811fde65f3ac1df1398a535f61a34 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Thu, 25 Feb 2021 19:14:22 -0800 Subject: [PATCH] Add root redirect for redbean webserver We have a webserver demo: make -j8 o//tool/net/redbean.com o/tool/net/redbean.com -v It's been a little bit confusing that until now you had to visit the following URL in order to see the default web page: http://127.0.0.1:8080/tool/net/redbean.html The following URLs will now redirect to the above page, but only if nothing's been defined for those paths and they would otherwise result in a 404 response: http://127.0.0.1:8080/ http://127.0.0.1:8080/index.html --- tool/net/redbean.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tool/net/redbean.c b/tool/net/redbean.c index 99e9533c8..051730ef0 100644 --- a/tool/net/redbean.c +++ b/tool/net/redbean.c @@ -846,7 +846,7 @@ static char *AppendContentEncodingGzip(char *p) { static char *AppendRedirect(char *p, const char *s) { VERBOSEF("%s %s %.*s redirect %s", clientaddrstr, kHttpMethod[req.method], req.uri.b - req.uri.a, inbuf + req.uri.a, s); - p = AppendStatus(p, 302, "Temporary Redirect"); + p = AppendStatus(p, 307, "Temporary Redirect"); p = AppendHeaderName(p, "Location"); p = STPCPY(p, s); return AppendCrlf(p); @@ -942,7 +942,7 @@ void HandleRequest(size_t got) { req.headers[kHttpReferer].b - req.headers[kHttpReferer].a, inbuf + req.headers[kHttpReferer].a); if ((location = LookupRedirect(path, pathlen))) { - p = AppendRedirect(p, DEFAULT_PATH); + p = AppendRedirect(p, location); } else if ((a = FindFile(path, pathlen))) { if (IsNotModified(a)) { VERBOSEF("%s %s %.*s not modified", clientaddrstr, @@ -1012,6 +1012,9 @@ void HandleRequest(size_t got) { } else { p = AppendVaryContentEncoding(p); } + } else if (!strncmp(path, "/", pathlen) || + !strncmp(path, "/index.html", pathlen)) { + p = AppendRedirect(p, DEFAULT_PATH); } else { WARNF("%s %s %.*s not found", clientaddrstr, kHttpMethod[req.method], pathlen, path);