summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Gass2017-10-12 17:50:28 +0200
committerNathan Gass2017-10-12 17:52:21 +0200
commit34d75f6328a8596ae5ec86e861ef13eb2582e2e4 (patch)
tree649944b5784025cde4a0368291c173fac1484cfd
parent846a5a273275a4b7c9df066fcc1317418e283ec8 (diff)
downloaditools-34d75f6328a8596ae5ec86e861ef13eb2582e2e4.tar.gz
itools-34d75f6328a8596ae5ec86e861ef13eb2582e2e4.tar.bz2
itools-34d75f6328a8596ae5ec86e861ef13eb2582e2e4.zip
use CURLOPT_FOLLOWLOCATION in it_url::get
-rw-r--r--it_url.class27
-rwxr-xr-xtests/it_url.t8
2 files changed, 12 insertions, 23 deletions
diff --git a/it_url.class b/it_url.class
index e7c50c5..53bf949 100644
--- a/it_url.class
+++ b/it_url.class
@@ -127,16 +127,7 @@ function get($p=null, $timeout=5)
else # called statically
$url = new it_url($p['url']);
- $result = $url->request($p);
-
- # Handle redirects (supports relative and global) but not for HTTP 201 Created because that can send the Location of the new resource
- if ($url->headers['Location'] && $url->result != 201 && preg_match('#^(https?://[^/]*)?(/)?(.*)$#i', $url->headers['Location'], $parts) && ($parts[1] != $url->url))
- {
- unset($p['url'], $p['headers']['Host']);
- $url->it_url($parts[1] ? $parts[1].$parts[2].$parts[3] : $url->protocol.'://'.$url->realhostname.($parts[2] ? $parts[2].$parts[3] : '/'.dirname($url->path).'/'.$parts[3]));
- if (++$url->redir <= 4) /* Avoid infinite redirects */
- return $url->get($p);
- }
+ $result = $url->request($p + ['followlocation' => true]);
if (!$result && $p['retries'] > 0 && $url->result < 400)
{
@@ -179,7 +170,7 @@ static function _default_headers($url, $p)
static function curl_opts($p=array())
{
- $p += array('totaltimeout' => "999999", 'timeout' => 5);
+ $p += array('totaltimeout' => "999999", 'timeout' => 5, 'followlocation' => true);
$add = [];
foreach ($p['headers'] as $header => $value)
@@ -213,7 +204,7 @@ static function curl_opts($p=array())
CURLOPT_TIMEOUT => $p['totaltimeout'],
CURLOPT_LOW_SPEED_LIMIT => 5,
CURLOPT_LOW_SPEED_TIME => $p['timeout'],
- CURLOPT_FOLLOWLOCATION => false,
+ CURLOPT_FOLLOWLOCATION => $p['followlocation'],
CURLOPT_HTTPHEADER => $headers,
CURLOPT_CUSTOMREQUEST => $p['method'] ?: null,
CURLOPT_NOBODY => $p['method'] == 'HEAD',
@@ -243,8 +234,7 @@ function request($p=array())
$url->headers = array();
$p['headers'] = (array)$p['headers'] + self::_default_headers($url, $p);
- $opts = array(CURLOPT_FOLLOWLOCATION => false, CURLOPT_HEADER => 1) + self::curl_opts($p + array('user' => $this->user, 'pass' => $this->pass));
-
+ $opts = array(CURLOPT_HEADER => 1) + self::curl_opts($p + array('user' => $this->user, 'pass' => $this->pass, 'followlocation' => false));
$curl = curl_init($url->url);
curl_setopt_array($curl, $opts);
@@ -253,10 +243,9 @@ function request($p=array())
EDC('curlinfo', curl_getinfo($curl));
if ($got) {
- list($url->header, $url->data) = explode("\r\n\r\n", $got, 2);
- while (preg_match('#^(HTTP\S+)\s100\sContinue$#', $url->header))
- list($url->header, $url->data) = explode("\r\n\r\n", $url->data, 2);
- $url->header .= "\r\n\r\n";
+ $headersize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
+ $url->header = array_slice(explode("\r\n\r\n", trim(substr($got, 0, $headersize))), -1)[0] . "\r\n\r\n";
+ $url->data = substr($got, $headersize);
$url->parse_http_header($url->header);
@@ -298,7 +287,7 @@ function get_multi($p=null)
$url = new it_url;
$p['headers'] = (array)$p['headers'] + array_diff_key(self::_default_headers($url, $p), ['Host' => null]);
- $opts = array(CURLOPT_FOLLOWLOCATION => true) + self::curl_opts($p);
+ $opts = self::curl_opts($p);
$mh = curl_multi_init();
diff --git a/tests/it_url.t b/tests/it_url.t
index 9a4594e..eb36dd6 100755
--- a/tests/it_url.t
+++ b/tests/it_url.t
@@ -209,7 +209,7 @@ handle_server(
is(
it_url::get(['url' => 'http://localhost:8000/relative_redirect', 'it_error' => false]),
"Testserver output after relative redirect",
- 'TODO: it_url::get() follows relative redirect correctly'
+ 'it_url::get() follows relative redirect correctly'
)
);
@@ -217,7 +217,7 @@ handle_server(
is(
it_url::get('http://localhost:8000/nohost_redirect'),
"Testserver output after nohost redirect",
- 'TODO: it_url::get() follows redirect without host correctly'
+ 'it_url::get() follows redirect without host correctly'
)
);
@@ -231,13 +231,13 @@ handle_server(
$output = handle_server(
ok(
- !it_url::get(U('http://localhost:8000/redirect_loop', array('num' => 10))),
+ !it_url::get(array('url' => U('http://localhost:8000/redirect_loop', array('num' => 40)), 'it_error' => false)),
'it_url::get() handles redirect loop'
)
);
$last_num = it::match('num=(\d+)', end($output));
if (!ok(
- $last_num == 5,
+ $last_num == 20,
'it_url::get() aborts redirect loop after 5 redirects'
))
diag($output);