Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed reading in streamed body using fastcgi #7509

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions main/fastcgi.c
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,7 @@ int fcgi_read(fcgi_request *req, char *str, int len)
req->in_len = (hdr.contentLengthB1 << 8) | hdr.contentLengthB0;
req->in_pad = hdr.paddingLength;
if (req->in_len == 0) {
req->in_len = -1;
return n;
}
}
Expand Down Expand Up @@ -1319,6 +1320,11 @@ int fcgi_is_closed(fcgi_request *req)
return (req->fd < 0);
}

int fcgi_is_eof(fcgi_request *req)
{
return (req->in_len == -1);
}

static int fcgi_is_allowed(void) {
int i;

Expand Down
1 change: 1 addition & 0 deletions main/fastcgi.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ void fcgi_shutdown(void);
int fcgi_is_fastcgi(void);
int fcgi_is_closed(fcgi_request *req);
void fcgi_close(fcgi_request *req, int force, int destroy);
int fcgi_is_eof(fcgi_request *req);
int fcgi_in_shutdown(void);
void fcgi_terminate(void);
int fcgi_listen(const char *path, int backlog);
Expand Down
8 changes: 4 additions & 4 deletions sapi/cgi/cgi_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -504,11 +504,7 @@ static size_t sapi_fcgi_read_post(char *buffer, size_t count_bytes)
size_t read_bytes = 0;
int tmp_read_bytes;
fcgi_request *request = (fcgi_request*) SG(server_context);
size_t remaining = SG(request_info).content_length - SG(read_post_bytes);

if (remaining < count_bytes) {
count_bytes = remaining;
}
while (read_bytes < count_bytes) {
size_t diff = count_bytes - read_bytes;
int to_read = (diff > INT_MAX) ? INT_MAX : (int)diff;
Expand All @@ -518,6 +514,10 @@ static size_t sapi_fcgi_read_post(char *buffer, size_t count_bytes)
break;
}
read_bytes += tmp_read_bytes;

if (fcgi_is_eof(request)) {
break;
}
}
return read_bytes;
}
Expand Down
8 changes: 3 additions & 5 deletions sapi/fpm/fpm/fpm_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -429,11 +429,6 @@ static size_t sapi_cgi_read_post(char *buffer, size_t count_bytes) /* {{{ */
{
uint32_t read_bytes = 0;
int tmp_read_bytes;
size_t remaining = SG(request_info).content_length - SG(read_post_bytes);

if (remaining < count_bytes) {
count_bytes = remaining;
}
while (read_bytes < count_bytes) {
fcgi_request *request = (fcgi_request*) SG(server_context);
if (request_body_fd == -1) {
Expand All @@ -450,6 +445,9 @@ static size_t sapi_cgi_read_post(char *buffer, size_t count_bytes) /* {{{ */
}
}

if (fcgi_is_eof(request)) {
break;
}
/* If REQUEST_BODY_FILE variable not available - read post body from fastcgi stream */
if (request_body_fd < 0) {
tmp_read_bytes = fcgi_read(request, buffer + read_bytes, count_bytes - read_bytes);
Expand Down
60 changes: 60 additions & 0 deletions sapi/fpm/tests/request-body-chunked-51191.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
--TEST--
FPM: Test reading in request body without Content-Length header
--SKIPIF--
<?php include "skipif.inc"; ?>
--FILE--
<?php

require_once "tester.inc";

$cfg = <<<EOT
[global]
error_log = {{FILE:LOG}}
[unconfined]
listen = {{ADDR}}
pm = dynamic
pm.max_children = 5
pm.start_servers = 1
pm.min_spare_servers = 1
pm.max_spare_servers = 3
EOT;

$code = <<<EOT
<?php
echo "Test Start\n";
var_dump(file_get_contents("php://input"));
echo "Test End\n";
EOT;

$headers = [];
$tester = new FPM\Tester($cfg, $code);
$tester->start();
$tester->expectLogStartNotices();
$tester->request(
'',
[],
null,
null,
null,
null,
false,
"The body"
)->expectBody(
[
'Test Start',
'string(8) "The body"',
'Test End',
]
);
$tester->terminate();
$tester->close();

?>
Done
--EXPECT--
Done
--CLEAN--
<?php
require_once "tester.inc";
FPM\Tester::clean();
?>
6 changes: 4 additions & 2 deletions sapi/fpm/tests/tester.inc
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ class Tester
* @param string|null $errorMessage
* @param bool $connKeepAlive
* @param string|null $scriptFilename = null
* @param string|null $stdin = null
* @return Response
*/
public function request(
Expand All @@ -589,7 +590,8 @@ class Tester
string $successMessage = null,
string $errorMessage = null,
bool $connKeepAlive = false,
string $scriptFilename = null
string $scriptFilename = null,
?string $stdin = null
) {
if ($this->hasError()) {
return new Response(null, true);
Expand All @@ -599,7 +601,7 @@ class Tester

try {
$this->response = new Response(
$this->getClient($address, $connKeepAlive)->request_data($params, false)
$this->getClient($address, $connKeepAlive)->request_data($params, $stdin)
);
$this->message($successMessage);
} catch (\Exception $exception) {
Expand Down