Skip to content

Commit a73780e

Browse files
committed
The uploaded file name does not need to be url-decoded
1 parent 848998a commit a73780e

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed

ext-src/php_swoole_http.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ static inline bool swoole_http_has_crlf(const char *value, size_t length) {
295295
return false;
296296
}
297297

298-
void swoole_http_parse_cookie(zval *array, const char *at, size_t length);
298+
void swoole_http_parse_cookie(zval *array, const char *at, size_t length, bool url_decode = true);
299299

300300
swoole::http::Context *php_swoole_http_request_get_context(zval *zobject);
301301
void php_swoole_http_request_set_context(zval *zobject, swoole::http::Context *context);

ext-src/swoole_http_request.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ bool HttpContext::parse_form_data(const char *boundary_str, int boundary_len) {
276276
return true;
277277
}
278278

279-
void swoole_http_parse_cookie(zval *zarray, const char *at, size_t length) {
279+
void swoole_http_parse_cookie(zval *zarray, const char *at, size_t length, bool url_decode) {
280280
char keybuf[SW_HTTP_COOKIE_KEYLEN];
281281
char valbuf[SW_HTTP_COOKIE_VALLEN];
282282
char *_c = (char *) at;
@@ -354,7 +354,9 @@ void swoole_http_parse_cookie(zval *zarray, const char *at, size_t length) {
354354
memcpy(valbuf, (char *) at + j, vlen);
355355
valbuf[vlen] = 0;
356356
_value = http_trim_double_quote(valbuf, &vlen);
357-
vlen = php_url_decode(_value, vlen);
357+
if (url_decode) {
358+
vlen = php_url_decode(_value, vlen);
359+
}
358360
if (klen > 1) {
359361
add_assoc_stringl_ex(zarray, keybuf, klen - 1, _value, vlen);
360362
}
@@ -518,7 +520,7 @@ static int multipart_body_on_header_value(multipart_parser *p, const char *at, s
518520

519521
zval tmp_array;
520522
array_init(&tmp_array);
521-
swoole_http_parse_cookie(&tmp_array, at + sizeof("form-data;") - 1, length - sizeof("form-data;") + 1);
523+
swoole_http_parse_cookie(&tmp_array, at + sizeof("form-data;") - 1, length - sizeof("form-data;") + 1, false);
522524

523525
zval *zform_name;
524526
if (!(zform_name = zend_hash_str_find(Z_ARRVAL(tmp_array), ZEND_STRL("name")))) {

tests/swoole_http_server/upload4.phpt

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
--TEST--
2+
swoole_http_server: upload 04
3+
--SKIPIF--
4+
<?php
5+
require __DIR__ . '/../include/skipif.inc';
6+
skip_if_function_not_exist('curl_init');
7+
?>
8+
--FILE--
9+
<?php
10+
require __DIR__ . '/../include/bootstrap.php';
11+
12+
use Swoole\Http\Server;
13+
14+
const FILENAME = "test-+=.jpg";
15+
16+
$pm = new ProcessManager;
17+
$pm->parentFunc = function () use ($pm) {
18+
$ch = curl_init();
19+
curl_setopt($ch, CURLOPT_URL, "http://127.0.0.1:{$pm->getFreePort()}");
20+
curl_setopt($ch, CURLOPT_HEADER, 0);
21+
curl_setopt($ch, CURLOPT_POST, 1); //设置为POST方式
22+
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
23+
24+
$file = TEST_IMAGE;
25+
26+
$post_data = array('test' => str_repeat('a', 80));
27+
28+
$cfile = curl_file_create($file);
29+
$cfile->setPostFilename(FILENAME);
30+
$post_data['file'] = $cfile;
31+
32+
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); //POST数据
33+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
34+
$res = curl_exec($ch);
35+
Assert::assert(!empty($res));
36+
Assert::eq($res, FILENAME);
37+
curl_close($ch);
38+
39+
$pm->kill();
40+
};
41+
42+
$pm->childFunc = function () use ($pm) {
43+
$http = new Server('127.0.0.1', $pm->getFreePort(), SWOOLE_BASE);
44+
45+
$http->set([
46+
'log_file' => '/dev/null'
47+
]);
48+
49+
$http->on("WorkerStart", function () use ($pm) {
50+
$pm->wakeup();
51+
});
52+
53+
$http->on("request", function (swoole_http_request $request, swoole_http_response $response) {
54+
$response->end($request->files['file']['name']);
55+
});
56+
57+
$http->start();
58+
};
59+
60+
$pm->childFirst();
61+
$pm->run();
62+
?>
63+
--EXPECT--

0 commit comments

Comments
 (0)