Skip to content

Commit

Permalink
Get rid off (un)serialize Response to allow arbitrary binary data tra…
Browse files Browse the repository at this point in the history
…nsfer

fix #1
  • Loading branch information
solcloud committed Dec 3, 2021
1 parent 1f0a284 commit 4b0f8fa
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/AbstractProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function __construct(int $numberOfInternalProxyAfterThisProxy, ?IRequestD
}

/**
* Process Request and print Response
* Process Request and print Response, please dont print anything after or we will add exit() :)
* @param Request $request if NULL Request will be parsed from POST data using requestPostKey
*/
public function process(Request $request = null): void
Expand Down Expand Up @@ -158,7 +158,7 @@ protected function forward(string $url, array $additionalData = [])

$response = $this->curlRequestFactory->fetchResponse($this->createCommunicationRequest($url, $postFields));

$this->setResponseFromSerializedString($response->getBody());
$this->setResponseFromSerialized($response);
}

/**
Expand Down Expand Up @@ -194,20 +194,34 @@ private function createCommunicationRequest(string $url, array $postFields): Req
*/
protected function printResponse()
{
echo serialize($this->getResponse());
$body = $this->getResponse()->getBody();
$this->getResponse()->setBody('');
header('x-solcloud-proxy: ' . base64_encode(serialize($this->getResponse())));
echo $body;
$this->getResponse()->setBody($body);
}

/**
* Try to set Response by unserializing $serializedResponseString
* Try to set "target" Response by inspecting $internalResponse
* @throws ProxyException or subclass - if unserializition failed or Response has exception
*/
protected function setResponseFromSerializedString(string $serializedResponseString): void
protected function setResponseFromSerialized(Response $internalResponse): void
{
$response = @unserialize($serializedResponseString);
$data = $internalResponse->getLastHeadersFormatted()['x-solcloud-proxy'] ?? false;
if ($data) {
$data = base64_decode($data, true);
}
if ($data === false) {
throw new ProxyException('Decoding of Response failed');
}

$response = @unserialize($data);
if ($response === false) {
throw new ProxyException('Unserialization of Response failed');
}

/** @var Response $response */
$response->setBody($internalResponse->getBody());
$this->setResponse($response);
}

Expand Down

0 comments on commit 4b0f8fa

Please sign in to comment.