Skip to content

Commit

Permalink
feat: pass only a specified headers to proxy request
Browse files Browse the repository at this point in the history
  • Loading branch information
fakeheal committed Jun 15, 2023
1 parent 63a816f commit 8981ed8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
23 changes: 20 additions & 3 deletions src/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Proxy
*/
public function __construct(
private readonly array $allowedHosts,
private readonly array $allowedHeaders = ['Content-Type', 'Accept'],
private ?Request $request = null,
private ?Response $response = null,
private ?Client $client = null
Expand Down Expand Up @@ -66,9 +67,7 @@ private function redirect(): Proxy
array_merge([
// response to proxy through 404, 500, etc
'http_errors' => false,
// "redirect" all headers
'headers' => $this->request->headers->all(),
], $this->buildParameters())
], $this->buildParameters(), $this->buildHeaders())
);

// pass response headers from proxy request to our response
Expand Down Expand Up @@ -126,4 +125,22 @@ private function buildParameters(): array
'form_params' => $this->request->getPayload()->all()
];
}

/**
* Builds headers from incoming request, filtering out everything, but $allowedHeaders.
* @return array
*/
private function buildHeaders(): array
{
$lowercaseAllowedHeaders = array_map(fn(string $value) => strtolower($value), $this->allowedHeaders);

$headers = array_filter(
$this->request->headers->all(),
fn(string $key) => in_array($key, $lowercaseAllowedHeaders),
ARRAY_FILTER_USE_KEY
);

return compact('headers');
}

}
12 changes: 6 additions & 6 deletions tests/Unit/ProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
it('throws exception if disallowed host is passed', function () {
$request = new Request(['url' => 'https://google.com']);

(new Proxy(['https://definitely-not-google.com'], $request))
(new Proxy(['https://definitely-not-google.com'], ['Content-Type', 'Accept'], $request))
->handle();
})->throws(HostNotAllowedException::class);

Expand All @@ -25,7 +25,7 @@
$request = new Request(['url' => 'https://google.com']);
$response = new HttpFoundationResponse();

(new Proxy(['google.com'], $request, $response, $client))
(new Proxy(['google.com'], ['X-Foo'], $request, $response, $client))
->handle();

expect($response->headers->get('X-Foo'))->toBe('Bar');
Expand All @@ -47,7 +47,7 @@
$request = new Request(['url' => 'https://google.com'], [], [], [], [], ['HTTP_X-Foo' => 'Bar']);
$response = new HttpFoundationResponse();

(new Proxy(['google.com'], $request, $response, $client))
(new Proxy(['google.com'], ['X-Foo'], $request, $response, $client))
->handle();

expect(count($container))->toBe(1)
Expand All @@ -71,7 +71,7 @@
$request = new Request(['url' => 'https://google.com', 'param1' => 'value1', 'param2' => 'value2']);
$response = new HttpFoundationResponse();

(new Proxy(['google.com'], $request, $response, $client))
(new Proxy(['google.com'], [], $request, $response, $client))
->handle();


Expand All @@ -97,7 +97,7 @@

$response = new HttpFoundationResponse();

(new Proxy(['google.com'], $request, $response, $client))
(new Proxy(['google.com'], [], $request, $response, $client))
->handle();


Expand All @@ -123,7 +123,7 @@

$response = new HttpFoundationResponse();

(new Proxy(['google.com'], $request, $response, $client))
(new Proxy(['google.com'], [], $request, $response, $client))
->handle();


Expand Down

0 comments on commit 8981ed8

Please sign in to comment.