summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-07-09 14:42:40 -0400
committerJune McEnroe <june@causal.agency>2020-07-09 14:42:40 -0400
commite716cd8032b9679641cb283e73542848453e24ae (patch)
tree3c49e0b12a1817185ece5128f37424388adc55f1
parentImplement empty CGI/FastCGI server (diff)
downloadscooper-e716cd8032b9679641cb283e73542848453e24ae.tar.gz
scooper-e716cd8032b9679641cb283e73542848453e24ae.zip
Return basic 404 and 405
Diffstat (limited to '')
-rw-r--r--scooper.c38
1 files changed, 29 insertions, 9 deletions
diff --git a/scooper.c b/scooper.c
index 130ea15..291abb0 100644
--- a/scooper.c
+++ b/scooper.c
@@ -58,10 +58,10 @@ static const char *Pages[PagesLen] = {
 };
 
 #define ENUM_KEYS \
-	X(Network, "network", kvalid_string) \
-	X(Context, "context", kvalid_string) \
-	X(After, "after", kvalid_string) \
-	X(Query, "query", kvalid_string)
+	X(Network, "network", kvalid_stringne) \
+	X(Context, "context", kvalid_stringne) \
+	X(After, "after", kvalid_stringne) \
+	X(Query, "query", kvalid_stringne)
 
 enum {
 #define X(key, name, valid) key,
@@ -76,7 +76,25 @@ static const struct kvalid Keys[KeysLen] = {
 #undef X
 };
 
-static void request(struct kreq *req) {
+static enum kcgi_err fail(struct kreq *req, enum khttp status) {
+	return khttp_head(req, kresps[KRESP_STATUS], "%s", khttps[status])
+		|| khttp_head(
+			req, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_TEXT_PLAIN]
+		)
+		|| khttp_body(req)
+		|| khttp_puts(req, khttps[status])
+		|| khttp_putc(req, '\n');
+}
+
+static enum kcgi_err request(struct kreq *req) {
+	if (req->method != KMETHOD_HEAD && req->method != KMETHOD_GET) {
+		return fail(req, KHTTP_405);
+	}
+	if (req->mime != KMIME_TEXT_HTML || req->page == PagesLen) {
+		return fail(req, KHTTP_404);
+	}
+
+	return KCGI_OK;
 }
 
 int main(int argc, char *argv[]) {
@@ -126,16 +144,18 @@ int main(int argc, char *argv[]) {
 			KCGI_OK == (error = khttp_fcgi_parse(fcgi, &req));
 			khttp_free(&req)
 		) {
-			request(&req);
+			error = request(&req);
+			if (error && error != KCGI_HUP) break;
 		}
-		errx(EX_DATAERR, "khttp_fcgi_parse: %s", kcgi_strerror(error));
+		errx(EX_PROTOCOL, "khttp_fcgi_parse: %s", kcgi_strerror(error));
 	} else {
 		struct kreq req;
 		enum kcgi_err error = khttp_parse(
 			&req, Keys, KeysLen, Pages, PagesLen, Networks
 		);
-		if (error) errx(EX_DATAERR, "khttp_parse: %s", kcgi_strerror(error));
-		request(&req);
+		if (error) errx(EX_PROTOCOL, "khttp_parse: %s", kcgi_strerror(error));
+		error = request(&req);
+		if (error) errx(EX_PROTOCOL, "%s", kcgi_strerror(error));
 		khttp_free(&req);
 	}
 }
>Add line numbers to hiJune McEnroe 2019-02-17Always split spans after newlinesJune McEnroe 2019-02-15Color format specifiers light cyan in vimJune McEnroe 2019-02-15Highlight Interp as yellowJune McEnroe 2019-02-15Highlight strings in sh command substitutionsJune McEnroe 2019-02-15Add nmap gpJune McEnroe 2019-02-14Avoid newline when copying URL to pasteboardJune McEnroe 2019-02-13Add forgotten "sixth" book of H2G2June McEnroelass='logmsg'> 2019-01-13Use colours in cash promptsJune McEnroe 2019-01-12Set PSlit like NetBSD shJune McEnroe 2019-01-12Install gnupg2 from pkgsrc and symlink gpgJune McEnroe 2019-01-12Reference cash builtin man pages in cash.1 SEE ALSOJune McEnroe 2019-01-12Restore cash builtin man page datesJune McEnroe 2019-01-12Use local libeditJune McEnroe 2019-01-12Replace libedit MakefileJune McEnroe 2019-01-11Import /usr/src/lib/libedit from NetBSD 8.0June McEnroe 2019-01-11Add PSlit for prompt escapesJune McEnroe 2019-01-11Don't make depend automaticallyJune McEnroe