http-parser.c

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

commit 23bb6d7ba0554cd8fe0026229e58f42922ac87ef
parent 8c9dbbc716ab7914b2160ed4453b9bccabb5200d
Author: finwo <finwo@pm.me>
Date:   Tue, 16 May 2023 22:05:06 +0200

Use strdup in http_parser_header_set instead of split malloc/strcpy

Diffstat:
Msrc/http-parser.c | 16+++++++---------
1 file changed, 7 insertions(+), 9 deletions(-)

diff --git a/src/http-parser.c b/src/http-parser.c @@ -80,24 +80,22 @@ char *http_parser_header_get(struct http_parser_message *subject, char *key) { */ void http_parser_header_set(struct http_parser_message *subject, char *key, char *value) { struct http_parser_header *header = malloc(sizeof(struct http_parser_header)); - header->key = malloc(strlen(key)+1); - header->value = malloc(strlen(value)+1); - header->next = subject->headers; + header->key = strdup(key); + header->value = strdup(value); + header->next = subject->headers; subject->headers = header; - strcpy(header->key, key); - strcpy(header->value, value); } /** * Frees everything in a http_message that was malloc'd by http-parser */ void http_parser_message_free(struct http_parser_message *subject) { - if (subject->method) free(subject->method); - if (subject->path) free(subject->path); + if (subject->method ) free(subject->method); + if (subject->path ) free(subject->path); if (subject->version) free(subject->version); - if (subject->body) free(subject->body); + if (subject->body ) free(subject->body); if (subject->headers) http_parser_header_free(subject->headers); - if (subject->buf) free(subject->buf); + if (subject->buf ) free(subject->buf); free(subject); }