Mime Parser: throws Exception with MimePartMessage contents

This commit is contained in:
Petr Bechyně 2017-06-11 22:36:20 +02:00
parent dcd5ff5234
commit f74e4b08ce
2 changed files with 45 additions and 7 deletions

View File

@ -0,0 +1,33 @@
<?php
namespace BeSimple\SoapCommon\Mime;
use Exception;
class CouldNotParseMimeMessageException extends Exception
{
private $mimePartMessage;
private $headers;
public function __construct($message, $mimePartMessage, array $headers)
{
$this->mimePartMessage = $mimePartMessage;
$this->headers = $headers;
parent::__construct($message);
}
public function getMimePartMessage()
{
return $this->mimePartMessage;
}
public function hasHeaders()
{
return count($this->headers) > 0;
}
public function getHeaders()
{
return $this->headers;
}
}

View File

@ -16,7 +16,6 @@ use BeSimple\SoapCommon\Mime\Boundary\MimeBoundaryAnalyser;
use BeSimple\SoapCommon\Mime\Parser\ContentTypeParser; use BeSimple\SoapCommon\Mime\Parser\ContentTypeParser;
use BeSimple\SoapCommon\Mime\Parser\ParsedPartList; use BeSimple\SoapCommon\Mime\Parser\ParsedPartList;
use BeSimple\SoapCommon\Mime\Parser\ParsedPartsGetter; use BeSimple\SoapCommon\Mime\Parser\ParsedPartsGetter;
use Exception;
/** /**
* Simple Multipart-Mime parser. * Simple Multipart-Mime parser.
@ -48,12 +47,14 @@ class Parser
} }
if (MimeBoundaryAnalyser::hasMessageBoundary($mimeMessageLines) === true) { if (MimeBoundaryAnalyser::hasMessageBoundary($mimeMessageLines) === true) {
if ($mimeMessageLineCount <= 1) { if ($mimeMessageLineCount <= 1) {
throw new Exception( throw new CouldNotParseMimeMessageException(
sprintf( sprintf(
'Cannot parse MultiPart message of %d characters: got unexpectable low number of lines: %s', 'Cannot parse MultiPart message of %d characters: got unexpectable low number of lines: %s',
mb_strlen($mimeMessage), mb_strlen($mimeMessage),
(string)$mimeMessageLineCount (string)$mimeMessageLineCount
) ),
$mimeMessage,
$headers
); );
} }
$parsedPartList = ParsedPartsGetter::getPartsFromMimeMessageLines( $parsedPartList = ParsedPartsGetter::getPartsFromMimeMessageLines(
@ -62,18 +63,22 @@ class Parser
$hasHttpRequestHeaders $hasHttpRequestHeaders
); );
if ($parsedPartList->hasParts() === false) { if ($parsedPartList->hasParts() === false) {
throw new Exception( throw new CouldNotParseMimeMessageException(
'Could not parse MimeMessage: no Parts for MultiPart given' 'Could not parse MimeMessage: no Parts for MultiPart given',
$mimeMessage,
$headers
); );
} }
if ($parsedPartList->hasExactlyOneMainPart() === false) { if ($parsedPartList->hasExactlyOneMainPart() === false) {
throw new Exception( throw new CouldNotParseMimeMessageException(
sprintf( sprintf(
'Could not parse MimeMessage %s HTTP headers: unexpected count of main ParsedParts: %s (total: %d)', 'Could not parse MimeMessage %s HTTP headers: unexpected count of main ParsedParts: %s (total: %d)',
$hasHttpRequestHeaders ? 'with' : 'w/o', $hasHttpRequestHeaders ? 'with' : 'w/o',
implode(', ', $parsedPartList->getPartContentIds()), implode(', ', $parsedPartList->getPartContentIds()),
$parsedPartList->getMainPartCount() $parsedPartList->getMainPartCount()
) ),
$mimeMessage,
$headers
); );
} }
self::appendPartsToMultiPart( self::appendPartsToMultiPart(