http-parser.c

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

commit ae4b3afd676675e69f3617d1c72cc6bdcddc75be
parent b0f652b99de4c6f8235319d11df9a7393543db46
Author: finwo <finwo@pm.me>
Date:   Tue,  8 Aug 2023 00:15:01 +0200

Prefix request path with / during print if missing

Diffstat:
Msrc/http-parser.c | 13++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/src/http-parser.c b/src/http-parser.c @@ -401,9 +401,16 @@ struct buf * http_parser_sprint_request(struct http_parser_message *request) { result->cap = 65536; result->data = calloc(1, result->cap); + char *tmppath; char *path = request->path; + int isPathAllocced = 0; if (!path) path = "/"; - if (!strlen(path)) path = "/"; + if (strstr(path, "/") != path) { + tmppath = path; + path = NULL; + isPathallocced = true; + aprintf(&path, "/%s", tmppath); + } // Status sprintf(result->data, @@ -449,6 +456,10 @@ struct buf * http_parser_sprint_request(struct http_parser_message *request) { buf_append(result, "0\r\n\r\n", 5); } + if (isPathAllocced) { + free(path); + } + return result; }