addendum to #92
authorMichael Wallner <mike@php.net>
Fri, 16 Aug 2019 12:02:13 +0000 (14:02 +0200)
committerMichael Wallner <mike@php.net>
Fri, 16 Aug 2019 12:02:13 +0000 (14:02 +0200)
ignore numeric array keys for file entries

src/php_http_message_body.c
tests/gh-issue92.phpt [new file with mode: 0644]

index f0068b817a0fdf6714beac88d83b6f59f7250dae..db86ab07ea8129e8c7a18d14ee306579f060c3a6 100644 (file)
@@ -392,14 +392,17 @@ static ZEND_RESULT_CODE add_recursive_files(php_http_message_body_t *body, const
                        ZEND_HASH_FOREACH_KEY_VAL_IND(files, key.h, key.key, val)
                        {
                                if (Z_TYPE_P(val) == IS_ARRAY || Z_TYPE_P(val) == IS_OBJECT) {
-                                       char *str = format_key(&key, name);
+                                       char *str = key.key ? format_key(&key, name) : NULL;
+                                       const char *prefix = str ?: name;
 
-                                       if (SUCCESS != add_recursive_files(body, str, HASH_OF(val))) {
+                                       if (SUCCESS != add_recursive_files(body, prefix, HASH_OF(val))) {
                                                efree(str);
                                                HT_UNPROTECT_RECURSION(files);
                                                return FAILURE;
                                        }
-                                       efree(str);
+                                       if (str) {
+                                               efree(str);
+                                       }
                                }
                        }
                        ZEND_HASH_FOREACH_END();
diff --git a/tests/gh-issue92.phpt b/tests/gh-issue92.phpt
new file mode 100644 (file)
index 0000000..eb604c8
--- /dev/null
@@ -0,0 +1,165 @@
+--TEST--
+gh-issue #93: message body add form ignores numeric indices
+--SKIPIF--
+<?php
+include "skipif.inc";
+?>
+--FILE--
+<?php
+echo "Test\n";
+
+$temp = new http\Message\Body;
+$temp->addForm(
+       array("foo", "bar", "baz"),
+       array(
+               array(
+                       "file" => __FILE__,
+                       "name" => "upload",
+                       "type" => "text/plain",
+               ),
+               "dir" => array(
+                       array(
+                               "file" => __FILE__,
+                               "name" => 1,
+                               "type" => "text/plain",
+                       ),
+                       array(
+                               "file" => __FILE__,
+                               "name" => 2,
+                               "type" => "text/plain",
+                       ),
+               ),
+       )
+);
+
+echo $temp;
+
+?>
+DONE
+--EXPECTF--
+Test
+--%x.%x
+Content-Disposition: form-data; name="0"
+
+foo
+--%x.%x
+Content-Disposition: form-data; name="1"
+
+bar
+--%x.%x
+Content-Disposition: form-data; name="2"
+
+baz
+--%x.%x
+Content-Disposition: form-data; name="upload"; filename="gh-issue92.php"
+Content-Transfer-Encoding: binary
+Content-Type: text/plain
+
+<?php
+echo "Test\n";
+
+$temp = new http\Message\Body;
+$temp->addForm(
+       array("foo", "bar", "baz"),
+       array(
+               array(
+                       "file" => __FILE__,
+                       "name" => "upload",
+                       "type" => "text/plain",
+               ),
+               "dir" => array(
+                       array(
+                               "file" => __FILE__,
+                               "name" => 1,
+                               "type" => "text/plain",
+                       ),
+                       array(
+                               "file" => __FILE__,
+                               "name" => 2,
+                               "type" => "text/plain",
+                       ),
+               ),
+       )
+);
+
+echo $temp;
+
+?>
+DONE
+
+--%x.%x
+Content-Disposition: form-data; name="dir[1]"; filename="gh-issue92.php"
+Content-Transfer-Encoding: binary
+Content-Type: text/plain
+
+<?php
+echo "Test\n";
+
+$temp = new http\Message\Body;
+$temp->addForm(
+       array("foo", "bar", "baz"),
+       array(
+               array(
+                       "file" => __FILE__,
+                       "name" => "upload",
+                       "type" => "text/plain",
+               ),
+               "dir" => array(
+                       array(
+                               "file" => __FILE__,
+                               "name" => 1,
+                               "type" => "text/plain",
+                       ),
+                       array(
+                               "file" => __FILE__,
+                               "name" => 2,
+                               "type" => "text/plain",
+                       ),
+               ),
+       )
+);
+
+echo $temp;
+
+?>
+DONE
+
+--%x.%x
+Content-Disposition: form-data; name="dir[2]"; filename="gh-issue92.php"
+Content-Transfer-Encoding: binary
+Content-Type: text/plain
+
+<?php
+echo "Test\n";
+
+$temp = new http\Message\Body;
+$temp->addForm(
+       array("foo", "bar", "baz"),
+       array(
+               array(
+                       "file" => __FILE__,
+                       "name" => "upload",
+                       "type" => "text/plain",
+               ),
+               "dir" => array(
+                       array(
+                               "file" => __FILE__,
+                               "name" => 1,
+                               "type" => "text/plain",
+                       ),
+                       array(
+                               "file" => __FILE__,
+                               "name" => 2,
+                               "type" => "text/plain",
+                       ),
+               ),
+       )
+);
+
+echo $temp;
+
+?>
+DONE
+
+--%x.%x--
+DONE