Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit b9ee4fc9f1abd1639bc20a5536f87ad7a5318dea 1 parent d9a3ab8
Mark Story markstory authored
2  lib/Cake/Network/Http/HttpSocket.php
View
@@ -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);
}
42 lib/Cake/Test/Case/Network/Http/HttpSocketTest.php
View
@@ -763,6 +763,38 @@ public function testRequestCustomResponse() {
}
/**
+ * 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
*
* @return void
@@ -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',
Please sign in to comment.
Something went wrong with that request. Please try again.