Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions src/HttpMultipart/HttpFoundation/MultipartResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,45 @@ class MultipartResponse extends ResourceResponse {
/**
* @var Response[]
*/
protected $parts;
protected $parts = [];

/**
* Constructor.
*/
public function __construct(array $parts = NULL, $status = 200, $headers = [], $subtype = NULL) {
parent::__construct(NULL, $status, $headers);

if ($parts) {
$this->setParts($parts);
}

$this->subtype = $subtype ?: 'mixed';
$this->boundary = md5(microtime());
}

if (NULL !== $parts) {
$this->setParts($parts);
/**
* {@inheritdoc}
*/
public function prepare(Request $request) {
$this->headers->set('Content-Type', sprintf('multipart/%s; boundary="%s"', $this->subtype, $this->boundary));
$this->headers->set('Transfer-Encoding', 'chunked');

// Prepare the response content from the parts.
$parts = $this->getParts();

if ($parts) {
$content = '';
foreach ($this->getParts() as $part) {

foreach ($parts as $part) {
$content .= "--{$this->boundary}\r\n";
$content .= "Content-Type: {$part->headers->get('Content-Type')}\r\n\r\n";
$content .= \Drupal::service('serializer')
->serialize($part->getResponseData(), 'json');
$content .= $part->getContent();
$content .= "\r\n";
}

$content .= "--{$this->boundary}--";
$this->setContent($content);
}
}

/**
* {@inheritdoc}
*/
public function prepare(Request $request) {
$this->headers->set('Content-Type', "multipart/{$this->subtype}; boundary=\"{$this->boundary}\"");
$this->headers->set('Transfer-Encoding', 'chunked');

return parent::prepare($request);
}
Expand Down
18 changes: 2 additions & 16 deletions src/HttpMultipart/ResourceMultipartResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,10 @@ public function prepare(Request $request) {
public function sendContent() {
// This fixes the "Malformed encoding found in chunked-encoding"
// error message in curl and makes possible to get the correct response body.
$size = $this->getSize();
$size = strlen($this->getContent());
echo "$size\r\n";
parent::sendContent();
}

/**
* Returns the length of all the parts in the response body.
*
* @return int
*/
protected function getSize() {
$size = 0;
foreach ($this->parts as $part) {
$content = $part->getContent();
$output = "--{$this->boundary}" . "{$part->headers}" . $content;
$size += strlen($output);
}
return $size;
parent::sendContent();
}

}