/** * 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 Body */ public static function fromMessage(ReadableStream $stream, HttpMessage $message) : Body { $close = false; if ($message instanceof HttpResponse && \in_array('close', $message->getHeaderTokenValues('Connection'))) { $close = true; } $body = new static($stream, $close); 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); } return $body; }