/** * @param In $inputStream * @param MessageInterface $message * * @throws HttpMapperInvalidHeaderException * @throws MapperInvalidDataException * @throws MapperMaxLineLengthException * * Parse http headers and body from the stream, and map it to $message */ protected function readHttpHeaderAndBody(In $inputStream, MessageInterface $message) { while (!$inputStream->eof()) { try { $line = $this->readHttpLine($inputStream, false, $offset); } catch (MapperInvalidDataEofFoundException $e) { // If this exception is caught, it means that the message doesn't have a body. // So parse the remaining data in the buffer $line = $e->getRemainingBufferData(); } if ('' === $line) { // We reached the body $message->setBody($inputStream->readAll()); // stop the loop as the mapping is finished break; } elseif (0 === preg_match(static::HTTP_HEADER_PATTERN, $line, $parsedHeader)) { throw new HttpMapperInvalidHeaderException(sprintf("The given HTTP header line is not valid: \"%s\". Expected format: HEADER-NAME: VALUE.", $line)); } $message->setHeader($parsedHeader["header"], $parsedHeader["content"]); } }