/** * Creates and returns a new FastCGI client instance. * * @param \AppserverIo\Server\Interfaces\RequestContextInterface $requestContext A requests context instance * @param \React\EventLoop\LoopInterface $loop The event loop instance * * @return \Crunch\FastCGI\Connection The FastCGI connection instance */ protected function getFastCgiClient(RequestContextInterface $requestContext, LoopInterface $loop) { // initialize default host/port/DNS server $host = FcgiModule::DEFAULT_FAST_CGI_IP; $port = FcgiModule::DEFAULT_FAST_CGI_PORT; $dnsServer = FcgiModule::DEFAULT_DNS_SERVER; // set the connection data to be used for the Fast-CGI connection $fileHandlerVariables = array(); // check if we've configured module variables if ($requestContext->hasModuleVar(ModuleVars::VOLATILE_FILE_HANDLER_VARIABLES)) { // load the volatile file handler variables and set connection data $fileHandlerVariables = $requestContext->getModuleVar(ModuleVars::VOLATILE_FILE_HANDLER_VARIABLES); if (isset($fileHandlerVariables[FcgiModule::PARAM_HOST])) { $host = $fileHandlerVariables[FcgiModule::PARAM_HOST]; } if (isset($fileHandlerVariables[FcgiModule::PARAM_PORT])) { $port = $fileHandlerVariables[FcgiModule::PARAM_PORT]; } if (isset($fileHandlerVariables[FcgiModule::PARAM_DNS_SERVER])) { $dnsServer = $fileHandlerVariables[FcgiModule::PARAM_DNS_SERVER]; } } // initialize the socket connector with the DNS resolver $dnsResolverFactory = new DnsResolverFactory(); $dns = $dnsResolverFactory->createCached($dnsServer, $loop); // initialize the FastCGI factory with the connector $connector = new SocketConnector($loop, $dns); $factory = new FcgiClientFactory($loop, $connector); // initialize the FastCGI client with the FastCGI server IP and port return $factory->createClient($host, $port); }
require __DIR__ . '/../vendor/autoload.php'; use Crunch\FastCGI\Client\Client; use Crunch\FastCGI\Client\ClientException; use Crunch\FastCGI\Client\Factory as FastCGIClientFactory; use Crunch\FastCGI\Protocol\RequestParameters; use React\Dns\Resolver\Factory as DnsResolverFactory; use React\EventLoop\Factory as EventLoopFactory; use React\SocketClient\Connector as SocketConnector; use React\Promise as promise; $loop = EventLoopFactory::create(); $dnsResolverFactory = new DnsResolverFactory(); $dns = $dnsResolverFactory->createCached('0.0.0.0', $loop); $connector = new SocketConnector($loop, $dns); $factory = new FastCGIClientFactory($loop, $connector); $factory->createClient('127.0.0.1', 1337)->then(function (Client $client) use($argv) { $name = @$argv[1] ?: 'World'; $data = "name={$name}"; $request = $client->newRequest(new RequestParameters(['REQUEST_METHOD' => 'POST', 'SCRIPT_FILENAME' => __DIR__ . '/docroot/hello-world.php', 'CONTENT_TYPE' => 'application/x-www-form-urlencoded', 'CONTENT_LENGTH' => strlen($data)]), new \Crunch\FastCGI\ReaderWriter\StringReader($data)); $request2 = $client->newRequest(new RequestParameters(['REQUEST_METHOD' => 'POST', 'SCRIPT_FILENAME' => __DIR__ . '/docroot/hello-world.php', 'CONTENT_TYPE' => 'application/x-www-form-urlencoded', 'CONTENT_LENGTH' => strlen($data)]), new \Crunch\FastCGI\ReaderWriter\StringReader($data)); $responseHandler = function ($response) use($client) { echo "\n" . $response->getContent()->read() . \PHP_EOL; }; $failHandler = function (ClientException $fail) { echo "Request failed: {$fail->getMessage()}"; return $fail; }; $x = $client->sendRequest($request)->then($responseHandler, $failHandler); $y = $client->sendRequest($request2)->then($responseHandler, $failHandler); $z = $client->sendRequest($request2)->then($responseHandler, $failHandler); $all = promise\all([$x, $y, $z]);
/** * @medium */ public function testSendRequestWithOversizedParameters() { $this->expectPHPFPMRunning(); $loop = LoopFactory::create(); $dnsResolverFactory = new ResolverFactory(); $dns = $dnsResolverFactory->createCached('0.0.0.0', $loop); $connector = new SocketConnector($loop, $dns); $factory = new ClientFactory($loop, $connector); $factory->createClient('127.0.0.1', 9331)->then(function (Client $client) { $params = ['GATEWAY_INTERFACE' => 'FastCGI/1.0', 'REQUEST_METHOD' => 'POST', 'SCRIPT_FILENAME' => __DIR__ . '/Resources/scripts/echo.php', 'CONTENT_TYPE' => 'application/x-www-form-urlencoded', 'CONTENT_LENGTH' => strlen('foo=bar')]; for ($i = 1; $i < 4000; $i++) { $params["param{$i}"] = "value{$i}"; } $request = $client->newRequest(new RequestParameters($params), new StringReader('foo=bar')); $client->sendRequest($request)->then(function (Response $response) use($client) { $client->close(); list($header, $body) = explode("\r\n\r\n", $response->getContent()->read()); list($server) = unserialize($body); self::assertEquals(7, $server['CONTENT_LENGTH']); }); return $client; }); $loop->run(); }