summaryrefslogtreecommitdiffstats
path: root/src/server/picohttpparser/picohttpparser.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/server/picohttpparser/picohttpparser.c')
-rw-r--r--src/server/picohttpparser/picohttpparser.c67
1 files changed, 46 insertions, 21 deletions
diff --git a/src/server/picohttpparser/picohttpparser.c b/src/server/picohttpparser/picohttpparser.c
index cfa05ef..f077016 100644
--- a/src/server/picohttpparser/picohttpparser.c
+++ b/src/server/picohttpparser/picohttpparser.c
@@ -36,8 +36,6 @@
#endif
#include "picohttpparser.h"
-/* $Id$ */
-
#if __GNUC__ >= 3
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
@@ -73,9 +71,9 @@
#define ADVANCE_TOKEN(tok, toklen) \
do { \
const char *tok_start = buf; \
- static const char ALIGNED(16) ranges2[] = "\000\040\177\177"; \
+ static const char ALIGNED(16) ranges2[16] = "\000\040\177\177"; \
int found2; \
- buf = findchar_fast(buf, buf_end, ranges2, sizeof(ranges2) - 1, &found2); \
+ buf = findchar_fast(buf, buf_end, ranges2, 4, &found2); \
if (!found2) { \
CHECK_EOF(); \
} \
@@ -138,15 +136,11 @@ static const char *get_token_to_eol(const char *buf, const char *buf_end, struct
const char *token_start = buf;
#ifdef __SSE4_2__
- static const char ranges1[] = "\0\010"
- /* allow HT */
- "\012\037"
- /* allow SP and up to but not including DEL */
- "\177\177"
- /* allow chars w. MSB set */
- ;
+ static const char ALIGNED(16) ranges1[16] = "\0\010" /* allow HT */
+ "\012\037" /* allow SP and up to but not including DEL */
+ "\177\177"; /* allow chars w. MSB set */
int found;
- buf = findchar_fast(buf, buf_end, ranges1, sizeof(ranges1) - 1, &found);
+ buf = findchar_fast(buf, buf_end, ranges1, 6, &found);
if (found)
goto FOUND_CTL;
#else
@@ -325,9 +319,21 @@ static const char *parse_headers(const char *buf, const char *buf_end, struct ph
headers[*num_headers].name.s = NULL;
headers[*num_headers].name.l = 0;
}
- if ((buf = get_token_to_eol(buf, buf_end, &headers[*num_headers].value, ret)) == NULL) {
+ struct string value;
+ // DELETE
+ if ((buf = get_token_to_eol(buf, buf_end, &value, ret)) == NULL) {
return NULL;
}
+ /* remove trailing SPs and HTABs */
+ const char *value_end = value.s + value.l;
+ for (; value_end != value.s; --value_end) {
+ const char c = *(value_end - 1);
+ if (!(c == ' ' || c == '\t')) {
+ break;
+ }
+ }
+ headers[*num_headers].value.s = value.s;
+ headers[*num_headers].value.l = value_end - value.s;
}
return buf;
}
@@ -347,9 +353,17 @@ static const char *parse_request(const char *buf, const char *buf_end, struct st
/* parse request line */
ADVANCE_TOKEN(method->s, method->l);
- ++buf;
+ do {
+ ++buf;
+ } while (*buf == ' ');
ADVANCE_TOKEN(path->s, path->l);
- ++buf;
+ do {
+ ++buf;
+ } while (*buf == ' ');
+ if (method->l == 0 || path->l == 0) {
+ *ret = -1;
+ return NULL;
+ }
if ((buf = parse_http_version(buf, buf_end, minor_version, ret)) == NULL) {
return NULL;
}
@@ -402,10 +416,13 @@ static const char *parse_response(const char *buf, const char *buf_end, int *min
return NULL;
}
/* skip space */
- if (*buf++ != ' ') {
+ if (*buf != ' ') {
*ret = -1;
return NULL;
}
+ do {
+ ++buf;
+ } while (*buf == ' ');
/* parse status code, we want at least [:digit:][:digit:][:digit:]<other char> to try to parse */
if (buf_end - buf < 4) {
*ret = -2;
@@ -413,13 +430,21 @@ static const char *parse_response(const char *buf, const char *buf_end, int *min
}
PARSE_INT_3(status);
- /* skip space */
- if (*buf++ != ' ') {
- *ret = -1;
+ /* get message includig preceding space */
+ if ((buf = get_token_to_eol(buf, buf_end, msg, ret)) == NULL) {
return NULL;
}
- /* get message */
- if ((buf = get_token_to_eol(buf, buf_end, msg, ret)) == NULL) {
+ if (msg->l == 0) {
+ /* ok */
+ } else if (*msg->s == ' ') {
+ /* remove preceding space */
+ do {
+ ++msg->s;
+ --msg->l;
+ } while (*msg->s == ' ');
+ } else {
+ /* garbage found after status code */
+ *ret = -1;
return NULL;
}