http-parser.c

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

commit 952f81b4faa8fc923fab2f5fc7da82b9b28deca1
parent 23bb6d7ba0554cd8fe0026229e58f42922ac87ef
Author: finwo <finwo@pm.me>
Date:   Thu,  1 Jun 2023 23:16:38 +0200

Added support for methods longer than 6 characters

Diffstat:
Msrc/http-parser.c | 4++--
Mtest.c | 15+++++++++++++++
2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/src/http-parser.c b/src/http-parser.c @@ -494,10 +494,10 @@ void http_parser_request_data(struct http_parser_message *request, char *data, i *(index) = '\0'; // Read method and path - request->method = calloc(1, 7); + request->method = calloc(1, 16); request->path = calloc(1, 8192); request->version = calloc(1, 4); - if (sscanf(request->body, "%6s %8191s HTTP/%3s", request->method, request->path, request->version) != 3) { + if (sscanf(request->body, "%15s %8191s HTTP/%3s", request->method, request->path, request->version) != 3) { request->_state = HTTP_PARSER_STATE_PANIC; return; } diff --git a/test.c b/test.c @@ -69,6 +69,12 @@ char *postChunkedMessage = "0\r\n" ; +char *optionsRequest = + "OPTIONS /hello/world HTTP/1.1\r\n" + "Host: localhost\r\n" + "\r\n" +; + char *responseMessage = "HTTP/1.0 200 OK\r\n" "Content-Length: 13\r\n" @@ -139,6 +145,15 @@ int main() { ASSERT("request->path is /foobar", strcmp(request->path, "/foobar") == 0); ASSERT("request->body is \"Helo World\\r\\n\"", strcmp(request->body, "Hello World\r\n") == 0); + http_parser_message_free(request); + request = http_parser_request_init(); + http_parser_request_data(request, optionsRequest, strlen(optionsRequest)); + + printf("# OPTIONS request\n"); + ASSERT("request->version is 1.1", strcmp(request->version, "1.1") == 0); + ASSERT("request->method is OPTIONS", strcmp(request->method, "OPTIONS") == 0); + ASSERT("request->path is /hello/world", strcmp(request->path, "/hello/world") == 0); + printf("# Pre-loaded response\n"); ASSERT("response->status = 200", response->status == 200);