esp_http_server : Bugfix in parsing of empty header values

This MR is intended to fix incorrect parsing of HTTP requests when empty header values are present.

The issue is was due to asymmetric behavior of `http_parser` library, which in case of:

    non-empty header values : invokes callbacks with the pointer to the start of a value
    empty header values : invokes callbacks with pointer to the start of next header or section

Since HTTP server relies on this pointer (along with length of the value) to locate the end of a value, and replace the line terminators (CRLFs) with null characters, the second case needed to be handled correctly.

Closes IDFGH-1539

Closes https://github.com/espressif/esp-idf/issues/3803
This commit is contained in:
Chinmay
2019-09-09 12:05:40 +05:30
committed by bot
parent 9f3f7009c0
commit 38b1c93764
4 changed files with 160 additions and 2 deletions

View File

@@ -267,6 +267,23 @@ static esp_err_t cb_header_value(http_parser *parser, const char *at, size_t len
parser_data->last.at = at;
parser_data->last.length = 0;
parser_data->status = PARSING_HDR_VALUE;
if (length == 0) {
/* As per behavior of http_parser, when length > 0,
* `at` points to the start of CRLF. But, in the
* case when header value is empty (zero length),
* then `at` points to the position right after
* the CRLF. Since for our purpose we need `last.at`
* to point to exactly where the CRLF starts, it
* needs to be adjusted by the right offset */
char *at_adj = (char *)parser_data->last.at;
/* Find the end of header field string */
while (*(--at_adj) != ':');
/* Now skip leading spaces' */
while (*(++at_adj) == ' ');
/* Now we are at the right position */
parser_data->last.at = at_adj;
}
} else if (parser_data->status != PARSING_HDR_VALUE) {
ESP_LOGE(TAG, LOG_FMT("unexpected state transition"));
parser_data->error = HTTPD_500_INTERNAL_SERVER_ERROR;