Skip to content

Commit 60ce931

Browse files
committed
Dont add path to an url we already added a path to.
1 parent 00f4c37 commit 60ce931

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

spec/Plugin/AddPathPluginSpec.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ function it_adds_path(
4141

4242
$uri->withPath('/api/users')->shouldBeCalled()->willReturn($uri);
4343
$uri->getPath()->shouldBeCalled()->willReturn('/users');
44+
$uri->__toString()->shouldBeCalled()->willReturn('https://example.com/api/users');
4445

4546
$this->beConstructedWith($host);
4647
$this->handleRequest($request, function () {}, function () {});
@@ -61,6 +62,7 @@ function it_removes_ending_slashes(
6162

6263
$uri->withPath('/api/users')->shouldBeCalled()->willReturn($uri);
6364
$uri->getPath()->shouldBeCalled()->willReturn('/users');
65+
$uri->__toString()->shouldBeCalled()->willReturn('https://example.com/api/users');
6466

6567
$this->beConstructedWith($host);
6668
$this->handleRequest($request, function () {}, function () {});

spec/Plugin/BaseUriPluginSpec.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,21 @@ function it_adds_domain_and_path(
3636
UriInterface $host,
3737
UriInterface $uri
3838
) {
39-
$host->getScheme()->shouldBeCalled()->willReturn('http://');
39+
$host->getScheme()->shouldBeCalled()->willReturn('https://');
4040
$host->getHost()->shouldBeCalled()->willReturn('example.com');
4141
$host->getPort()->shouldBeCalled()->willReturn(8000);
4242
$host->getPath()->shouldBeCalled()->willReturn('/api');
4343

4444
$request->getUri()->shouldBeCalled()->willReturn($uri);
4545
$request->withUri($uri)->shouldBeCalled()->willReturn($request);
4646

47-
$uri->withScheme('http://')->shouldBeCalled()->willReturn($uri);
47+
$uri->withScheme('https://')->shouldBeCalled()->willReturn($uri);
4848
$uri->withHost('example.com')->shouldBeCalled()->willReturn($uri);
4949
$uri->withPort(8000)->shouldBeCalled()->willReturn($uri);
5050
$uri->withPath('/api/users')->shouldBeCalled()->willReturn($uri);
5151
$uri->getHost()->shouldBeCalled()->willReturn('');
5252
$uri->getPath()->shouldBeCalled()->willReturn('/users');
53+
$uri->__toString()->shouldBeCalled()->willReturn('https://example.com/api/users');
5354

5455
$this->beConstructedWith($host);
5556
$this->handleRequest($request, function () {}, function () {});
@@ -60,15 +61,15 @@ function it_adds_domain(
6061
UriInterface $host,
6162
UriInterface $uri
6263
) {
63-
$host->getScheme()->shouldBeCalled()->willReturn('http://');
64+
$host->getScheme()->shouldBeCalled()->willReturn('https://');
6465
$host->getHost()->shouldBeCalled()->willReturn('example.com');
6566
$host->getPort()->shouldBeCalled()->willReturn(8000);
6667
$host->getPath()->shouldBeCalled()->willReturn('/');
6768

6869
$request->getUri()->shouldBeCalled()->willReturn($uri);
6970
$request->withUri($uri)->shouldBeCalled()->willReturn($request);
7071

71-
$uri->withScheme('http://')->shouldBeCalled()->willReturn($uri);
72+
$uri->withScheme('https://')->shouldBeCalled()->willReturn($uri);
7273
$uri->withHost('example.com')->shouldBeCalled()->willReturn($uri);
7374
$uri->withPort(8000)->shouldBeCalled()->willReturn($uri);
7475
$uri->getHost()->shouldBeCalled()->willReturn('');
@@ -82,19 +83,20 @@ function it_replaces_domain_and_adds_path(
8283
UriInterface $host,
8384
UriInterface $uri
8485
) {
85-
$host->getScheme()->shouldBeCalled()->willReturn('http://');
86+
$host->getScheme()->shouldBeCalled()->willReturn('https://');
8687
$host->getHost()->shouldBeCalled()->willReturn('example.com');
8788
$host->getPort()->shouldBeCalled()->willReturn(8000);
8889
$host->getPath()->shouldBeCalled()->willReturn('/api');
8990

9091
$request->getUri()->shouldBeCalled()->willReturn($uri);
9192
$request->withUri($uri)->shouldBeCalled()->willReturn($request);
9293

93-
$uri->withScheme('http://')->shouldBeCalled()->willReturn($uri);
94+
$uri->withScheme('https://')->shouldBeCalled()->willReturn($uri);
9495
$uri->withHost('example.com')->shouldBeCalled()->willReturn($uri);
9596
$uri->withPort(8000)->shouldBeCalled()->willReturn($uri);
9697
$uri->withPath('/api/users')->shouldBeCalled()->willReturn($uri);
9798
$uri->getPath()->shouldBeCalled()->willReturn('/users');
99+
$uri->__toString()->shouldBeCalled()->willReturn('https://example.com/api/users');
98100

99101
$this->beConstructedWith($host, ['replace' => true]);
100102
$this->handleRequest($request, function () {}, function () {});

src/Plugin/AddPathPlugin.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,17 @@ public function __construct(UriInterface $uri)
4646
*/
4747
public function handleRequest(RequestInterface $request, callable $next, callable $first)
4848
{
49-
$identifier = spl_object_hash((object) $first);
49+
// Use the request URI as an identifier
50+
$identifier = sha1((string) (string) $request->getUri());
5051

52+
// If the current URL is a result of a previous rewrite, then do nothing.
5153
if (!array_key_exists($identifier, $this->alteredRequests)) {
52-
$request = $request->withUri($request->getUri()
53-
->withPath($this->uri->getPath().$request->getUri()->getPath())
54-
);
55-
$this->alteredRequests[$identifier] = $identifier;
54+
$prefixedPath = $this->uri->getPath().$request->getUri()->getPath();
55+
$prefixedUri = $request->getUri()->withPath($prefixedPath);
56+
$request = $request->withUri($prefixedUri);
57+
58+
// Store for future checks
59+
$this->alteredRequests[sha1((string) $prefixedUri)] = true;
5660
}
5761

5862
return $next($request);

0 commit comments

Comments
 (0)