Browse Source

don't follow redirects on appengine URLs

see https://cloud.google.com/appengine/docs/php/urlfetch/
pull/39/head
Aaron Parecki 8 years ago
parent
commit
7b955b53f2
1 changed files with 22 additions and 18 deletions
  1. +22
    -18
      lib/HTTP.php

+ 22
- 18
lib/HTTP.php View File

@ -8,11 +8,7 @@ class HTTP {
public function get($url) { public function get($url) {
$ch = curl_init($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); $response = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
return array( return array(
@ -27,13 +23,7 @@ class HTTP {
public function post($url, $body, $headers=array()) { public function post($url, $body, $headers=array()) {
$ch = curl_init($url); $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); $response = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
return array( return array(
@ -48,12 +38,7 @@ class HTTP {
public function head($url) { public function head($url) {
$ch = curl_init($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); $response = curl_exec($ch);
return array( return array(
'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE), '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) { public static function error_string_from_code($code) {
switch($code) { switch($code) {
case 0: case 0:

Loading…
Cancel
Save