/** * @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); }
/** * @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) { // Write command line $commandLine = sprintf("%s %s", $message->getHeader(HttpMessage::HEADER_HTTP_METHOD), $message->getHeader(HttpMessage::HEADER_HTTP_URI)); $messageHttpVersion = $message->getHeader(HttpMessage::HEADER_HTTP_VERSION); if (null !== $messageHttpVersion && static::DEFAULT_HTTP_VERSION !== $messageHttpVersion) { $commandLine .= " HTTP/" . $messageHttpVersion; } $this->writeHttpLine($outputStream, $commandLine); foreach ($message->getHeadersByNameFormat(static::HTTP_HEADER_NAME_PATTERN) as $name => $header) { $this->writeHttpLine($outputStream, sprintf("%s: %s", $name, $header)); } $body = $message->getBodyAsString(); if ('' !== $body) { // Write the empty line between headers and the body $this->writeHttpLine($outputStream, ''); $outputStream->writeString($body); } }
/** * @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"]); } }
/** * @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); }
/** * @return bool * * Return true if the ExchangeInterface::setException() is called, or if one of in/out MessageInterface::isFault() * is true. */ public function isFailed() : bool { return null !== $this->exception || $this->in->isFault() || $this->hasOut() && $this->out->isFault(); }
/** * @param MessageInterface $message * @return void * * Copy $message contents in $this. */ public function copyFromMessage(MessageInterface $message) { $this->clearAttachments(); $this->clearHeaders(); $this->exchange = $message->getExchange(); $this->setMessageId($message->getMessageId()); $this->setHeaders($message->getHeaders()); $this->setBody($message->getBody()); $this->setFault($message->isFault()); }