Skip to content

Commit

Permalink
The uploaded file name does not need to be url-decoded
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Aug 27, 2021
1 parent 848998a commit a73780e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
2 changes: 1 addition & 1 deletion ext-src/php_swoole_http.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ static inline bool swoole_http_has_crlf(const char *value, size_t length) {
return false;
}

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

swoole::http::Context *php_swoole_http_request_get_context(zval *zobject);
void php_swoole_http_request_set_context(zval *zobject, swoole::http::Context *context);
Expand Down
8 changes: 5 additions & 3 deletions ext-src/swoole_http_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ bool HttpContext::parse_form_data(const char *boundary_str, int boundary_len) {
return true;
}

void swoole_http_parse_cookie(zval *zarray, const char *at, size_t length) {
void swoole_http_parse_cookie(zval *zarray, const char *at, size_t length, bool url_decode) {
char keybuf[SW_HTTP_COOKIE_KEYLEN];
char valbuf[SW_HTTP_COOKIE_VALLEN];
char *_c = (char *) at;
Expand Down Expand Up @@ -354,7 +354,9 @@ void swoole_http_parse_cookie(zval *zarray, const char *at, size_t length) {
memcpy(valbuf, (char *) at + j, vlen);
valbuf[vlen] = 0;
_value = http_trim_double_quote(valbuf, &vlen);
vlen = php_url_decode(_value, vlen);
if (url_decode) {
vlen = php_url_decode(_value, vlen);
}
if (klen > 1) {
add_assoc_stringl_ex(zarray, keybuf, klen - 1, _value, vlen);
}
Expand Down Expand Up @@ -518,7 +520,7 @@ static int multipart_body_on_header_value(multipart_parser *p, const char *at, s

zval tmp_array;
array_init(&tmp_array);
swoole_http_parse_cookie(&tmp_array, at + sizeof("form-data;") - 1, length - sizeof("form-data;") + 1);
swoole_http_parse_cookie(&tmp_array, at + sizeof("form-data;") - 1, length - sizeof("form-data;") + 1, false);

zval *zform_name;
if (!(zform_name = zend_hash_str_find(Z_ARRVAL(tmp_array), ZEND_STRL("name")))) {
Expand Down
63 changes: 63 additions & 0 deletions tests/swoole_http_server/upload4.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
--TEST--
swoole_http_server: upload 04
--SKIPIF--
<?php
require __DIR__ . '/../include/skipif.inc';
skip_if_function_not_exist('curl_init');
?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';

use Swoole\Http\Server;

const FILENAME = "test-+=.jpg";

$pm = new ProcessManager;
$pm->parentFunc = function () use ($pm) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://127.0.0.1:{$pm->getFreePort()}");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1); //设置为POST方式
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));

$file = TEST_IMAGE;

$post_data = array('test' => str_repeat('a', 80));

$cfile = curl_file_create($file);
$cfile->setPostFilename(FILENAME);
$post_data['file'] = $cfile;

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); //POST数据
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($ch);
Assert::assert(!empty($res));
Assert::eq($res, FILENAME);
curl_close($ch);

$pm->kill();
};

$pm->childFunc = function () use ($pm) {
$http = new Server('127.0.0.1', $pm->getFreePort(), SWOOLE_BASE);

$http->set([
'log_file' => '/dev/null'
]);

$http->on("WorkerStart", function () use ($pm) {
$pm->wakeup();
});

$http->on("request", function (swoole_http_request $request, swoole_http_response $response) {
$response->end($request->files['file']['name']);
});

$http->start();
};

$pm->childFirst();
$pm->run();
?>
--EXPECT--

0 comments on commit a73780e

Please sign in to comment.