function __construct(Request $request) { $this->request = $request; $headers = $request->getHeaders(); if (isset($headers['Content-Length']) && is_numeric($contentLength = $headers['Content-Length'])) { $this->contentLength = $contentLength; } $request->on('data', [$this, 'handleData']); $request->on('close', [$this, 'handleClose']); $request->on('error', [$this, 'handleError']); }
public function load($url, $deep) { if (null !== $this->url) { throw new \RuntimeException("This Loader object already loading an url."); } $url = filter_var($url, FILTER_VALIDATE_URL); if (false === $url) { return false; } $this->url = $url; $this->deep = $deep; $this->request = $this->client->request('GET', $url); $this->request->on('response', array($this, 'onResponse')); $this->request->end(); return true; }
/** * Handle a request * * @param ReactRequest $request * @param ReactResponse $response */ public function onRequest(ReactRequest $request, ReactResponse $response) { $content = ''; $headers = $request->getHeaders(); $contentLength = isset($headers['Content-Length']) ? (int) $headers['Content-Length'] : 0; $request->on('data', function ($data) use($request, $response, &$content, $contentLength) { // Read data (may be empty for GET request) $content .= $data; // Handle request after receive if (strlen($content) >= $contentLength) { $symfonyRequest = static::mapRequest($request, $content); try { // Execute $symfonyResponse = $this->application->handle($symfonyRequest); } catch (\Throwable $t) { // Executed only in PHP 7, will not match in PHP 5.x $this->fatalError($response, $t); return; } catch (\Exception $e) { // Executed only in PHP 5.x, will not be reached in PHP 7 $this->fatalError($response, $e); return; } static::mapResponse($response, $symfonyResponse); if ($this->application instanceof SymfonyHttpKernel\TerminableInterface) { $this->application->terminate($symfonyRequest, $symfonyResponse); } } }); }
/** * {@inheritdoc} */ public function onRequest(ReactRequest $request, ReactResponse $response) { if (NULL === $this->application) { return; } $content = ''; $headers = $request->getHeaders(); $contentLength = isset($headers['Content-Length']) ? (int) $headers['Content-Length'] : 0; $request->on('data', function ($data) use($request, $response, &$content, $contentLength) { // Read data (may be empty for GET request). $content .= $data; // Handle request after receive. if (strlen($content) >= $contentLength) { $syRequest = self::mapRequest($request, $content); try { $syResponse = $this->application->handle($syRequest); } catch (\Exception $exception) { // Internal server error. $response->writeHead(500); $response->end(); return; } self::mapResponse($response, $syResponse); if ($this->application instanceof TerminableInterface) { $this->application->terminate($syRequest, $syResponse); } } }); }
public function __invoke(Request $request, Response $response) { if (session_status() === PHP_SESSION_NONE) { session_start(); } if ($request->getMethod() !== SymfonyRequest::METHOD_GET) { $request->on('data', function ($postData) use($request, $response) { $this->doRequest($request, $response, $postData); }); } else { $this->doRequest($request, $response); } }
public function __invoke(Request $request, Response $response) { if (session_status() === PHP_SESSION_NONE) { session_start(); } if ($request->getMethod() === 'POST') { $request->on('data', function ($postData) use($request, $response) { parse_str($postData, $postDataArray); $this->doRequest($request, $response, $postDataArray); }); } else { $this->doRequest($request, $response); } }
/** * @param \React\Http\Request $request * @param \React\Http\Response $response */ public function processRequest($request, $response) { $request->on('data', function ($dataBuffer) use($request, $response) { $this->request = new Request(); $this->request->setContent($dataBuffer); $this->request->setReactRequest($request); $this->response = new Response(); $this->response->setReactResponse($response); $allow = $this->getServiceManager()->getAllowOverride(); $this->getServiceManager()->setAllowOverride(true); $this->getServiceManager()->setService('Request', $this->request); $this->getServiceManager()->setService('Response', $this->response); $this->getServiceManager()->setAllowOverride($allow); $event = $this->getMvcEvent(); $event->setError(null); $event->setRequest($this->getRequest()); $event->setResponse($this->getResponse()); $this->run(); }); }
private function initRouter() { $this->router = new RouteCollector(); $this->router->get('/cmd{id:\\d+}', function ($id) { $controller = new Controller($this); $content = $controller->actionIncrement($id); return $content; }); $this->router->get('/cmd', function () { $controller = new Controller($this); $this->responseContentType = 'application/json'; $content = $controller->actionSummary(); return $content; }); $this->router->post('/cmd{id:\\d+}', function ($id) { $this->request->on('data', function ($data) use($id) { parse_str($data, $data); $controller = new Controller($this); $content = $controller->actionUpdate($id, $data); return $content; }); }); }
/** * main push request/ websocket response loop */ function onRequest(Request $request, Response $response) { $content = ''; $headers = $request->getHeaders(); $contentLength = isset($headers['Content-Length']) ? (int) $headers['Content-Length'] : null; // length required, chunked encoding not supported if (null === $contentLength) { $response->writeHead(411); $response->end(); return; } $request->on('data', function ($data) use($request, $response, &$content, $contentLength) { // read data (may be empty for GET request) $content .= $data; // handle request after receive if (strlen($content) >= $contentLength) { $headers = array('Content-Type' => 'application/json'); try { $data = $this->hub->handlePushMessage($content); $headers['Content-Length'] = strlen($data); if (null === $data) { $response->writeHead(400, $headers); $response->end(); } else { $response->writeHead(200, $headers); $response->end($data); } } catch (\Exception $exception) { $data = $this->getExceptionAsJson($exception, true); $headers['Content-Length'] = strlen($data); $response->writeHead(500, $headers); // internal server error $response->end($data); } } }); }
/** * Constructor * * @param React\HttpClient\Client $client * @param Irto\OAuth2Proxy\Server $server * @param React\Http\Request $request original request * * @return Irto\OAuth2Proxy\ProxyRequest */ public function __construct(HttpClient $client, Server $server, Request $request) { $headers = $request->getHeaders(); $headers = array_combine(array_map('strtolower', array_keys($headers)), $headers); $headers = Arr::except($headers, $this->headersNotAllowed); $this->original = $request; $this->client = $client; $this->server = $server; $this->query = new Collection(); $headers = new Collection($headers); $headers->put('cookie', new Collection((new \Guzzle\Parser\Cookie\CookieParser())->parseCookie($headers->get('cookie')))); $this->headers = $headers; $request->on('data', array($this, 'write')); }
public function handle(\React\Http\Request $request, \React\Http\Response $response) { $this->post_params = []; $request->on('data', function ($body) use($request, $response) { $this->request_body = $body; parse_str($body, $this->post_params); $this->handleRequest($request, $response); }); }
private function onRequest(ReactRequest $request, ReactResponse $response) { $start = microtime(true); $request->on('data', function ($data) use($request, $response, $start) { $psrResponse = $this->requestHandler->execute($this->convertFromReactToPsrRequest($request, $data), new PsrResponse()); $this->logger->debug('Processing took ' . number_format((microtime(true) - $start) * 1000, 3) . ' milliseconds'); $response->writeHead($psrResponse->getStatusCode(), $psrResponse->getHeaders()); $response->end($psrResponse->getBody()->__toString()); }); }
/** * Resolve request */ public function resolve() { $this->request->on('data', [$this, 'receiveData']); $this->request->on('end', [$this, 'cleanUp']); }
public function handle(\React\Http\Request $request, \React\Http\Response $response) { $this->post_params = []; $request->on('data', function ($body) use($request, $response) { $isJson = function ($string) { $result = json_decode($string); return json_last_error() == JSON_ERROR_NONE && !empty($result); }; $this->request_body = $body; if ($isJson($body)) { $this->post_params = json_decode($body, true); } else { parse_str($body, $this->post_params); } $this->handleRequest($request, $response); }); }