Пример #1
0
 /**
  * {@inheritdoc}
  */
 public function prepareMessage(HttpMessage $message) : HttpMessage
 {
     if (!$message->hasHeader('Content-Type')) {
         $message = $message->withHeader('Content-Type', Filesystem::guessMimeTypeFromFilename($this->file));
     }
     return $message;
 }
Пример #2
0
 /**
  * Assemble HTTP body settings from the given HTTP message.
  * 
  * Exceptions thrown by this method use codes that can be sent as HTTP response status codes.
  * 
  * @param ReadableStream $stream
  * @param HttpMessage $message
  * @return Http1Body
  */
 public static function fromMessage(ReadableStream $stream, HttpMessage $message) : Http1Body
 {
     $body = new static($stream);
     if ($message->hasHeader('Transfer-Encoding')) {
         $encodings = \strtolower($message->getHeaderLine('Transfer-Encoding'));
         $encodings = \array_map('trim', \explode(',', $encodings));
         if (\in_array('chunked', $encodings)) {
             $body->setChunkEncoded(true);
         } elseif (!empty($encodings)) {
             throw new StatusException(Http::NOT_IMPLEMENTED, 'Unsupported transfer encoding detected');
         }
     } elseif ($message->hasHeader('Content-Length')) {
         $len = $message->getHeaderLine('Content-Length');
         if (!\preg_match("'^[0-9]+\$'", $len)) {
             throw new StatusException(Http::BAD_REQUEST, \sprintf('Invalid content length value specified: "%s"', $len));
         }
         $body->setLength((int) $len);
     }
     if ($message->hasHeader('Content-Encoding')) {
         if (!$message instanceof HttpResponse) {
             throw new StatusException(Http::BAD_REQUEST, 'Compressed request bodies are not supported');
         }
         $body->setCompression($message->getHeaderLine('Content-Encoding'));
     }
     return $body;
 }