http-parser.c

Small C library to parse HTTP requests
Log | Files | Refs | README | LICENSE

commit dda799a58d74b36a757698633701de4d1c0a746e
parent 05377074479f4546d8be6c9ce5715503094e3225
Author: finwo <finwo@pm.me>
Date:   Sat,  9 Nov 2019 21:32:23 +0100

Added separate query support

Diffstat:
Msrc/http-parser.c | 12++++++++++--
Msrc/http-parser.h | 1+
2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/src/http-parser.c b/src/http-parser.c @@ -78,9 +78,9 @@ void http_parser_request_data(struct http_parser_request *request, char *data, i // Read method and path request->method = calloc(1, 7); - request->path = calloc(1, 512); + request->path = calloc(1, 8192); request->version = calloc(1, 4); - if (sscanf(request->body, "%6s %511s HTTP/%3s", request->method, request->path, request->version) != 3) { + if (sscanf(request->body, "%6s %8191s HTTP/%3s", request->method, request->path, request->version) != 3) { request->state = HTTP_PARSER_STATE_PANIC; return; } @@ -93,6 +93,14 @@ void http_parser_request_data(struct http_parser_request *request, char *data, i request->body = buf; request->bodysize = newsize; + // Detect query + // No need to malloc, already done by sscanf + index = strstr(request->path, "?"); + if (index) { + *(index) = '\0'; + request->query = index + 1; + } + // Signal we're now reading headers request->state = HTTP_PARSER_STATE_HEADER; break; diff --git a/src/http-parser.h b/src/http-parser.h @@ -26,6 +26,7 @@ struct http_parser_request { int state; char *method; char *path; + char *query; char *version; struct http_parser_header *headers; char *body;