@@ -282,7 +282,7 @@ public function getLastRawResponse()
282282 /**
283283 * Get the redirections count
284284 *
285- * @return integer
285+ * @return int
286286 */
287287 public function getRedirectionsCount ()
288288 {
@@ -298,8 +298,18 @@ public function getRedirectionsCount()
298298 public function setUri ($ uri )
299299 {
300300 if (!empty ($ uri )) {
301+ // remember host of last request
302+ $ lastHost = $ this ->getRequest ()->getUri ()->getHost ();
301303 $ this ->getRequest ()->setUri ($ uri );
302304
305+ // if host changed, the HTTP authentication should be cleared for security
306+ // reasons, see #4215 for a discussion - currently authentication is also
307+ // cleared for peer subdomains due to technical limits
308+ $ nextHost = $ this ->getRequest ()->getUri ()->getHost ();
309+ if (!preg_match ('/ ' . preg_quote ($ lastHost , '/ ' ) . '$/i ' , $ nextHost )) {
310+ $ this ->clearAuth ();
311+ }
312+
303313 // Set auth if username and password has been specified in the uri
304314 if ($ this ->getUri ()->getUser () && $ this ->getUri ()->getPassword ()) {
305315 $ this ->setAuth ($ this ->getUri ()->getUser (), $ this ->getUri ()->getPassword ());
@@ -444,6 +454,37 @@ public function setParameterGet(array $query)
444454 return $ this ;
445455 }
446456
457+ /**
458+ * Reset all the HTTP parameters (request, response, etc)
459+ *
460+ * @param bool $clearCookies Also clear all valid cookies? (defaults to false)
461+ * @param bool $clearAuth Also clear http authentication? (defaults to true)
462+ * @return Client
463+ */
464+ public function resetParameters ($ clearCookies = false , $ clearAuth = true )
465+ {
466+ $ uri = $ this ->getUri ();
467+
468+ $ this ->streamName = null ;
469+ $ this ->encType = null ;
470+ $ this ->request = null ;
471+ $ this ->response = null ;
472+ $ this ->lastRawRequest = null ;
473+ $ this ->lastRawResponse = null ;
474+
475+ $ this ->setUri ($ uri );
476+
477+ if ($ clearCookies ) {
478+ $ this ->clearCookies ();
479+ }
480+
481+ if ($ clearAuth ) {
482+ $ this ->clearAuth ();
483+ }
484+
485+ return $ this ;
486+ }
487+
447488 /**
448489 * Return the current cookies
449490 *
@@ -673,6 +714,14 @@ public function setAuth($user, $password, $type = self::AUTH_BASIC)
673714 return $ this ;
674715 }
675716
717+ /**
718+ * Clear http authentication
719+ */
720+ public function clearAuth ()
721+ {
722+ $ this ->auth = array ();
723+ }
724+
676725 /**
677726 * Calculate the response value according to the HTTP authentication type
678727 *
@@ -728,31 +777,6 @@ protected function calcAuthDigest($user, $password, $type = self::AUTH_BASIC, $d
728777 return $ response ;
729778 }
730779
731- /**
732- * Reset all the HTTP parameters (auth,cookies,request, response, etc)
733- *
734- * @param bool $clearCookies Also clear all valid cookies? (defaults to false)
735- * @return Client
736- */
737- public function resetParameters ($ clearCookies = false )
738- {
739- $ uri = $ this ->getUri ();
740-
741- $ this ->auth = null ;
742- $ this ->streamName = null ;
743- $ this ->encType = null ;
744- $ this ->request = null ;
745- $ this ->response = null ;
746-
747- $ this ->setUri ($ uri );
748-
749- if ($ clearCookies ) {
750- $ this ->clearCookies ();
751- }
752-
753- return $ this ;
754- }
755-
756780 /**
757781 * Dispatch
758782 *
@@ -897,13 +921,15 @@ public function send(Request $request = null)
897921 ((! $ this ->config ['strictredirects ' ]) && ($ response ->getStatusCode () == 302 ||
898922 $ response ->getStatusCode () == 301 ))) {
899923
900- $ this ->resetParameters ();
924+ $ this ->resetParameters (false , false );
901925 $ this ->setMethod (Request::METHOD_GET );
902926 }
903927
928+
904929 // If we got a well formed absolute URI
905930 if (($ scheme = substr ($ location , 0 , 6 )) &&
906931 ($ scheme == 'http:/ ' || $ scheme == 'https: ' )) {
932+ // setURI() clears parameters if host changed, see #4215
907933 $ this ->setUri ($ location );
908934 } else {
909935
@@ -933,12 +959,26 @@ public function send(Request $request = null)
933959 break ;
934960 }
935961
936- } while ($ this ->redirectCounter < $ this ->config ['maxredirects ' ]);
962+ } while ($ this ->redirectCounter <= $ this ->config ['maxredirects ' ]);
937963
938964 $ this ->response = $ response ;
939965 return $ response ;
940966 }
941967
968+ /**
969+ * Fully reset the HTTP client (auth, cookies, request, response, etc.)
970+ *
971+ * @return Client
972+ */
973+ public function reset ()
974+ {
975+ $ this ->resetParameters ();
976+ $ this ->clearAuth ();
977+ $ this ->clearCookies ();
978+
979+ return $ this ;
980+ }
981+
942982 /**
943983 * Set a file to upload (using a POST request)
944984 *
@@ -1004,7 +1044,7 @@ public function removeFileUpload($filename)
10041044 *
10051045 * @param string $domain
10061046 * @param string $path
1007- * @param boolean $secure
1047+ * @param bool $secure
10081048 * @return Header\Cookie|bool
10091049 */
10101050 protected function prepareCookies ($ domain , $ path , $ secure )
0 commit comments