Exemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function send(HttpConnectorContext $context, HttpRequest $request) : Awaitable
 {
     if (!$context instanceof ConnectorContext) {
         throw new \InvalidArgumentException('Invalid connector context passed');
     }
     return new Coroutine(function () use($context, $request) {
         $uri = $request->getUri();
         $key = \sprintf('%s://%s', $uri->getScheme(), $uri->getHostWithPort(true));
         if ($context->conn) {
             $conn = $context->conn;
         } else {
             $conn = new Connection($context->socket, new HPack($this->hpackContext));
             if ($this->logger) {
                 $conn->setLogger($this->logger);
             }
             (yield $conn->performClientHandshake());
             $this->connections[$key] = $conn;
         }
         $request = $request->withProtocolVersion('2.0');
         $request = $request->withHeader('Date', \gmdate(Http::DATE_RFC1123));
         $sent = 0;
         $response = (yield $conn->openStream()->sendRequest($request, $sent));
         if ($this->logger) {
             $this->logger->info('{ip} "{method} {target} HTTP/{protocol}" {status} {size}', ['ip' => $request->getClientAddress(), 'method' => $request->getMethod(), 'target' => $request->getRequestTarget(), 'protocol' => $response->getProtocolVersion(), 'status' => $response->getStatusCode(), 'size' => $sent ?: '-']);
         }
         if (isset($this->connecting[$key])) {
             $context->connected = true;
             $context->conn = $conn;
             $context->dispose();
         }
         return $response;
     });
 }