Skip to content

Commit f8f96ef

Browse files
authored
Merge pull request jenssegers#53 from jmortlock/fix-location-rewrite
Use the first location header object as the original redirect uri
2 parents 8ded271 + e40a6bd commit f8f96ef

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ php:
99

1010
sudo: false
1111

12+
dist: trusty
13+
1214
before_script:
1315
- composer self-update
1416
- travis_retry composer install --no-interaction --prefer-source

src/Filter/RewriteLocationFilter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public function __invoke(RequestInterface $request, ResponseInterface $response,
1717
$response = $next($request, $response);
1818

1919
if ($response->hasHeader(self::LOCATION)) {
20-
$original = parse_url($response->getHeader(self::LOCATION));
20+
$location = $response->getHeader(self::LOCATION)[0];
21+
$original = parse_url($location);
2122

2223
$target = rtrim(str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']), '/');
2324

@@ -30,10 +31,9 @@ public function __invoke(RequestInterface $request, ResponseInterface $response,
3031
}
3132

3233
$response = $response
33-
->withHeader('X-Proxy-Location', $response->getHeader(self::LOCATION))
34+
->withHeader('X-Proxy-Location', $location)
3435
->withHeader(self::LOCATION, $target);
3536
}
36-
3737
return $response;
3838
}
3939
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php namespace Proxy\Filter;
2+
3+
use Zend\Diactoros\Request;
4+
use Zend\Diactoros\Response;
5+
6+
class RewriteLocationFilterTest extends \PHPUnit_Framework_TestCase
7+
{
8+
/**
9+
* @var RewriteLocationFilter
10+
*/
11+
private $filter;
12+
13+
public function setUp()
14+
{
15+
$this->filter = new RewriteLocationFilter();
16+
}
17+
18+
/**
19+
* @test
20+
*/
21+
public function filter_rewrites_location()
22+
{
23+
$_SERVER['SCRIPT_NAME'] = "";
24+
$redirect_url = 'http://www.example.com/path?arg1=123&arg2=456';
25+
$request = new Request;
26+
$response = new Response('php://memory', 200, [RewriteLocationFilter::LOCATION => $redirect_url]);
27+
$next = function () use ($response) { return $response; };
28+
29+
$response = call_user_func($this->filter, $request, $response, $next);
30+
31+
$this->assertTrue($response->hasHeader('X-Proxy-Location'));
32+
$this->assertTrue($response->hasHeader(RewriteLocationFilter::LOCATION));
33+
$this->assertEquals('/path?arg1=123&arg2=456', $response->getHeaderLine(RewriteLocationFilter::LOCATION));
34+
}
35+
36+
}

0 commit comments

Comments
 (0)