fix http\Params::__toString() with RFC5988 payload; fix tests
authorMichael Wallner <mike@php.net>
Thu, 21 May 2015 07:55:15 +0000 (09:55 +0200)
committerMichael Wallner <mike@php.net>
Thu, 21 May 2015 10:30:07 +0000 (12:30 +0200)
php_http_params.c
tests/bug69313.phpt
tests/client019.phpt
tests/client020.phpt
tests/client025.phpt
tests/helper/dump.inc [new file with mode: 0644]
tests/helper/proxy.inc
tests/helper/upload.inc
tests/params016.phpt [new file with mode: 0644]
tests/params017.phpt [new file with mode: 0644]

index b91314df617e8766d0d772f62d45939955ec8f6a..5adeb91c761f390bd3e6705ea71eb6eaf5225422 100644 (file)
@@ -63,27 +63,32 @@ static inline void sanitize_escaped(zval *zv TSRMLS_DC)
        php_stripcslashes(Z_STRVAL_P(zv), &Z_STRLEN_P(zv));
 }
 
        php_stripcslashes(Z_STRVAL_P(zv), &Z_STRLEN_P(zv));
 }
 
-static inline void prepare_escaped(zval *zv TSRMLS_DC)
+static inline void quote_string(zval *zv, zend_bool force TSRMLS_DC)
 {
 {
-       if (Z_TYPE_P(zv) == IS_STRING) {
-               int len = Z_STRLEN_P(zv);
+       int len = Z_STRLEN_P(zv);
 
 
-               Z_STRVAL_P(zv) = php_addcslashes(Z_STRVAL_P(zv), Z_STRLEN_P(zv), &Z_STRLEN_P(zv), 1,
-                               ZEND_STRL("\0..\37\173\\\"") TSRMLS_CC);
+       Z_STRVAL_P(zv) = php_addcslashes(Z_STRVAL_P(zv), Z_STRLEN_P(zv), &Z_STRLEN_P(zv), 1,
+                       ZEND_STRL("\0..\37\173\\\"") TSRMLS_CC);
 
 
-               if (len != Z_STRLEN_P(zv) || strpbrk(Z_STRVAL_P(zv), "()<>@,;:\"[]?={} ")) {
-                       zval tmp = *zv;
-                       int len = Z_STRLEN_P(zv) + 2;
-                       char *str = emalloc(len + 1);
+       if (force || len != Z_STRLEN_P(zv) || strpbrk(Z_STRVAL_P(zv), "()<>@,;:\"[]?={} ")) {
+               zval tmp = *zv;
+               int len = Z_STRLEN_P(zv) + 2;
+               char *str = emalloc(len + 1);
 
 
-                       str[0] = '"';
-                       memcpy(&str[1], Z_STRVAL_P(zv), Z_STRLEN_P(zv));
-                       str[len-1] = '"';
-                       str[len] = '\0';
+               str[0] = '"';
+               memcpy(&str[1], Z_STRVAL_P(zv), Z_STRLEN_P(zv));
+               str[len-1] = '"';
+               str[len] = '\0';
 
 
-                       zval_dtor(&tmp);
-                       ZVAL_STRINGL(zv, str, len, 0);
-               }
+               zval_dtor(&tmp);
+               ZVAL_STRINGL(zv, str, len, 0);
+       }
+}
+
+static inline void prepare_escaped(zval *zv TSRMLS_DC)
+{
+       if (Z_TYPE_P(zv) == IS_STRING) {
+               quote_string(zv, 0 TSRMLS_CC);
        } else {
                zval_dtor(zv);
                ZVAL_EMPTY_STRING(zv);
        } else {
                zval_dtor(zv);
                ZVAL_EMPTY_STRING(zv);
@@ -297,6 +302,14 @@ static inline void sanitize_rfc5988(char *str, size_t len, zval *zv TSRMLS_DC)
        php_trim(str, len, " ><", 3, zv, 3 TSRMLS_CC);
 }
 
        php_trim(str, len, " ><", 3, zv, 3 TSRMLS_CC);
 }
 
+static inline void prepare_rfc5988(zval *zv TSRMLS_DC)
+{
+       if (Z_TYPE_P(zv) != IS_STRING) {
+               zval_dtor(zv);
+               ZVAL_EMPTY_STRING(zv);
+       }
+}
+
 static void utf8encode(zval *zv)
 {
        size_t pos, len = 0;
 static void utf8encode(zval *zv)
 {
        size_t pos, len = 0;
@@ -369,7 +382,11 @@ static inline void prepare_key(unsigned flags, char *old_key, size_t old_len, ch
        }
 
        if (flags & PHP_HTTP_PARAMS_ESCAPED) {
        }
 
        if (flags & PHP_HTTP_PARAMS_ESCAPED) {
-               prepare_escaped(&zv TSRMLS_CC);
+               if (flags & PHP_HTTP_PARAMS_RFC5988) {
+                       prepare_rfc5988(&zv TSRMLS_CC);
+               } else {
+                       prepare_escaped(&zv TSRMLS_CC);
+               }
        }
 
        *new_key = Z_STRVAL(zv);
        }
 
        *new_key = Z_STRVAL(zv);
@@ -762,6 +779,17 @@ static inline void shift_rfc5988(php_http_buffer_t *buf, char *key_str, size_t k
        efree(str);
 }
 
        efree(str);
 }
 
+static inline void shift_rfc5988_val(php_http_buffer_t *buf, zval *zv, const char *vss, size_t vsl, unsigned flags TSRMLS_DC)
+{
+       zval *tmp = php_http_zsep(1, IS_STRING, zv);
+
+       quote_string(tmp, 1 TSRMLS_CC);
+       php_http_buffer_append(buf, vss, vsl);
+       php_http_buffer_append(buf, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
+
+       zval_ptr_dtor(&tmp);
+}
+
 static inline void shift_val(php_http_buffer_t *buf, zval *zvalue, const char *vss, size_t vsl, unsigned flags TSRMLS_DC)
 {
        if (Z_TYPE_P(zvalue) != IS_BOOL) {
 static inline void shift_val(php_http_buffer_t *buf, zval *zvalue, const char *vss, size_t vsl, unsigned flags TSRMLS_DC)
 {
        if (Z_TYPE_P(zvalue) != IS_BOOL) {
@@ -802,6 +830,21 @@ static void shift_arg(php_http_buffer_t *buf, char *key_str, size_t key_len, zva
                }
        } else {
                shift_key(buf, key_str, key_len, ass, asl, flags TSRMLS_CC);
                }
        } else {
                shift_key(buf, key_str, key_len, ass, asl, flags TSRMLS_CC);
+
+               if (flags & PHP_HTTP_PARAMS_RFC5988) {
+                       switch (key_len) {
+                       case lenof("rel"):
+                       case lenof("title"):
+                       case lenof("anchor"):
+                               /* some args must be quoted */
+                               if (0 <= php_http_select_str(key_str, 3, "rel", "title", "anchor")) {
+                                       shift_rfc5988_val(buf, zvalue, vss, vsl, flags TSRMLS_CC);
+                                       return;
+                               }
+                               break;
+                       }
+               }
+
                shift_val(buf, zvalue, vss, vsl, flags TSRMLS_CC);
        }
 }
                shift_val(buf, zvalue, vss, vsl, flags TSRMLS_CC);
        }
 }
index d2bea4f00fce9d85b728de1a4b057cd86547fd11..c1d56efc54d2cb6f69f884e77e6333c684eafec6 100644 (file)
@@ -8,7 +8,7 @@ skip_client_test();
 --FILE--
 <?php
 
 --FILE--
 <?php
 
-
+include "helper/dump.inc";
 include "helper/server.inc";
 
 echo "Test\n";
 include "helper/server.inc";
 
 echo "Test\n";
@@ -20,7 +20,7 @@ server("proxy.inc", function($port, $stdin, $stdout, $stderr) {
        $client = new http\Client();
        $client->enqueue($request);
        $client->send();
        $client = new http\Client();
        $client->enqueue($request);
        $client->send();
-       echo $client->getResponse();
+       dump_message(null, $client->getResponse());
 });
 
 ?>
 });
 
 ?>
@@ -30,16 +30,16 @@ Done
 Test
 HTTP/1.1 200 OK
 Accept-Ranges: bytes
 Test
 HTTP/1.1 200 OK
 Accept-Ranges: bytes
+Content-Length: %d
 Etag: "%s"
 X-Original-Transfer-Encoding: chunked
 Etag: "%s"
 X-Original-Transfer-Encoding: chunked
-Content-Length: %d
 
 GET / HTTP/1.1
 
 GET / HTTP/1.1
-User-Agent: %s
-Host: localhost:%d
 Accept: */*
 Accept: */*
-Content-Type: text/plain
 Content-Length: 3
 Content-Length: 3
+Content-Type: text/plain
+Host: localhost:%d
+User-Agent: %s
 X-Original-Content-Length: 3
 
 foo
 X-Original-Content-Length: 3
 
 foo
index c41a260f0acff98b8e4d29167374ee97f5f018ae..9666b976a618a6f297df56448ac70df54ae18c58 100644 (file)
@@ -41,8 +41,9 @@ server("proxy.inc", function($port, $stdin, $stdout, $stderr) {
 Test
 Server on port %d
 CONNECT www.example.com:80 HTTP/1.1
 Test
 Server on port %d
 CONNECT www.example.com:80 HTTP/1.1
+Hello: there!
 Host: www.example.com:80
 Host: www.example.com:80
-User-Agent: PECL_HTTP/%s PHP/%s libcurl/%s
 Proxy-Connection: Keep-Alive
 Proxy-Connection: Keep-Alive
-Hello: there!
+User-Agent: PECL_HTTP/%s PHP/%s libcurl/%s
+
 ===DONE===
 ===DONE===
index 7ea5d60d87eceee5eab42c7fd6cc0f32afe84c07..ed86f4a727e87d5b4520b3727efcdcf8010fc0d1 100644 (file)
@@ -35,7 +35,8 @@ server("proxy.inc", function($port, $stdin, $stdout, $stderr) {
 Test
 Server on port %d
 GET / HTTP/1.1
 Test
 Server on port %d
 GET / HTTP/1.1
-User-Agent: PECL_HTTP/%s PHP/%s libcurl/%s
-Host: localhost:%d
 Accept: */*
 Accept: */*
+Host: localhost:%d
+User-Agent: PECL_HTTP/%s PHP/%s libcurl/%s
+
 ===DONE===
 ===DONE===
index 3c4793e96b5793a5a2b25f31fa1c80e4b59b7c7f..3f90cbd7b8314f2db4dd22551dab3c5c99bd7fa4 100644 (file)
@@ -7,6 +7,7 @@ include "skipif.inc";
 --FILE--
 <?php 
 
 --FILE--
 <?php 
 
+include "helper/dump.inc";
 include "helper/server.inc";
 
 echo "Test\n";
 include "helper/server.inc";
 
 echo "Test\n";
@@ -16,7 +17,7 @@ server("proxy.inc", function($port) {
        $request = new http\Client\Request("PUT", "http://localhost:$port");
        $request->setOptions(array("resume" => 1, "expect_100_timeout" => 0));
        $request->getBody()->append("123");
        $request = new http\Client\Request("PUT", "http://localhost:$port");
        $request->setOptions(array("resume" => 1, "expect_100_timeout" => 0));
        $request->getBody()->append("123");
-       echo $client->enqueue($request)->send()->getResponse();
+       dump_message(null, $client->enqueue($request)->send()->getResponse());
 });
 // Content-length is 2 instead of 3 in older libcurls
 ?>
 });
 // Content-length is 2 instead of 3 in older libcurls
 ?>
@@ -25,17 +26,17 @@ server("proxy.inc", function($port) {
 Test
 HTTP/1.1 200 OK
 Accept-Ranges: bytes
 Test
 HTTP/1.1 200 OK
 Accept-Ranges: bytes
+Content-Length: %d
 Etag: "%x"
 X-Original-Transfer-Encoding: chunked
 Etag: "%x"
 X-Original-Transfer-Encoding: chunked
-Content-Length: %d
 
 PUT / HTTP/1.1
 
 PUT / HTTP/1.1
-Content-Range: bytes 1-2/3
-User-Agent: %s
-Host: localhost:%d
 Accept: */*
 Content-Length: %d
 Accept: */*
 Content-Length: %d
+Content-Range: bytes 1-2/3
 Expect: 100-continue
 Expect: 100-continue
+Host: localhost:%d
+User-Agent: %s
 X-Original-Content-Length: %d
 
 23===DONE===
 X-Original-Content-Length: %d
 
 23===DONE===
diff --git a/tests/helper/dump.inc b/tests/helper/dump.inc
new file mode 100644 (file)
index 0000000..5f5f367
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+
+function dump_message($stream, http\Message $msg, $parent = false) {
+       if (!is_resource($stream)) {
+               $stream = fopen("php://output", "w");
+       }
+       fprintf($stream, "%s\n", $msg->getInfo());
+       $headers = $msg->getHeaders();
+       ksort($headers);
+       foreach ($headers as $key => $val) {
+               fprintf($stream, "%s: %s\n", $key, $val);
+       }
+       fprintf($stream, "\n");
+       $msg->getBody()->toStream($stream);
+       
+       if ($parent && ($msg = $msg->getParentMessage())) {
+               dump_message($stream, $msg, true);
+       }
+}
+
+?>
\ No newline at end of file
index 80a007353c4f81e32a924ea7fc955e47d8217074..f99dd97cf735575c0cbd7857d0ab77bae9ead7b6 100644 (file)
@@ -1,5 +1,6 @@
 <?php 
 
 <?php 
 
+include "dump.inc";
 include "server.inc";
 
 serve(function($client) {
 include "server.inc";
 
 serve(function($client) {
@@ -18,6 +19,6 @@ serve(function($client) {
        /* return the initial message as response body */
        $response = new http\Env\Response;
        /* avoid OOM with $response->getBody()->append($request); */
        /* return the initial message as response body */
        $response = new http\Env\Response;
        /* avoid OOM with $response->getBody()->append($request); */
-       $request->toStream($response->getBody()->getResource());
+       dump_message($response->getBody()->getResource(), $request);
        $response->send($client);
 });
        $response->send($client);
 });
index 9502d2b6cdeb692e226508f867e8f8a92e07a226..ddc06a88f626b45f01cbc8094b48c9b79ca75096 100644 (file)
@@ -1,5 +1,6 @@
 <?php 
 
 <?php 
 
+include "dump.inc";
 include "server.inc";
 
 serve(function($client) {
 include "server.inc";
 
 serve(function($client) {
@@ -15,6 +16,6 @@ serve(function($client) {
        /* return the initial message as response body */
        $response = new http\Env\Response;
        /* avoid OOM with $response->getBody()->append($request); */
        /* return the initial message as response body */
        $response = new http\Env\Response;
        /* avoid OOM with $response->getBody()->append($request); */
-       $request->toStream($response->getBody()->getResource());
+       dump_message($response->getBody()->getResource(), $request);
        $response->send($client);
 });
        $response->send($client);
 });
diff --git a/tests/params016.phpt b/tests/params016.phpt
new file mode 100644 (file)
index 0000000..e5fbd97
--- /dev/null
@@ -0,0 +1,46 @@
+--TEST--
+header params rfc5988
+--SKIPIF--
+<?php
+include "skipif.inc";
+?>
+--FILE--
+<?php
+echo "Test\n";
+
+$link = <<<EOF
+<https://api.github.com/search/code?q=addClass+user%3Amozilla&page=2>; rel="next", <https://api.github.com/search/code?q=addClass+user%3Amozilla&page=34>; rel="last"
+EOF;
+
+$p = new http\Params($link, ",", ";", "=",
+       http\Params::PARSE_RFC5988 | http\Params::PARSE_ESCAPED);
+var_dump($p->params);
+var_dump((string)$p);
+?>
+===DONE===
+--EXPECT--
+Test
+array(2) {
+  ["https://api.github.com/search/code?q=addClass+user%3Amozilla&page=2"]=>
+  array(2) {
+    ["value"]=>
+    bool(true)
+    ["arguments"]=>
+    array(1) {
+      ["rel"]=>
+      string(4) "next"
+    }
+  }
+  ["https://api.github.com/search/code?q=addClass+user%3Amozilla&page=34"]=>
+  array(2) {
+    ["value"]=>
+    bool(true)
+    ["arguments"]=>
+    array(1) {
+      ["rel"]=>
+      string(4) "last"
+    }
+  }
+}
+string(162) "<https://api.github.com/search/code?q=addClass+user%3Amozilla&page=2>;rel="next",<https://api.github.com/search/code?q=addClass+user%3Amozilla&page=34>;rel="last""
+===DONE===
diff --git a/tests/params017.phpt b/tests/params017.phpt
new file mode 100644 (file)
index 0000000..b3587e5
--- /dev/null
@@ -0,0 +1,69 @@
+--TEST--
+header params rfc5988
+--SKIPIF--
+<?php
+include "skipif.inc";
+?>
+--FILE--
+<?php
+echo "Test\n";
+
+$link = <<<EOF
+Link: </TheBook/chapter2>;
+         rel="previous"; title*=UTF-8'de'letztes%20Kapitel,
+         </TheBook/chapter4>;
+         rel="next"; title*=UTF-8'de'n%c3%a4chstes%20Kapitel
+EOF;
+
+$p = current(http\Header::parse($link, "http\\Header"))->getParams(
+       http\Params::DEF_PARAM_SEP,
+       http\Params::DEF_ARG_SEP,
+       http\Params::DEF_VAL_SEP,
+       http\Params::PARSE_RFC5988 | http\Params::PARSE_ESCAPED
+);
+var_dump($p->params);
+var_dump((string)$p);
+?>
+===DONE===
+--EXPECTF--
+Test
+array(2) {
+  ["/TheBook/chapter2"]=>
+  array(2) {
+    ["value"]=>
+    bool(true)
+    ["arguments"]=>
+    array(2) {
+      ["rel"]=>
+      string(8) "previous"
+      ["*rfc5987*"]=>
+      array(1) {
+        ["title"]=>
+        array(1) {
+          ["de"]=>
+          string(15) "letztes Kapitel"
+        }
+      }
+    }
+  }
+  ["/TheBook/chapter4"]=>
+  array(2) {
+    ["value"]=>
+    bool(true)
+    ["arguments"]=>
+    array(2) {
+      ["rel"]=>
+      string(4) "next"
+      ["*rfc5987*"]=>
+      array(1) {
+        ["title"]=>
+        array(1) {
+          ["de"]=>
+          string(17) "nächstes Kapitel"
+        }
+      }
+    }
+  }
+}
+string(139) "</TheBook/chapter2>;rel="previous";title*=utf-8'de'letztes%20Kapitel,</TheBook/chapter4>;rel="next";title*=utf-8'de'n%C3%A4chstes%20Kapitel"
+===DONE===