fix #46 (HTTP/2 response message parsing broken with libcurl >= 7.49.1)
authorMichael Wallner <mike@php.net>
Wed, 27 Jul 2016 12:47:05 +0000 (14:47 +0200)
committerMichael Wallner <mike@php.net>
Wed, 27 Jul 2016 12:47:05 +0000 (14:47 +0200)
src/php_http_info.c
src/php_http_version.c
tests/client022.phpt

index 4fb067fcd2610e63a4110663e3172a3e62d9ca64..800821521154cb0e4966275a0a5e76da98ed56ae 100644 (file)
@@ -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);
index 7adef9d3c897ce66f5923b981c74bd3b46a992d0..c861a6470a2ba084a5502e3066193547e43967c8 100644 (file)
@@ -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;
 }
index e3f7f118812a86cc1faf11a4fd02615fbbfbc553..a59971ccbcaccb7b99bdac8e83ece4c51ede3cbd 100644 (file)
@@ -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();
 });