/** * @param Out $outputStream The stream where the formatted message has to be written * @param MessageInterface $message * * @throws \Avalonia\Component\Message\Exception\MapperInvalidArgumentException if the given $message is not of the good type * @throws \Avalonia\Component\Message\Exception\MapperInvalidDataException if the given $rawData doesn't not contain required headers/attachments/body * * Writes a formatted message into the output stream */ public function mapMessageToStream(Out $outputStream, MessageInterface $message) { $outputMessage = ''; $httpVersion = $message->getHeader(HttpMessage::HEADER_HTTP_VERSION, static::DEFAULT_HTTP_VERSION); if (static::DEFAULT_HTTP_VERSION !== $httpVersion) { // If http version is 1.x write status line and headers $outputMessage .= sprintf('HTTP/%s %s %s%s', $httpVersion, $message->getHeader(HttpMessage::HEADER_HTTP_STATUS_CODE), $message->getHeader(HttpMessage::HEADER_HTTP_STATUS_MESSAGE), static::CRLF); foreach ($message->getHeadersByNameFormat(static::HTTP_HEADER_NAME_PATTERN) as $name => $header) { $outputMessage .= sprintf("%s: %s%s", $name, $header, static::CRLF); } // Add empty line to end headers section and write body $outputMessage .= static::CRLF; } $outputMessage .= $message->getBodyAsString(); $outputStream->writeString($outputMessage); }
/** * @param Out $outputStream The stream where the formatted message has to be written * @param MessageInterface $message * * @throws \Avalonia\Component\Message\Exception\MapperInvalidArgumentException if the given $message is not of the good type * @throws \Avalonia\Component\Message\Exception\MapperInvalidDataException if the given $rawData doesn't not contain required headers/attachments/body * * Writes a formatted message into the output stream */ public function mapMessageToStream(Out $outputStream, MessageInterface $message) { $outputMessage = ''; // First, build the query string $queryHeadersPrefix = str_replace(".", "\\.", HttpMessage::HTTP_QUERY_HEADER_PREFIX); $queryHeadersArray = []; foreach ($message->getHeadersByNameFormat(sprintf("/^%s/", $queryHeadersPrefix)) as $name => $value) { // remove query. for the header name $queryHeadersArray[substr($name, strlen(HttpMessage::HTTP_QUERY_HEADER_PREFIX))] = $value; } $queryString = http_build_query($queryHeadersArray); // Then build the uri $uri = $message->getHeader(HttpMessage::HEADER_HTTP_URI); if ('' !== $queryString) { $uri .= "?" . $queryString; } // Write command line $commandLine = sprintf("%s %s", $message->getHeader(HttpMessage::HEADER_HTTP_METHOD), $uri); $httpVersion = $message->getHeader(HttpMessage::HEADER_HTTP_VERSION, static::DEFAULT_HTTP_VERSION); if (static::DEFAULT_HTTP_VERSION !== $httpVersion) { $commandLine .= " HTTP/" . $httpVersion; } $outputMessage .= $commandLine . static::CRLF; foreach ($message->getHeadersByNameFormat(static::HTTP_HEADER_NAME_PATTERN) as $name => $header) { $outputMessage .= sprintf("%s: %s%s", $name, $header, static::CRLF); } // Write an empty line to end headers section if the HTTP version is 1.x // This empty line is not necessary for http 0.9 $httpVersion = $message->getHeader(HttpMessage::HEADER_HTTP_VERSION); if ("0.9" !== $httpVersion && null !== $httpVersion) { $outputMessage .= static::CRLF; } $outputMessage .= $message->getBodyAsString(); $outputStream->writeString($outputMessage); }