diff options
author | June McEnroe <june@causal.agency> | 2020-07-09 20:39:38 -0400 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2020-07-09 20:39:38 -0400 |
commit | ec20355dfdcf27f43a7341d225edddd605a3ba7c (patch) | |
tree | 5ee822c7d27a800bb8e717af120f8ee7d42c1344 | |
parent | Implement very basic events listing (diff) | |
download | scooper-ec20355dfdcf27f43a7341d225edddd605a3ba7c.tar.gz scooper-ec20355dfdcf27f43a7341d225edddd605a3ba7c.zip |
Add before param in redirect
-rw-r--r-- | events.c | 29 | ||||
-rw-r--r-- | server.h | 8 |
2 files changed, 31 insertions, 6 deletions
diff --git a/events.c b/events.c index 04fdbf8..f40e5ae 100644 --- a/events.c +++ b/events.c @@ -18,6 +18,7 @@ #include <stdio.h> #include <stdlib.h> #include <sysexits.h> +#include <time.h> #include "server.h" @@ -72,19 +73,35 @@ enum kcgi_err pageEvents(struct kreq *req) { if (!req->fieldmap[Network] || !req->fieldmap[Context]) { return httpFail(req, KHTTP_400); } + const char *network = req->fieldmap[Network]->parsed.s; + const char *context = req->fieldmap[Context]->parsed.s; + if (!req->fieldmap[After] && !req->fieldmap[Before]) { - return httpFail(req, KHTTP_400); - } + struct tm *tm = gmtime(&(time_t) { time(NULL) }); + if (!tm) err(EX_OSERR, "gmtime"); - enum kcgi_err error = httpHead(req, KHTTP_200, KMIME_TEXT_HTML); - if (req->method == KMETHOD_HEAD) return error; + char time[sizeof("0000-00-00T00:00:00")]; + strftime(time, sizeof(time), "%FT%T", tm); + + char *url = khttp_urlpart( + NULL, NULL, Pages[Events], + Keys[Network].name, network, + Keys[Context].name, context, + Keys[Before].name, time, + NULL + ); + enum kcgi_err error = httpRedirect(req, url); + free(url); + return error; + } - const char *network = req->fieldmap[Network]->parsed.s; - const char *context = req->fieldmap[Context]->parsed.s; const char *time = req->fieldmap[Before] ? req->fieldmap[Before]->parsed.s : req->fieldmap[After]->parsed.s; + enum kcgi_err error = httpHead(req, KHTTP_200, KMIME_TEXT_HTML); + if (req->method == KMETHOD_HEAD) return error; + struct khtmlreq html; error = error || khttp_body(req) diff --git a/server.h b/server.h index 0e77f7c..b1a22ee 100644 --- a/server.h +++ b/server.h @@ -121,6 +121,14 @@ httpHead(struct kreq *req, enum khttp http, enum kmime mime) { || khttp_head(req, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[mime]); } +static inline enum kcgi_err +httpRedirect(struct kreq *req, const char *url) { + return httpHead(req, KHTTP_302, KMIME_TEXT_PLAIN) + || khttp_head(req, kresps[KRESP_LOCATION], "%s", url) + || khttp_body(req) + || khttp_printf(req, "%s\n", url); +} + static inline enum kcgi_err httpFail(struct kreq *req, enum khttp http) { return httpHead(req, http, KMIME_TEXT_PLAIN) || khttp_body(req) |