about summary refs log tree commit diff
diff options
context:
space:
mode:
-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);
 	}
 }