X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=http_headers_api.c;h=70f36ce285052578b8df7acc1c4914d1054007f9;hp=7ac475692aff101d66d18fd7d0ca278350686bd6;hb=5773d11d8c9c28fb8b0e3389258f548fc4717892;hpb=f41f0417afd1d0ad0609fde76a99d907117ed669 diff --git a/http_headers_api.c b/http_headers_api.c index 7ac4756..70f36ce 100644 --- a/http_headers_api.c +++ b/http_headers_api.c @@ -326,11 +326,16 @@ PHP_HTTP_API http_range_status _http_get_request_ranges(HashTable *ranges, size_ PHP_HTTP_API STATUS _http_parse_headers_ex(const char *header, HashTable *headers, zend_bool prettify, http_info_callback callback_func, void **callback_data TSRMLS_DC) { - const char *colon = NULL, *line = header; + const char *colon = NULL, *line = NULL; zval array; - + INIT_ZARR(array, headers); + /* skip leading ws */ + while (isspace(*header)) ++header; + line = header; + +#define MORE_HEADERS (*(line-1) && !(*(line-1) == '\n' && (*line == '\n' || *line == '\r'))) do { int value_len = 0; @@ -348,54 +353,66 @@ PHP_HTTP_API STATUS _http_parse_headers_ex(const char *header, HashTable *header if ((!*(line - 1)) || ((*line != ' ') && (*line != '\t'))) { http_info i; - /* response/request line */ if (SUCCESS == http_info_parse(header, &i)) { + /* response/request line */ callback_func(callback_data, &headers, &i TSRMLS_CC); http_info_dtor(&i); Z_ARRVAL(array) = headers; - } else - - /* "header: value" pair */ - if (colon) { - - /* skip empty key */ + } else if (colon) { + /* "header: value" pair */ if (header != colon) { - zval **previous = NULL; - char *value; int keylen = colon - header; - char *key = estrndup(header, keylen); - - if (prettify) { - key = pretty_key(key, keylen, 1, 1); - } - - value_len += line - colon - 1; - + const char *key = header; + /* skip leading ws */ - while (isspace(*(++colon))) --value_len; + while (keylen && isspace(*key)) --keylen && ++key; /* skip trailing ws */ - while (isspace(colon[value_len - 1])) --value_len; - - if (value_len > 0) { - value = estrndup(colon, value_len); - } else { - value = estrdup(""); - value_len = 0; - } - - /* if we already have got such a header make an array of those */ - if (SUCCESS == zend_hash_find(headers, key, keylen + 1, (void *) &previous)) { - /* convert to array */ - if (Z_TYPE_PP(previous) != IS_ARRAY) { - convert_to_array(*previous); + while (keylen && isspace(key[keylen - 1])) --keylen; + + if (keylen > 0) { + zval **previous = NULL; + char *value; + char *keydup = estrndup(key, keylen); + + if (prettify) { + keydup = pretty_key(keydup, keylen, 1, 1); + } + + value_len += line - colon - 1; + + /* skip leading ws */ + while (isspace(*(++colon))) --value_len; + /* skip trailing ws */ + while (isspace(colon[value_len - 1])) --value_len; + + if (value_len > 0) { + value = estrndup(colon, value_len); + } else { + value = estrdup(""); + value_len = 0; + } + + /* if we already have got such a header make an array of those */ + if (SUCCESS == zend_hash_find(headers, keydup, keylen + 1, (void *) &previous)) { + /* convert to array */ + if (Z_TYPE_PP(previous) != IS_ARRAY) { + convert_to_array(*previous); + } + add_next_index_stringl(*previous, value, value_len, 0); + } else { + add_assoc_stringl(&array, keydup, value, value_len, 0); } - add_next_index_stringl(*previous, value, value_len, 0); - } else { - add_assoc_stringl(&array, key, value, value_len, 0); + efree(keydup); + } else { + /* empty key (" : ...") */ + return FAILURE; } - efree(key); + } else { + /* empty key (": ...") */ + return FAILURE; } - } else { + } else if (MORE_HEADERS) { + /* a line without a colon */ return FAILURE; } colon = NULL; @@ -404,7 +421,7 @@ PHP_HTTP_API STATUS _http_parse_headers_ex(const char *header, HashTable *header } break; } - } while (*(line-1) && !(*(line-1) == '\n' && (*line == '\n' || *line == '\r'))); + } while (MORE_HEADERS); return SUCCESS; }