http-parser.c

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

commit b0b2beb907b69bf3d98c7789e3d2113bdf7e2683
parent ffafc4f718641a4f729de1af55ba46f4277bff05
Author: finwo <finwo@pm.me>
Date:   Sun, 10 Nov 2019 17:33:07 +0100

Added request->ready

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

diff --git a/src/http-parser.c b/src/http-parser.c @@ -215,7 +215,7 @@ static int http_parser_message_read_header(struct http_parser_message *message) void http_parser_pair_request_data(struct http_parser_pair *pair, char *data, int size) { struct http_parser_event *ev; http_parser_request_data(pair->request, data, size); - if (pair->request->_state == HTTP_PARSER_STATE_RESPONSE) { + if (pair->request->_state == HTTP_PARSER_STATE_DONE) { if (pair->onRequest) { ev = calloc(1,sizeof(struct http_parser_event)); ev->request = pair->request; @@ -291,7 +291,7 @@ void http_parser_request_data(struct http_parser_message *request, char *data, i ) { request->_state = HTTP_PARSER_STATE_BODY; } else { - request->_state = HTTP_PARSER_STATE_RESPONSE; + request->_state = HTTP_PARSER_STATE_DONE; } } break; @@ -313,8 +313,8 @@ void http_parser_request_data(struct http_parser_message *request, char *data, i // Fetch the content length aContentLength = http_parser_header_get(request, "content-length"); if (!aContentLength) { - request->_state = HTTP_PARSER_STATE_RESPONSE; - return; + request->_state = HTTP_PARSER_STATE_DONE; + break; } iContentLength = atoi(aContentLength); @@ -325,7 +325,7 @@ void http_parser_request_data(struct http_parser_message *request, char *data, i // Change size to indicated size request->bodysize = iContentLength; - request->_state = HTTP_PARSER_STATE_RESPONSE; + request->_state = HTTP_PARSER_STATE_DONE; break; case HTTP_PARSER_STATE_BODY_CHUNKED: @@ -357,11 +357,8 @@ void http_parser_request_data(struct http_parser_message *request, char *data, i // 0 = EOF if (request->chunksize == 0) { - request->_state = HTTP_PARSER_STATE_RESPONSE; - free(request->body); - request->body = request->buf; - request->buf = NULL; - return; + request->_state = HTTP_PARSER_STATE_DONE; + break; } } @@ -389,7 +386,17 @@ void http_parser_request_data(struct http_parser_message *request, char *data, i break; - case HTTP_PARSER_STATE_RESPONSE: + case HTTP_PARSER_STATE_DONE: + + // Temporary buffer > direct buffer + if (request->buf) { + free(request->body); + request->body = request->buf; + request->buf = NULL; + } + + // Mark the request as ready + request->ready = 1; return; } } diff --git a/src/http-parser.h b/src/http-parser.h @@ -9,7 +9,7 @@ extern "C" { #define HTTP_PARSER_STATE_HEADER 1 #define HTTP_PARSER_STATE_BODY 2 #define HTTP_PARSER_STATE_BODY_CHUNKED 3 -#define HTTP_PARSER_STATE_RESPONSE 4 +#define HTTP_PARSER_STATE_DONE 4 #define HTTP_PARSER_STATE_PANIC 666 struct http_parser_header { @@ -25,6 +25,7 @@ struct http_parser_event { }; struct http_parser_message { + int ready; int status; char *method; char *path;