Skip to content

Commit

Permalink
URL decode redirect urls.
Browse files Browse the repository at this point in the history
Some servers send url encoded Location headers. Decode location headers
before processing a redirect.

Fixes #3310
  • Loading branch information
markstory committed Oct 30, 2012
1 parent d9a3ab8 commit b9ee4fc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Cake/Network/Http/HttpSocket.php
Expand Up @@ -403,7 +403,7 @@ public function request($request = array()) {
}

if ($this->request['redirect'] && $this->response->isRedirect()) {
$request['uri'] = $this->response->getHeader('Location');
$request['uri'] = trim(urldecode($this->response->getHeader('Location')), '=');
$request['redirect'] = is_int($this->request['redirect']) ? $this->request['redirect'] - 1 : $this->request['redirect'];
$this->response = $this->request($request);
}
Expand Down
42 changes: 42 additions & 0 deletions lib/Cake/Test/Case/Network/Http/HttpSocketTest.php
Expand Up @@ -762,6 +762,38 @@ public function testRequestCustomResponse() {
$this->assertEquals('HTTP/1.x 2', $response->first10);
}

/**
* Test that redirect urls are urldecoded
*
* @return void
*/
public function testRequestWithRedirectUrlEncoded() {
$request = array(
'uri' => 'http://localhost/oneuri',
'redirect' => 1
);
$serverResponse1 = "HTTP/1.x 302 Found\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\nLocation: http://i.cmpnet.com%2Ftechonline%2Fpdf%2Fa.pdf=\r\n\r\n";
$serverResponse2 = "HTTP/1.x 200 OK\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\n\r\n<h1>You have been redirected</h1>";

$this->Socket->expects($this->at(1))
->method('read')
->will($this->returnValue($serverResponse1));

$this->Socket->expects($this->at(3))
->method('write')
->with($this->logicalAnd(
$this->stringContains('Host: i.cmpnet.com'),
$this->stringContains('GET /techonline/pdf/a.pdf')
));

$this->Socket->expects($this->at(4))
->method('read')
->will($this->returnValue($serverResponse2));

$response = $this->Socket->request($request);
$this->assertEquals('<h1>You have been redirected</h1>', $response->body());
}

/**
* testRequestWithRedirect method
*
Expand All @@ -781,6 +813,11 @@ public function testRequestWithRedirectAsTrue() {
$this->assertEquals('<h1>You have been redirected</h1>', $response->body());
}

/**
* Test that redirects with a count limit are decremented.
*
* @return void
*/
public function testRequestWithRedirectAsInt() {
$request = array(
'uri' => 'http://localhost/oneuri',
Expand All @@ -795,6 +832,11 @@ public function testRequestWithRedirectAsInt() {
$this->assertEquals(1, $this->Socket->request['redirect']);
}

/**
* Test that redirects after the redirect count reaches 9 are not followed.
*
* @return void
*/
public function testRequestWithRedirectAsIntReachingZero() {
$request = array(
'uri' => 'http://localhost/oneuri',
Expand Down

0 comments on commit b9ee4fc

Please sign in to comment.