Strip leading whitespace from header values

This commit is contained in:
Nick Krichevsky 2018-09-01 21:28:52 -04:00
parent b500854c1a
commit 31517671eb

View file

@ -1,8 +1,30 @@
#include "http_types.h"
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
/**
* Identical to strcpy, but left strips ' ' and '\t'
*
* @param dest Buffer to copy to
* @param src Buffer to copy from
*/
static char *strcpy_lstrip(char *dest, const char *src) {
char *dest_cursor;
const char *src_cursor;
bool non_whitespace_hit = false;
for (dest_cursor = dest, src_cursor = src; *src_cursor != '\0'; (dest_cursor++, src_cursor++)) {
if (non_whitespace_hit || !(*src_cursor == ' ' || *src_cursor == '\t')) {
*dest_cursor = *src_cursor;
} else {
non_whitespace_hit = true;
}
}
return dest;
}
/**
* Create a new header and append it to the end of the list of existing headers
*
@ -23,7 +45,7 @@ struct http_header *insert_header(const char *header_name, const char *header_va
int contents_length = strlen(header_name) + 1;
new_header->value = malloc(contents_length * sizeof(char));
strcpy(new_header->value, header_value);
strcpy_lstrip(new_header->value, header_value);
// If we have an existing header, append it to the end of our list of headers.
if (existing_header != NULL) {