diff --git a/lib/HTTP.php b/lib/HTTP.php index a2b7a7f..0d66897 100644 --- a/lib/HTTP.php +++ b/lib/HTTP.php @@ -31,7 +31,7 @@ class HTTP { } private function _class($url) { - if(preg_match('/brid\.gy|appspot\.com/', $url)) { + if(should_follow_redirects($url)) { return 'p3k\HTTPStream'; } else { return 'p3k\HTTPCurl'; diff --git a/lib/HTTPCurl.php b/lib/HTTPCurl.php index 000ab2c..6c0ed29 100644 --- a/lib/HTTPCurl.php +++ b/lib/HTTPCurl.php @@ -54,18 +54,16 @@ class HTTPCurl { } private function _set_curlopts($ch, $url) { - $host = parse_url($url, PHP_URL_HOST); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, true); // Special-case appspot.com URLs to not follow redirects. // https://cloud.google.com/appengine/docs/php/urlfetch/ - if(substr($host, -12) == '.appspot.com') { - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); - } else { + if(should_follow_redirects($url)) { curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_MAXREDIRS, $this->max_redirects); + } else { + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); } curl_setopt($ch, CURLOPT_TIMEOUT_MS, round($this->timeout * 1000)); diff --git a/lib/HTTPStream.php b/lib/HTTPStream.php index 95bc3e2..cba227b 100644 --- a/lib/HTTPStream.php +++ b/lib/HTTPStream.php @@ -72,8 +72,6 @@ class HTTPStream { } private function _stream_context($method, $url, $body=false, $headers=[]) { - $host = parse_url($url, PHP_URL_HOST); - $options = [ 'method' => $method, 'timeout' => $this->timeout, @@ -90,11 +88,11 @@ class HTTPStream { // Special-case appspot.com URLs to not follow redirects. // https://cloud.google.com/appengine/docs/php/urlfetch/ - if(substr($host, -12) == '.appspot.com') { - $options['follow_location'] = 0; - } else { + if(should_follow_redirects($url)) { $options['follow_location'] = 1; $options['max_redirects'] = $this->max_redirects; + } else { + $options['follow_location'] = 0; } return stream_context_create(['http' => $options]); diff --git a/lib/helpers.php b/lib/helpers.php index 2982154..00e55d5 100644 --- a/lib/helpers.php +++ b/lib/helpers.php @@ -9,3 +9,12 @@ function view($template, $data=[]) { function normalize_url($url) { return parse_url($url, PHP_URL_PATH) == '' ? $url.'/' : $url; } + +function should_follow_redirects($url) { + $host = parse_url($url, PHP_URL_HOST); + if(preg_match('/brid\.gy|appspot\.com|blogspot\.com|youtube\.com/', $host)) { + return false; + } else { + return true; + } +} \ No newline at end of file