Skip to content

Commit

Permalink
Fixed reading in streamed body using fastcgi
Browse files Browse the repository at this point in the history
fixes #51191
  • Loading branch information
Tim Siebels committed Sep 23, 2021
1 parent 082c5b8 commit 0dd2092
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 7 deletions.
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: 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();
?>
5 changes: 3 additions & 2 deletions sapi/fpm/tests/tester.inc
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,8 @@ class Tester
string $address = null,
string $successMessage = null,
string $errorMessage = null,
bool $connKeepAlive = false
bool $connKeepAlive = false,
$stdin = false
) {
if ($this->hasError()) {
return new Response(null, true);
Expand Down Expand Up @@ -577,7 +578,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

0 comments on commit 0dd2092

Please sign in to comment.