Skip to content

Commit

Permalink
Fixed issue with missing headers with empty content.
Browse files Browse the repository at this point in the history
  • Loading branch information
lotharthesavior committed Oct 5, 2024
1 parent 5c3bd0f commit a82c58c
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 87 deletions.
12 changes: 6 additions & 6 deletions config/jacked-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@
// Running server default options
// ------------------------------------------------------------

'server-protocol' => 'HTTP/1.1',
'content-type' => 'text/html',
'reactor-num' => $_ENV['JACKED_SERVER_REACTOR_NUM'] ?? Util::getCPUNum() + 2,
'worker-num' => $_ENV['JACKED_SERVER_WORKER_NUM'] ?? Util::getCPUNum() + 2,
// @phpstan-ignore-next-line
'input-file' => $_ENV['JACKED_SERVER_INPUT_FILE'] ?? ROOT_DIR . '/index.php',
'openswoole-server-settings' => [
Expand All @@ -45,8 +41,8 @@
),

// reactor and workers
'reactor_num' => 4,
'worker_num' => 4,
'reactor_num' => $_ENV['JACKED_SERVER_REACTOR_NUM'] ?? Util::getCPUNum() + 2,
'worker_num' => $_ENV['JACKED_SERVER_WORKER_NUM'] ?? Util::getCPUNum() + 2,

// timeout
'max_request_execution_time' => 60,
Expand All @@ -58,6 +54,10 @@

// @phpstan-ignore-next-line
// 'pid_file' => $_ENV['JACKED_SERVER_PID_FILE'] ?? ROOT_DIR . '/jacked-server.pid',

'http_compression' => ($_ENV['JACKED_SERVER_HTTP_COMPRESSION'] ?? 'true') === 'true',
'http_compression_level' => (int) ($_ENV['JACKED_SERVER_HTTP_COMPRESSION_LEVEL'] ?? 1),
'http_parse_cookie' => ($_ENV['JACKED_SERVER_HTTP_PARSE_COOKIE'] ?? 'true') === 'true',
],

// ------------------------------------------------------------
Expand Down
8 changes: 7 additions & 1 deletion src/Data/OpenSwooleConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public function __construct(
public ?string $sslKeyFile,
public bool $openHttpProtocol,
// public string $pidFile,
public bool $httpCompression,
public int $httpCompressionLevel,
public bool $httpParseCookie = false,
public array $staticHandlerLocations = [],
) {
}
Expand All @@ -32,14 +35,17 @@ public static function rules(): array
return [
'documentRoot' => ['required', 'string'],
'enableStaticHandler' => ['required', 'boolean'],
'staticHandlerLocations' => ['array'],
'reactorNum' => ['required', 'integer'],
'workerNum' => ['required', 'integer'],
'maxRequestExecutionTime' => ['required', 'integer'],
'sslCertFile' => ['nullable', 'string'],
'sslKeyFile' => ['nullable', 'string'],
'openHttpProtocol' => ['required', 'boolean'],
// 'pidFile' => ['required', 'string'],
'httpCompression' => ['required', 'boolean'],
'httpCompressionLevel' => ['required', 'integer'],
'httpParseCookie' => ['required', 'boolean'],
'staticHandlerLocations' => ['array'],
];
}

Expand Down
25 changes: 13 additions & 12 deletions src/Services/FastCgiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,11 @@ private function connect()
/**
* Build a FastCGI packet
*
* @param Integer $type Type of the packet
* @param String $content Content of the packet
* @param Integer $requestId RequestId
* @param int $type Type of the packet
* @param string $content Content of the packet
* @param int $requestId RequestId
*/
private function buildPacket($type, $content, $requestId = 1)
private function buildPacket(int $type, string $content, int $requestId = 1): string
{
$clen = strlen($content);
return chr(self::VERSION_1) /* version */
Expand All @@ -265,11 +265,11 @@ private function buildPacket($type, $content, $requestId = 1)
/**
* Build an FastCGI Name value pair
*
* @param String $name Name
* @param String $value Value
* @return String FastCGI Name value pair
* @param string $name Name
* @param string $value Value
* @return string FastCGI Name value pair
*/
private function buildNvpair($name, $value)
private function buildNvpair(string $name, string $value): string
{
$nlen = strlen($name);
$vlen = strlen($value);
Expand All @@ -294,10 +294,11 @@ private function buildNvpair($name, $value)
/**
* Read a set of FastCGI Name value pairs
*
* @param String $data Data containing the set of FastCGI NVPair
* @return array of NVPair
* @param string $data Data containing the set of FastCGI NVPair
* @param ?int $length Length of the data
* @return array<mixed> of NVPair
*/
private function readNvpair($data, $length = null)
private function readNvpair(string $data, ?int $length = null): array
{
$array = array();

Expand Down Expand Up @@ -681,7 +682,7 @@ public function requestStream(array $params, $stdin, callable $callback): string
}

if (!is_array($resp)) {
throw new \Exception('Read failed');
throw new Exception('Read failed');
}

switch (ord($resp['content'][4])) {
Expand Down
3 changes: 2 additions & 1 deletion src/Services/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,8 @@ private function sendResponse(
$streamCallback($response);
$response->end();
} elseif (!empty($body)) {
$response->write($body);
$response->header('Connection', ['close']);
$response->end($body);
} else {
$response->end();
}
Expand Down
54 changes: 3 additions & 51 deletions src/Services/Traits/HttpSupport.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ protected function addServerInfo(array &$requestOptions, array $serverInfo = [])
protected function addHeaders(array &$requestOptions, array $headers = []): void
{
foreach ($headers as $key => $value) {
// add extra headers with http_ prefix
$key = str_replace('-', '_', $key);
// here we are duplicating the header with the http_ prefix just in case,
// but it seems to work without it - some review is necessary
$requestOptions['http_' . $key] = $value;
$requestOptions[$key] = $value;
}
Expand Down Expand Up @@ -97,54 +97,6 @@ protected function checkPathTrailingSlash(array $requestOptions): void
}
}

/**
* @param Request $request
* @param Response $response
* @param string $host
* @param int $port
* @param array<array-key, string> $allowedHeaders
* @return void
*/
protected function proxyRequest(
Request $request,
Response $response,
string $host,
int $port,
array $allowedHeaders,
): void {
$this->report($this->logPrefix . 'Proxy Request: {pathInfo}', context: [
'pathInfo' => $request->server['path_info'] ?? '',
'requestOptions' => $request->server,
'content' => $request->rawContent(),
], skipPrint: true);
$this->report($this->logPrefix . 'Request Time: {time}', context: [
'time' => Carbon::now()->format('Y-m-d H:i:s'),
]);

$client = new CoroutineHttpClient($host, $port);
$client->setHeaders([
'Host' => $host . ':' . $port,
'User-Agent' => 'Jacked Server HTTP Proxy',
]);
$client->set([ 'timeout' => $this->timeout]);
$status = $client->execute($request->server['request_uri']);

$headers = $client->headers;
$body = $client->body;
$client->close();

foreach ($headers ?? [] as $key => $value) {
if (in_array($key, $allowedHeaders)) {
$response->header($key, $value);
}
}
$response->status($status);

$response->write($body);

$response->end();
}

protected function executeRequest(array $requestOptions, string $content, Response $response): JackedResponse|null
{
$result = '';
Expand Down Expand Up @@ -251,7 +203,7 @@ private function prepareRequestOptions(

$requestUri = Arr::get($serverInfo, 'request_uri', '');

return array_change_key_case(array_filter(array_merge(
return array_change_key_case(array_merge(
$requestOptions,
$this->getProjectLocation(header: $header, serverInfo: $serverInfo, requestUri: $requestUri),
[
Expand All @@ -264,7 +216,7 @@ private function prepareRequestOptions(
),
'server_name' => Arr::get($requestOptions, 'http_host'),
],
)), CASE_UPPER);
), CASE_UPPER);
}

private function getProjectLocation(array $header, array $serverInfo, string $requestUri): array
Expand Down
4 changes: 0 additions & 4 deletions tests/Sample/config/jacked-server-http.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@
// Running server default options
// ------------------------------------------------------------

'server-protocol' => 'HTTP/1.1',
'content-type' => 'text/html',
'reactor-num' => $_ENV['JACKED_SERVER_REACTOR_NUM'] ?? Util::getCPUNum() + 2,
'worker-num' => $_ENV['JACKED_SERVER_WORKER_NUM'] ?? Util::getCPUNum() + 2,
// @phpstan-ignore-next-line
'input-file' => '/index.php',
'openswoole-server-settings' => [
Expand Down
4 changes: 0 additions & 4 deletions tests/Sample/config/jacked-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@
// Running server default options
// ------------------------------------------------------------

'server-protocol' => 'HTTP/1.1',
'content-type' => 'text/html',
'reactor-num' => $_ENV['JACKED_SERVER_REACTOR_NUM'] ?? Util::getCPUNum() + 2,
'worker-num' => $_ENV['JACKED_SERVER_WORKER_NUM'] ?? Util::getCPUNum() + 2,
// @phpstan-ignore-next-line
'input-file' => '/index.php',
'openswoole-server-settings' => [
Expand Down
4 changes: 0 additions & 4 deletions tests/Sample/ws-auth/jacked-server-with-ws-auth-2.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@
// Running server default options
// ------------------------------------------------------------

'server-protocol' => 'HTTP/1.1',
'content-type' => 'text/html',
'reactor-num' => $_ENV['JACKED_SERVER_REACTOR_NUM'] ?? Util::getCPUNum() + 2,
'worker-num' => $_ENV['JACKED_SERVER_WORKER_NUM'] ?? Util::getCPUNum() + 2,
// @phpstan-ignore-next-line
'input-file' => $_ENV['JACKED_SERVER_INPUT_FILE'] ?? ROOT_DIR . '/index.php',
'openswoole-server-settings' => [
Expand Down
4 changes: 0 additions & 4 deletions tests/Sample/ws-auth/jacked-server-with-ws-auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@
// Running server default options
// ------------------------------------------------------------

'server-protocol' => 'HTTP/1.1',
'content-type' => 'text/html',
'reactor-num' => $_ENV['JACKED_SERVER_REACTOR_NUM'] ?? Util::getCPUNum() + 2,
'worker-num' => $_ENV['JACKED_SERVER_WORKER_NUM'] ?? Util::getCPUNum() + 2,
// @phpstan-ignore-next-line
'input-file' => $_ENV['JACKED_SERVER_INPUT_FILE'] ?? ROOT_DIR . '/index.php',
'openswoole-server-settings' => [
Expand Down

0 comments on commit a82c58c

Please sign in to comment.