Beispiel #1
0
 /**
  * Have the upgrade handler take control of the given socket connection.
  * 
  * This method will send an HTTP/1 upgrade response before the handler takes over.
  */
 protected function upgradeResultConnection(UpgradeResultHandler $handler, SocketStream $socket, HttpRequest $request, HttpResponse $response) : \Generator
 {
     (yield $request->getBody()->discard());
     (yield $response->getBody()->discard());
     $response = $this->normalizeResponse($request, $response);
     $reason = \trim($response->getReasonPhrase());
     if ($reason === '') {
         $reason = \trim(Http::getReason($response->getStatusCode()));
     }
     $buffer = \sprintf("HTTP/%s %u%s\r\n", $response->getProtocolVersion(), $response->getStatusCode(), \rtrim(' ' . $reason));
     if ($response->getStatusCode() === Http::SWITCHING_PROTOCOLS) {
         $buffer .= "Connection: upgrade\r\n";
     }
     foreach ($response->getHeaders() as $name => $header) {
         $name = Http::normalizeHeaderName($name);
         foreach ($header as $value) {
             $buffer .= $name . ': ' . $value . "\r\n";
         }
     }
     (yield $socket->write($buffer . "\r\n"));
     (yield $socket->flush());
     if ($this->logger) {
         $this->logger->info('{ip} "{method} {target} HTTP/{protocol}" {status} {size}', ['ip' => $request->getClientAddress(), 'method' => $request->getMethod(), 'target' => $request->getRequestTarget(), 'protocol' => $request->getProtocolVersion(), 'status' => $response->getStatusCode(), 'size' => '-']);
     }
     return yield from $handler->upgradeConnection($socket, $request, $response);
 }
Beispiel #2
0
 /**
  * Check if the given response payload should be compressed.
  * 
  * @param HttpRequest $request
  * @param HttpResponse $response
  * @return bool
  */
 protected function isCompressable(HttpRequest $request, HttpResponse $response) : bool
 {
     if ($request->getMethod() === Http::HEAD) {
         return false;
     }
     if ($response->getBody() instanceof DeferredBody || Http::isResponseWithoutBody($response->getStatusCode())) {
         return false;
     }
     if ($response->hasHeader('Content-Encoding') || !$response->hasHeader('Content-Type')) {
         return false;
     }
     try {
         $media = $response->getContentType()->getMediaType();
     } catch (InvalidMediaTypeException $e) {
         return false;
     }
     if (isset($this->types[(string) $media])) {
         return true;
     }
     foreach ($media->getSubTypes() as $sub) {
         if (isset($this->subTypes[$sub])) {
             return true;
         }
     }
     return false;
 }