From 35b88e235d2a2271f533d2a9b9b197bd187ada82 Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Sat, 1 Sep 2018 20:00:53 -0400 Subject: [PATCH] Implement case_insensitive_strcmp --- common/http_types.c | 44 ++++++++++++++++++++++++++++++++------------ common/http_types.h | 3 ++- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/common/http_types.c b/common/http_types.c index 0da4ae1..ab52837 100644 --- a/common/http_types.c +++ b/common/http_types.c @@ -52,22 +52,42 @@ void free_headers(struct http_header *headers) { free(headers); } +static char *str_lower(const char *s) { + int length = strlen(s) + 1; + char *lower = malloc(length * sizeof(char)); + char *lower_cursor = lower; + for (const char *cursor = s; *cursor != '\0'; (cursor++, lower_cursor++)) { + *lower_cursor = tolower(*cursor); + } + + return lower; +} + +/** + * Performs strcmp on lowercase versions of a and b + * + * @param a A null terminated string. + * @param b A null terminated string. + * + * @return strcmp(lower_a, lower_b) + */ +int case_insensitive_strcmp(const char *a, const char *b) { + char *a_lower = str_lower(a); + char *b_lower = str_lower(b); + int cmp = strcmp(a, b); + free(a_lower); + free(b_lower); + + return cmp; +} + /** * 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) + * @return The result of strcmp(lower_case_header_name, actual_header_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; +int headercmp(struct http_header *header, const char *name) { + return case_insensitive_strcmp(header->name, name); } diff --git a/common/http_types.h b/common/http_types.h index 78344b8..05d1313 100644 --- a/common/http_types.h +++ b/common/http_types.h @@ -18,6 +18,7 @@ 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); +int case_insensitive_strcmp(const char *a, const char *b); +int headercmp(struct http_header *header, const char *name); #endif