From f6ea609f46d40c53c6f14a0aac2b4575f473f340 Mon Sep 17 00:00:00 2001 From: Andreas Schamberger Date: Tue, 3 Jan 2012 16:38:50 +0100 Subject: [PATCH] fix parser and add attachment support --- src/BeSimple/SoapCommon/Mime/Parser.php | 7 ++- src/BeSimple/SoapCommon/SoapKernel.php | 47 +++++++++++++++++++ src/BeSimple/SoapCommon/SoapMessage.php | 29 ++++++++++++ .../Tests/SoapCommon/Mime/ParserTest.php | 2 +- 4 files changed, 80 insertions(+), 5 deletions(-) diff --git a/src/BeSimple/SoapCommon/Mime/Parser.php b/src/BeSimple/SoapCommon/Mime/Parser.php index 92713d8..5ca618f 100644 --- a/src/BeSimple/SoapCommon/Mime/Parser.php +++ b/src/BeSimple/SoapCommon/Mime/Parser.php @@ -49,7 +49,7 @@ class Parser } $content = ''; $currentPart = $multipart; - $lines = preg_split("/\r\n|\n/", $mimeMessage); + $lines = preg_split("/(\r\n)/", $mimeMessage); foreach ($lines as $line) { // ignore http status code and POST * if (substr($line, 0, 5) == 'HTTP/' || substr($line, 0, 4) == 'POST') { @@ -74,7 +74,7 @@ class Parser unset($currentHeader); } if ($inHeader) { - if ($line == '') { + if (trim($line) == '') { $inHeader = false; continue; } @@ -111,7 +111,7 @@ class Parser } } else { if ($hitFirstBoundary === false) { - if ($line != '') { + if (trim($line) != '') { $inHeader = true; $currentHeader = $line; continue; @@ -120,7 +120,6 @@ class Parser $content .= $line . "\r\n"; } } - } return $multipart; } diff --git a/src/BeSimple/SoapCommon/SoapKernel.php b/src/BeSimple/SoapCommon/SoapKernel.php index fc349c8..2315b69 100644 --- a/src/BeSimple/SoapCommon/SoapKernel.php +++ b/src/BeSimple/SoapCommon/SoapKernel.php @@ -13,6 +13,8 @@ namespace BeSimple\SoapCommon; +use BeSimple\SoapCommon\Mime\Part as MimePart; + use BeSimple\SoapCommon\SoapRequest; use BeSimple\SoapCommon\SoapResponse; @@ -28,6 +30,13 @@ use BeSimple\SoapCommon\SoapResponseFilter; */ class SoapKernel { + /** + * Mime attachments. + * + * @var array(\BeSimple\SoapCommon\Mime\Part) + */ + protected $attachments = array(); + /** * Request filters. * @@ -42,6 +51,39 @@ class SoapKernel */ private $responseFilters = array(); + /** + * Add attachment. + * + * @param \BeSimple\SoapCommon\Mime\Part $attachment New attachment + * + * @return void + */ + public function addAttachment(MimePart $attachment) + { + $contentId = trim($part->getHeader('Content-ID'), '<>'); + + $this->attachments[$contentId] = $attachment; + } + + /** + * Get attachment and remove from array. + * + * @param string $contentId Content ID of attachment + * + * @return \BeSimple\SoapCommon\Mime\Part|null + */ + public function getAttachment($contentId) + { + if (isset($this->attachments[$contentId])) { + $part = $this->attachments[$contentId]; + unset($this->attachments[$contentId]); + + return $part; + } + + return null; + } + /** * Registers the given object either as filter for SoapRequests or as filter for SoapResponses * or as filter for both depending on the implemented interfaces. Inner filters have to be registered @@ -70,6 +112,9 @@ class SoapKernel */ public function filterRequest(SoapRequest $request) { + $request->setAttachments($this->attachments); + $this->attachments = array(); + foreach ($this->requestFilters as $filter) { $filter->filterRequest($request); } @@ -85,5 +130,7 @@ class SoapKernel foreach ($this->responseFilters as $filter) { $filter->filterResponse($response); } + + $this->attachments = $response->getAttachments(); } } \ No newline at end of file diff --git a/src/BeSimple/SoapCommon/SoapMessage.php b/src/BeSimple/SoapCommon/SoapMessage.php index 4bceb21..97e0a04 100644 --- a/src/BeSimple/SoapCommon/SoapMessage.php +++ b/src/BeSimple/SoapCommon/SoapMessage.php @@ -13,6 +13,8 @@ namespace BeSimple\SoapCommon; +use BeSimple\SoapCommon\Mime\Part as MimePart; + /** * Base class for SoapRequest and SoapResponse. * @@ -52,6 +54,13 @@ abstract class SoapMessage */ protected $action; + /** + * Mime attachments. + * + * @var array(\BeSimple\SoapCommon\Mime\Part) + */ + protected $attachments = array(); + /** * Message content (MIME Message or SOAP Envelope). * @@ -124,6 +133,26 @@ abstract class SoapMessage $this->action = $action; } + /** + * Get attachments. + * + * @return array(\BeSimple\SoapCommon\Mime\Part) + */ + public function getAttachments() + { + return $this->attachments; + } + + /** + * Set SOAP action. + * + * @param array(\BeSimple\SoapCommon\Mime\Part) $attachments Attachment array + */ + public function setAttachments(array $attachments) + { + $this->attachments = $attachments; + } + /** * Get message content (MIME Message or SOAP Envelope). * diff --git a/tests/BeSimple/Tests/SoapCommon/Mime/ParserTest.php b/tests/BeSimple/Tests/SoapCommon/Mime/ParserTest.php index 8efa1f9..1128325 100644 --- a/tests/BeSimple/Tests/SoapCommon/Mime/ParserTest.php +++ b/tests/BeSimple/Tests/SoapCommon/Mime/ParserTest.php @@ -52,7 +52,7 @@ class ParserTest extends \PHPUnit_Framework_TestCase $p2 = $mp->getPart('0x9d6ad00-0xa19ef48-0x9de7500-0xa4fae78-0xa382698'); $this->assertEquals('binary', $p2->getHeader('Content-Transfer-Encoding')); $this->assertEquals('application/binary', $p2->getHeader('Content-Type')); - $this->assertEquals(81, strlen($p2->getContent())); + $this->assertEquals(79, strlen($p2->getContent())); } public function testParserResponseAxis()