higgs

httpd experiment
git clone git://git.2f30.org/higgs
Log | Files | Refs

commit 21cc3f1587d4b97ce8b55a867cb87e9cac3bfb64
parent 8826009e9807a15f96ede86b1355106f56e4155f
Author: FRIGN <dev@frign.de>
Date:   Sun,  6 Dec 2015 17:36:55 +0100

Add semantics to parse headers

Should be improved with strtok

Diffstat:
Mhiggs.c | 58++++++++++++++++++++++++++++++++++++++++++++++++----------
1 file changed, 48 insertions(+), 10 deletions(-)

diff --git a/higgs.c b/higgs.c @@ -16,30 +16,68 @@ #include "config.h" -void +static char * +strdupcond(char *s, char *skip, char *delim) +{ + char *p; + + for (; !*s || strchr(skip, *s); s++) + ; + for (p = s; !strchr(delim, *p); p++) + ; + + return strndup(s, p - s); +} + +static void handle(int fd) { FILE *fp; size_t buflen = 0; - char *buf = NULL, *p, *url; + off_t range[2] = { -1, -1 }; + char *buf = NULL, *p, *url, *host; readline: - /* check "METHOD URL HTTP-VERSION" */ + /* check first line of HTTP request (use strtok?) */ if (!(fp = fdopen(fd, "r")) || getline(&buf, &buflen, fp) < 0) goto servererr; - if (strncmp(buf, "GET ", 4)) - goto clienterr; - if (!(p = strchr(buf + 4, ' ')) || p == buf + 4) + if (strncmp(buf, "GET", strlen("GET"))) goto clienterr; - if (!(url = strndup(buf + 4, p - (buf + 4)))) + if (!(url = strdupcond(buf + strlen("GET"), " ", " "))) goto servererr; - if (strncmp(p + 1, "HTTP/", 5)) + for (p = buf + strlen("GET"); *p == ' '; p++); + for (; *p && *p != ' '; p++); + for (; *p == ' '; p++); + if (strncmp(p, "HTTP/", strlen("HTTP/"))) goto clienterr; - /* read other headers and pick those we care about */ + /* read upcoming fields (use strtok? first \r -> \0) */ while (getline(&buf, &buflen, fp) >= 0 && strcmp(buf, "\r\n")) { - printf("%s", buf); + /* host */ + if (!strncmp(buf, "Host:", strlen("Host:"))) { + if (!(host = strdupcond(buf + strlen("Host:"), + " ", "\r\n"))) + goto servererr; + printf("FIELD HOST: %s\n", host); + /* range */ + } else if (!strncmp(buf, "Range:", strlen("Range:"))) { + if (!(p = strdupcond(buf + strlen("Range:"), + " ", "\r\n"))) + goto servererr; + printf("FIELD RANGE: %s\n", p); + free(p); + /* if-modified-since */ + } else if (!strncmp(buf, "If-Modified-Since:", + strlen("If-Modified-Since:"))) { + if (!(p = strdupcond(buf + strlen("If-Modified-Since:"), + " ", "\r\n"))) + goto servererr; + printf("FIELD MOD-SINCE: %s\n", p); + free(p); + } } + /* Response: Allow, Connection, Content-Range, Content-Type, + * Date, Server */ return; servererr: /* 500 internal server error */