tinywebd

view src/http.c @ 2:7bb4c2a0a360

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 15 Apr 2015 23:44:22 +0300
parents f425a9805d17
children 852a745503cf
line source
1 #include "http.h"
3 int http_parse_header(struct http_req_header *hdr, const char *buf, int bufsz)
4 {
5 int i, nlines = 0;
6 char *endhdr;
7 char *rqline = 0;
9 for(i=1; i<bufsz; i++) {
10 if(buf[i] == '\n' && buf[i - 1] == '\r') {
11 if(!rqline) {
12 rqline = alloca(i);
13 memcpy(rqline, buf, i - 1);
14 rqline[i - 1] = 0;
15 }
16 ++nlines;
17 }
18 }
20 if(!rqline)
21 return -1;
24 }
26 const char *http_strmsg(int code)
27 {
28 static const char **msgxxx[] = {
29 0, http_msg1xx, http_msg2xx, http_msg3xx, http_msg4xx, http_msg5xx
30 };
31 static int msgcount[] = {
32 0,
33 sizeof http_msg1xx / sizeof *http_msg1xx,
34 sizeof http_msg2xx / sizeof *http_msg2xx,
35 sizeof http_msg3xx / sizeof *http_msg3xx,
36 sizeof http_msg4xx / sizeof *http_msg4xx,
37 sizeof http_msg5xx / sizeof *http_msg5xx
38 };
40 int type = code / 100;
41 int idx = code % 100;
43 if(type < 1 || type >= sizeof msgxxx / sizeof *msgxxx) {
44 return "Invalid HTTP Status";
45 }
47 if(idx < 0 || idx >= msgcount[type]) {
48 return "Unknown HTTP Status";
49 }
51 return msgxxx[type][idx];
52 }