Skip to content
Permalink
Browse files

URL decode redirect urls.

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 b9ee4fc9f1abd1639bc20a5536f87ad7a5318dea
Showing with 43 additions and 1 deletion.
  1. +1 −1 lib/Cake/Network/Http/HttpSocket.php
  2. +42 −0 lib/Cake/Test/Case/Network/Http/HttpSocketTest.php
@@ -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);
}
@@ -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
*
@@ -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',
@@ -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',

0 comments on commit b9ee4fc

Please sign in to comment.
You can’t perform that action at this time.