Improvements for #79

This commit is contained in:
djmaze 2021-04-09 09:28:46 +02:00
parent 5f153aeb8d
commit 4ee905c568
2 changed files with 8 additions and 5 deletions

View file

@ -51,7 +51,7 @@ class Client
$this->HTTP->setAuth(3, $settings['userName'] ?? '', $settings['password'] ?? ''); $this->HTTP->setAuth(3, $settings['userName'] ?? '', $settings['password'] ?? '');
$this->HTTP->max_response_kb = 0; $this->HTTP->max_response_kb = 0;
$this->HTTP->timeout = 15; // timeout in seconds. $this->HTTP->timeout = 15; // timeout in seconds.
$this->HTTP->follow_location = false; $this->HTTP->max_redirects = 1;
} }
/** /**

View file

@ -15,7 +15,7 @@ abstract class Request
$timeout = 5, // timeout in seconds. $timeout = 5, // timeout in seconds.
$max_response_kb = 1024, $max_response_kb = 1024,
$user_agent, $user_agent,
$follow_location = true, $max_redirects = 0,
$verify_peer = false, $verify_peer = false,
$proxy = null; $proxy = null;
@ -110,9 +110,12 @@ abstract class Request
$method = \strtoupper($method); $method = \strtoupper($method);
$url = $request_url; $url = $request_url;
$etime = \time() + $this->timeout; $etime = \time() + $this->timeout;
if (\is_array($body)) { $body = \http_build_query($body, '', '&'); } $redirects = \max(0, $this->max_redirects);
if (\is_array($body)) {
$body = \http_build_query($body, '', '&');
}
if ($body && 'GET' === $method) { if ($body && 'GET' === $method) {
$url .= (\strpos($url, '?')?'&':'?').$body; $url .= (\strpos($url, '?') ? '&' : '?') . $body;
$body = null; $body = null;
} }
do do
@ -130,7 +133,7 @@ abstract class Request
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3 // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3
// In response to a request other than GET or HEAD, the user agent MUST NOT // In response to a request other than GET or HEAD, the user agent MUST NOT
// automatically redirect the request unless it can be confirmed by the user // automatically redirect the request unless it can be confirmed by the user
if ($this->follow_location && \is_null($body) && \in_array($result->status, array(301, 302, 303, 307))) { if ($redirects-- && \in_array($result->status, array(301, 302, 303, 307)) && \in_array($method, ['GET','HEAD','PROPFIND'])) {
$url = $result->getRedirectLocation(); $url = $result->getRedirectLocation();
} else { } else {
$result->final_uri = $url; $result->final_uri = $url;