Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fixed Zend\Http\Client to not loose the HTTP authentication when foll…

…owing a redirect
  • Loading branch information...
commit 3fa82d157bba281cb90d863e9cf88dca461e55b7 1 parent e196bc3
@driehle authored
Showing with 48 additions and 3 deletions.
  1. +15 −3 library/Zend/Http/Client.php
  2. +33 −0 tests/ZendTest/Http/ClientTest.php
View
18 library/Zend/Http/Client.php
@@ -533,6 +533,14 @@ public function clearCookies()
}
/**
+ * Clear http authentication
+ */
+ public function clearAuth()
+ {
+ $this->auth = array();
+ }
+
+ /**
* Set the headers (for the request)
*
* @param Headers|array $headers
@@ -732,13 +740,13 @@ protected function calcAuthDigest($user, $password, $type = self::AUTH_BASIC, $d
* Reset all the HTTP parameters (auth,cookies,request, response, etc)
*
* @param bool $clearCookies Also clear all valid cookies? (defaults to false)
+ * @param bool $clearAuth Also clear http authentication? (defaults to true)
* @return Client
*/
- public function resetParameters($clearCookies = false)
+ public function resetParameters($clearCookies = false, $clearAuth = true)
{
$uri = $this->getUri();
- $this->auth = null;
$this->streamName = null;
$this->encType = null;
$this->request = null;
@@ -749,6 +757,10 @@ public function resetParameters($clearCookies = false)
if ($clearCookies) {
$this->clearCookies();
}
+
+ if ($clearAuth) {
+ $this->clearAuth();
+ }
return $this;
}
@@ -897,7 +909,7 @@ public function send(Request $request = null)
((! $this->config['strictredirects']) && ($response->getStatusCode() == 302 ||
$response->getStatusCode() == 301))) {
- $this->resetParameters();
+ $this->resetParameters(false, false);
$this->setMethod(Request::METHOD_GET);
}
View
33 tests/ZendTest/Http/ClientTest.php
@@ -235,4 +235,37 @@ public function testIfMaxredirectWorksCorrectly()
// be requested, due to the maxredirects = 1 limit
$this->assertEquals($response->getContent(), "Page #2");
}
+
+ public function testIfClientDoesNotLooseAuthenticationOnRedirect()
+ {
+ // set up user credentials
+ $user = 'username123';
+ $password = 'password456';
+ $encoded = Client::encodeAuthHeader($user, $password, Client::AUTH_BASIC);
+
+ // set up two responses that simulate a redirection
+ $testAdapter = new Test();
+ $testAdapter->setResponse(
+ "HTTP/1.1 303 See Other\r\n"
+ . "Location: http://www.example.org/part2\r\n\r\n"
+ . "The URL of this page has changed."
+ );
+ $testAdapter->addResponse(
+ "HTTP/1.1 200 OK\r\n\r\n"
+ . "Welcome to this Website."
+ );
+
+ // create client with HTTP basic authentication
+ $client = new Client('http://www.example.org/part1', array(
+ 'adapter' => $testAdapter,
+ 'maxredirects' => 1
+ ));
+ $client->setAuth($user, $password, Client::AUTH_BASIC);
+
+ // do request
+ $response = $client->setMethod('GET')->send();
+
+ // the last request should contain the Authorization header
+ $this->assertTrue(strpos($client->getLastRawRequest(), $encoded) !== false);
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.