From ee21a57095f838337945bd3d2e19bd20bdfee3ea Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Wed, 27 Jul 2016 14:47:05 +0200 Subject: [PATCH 1/1] fix #46 (HTTP/2 response message parsing broken with libcurl >= 7.49.1) --- src/php_http_info.c | 28 ++++++++++++++++++++-------- src/php_http_version.c | 23 +++++++++++++++++------ tests/client022.phpt | 6 ++++-- 3 files changed, 41 insertions(+), 16 deletions(-) diff --git a/src/php_http_info.c b/src/php_http_info.c index 4fb067f..8008215 100644 --- a/src/php_http_info.c +++ b/src/php_http_info.c @@ -51,7 +51,7 @@ void php_http_info_free(php_http_info_t **i) php_http_info_t *php_http_info_parse(php_http_info_t *info, const char *pre_header TSRMLS_DC) { - const char *end, *http; + const char *end, *http, *off; zend_bool free_info = !info; /* sane parameter */ @@ -71,9 +71,21 @@ php_http_info_t *php_http_info_parse(php_http_info_t *info, const char *pre_head info = php_http_info_init(info TSRMLS_CC); - /* and nothing than SPACE or NUL after HTTP/X.x */ - if (!php_http_version_parse(&info->http.version, http TSRMLS_CC) - || (http[lenof("HTTP/X.x")] && (!PHP_HTTP_IS_CTYPE(space, http[lenof("HTTP/X.x")])))) { + if (!php_http_version_parse(&info->http.version, http TSRMLS_CC)) { + if (free_info) { + php_http_info_free(&info); + } + return NULL; + } + + /* clumsy fix for changed libcurl behaviour in 7.49.1, see https://github.com/curl/curl/issues/888 */ + off = &http[lenof("HTTP/X")]; + if (info->http.version.major < 2) { + off += 2; + } + + /* and nothing than SPACE or NUL after HTTP/X(.x) */ + if (*off && (!PHP_HTTP_IS_CTYPE(space, *off))) { if (free_info) { php_http_info_free(&info); } @@ -90,11 +102,11 @@ php_http_info_t *php_http_info_parse(php_http_info_t *info, const char *pre_head /* is response */ if (pre_header == http) { - const char *status = NULL, *code = http + sizeof("HTTP/X.x"); + const char *status = NULL, *code = off; info->type = PHP_HTTP_RESPONSE; while (' ' == *code) ++code; - if (code && end > code) { + if (end > code) { /* rfc7230#3.1.2 The status-code element is a 3-digit integer code */ PHP_HTTP_INFO(info).response.code = 100*(*code++ - '0'); PHP_HTTP_INFO(info).response.code += 10*(*code++ - '0'); @@ -120,7 +132,7 @@ php_http_info_t *php_http_info_parse(php_http_info_t *info, const char *pre_head } /* is request */ - else if (*(http - 1) == ' ' && (!http[lenof("HTTP/X.x")] || http[lenof("HTTP/X.x")] == '\r' || http[lenof("HTTP/X.x")] == '\n')) { + else if (*(http - 1) == ' ' && (!*off || *off == '\r' || *off == '\n')) { const char *url = strchr(pre_header, ' '); info->type = PHP_HTTP_REQUEST; @@ -155,7 +167,7 @@ php_http_info_t *php_http_info_parse(php_http_info_t *info, const char *pre_head return info; } - /* some darn header containing HTTP/X.x */ + /* some darn header containing HTTP/X(.x) */ else { if (free_info) { php_http_info_free(&info); diff --git a/src/php_http_version.c b/src/php_http_version.c index 7adef9d..c861a64 100644 --- a/src/php_http_version.c +++ b/src/php_http_version.c @@ -44,18 +44,29 @@ php_http_version_t *php_http_version_parse(php_http_version_t *v, const char *st major = *ptr++ - '0'; if (major >= 0 && major <= 9) { separator = *ptr++; - if (separator) { - if (separator != '.' && separator != ',') { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Non-standard version separator '%c' in HTTP protocol version '%s'", separator, ptr - 2); - } + switch (separator) { + default: + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Non-standard version separator '%c' in HTTP protocol version '%s'", separator, ptr - 2); + /* no break */ + case '.': + case ',': minor = *ptr - '0'; - if (minor >= 0 && minor <= 9) { - return php_http_version_init(v, major, minor TSRMLS_CC); + break; + + case ' ': + if (major > 1) { + minor = 0; + } else { + goto error; } } + if (minor >= 0 && minor <= 9) { + return php_http_version_init(v, major, minor TSRMLS_CC); + } } } + error: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not parse HTTP protocol version '%s'", str); return NULL; } diff --git a/tests/client022.phpt b/tests/client022.phpt index e3f7f11..a59971c 100644 --- a/tests/client022.phpt +++ b/tests/client022.phpt @@ -18,10 +18,12 @@ nghttpd(function($port) { $client->setOptions([ "protocol" => http\Client\Curl\HTTP_VERSION_2_0, "ssl" => [ - "cafile" => __DIR__."/helper/http2.crt", + "cainfo" => __DIR__."/helper/http2.crt", ] ]); - $client->enqueue(new http\Client\Request("GET", "https://localhost:$port")); + + $request = new http\Client\Request("GET", "https://localhost:$port"); + $client->enqueue($request); echo $client->send()->getResponse(); }); -- 2.30.2