http-parser.c

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

commit d900769bdeca0c06f19c2fdf7648eebb4b193b99
parent bbe128d997530b90311a5adcb6fd78e520225558
Author: finwo <finwo@pm.me>
Date:   Sat,  8 Jul 2023 22:09:31 +0200

Distinguish between header set and add

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

diff --git a/src/http-parser.c b/src/http-parser.c @@ -85,6 +85,19 @@ char *http_parser_header_get(struct http_parser_message *subject, const char *ke * Write a header into the subject's list of headers */ void http_parser_header_set(struct http_parser_message *subject, const char *key, const char *value) { + struct http_parser_header *header = _http_parser_header_get(subject, key); + if (header) { + free(header->value); + header->value = strdup(value); + } else { + http_parser_header_add(subject, key, value); + } +} + +/** + * Add a header into the subject's list of headers + */ +void http_parser_header_add(struct http_parser_message *subject, const char *key, const char *value) { struct http_parser_header *header = malloc(sizeof(struct http_parser_header)); header->key = strdup(key); header->value = strdup(value); diff --git a/src/http-parser.h b/src/http-parser.h @@ -54,6 +54,7 @@ struct http_parser_pair { // Header management char *http_parser_header_get(struct http_parser_message *subject, const char *key); void http_parser_header_set(struct http_parser_message *subject ,const char *key, char *value); +void http_parser_header_add(struct http_parser_message *subject ,const char *key, char *value); void http_parser_header_del(struct http_parser_message *subject, const char *key); struct http_parser_pair * http_parser_pair_init(void *udata);