Rewrite location header

This commit is contained in:
Marcel Pociot
2020-10-25 23:40:45 +01:00
parent 538c7da446
commit 880259657f
5 changed files with 56 additions and 6 deletions

View File

@@ -36,4 +36,16 @@ class Configuration
{
return intval($this->port);
}
public function getUrl(string $subdomain): string
{
$httpProtocol = $this->port() === 443 ? 'https' : 'http';
$host = $this->host();
if ($httpProtocol !== 'https') {
$host .= ":{$this->port()}";
}
return "{$subdomain}.{$host}";
}
}

View File

@@ -2,6 +2,7 @@
namespace App\Client\Http;
use App\Client\Configuration;
use App\Client\Http\Modifiers\CheckBasicAuthentication;
use App\Logger\RequestLogger;
use Clue\React\Buzz\Browser;
@@ -26,19 +27,26 @@ class HttpClient
/** @var Request */
protected $request;
protected $connectionData;
/** @var array */
protected $modifiers = [
CheckBasicAuthentication::class,
];
/** @var Configuration */
protected $configuration;
public function __construct(LoopInterface $loop, RequestLogger $logger)
public function __construct(LoopInterface $loop, RequestLogger $logger, Configuration $configuration)
{
$this->loop = $loop;
$this->logger = $logger;
$this->configuration = $configuration;
}
public function performRequest(string $requestData, WebSocket $proxyConnection = null, string $requestId = null)
public function performRequest(string $requestData, WebSocket $proxyConnection = null, $connectionData = null)
{
$this->connectionData = $connectionData;
$this->request = $this->parseRequest($requestData);
$this->logger->logRequest($requestData, $this->request);
@@ -84,7 +92,9 @@ class HttpClient
])
->send($request)
->then(function (ResponseInterface $response) use ($proxyConnection) {
if (! isset($response->buffer)) {
if (!isset($response->buffer)) {
$response = $this->rewriteResponseHeaders($response);
$response->buffer = str($response);
}
@@ -126,4 +136,25 @@ class HttpClient
{
return Request::fromString($data);
}
protected function rewriteResponseHeaders(ResponseInterface $response)
{
if (!$response->hasHeader('Location')) {
return $response;
}
$location = $response->getHeaderLine('Location');
if (!strstr($location, $this->connectionData->host)) {
return $response;
}
$location = str_replace(
$this->connectionData->host,
$this->configuration->getUrl($this->connectionData->subdomain),
$location
);
return $response->withHeader('Location', $location);
}
}

View File

@@ -32,7 +32,7 @@ class ProxyManager
], $this->loop)
->then(function (WebSocket $proxyConnection) use ($clientId, $connectionData) {
$proxyConnection->on('message', function ($message) use ($proxyConnection, $connectionData) {
$this->performRequest($proxyConnection, $connectionData->request_id, (string) $message);
$this->performRequest($proxyConnection, (string) $message, $connectionData);
});
$proxyConnection->send(json_encode([
@@ -76,8 +76,8 @@ class ProxyManager
});
}
protected function performRequest(WebSocket $proxyConnection, $requestId, string $requestData)
protected function performRequest(WebSocket $proxyConnection, string $requestData, $connectionData)
{
app(HttpClient::class)->performRequest((string) $requestData, $proxyConnection, $requestId);
app(HttpClient::class)->performRequest((string) $requestData, $proxyConnection, $connectionData);
}
}