From da0b969f2514df4e6d4febf78c54c8b4c34dcb9a Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Sat, 8 Mar 2014 14:25:15 +0000 Subject: Improve llcache header processing By skipping empty headers and correctly dealing with whitespace around header names we store fewer entries with better adherance to allowed values in http responses. --- content/llcache.c | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) (limited to 'content') diff --git a/content/llcache.c b/content/llcache.c index 7d6e6b64d..112a7fab9 100644 --- a/content/llcache.c +++ b/content/llcache.c @@ -382,11 +382,25 @@ static nserror llcache_fetch_split_header(const uint8_t *data, size_t len, char *n, *v; const uint8_t *colon; + /* Strip leading whitespace from name */ + while (data[0] == ' ' || data[0] == '\t' || + data[0] == '\r' || data[0] == '\n') { + data++; + } + /* Find colon */ colon = (const uint8_t *) strchr((const char *) data, ':'); if (colon == NULL) { /* Failed, assume a key with no value */ - n = strdup((const char *) data); + colon = data + strlen((const char *)data); + + /* Strip trailing whitespace from name */ + while ((colon > data) && + (colon[-1] == ' ' || colon[-1] == '\t' || + colon[-1] == '\r' || colon[-1] == '\n')) { + colon--; + } + n = strndup((const char *) data, colon - data); if (n == NULL) return NSERROR_NOMEM; @@ -398,12 +412,6 @@ static nserror llcache_fetch_split_header(const uint8_t *data, size_t len, } else { /* Split header into name & value */ - /* Strip leading whitespace from name */ - while (data[0] == ' ' || data[0] == '\t' || - data[0] == '\r' || data[0] == '\n') { - data++; - } - /* Strip trailing whitespace from name */ while (colon > data && (colon[-1] == ' ' || colon[-1] == '\t' || colon[-1] == '\r' || @@ -613,6 +621,13 @@ static nserror llcache_fetch_process_header(llcache_object *object, return error; } + /* deal with empty header */ + if (name[0] == 0) { + free(name); + free(value); + return NSERROR_OK; + } + /* Append header data to the object's headers array */ temp = realloc(object->headers, (object->num_headers + 1) * sizeof(llcache_header)); -- cgit v1.2.3