From 5afa947d89f6e843bb2708391d3727e5a1b8c0e5 Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Sat, 1 Sep 2018 19:35:56 -0400 Subject: [PATCH] Add headercmp --- common/http_types.c | 21 +++++++++++++++++++++ common/http_types.h | 1 + 2 files changed, 22 insertions(+) diff --git a/common/http_types.c b/common/http_types.c index 346173a..0da4ae1 100644 --- a/common/http_types.c +++ b/common/http_types.c @@ -1,4 +1,5 @@ #include "http_types.h" +#include #include #include @@ -50,3 +51,23 @@ void free_headers(struct http_header *headers) { // next_header should have already been freed by recursion free(headers); } + +/** + * Compare the name of a header to a given name, case insensitively + * + * @param name The name to compare against. + * + * @return The result of strcmp(lower_case_header_name, name) + */ +int headercmp(const char *name) { + int name_length = strlen(name) + 1; + char *lower_name = malloc(name_length * sizeof(char)); + char *lower_cursor = lower_name; + for (const char *cursor = name; *cursor != '\0'; (cursor++, lower_cursor++)) { + *lower_cursor = tolower(*cursor); + } + int cmp_result = strcmp(lower_name, name); + free(lower_name); + + return cmp_result; +} diff --git a/common/http_types.h b/common/http_types.h index 867a7b2..78344b8 100644 --- a/common/http_types.h +++ b/common/http_types.h @@ -18,5 +18,6 @@ struct http_header { struct http_header *insert_header(const char *header_name, const char *header_value, struct http_header *existing_header); void free_headers(struct http_header *headers); +int headercmp(const char *name); #endif