Add headercmp

This commit is contained in:
Nick Krichevsky 2018-09-01 19:35:56 -04:00
parent 0d211539f3
commit 5afa947d89
2 changed files with 22 additions and 0 deletions

View file

@ -1,4 +1,5 @@
#include "http_types.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
@ -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;
}

View file

@ -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