| 1 |
<?php |
| 2 |
/* |
| 3 |
* This file is part of the ManageWP Worker plugin. |
| 4 |
* |
| 5 |
* (c) ManageWP LLC <contact@managewp.com> |
| 6 |
* |
| 7 |
* For the full copyright and license information, please view the LICENSE |
| 8 |
* file that was distributed with this source code. |
| 9 |
*/ |
| 10 |
|
| 11 |
/** |
| 12 |
* Constructs a Multipart response into string or stream. |
| 13 |
*/ |
| 14 |
class MWP_Http_MultipartResponse extends MWP_Http_Response implements MWP_Http_StreamingResponseInterface |
| 15 |
{ |
| 16 |
|
| 17 |
/** @var string Multipart boundary */ |
| 18 |
private $boundary = ''; |
| 19 |
|
| 20 |
public function __construct($parts, $boundary = null, $statusCode = 200, array $headers = array()) |
| 21 |
{ |
| 22 |
if ($boundary === null) { |
| 23 |
$boundary = uniqid(); |
| 24 |
} |
| 25 |
|
| 26 |
$this->boundary = $boundary; |
| 27 |
$headers["content-type"] = "multipart/mixed; boundary=".$this->boundary; |
| 28 |
|
| 29 |
if (!isset($headers["content-transfer-encoding"])) { |
| 30 |
$headers["content-transfer-encoding"] = "binary"; |
| 31 |
} |
| 32 |
|
| 33 |
parent::__construct($parts, $statusCode, $headers); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* {@inheritdoc} |
| 38 |
*/ |
| 39 |
public function getContentAsString() |
| 40 |
{ |
| 41 |
/** @var MWP_Http_MultipartResponsePart[] $parts */ |
| 42 |
$parts = $this->content; |
| 43 |
|
| 44 |
// Multipart header |
| 45 |
$output = "\r\n".$this->getMultipartBoundary()."\r\n"; |
| 46 |
|
| 47 |
foreach ($parts as $part) { |
| 48 |
$output .= $this->createPartResponse($part).$this->getMultipartBoundary(); |
| 49 |
} |
| 50 |
|
| 51 |
// End multipart boundary |
| 52 |
$output .= '--'; |
| 53 |
|
| 54 |
return $output; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* @return MWP_Stream_Interface |
| 59 |
*/ |
| 60 |
public function createResponseStream() |
| 61 |
{ |
| 62 |
/** @var MWP_Http_MultipartResponsePart[] $parts */ |
| 63 |
$parts = $this->content; |
| 64 |
$stream = new MWP_Stream_Append(); |
| 65 |
|
| 66 |
$stream->addStream(MWP_Stream_Stream::factory("\r\n".$this->getMultipartBoundary())); |
| 67 |
|
| 68 |
foreach ($parts as $part) { |
| 69 |
$stream->addStream(MWP_Stream_Stream::factory("\r\n")); |
| 70 |
$stream->addStream($this->createPartStream($part)); |
| 71 |
$stream->addStream(MWP_Stream_Stream::factory("\r\n".$this->getMultipartBoundary())); |
| 72 |
} |
| 73 |
|
| 74 |
$stream->addStream(MWP_Stream_Stream::factory("--")); |
| 75 |
|
| 76 |
return $stream; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* @return string |
| 81 |
*/ |
| 82 |
public function getBoundary() |
| 83 |
{ |
| 84 |
return $this->boundary; |
| 85 |
} |
| 86 |
|
| 87 |
private function createPartResponse(MWP_Http_MultipartResponsePart $part) |
| 88 |
{ |
| 89 |
$response = ''; |
| 90 |
|
| 91 |
foreach ($part->getHeaders() as $header => $value) { |
| 92 |
if (strcasecmp("content-location", $header) === 0) { |
| 93 |
// Content-Location can contain special characters (like \r\n for example) |
| 94 |
$value = urlencode($value); |
| 95 |
} |
| 96 |
$response .= sprintf("%s: %s\r\n", strtolower($header), $value); |
| 97 |
} |
| 98 |
|
| 99 |
$response .= sprintf("\r\n%s\r\n", (string) $part->getBody()); |
| 100 |
|
| 101 |
return $response; |
| 102 |
} |
| 103 |
|
| 104 |
private function createPartStream(MWP_Http_MultipartResponsePart $part) |
| 105 |
{ |
| 106 |
$stream = new MWP_Stream_Append(); |
| 107 |
|
| 108 |
foreach ($part->getHeaders() as $header => $value) { |
| 109 |
if (strcasecmp("content-location", $header) === 0) { |
| 110 |
// Content-Location can contain special characters (like \r\n for example) |
| 111 |
$value = urlencode($value); |
| 112 |
} |
| 113 |
$stream->addStream(MWP_Stream_Stream::factory(sprintf("%s: %s\r\n", strtolower($header), $value))); |
| 114 |
} |
| 115 |
|
| 116 |
$body = $part->getBody(); |
| 117 |
|
| 118 |
// Manually output content-transfer-encoding header |
| 119 |
$stream->addStream(MWP_Stream_Stream::factory(sprintf("content-transfer-encoding: %s\r\n", $part->getEncoding()))); |
| 120 |
|
| 121 |
switch ($part->getEncoding()) { |
| 122 |
case 'binary': |
| 123 |
// No action required |
| 124 |
break; |
| 125 |
case 'base64': |
| 126 |
$body = new MWP_Stream_Base64EncodedStream($body); |
| 127 |
break; |
| 128 |
default: |
| 129 |
throw new MWP_Worker_Exception(MWP_Worker_Exception::GENERAL_ERROR, 'Encoding %s not supported.'); |
| 130 |
} |
| 131 |
|
| 132 |
$stream->addStream(MWP_Stream_Stream::factory("\r\n")); |
| 133 |
$stream->addStream($body); |
| 134 |
|
| 135 |
return $stream; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* @return string |
| 140 |
*/ |
| 141 |
private function getMultipartBoundary() |
| 142 |
{ |
| 143 |
return sprintf("--%s", $this->boundary); |
| 144 |
} |
| 145 |
} |
| 146 |
|