Skip to content
Open
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"php-mock/php-mock-phpunit": "^2.10",
"phpstan/phpstan": "^1.12",
"nyholm/psr7": "^1.8",
"guzzlehttp/guzzle": "^7.8",
"ext-json": "*",
"ext-xml": "*",
"ext-curl": "*",
Expand Down
8 changes: 8 additions & 0 deletions test/known_failures_Linux.json
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,10 @@
"nodejs-nodejs_compact_websocket-domain",
"nodejs-nodejs_header_websocket-domain",
"nodejs-nodejs_json_websocket-domain",
"nodejs-php_binary-accel_http-ip",
"nodejs-php_binary_http-ip",
"nodejs-php_compact_http-ip",
"nodejs-php_json_http-ip",
"nodejs-py_binary-accel_http-domain",
"nodejs-py_binary-accel_http-ip",
"nodejs-py_binary-accel_http-ip-ssl",
Expand Down Expand Up @@ -615,6 +619,10 @@
"perl-netstd_multi-binary_buffered-ip-ssl",
"perl-netstd_multi-binary_framed-ip",
"perl-netstd_multi-binary_framed-ip-ssl",
"php-cpp_accel-binary_http-ip",
"php-cpp_binary_http-ip",
"php-cpp_compact_http-ip",
"php-cpp_json_http-ip",
"py-cpp_accel-binary_http-domain",
"py-cpp_accel-binary_http-ip",
"py-cpp_accel-binary_http-ip-ssl",
Expand Down
77 changes: 77 additions & 0 deletions test/php/HttpRouter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/**
* Two roles, dispatched on PHP_SAPI:
*
* - CLI (sourced via require from TestServer.php): `pcntl_exec` into
* `php -S` with this file as the per-request router. $port and $protocol
* are read from the caller's scope.
* - cli-server: handle one HTTP request via HttpServer. The protocol is
* forwarded from the launcher through the THRIFT_TEST_PROTOCOL env var.
*/

// cli-server: per-request handler.
if (PHP_SAPI === 'cli-server') {
require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/protocols.php';
require_once __DIR__ . '/HttpServer.php';

// ThriftTest generated classes are not in Composer's PSR-4 map; register
// the classmap loader before requiring Handler.php so its
// `implements ThriftTestIf` resolves correctly.
$loader = new \Thrift\ClassLoader\ThriftClassLoader();
$loader->registerDefinition('ThriftTest', __DIR__ . '/gen-php-classmap');
$loader->register();

require_once __DIR__ . '/Handler.php';

$protocolFactory = thrift_test_protocol_factory(getenv('THRIFT_TEST_PROTOCOL') ?: 'binary');
$processor = new ThriftTest\ThriftTestProcessor(new Handler());

(new HttpServer($processor, $protocolFactory))->serve();
return;
}

// CLI launcher: sourced from TestServer.php; $port and $protocol are in scope.
if (!function_exists('pcntl_exec')) {
fwrite(STDERR, "PHP HTTP cross-test requires ext-pcntl.\n");
exit(1);
}

$env = getenv();
$env['THRIFT_TEST_PROTOCOL'] = $protocol;

echo "Starting the Test server (HTTP via php -S)...\n";

// -d flags mirror the launcher invocation in test/tests.json — keep in sync.
pcntl_exec(PHP_BINARY, [
'-dextension_dir=php_ext_dir',
'-dextension=thrift_protocol.so',
'-ddisplay_errors=stderr',
'-dlog_errors=0',
'-derror_reporting=E_ALL',
'-S', '127.0.0.1:' . $port,
__FILE__,
], $env);

fwrite(STDERR, "pcntl_exec failed\n");
exit(1);
117 changes: 117 additions & 0 deletions test/php/HttpServer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

declare(strict_types=1);

use Thrift\Factory\TProtocolFactory;
use Thrift\Transport\TMemoryBuffer;
use Thrift\Type\TMessageType;

/**
* Cross-test HTTP request handler for PHP's built-in web server (`php -S`).
*
* Reads php://input, peeks the Thrift message type, and for ONEWAY sends an
* empty HTTP 200 immediately while running the handler in a forked child so
* the client's one-way call returns without waiting for handler execution.
*
* Cli-server specific: closing STDIN/STDOUT/STDERR in the child relies on
* php -S worker semantics and does not translate to php-fpm or mod_php.
*/
class HttpServer
{
public function __construct(
private object $processor,
private TProtocolFactory $protocolFactory,
) {
}

public function serve(): void
{
$requestBody = (string) file_get_contents('php://input');

header('Content-Type: application/x-thrift');

if ($this->peekMessageType($requestBody) === TMessageType::ONEWAY
&& function_exists('pcntl_fork')
&& $this->dispatchOnewayAsync($requestBody)
) {
return;
}

$this->dispatchSync($requestBody);
}

private function peekMessageType(string $body): ?int
{
if ($body === '') {
return null;
}
try {
$type = null;
$name = null;
$seqid = null;
$this->protocolFactory
->getProtocol(new TMemoryBuffer($body))
->readMessageBegin($name, $type, $seqid);

return $type;
} catch (\Throwable) {
return null;
}
}

private function dispatchOnewayAsync(string $body): bool
{
header('Content-Length: 0');
$pid = pcntl_fork();
if ($pid > 0) {
return true;
}
if ($pid === 0) {
if (defined('STDIN')) {
fclose(STDIN);
}
if (defined('STDOUT')) {
fclose(STDOUT);
}
if (defined('STDERR')) {
fclose(STDERR);
}
$this->processor->process(
$this->protocolFactory->getProtocol(new TMemoryBuffer($body)),
$this->protocolFactory->getProtocol(new TMemoryBuffer()),
);
exit(0);
}

return false;
}

private function dispatchSync(string $body): void
{
$output = new TMemoryBuffer();
$this->processor->process(
$this->protocolFactory->getProtocol(new TMemoryBuffer($body)),
$this->protocolFactory->getProtocol($output),
);
echo $output->getBuffer();
}
}
Loading
Loading