From 7b955b53f2b8e3ec0c740b2a7e99bf45520d25c8 Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Sat, 27 Feb 2016 13:09:58 -0800 Subject: [PATCH] don't follow redirects on appengine URLs see https://cloud.google.com/appengine/docs/php/urlfetch/ --- lib/HTTP.php | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/lib/HTTP.php b/lib/HTTP.php index 526b866..fd39929 100644 --- a/lib/HTTP.php +++ b/lib/HTTP.php @@ -8,11 +8,7 @@ class HTTP { public function get($url) { $ch = curl_init($url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_HEADER, true); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); - curl_setopt($ch, CURLOPT_MAXREDIRS, $this->max_redirects); - curl_setopt($ch, CURLOPT_TIMEOUT_MS, round($this->timeout * 1000)); + $this->_set_curlopts($ch, $url); $response = curl_exec($ch); $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); return array( @@ -27,13 +23,7 @@ class HTTP { public function post($url, $body, $headers=array()) { $ch = curl_init($url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_POST, true); - curl_setopt($ch, CURLOPT_POSTFIELDS, $body); - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); - curl_setopt($ch, CURLOPT_HEADER, true); - curl_setopt($ch, CURLOPT_TIMEOUT_MS, round($this->timeout * 1000)); + $this->_set_curlopts($ch, $url); $response = curl_exec($ch); $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); return array( @@ -48,12 +38,7 @@ class HTTP { public function head($url) { $ch = curl_init($url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_HEADER, true); - curl_setopt($ch, CURLOPT_NOBODY, true); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); - curl_setopt($ch, CURLOPT_MAXREDIRS, $this->max_redirects); - curl_setopt($ch, CURLOPT_TIMEOUT_MS, round($this->timeout * 1000)); + $this->_set_curlopts($ch, $url); $response = curl_exec($ch); return array( 'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE), @@ -64,6 +49,25 @@ class HTTP { ); } + 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 { + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($ch, CURLOPT_MAXREDIRS, $this->max_redirects); + } + + curl_setopt($ch, CURLOPT_MAXREDIRS, $this->max_redirects); + curl_setopt($ch, CURLOPT_TIMEOUT_MS, round($this->timeout * 1000)); + } + public static function error_string_from_code($code) { switch($code) { case 0: