@@ -16,7 +16,7 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | |
| 18 | 18 | if (!class_exists('Google_Client')) { |
| 19 | - require_once dirname(__FILE__) . '/../autoload.php'; |
|
| 19 | + require_once dirname(__FILE__).'/../autoload.php'; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | class Google_Auth_Exception extends Google_Exception |
@@ -28,13 +28,13 @@ discard block |
||
| 28 | 28 | class Google_Auth_ComputeEngine extends Google_Auth_Abstract |
| 29 | 29 | { |
| 30 | 30 | const METADATA_AUTH_URL = |
| 31 | - 'http://metadata/computeMetadata/v1/instance/service-accounts/default/token'; |
|
| 31 | + 'http://metadata/computeMetadata/v1/instance/service-accounts/default/token'; |
|
| 32 | 32 | private $client; |
| 33 | 33 | private $token; |
| 34 | 34 | |
| 35 | 35 | public function __construct(Google_Client $client, $config = null) |
| 36 | 36 | { |
| 37 | - $this->client = $client; |
|
| 37 | + $this->client = $client; |
|
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | /** |
@@ -49,8 +49,8 @@ discard block |
||
| 49 | 49 | */ |
| 50 | 50 | public function authenticatedRequest(Google_Http_Request $request) |
| 51 | 51 | { |
| 52 | - $request = $this->sign($request); |
|
| 53 | - return $this->client->getIo()->makeRequest($request); |
|
| 52 | + $request = $this->sign($request); |
|
| 53 | + return $this->client->getIo()->makeRequest($request); |
|
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | /** |
@@ -59,20 +59,20 @@ discard block |
||
| 59 | 59 | */ |
| 60 | 60 | public function setAccessToken($token) |
| 61 | 61 | { |
| 62 | - $token = json_decode($token, true); |
|
| 63 | - if ($token == null) { |
|
| 64 | - throw new Google_Auth_Exception('Could not json decode the token'); |
|
| 65 | - } |
|
| 66 | - if (! isset($token['access_token'])) { |
|
| 67 | - throw new Google_Auth_Exception("Invalid token format"); |
|
| 68 | - } |
|
| 69 | - $token['created'] = time(); |
|
| 70 | - $this->token = $token; |
|
| 62 | + $token = json_decode($token, true); |
|
| 63 | + if ($token == null) { |
|
| 64 | + throw new Google_Auth_Exception('Could not json decode the token'); |
|
| 65 | + } |
|
| 66 | + if (! isset($token['access_token'])) { |
|
| 67 | + throw new Google_Auth_Exception("Invalid token format"); |
|
| 68 | + } |
|
| 69 | + $token['created'] = time(); |
|
| 70 | + $this->token = $token; |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | public function getAccessToken() |
| 74 | 74 | { |
| 75 | - return json_encode($this->token); |
|
| 75 | + return json_encode($this->token); |
|
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | /** |
@@ -81,29 +81,29 @@ discard block |
||
| 81 | 81 | */ |
| 82 | 82 | public function acquireAccessToken() |
| 83 | 83 | { |
| 84 | - $request = new Google_Http_Request( |
|
| 85 | - self::METADATA_AUTH_URL, |
|
| 86 | - 'GET', |
|
| 87 | - array( |
|
| 88 | - 'Metadata-Flavor' => 'Google' |
|
| 89 | - ) |
|
| 90 | - ); |
|
| 91 | - $request->disableGzip(); |
|
| 92 | - $response = $this->client->getIo()->makeRequest($request); |
|
| 84 | + $request = new Google_Http_Request( |
|
| 85 | + self::METADATA_AUTH_URL, |
|
| 86 | + 'GET', |
|
| 87 | + array( |
|
| 88 | + 'Metadata-Flavor' => 'Google' |
|
| 89 | + ) |
|
| 90 | + ); |
|
| 91 | + $request->disableGzip(); |
|
| 92 | + $response = $this->client->getIo()->makeRequest($request); |
|
| 93 | 93 | |
| 94 | - if ($response->getResponseHttpCode() == 200) { |
|
| 95 | - $this->setAccessToken($response->getResponseBody()); |
|
| 96 | - $this->token['created'] = time(); |
|
| 97 | - return $this->getAccessToken(); |
|
| 98 | - } else { |
|
| 99 | - throw new Google_Auth_Exception( |
|
| 100 | - sprintf( |
|
| 101 | - "Error fetching service account access token, message: '%s'", |
|
| 102 | - $response->getResponseBody() |
|
| 103 | - ), |
|
| 104 | - $response->getResponseHttpCode() |
|
| 105 | - ); |
|
| 106 | - } |
|
| 94 | + if ($response->getResponseHttpCode() == 200) { |
|
| 95 | + $this->setAccessToken($response->getResponseBody()); |
|
| 96 | + $this->token['created'] = time(); |
|
| 97 | + return $this->getAccessToken(); |
|
| 98 | + } else { |
|
| 99 | + throw new Google_Auth_Exception( |
|
| 100 | + sprintf( |
|
| 101 | + "Error fetching service account access token, message: '%s'", |
|
| 102 | + $response->getResponseBody() |
|
| 103 | + ), |
|
| 104 | + $response->getResponseHttpCode() |
|
| 105 | + ); |
|
| 106 | + } |
|
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | /** |
@@ -114,17 +114,17 @@ discard block |
||
| 114 | 114 | */ |
| 115 | 115 | public function sign(Google_Http_Request $request) |
| 116 | 116 | { |
| 117 | - if ($this->isAccessTokenExpired()) { |
|
| 118 | - $this->acquireAccessToken(); |
|
| 119 | - } |
|
| 117 | + if ($this->isAccessTokenExpired()) { |
|
| 118 | + $this->acquireAccessToken(); |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | - $this->client->getLogger()->debug('Compute engine service account authentication'); |
|
| 121 | + $this->client->getLogger()->debug('Compute engine service account authentication'); |
|
| 122 | 122 | |
| 123 | - $request->setRequestHeaders( |
|
| 124 | - array('Authorization' => 'Bearer ' . $this->token['access_token']) |
|
| 125 | - ); |
|
| 123 | + $request->setRequestHeaders( |
|
| 124 | + array('Authorization' => 'Bearer ' . $this->token['access_token']) |
|
| 125 | + ); |
|
| 126 | 126 | |
| 127 | - return $request; |
|
| 127 | + return $request; |
|
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | /** |
@@ -133,14 +133,14 @@ discard block |
||
| 133 | 133 | */ |
| 134 | 134 | public function isAccessTokenExpired() |
| 135 | 135 | { |
| 136 | - if (!$this->token || !isset($this->token['created'])) { |
|
| 137 | - return true; |
|
| 138 | - } |
|
| 136 | + if (!$this->token || !isset($this->token['created'])) { |
|
| 137 | + return true; |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | - // If the token is set to expire in the next 30 seconds. |
|
| 141 | - $expired = ($this->token['created'] |
|
| 142 | - + ($this->token['expires_in'] - 30)) < time(); |
|
| 140 | + // If the token is set to expire in the next 30 seconds. |
|
| 141 | + $expired = ($this->token['created'] |
|
| 142 | + + ($this->token['expires_in'] - 30)) < time(); |
|
| 143 | 143 | |
| 144 | - return $expired; |
|
| 144 | + return $expired; |
|
| 145 | 145 | } |
| 146 | 146 | } |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | |
| 18 | 18 | if (!class_exists('Google_Client')) { |
| 19 | - require_once dirname(__FILE__) . '/../autoload.php'; |
|
| 19 | + require_once dirname(__FILE__).'/../autoload.php'; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | /** |
@@ -63,7 +63,7 @@ discard block |
||
| 63 | 63 | if ($token == null) { |
| 64 | 64 | throw new Google_Auth_Exception('Could not json decode the token'); |
| 65 | 65 | } |
| 66 | - if (! isset($token['access_token'])) { |
|
| 66 | + if (!isset($token['access_token'])) { |
|
| 67 | 67 | throw new Google_Auth_Exception("Invalid token format"); |
| 68 | 68 | } |
| 69 | 69 | $token['created'] = time(); |
@@ -121,7 +121,7 @@ discard block |
||
| 121 | 121 | $this->client->getLogger()->debug('Compute engine service account authentication'); |
| 122 | 122 | |
| 123 | 123 | $request->setRequestHeaders( |
| 124 | - array('Authorization' => 'Bearer ' . $this->token['access_token']) |
|
| 124 | + array('Authorization' => 'Bearer '.$this->token['access_token']) |
|
| 125 | 125 | ); |
| 126 | 126 | |
| 127 | 127 | return $request; |
@@ -38,20 +38,20 @@ discard block |
||
| 38 | 38 | */ |
| 39 | 39 | public static function isRequestCacheable(Google_Http_Request $resp) |
| 40 | 40 | { |
| 41 | - $method = $resp->getRequestMethod(); |
|
| 42 | - if (! in_array($method, self::$CACHEABLE_HTTP_METHODS)) { |
|
| 43 | - return false; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - // Don't cache authorized requests/responses. |
|
| 47 | - // [rfc2616-14.8] When a shared cache receives a request containing an |
|
| 48 | - // Authorization field, it MUST NOT return the corresponding response |
|
| 49 | - // as a reply to any other request... |
|
| 50 | - if ($resp->getRequestHeader("authorization")) { |
|
| 51 | - return false; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - return true; |
|
| 41 | + $method = $resp->getRequestMethod(); |
|
| 42 | + if (! in_array($method, self::$CACHEABLE_HTTP_METHODS)) { |
|
| 43 | + return false; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + // Don't cache authorized requests/responses. |
|
| 47 | + // [rfc2616-14.8] When a shared cache receives a request containing an |
|
| 48 | + // Authorization field, it MUST NOT return the corresponding response |
|
| 49 | + // as a reply to any other request... |
|
| 50 | + if ($resp->getRequestHeader("authorization")) { |
|
| 51 | + return false; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + return true; |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | /** |
@@ -64,48 +64,48 @@ discard block |
||
| 64 | 64 | */ |
| 65 | 65 | public static function isResponseCacheable(Google_Http_Request $resp) |
| 66 | 66 | { |
| 67 | - // First, check if the HTTP request was cacheable before inspecting the |
|
| 68 | - // HTTP response. |
|
| 69 | - if (false == self::isRequestCacheable($resp)) { |
|
| 70 | - return false; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - $code = $resp->getResponseHttpCode(); |
|
| 74 | - if (! in_array($code, self::$CACHEABLE_STATUS_CODES)) { |
|
| 75 | - return false; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - // The resource is uncacheable if the resource is already expired and |
|
| 79 | - // the resource doesn't have an ETag for revalidation. |
|
| 80 | - $etag = $resp->getResponseHeader("etag"); |
|
| 81 | - if (self::isExpired($resp) && $etag == false) { |
|
| 82 | - return false; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - // [rfc2616-14.9.2] If [no-store is] sent in a response, a cache MUST NOT |
|
| 86 | - // store any part of either this response or the request that elicited it. |
|
| 87 | - $cacheControl = $resp->getParsedCacheControl(); |
|
| 88 | - if (isset($cacheControl['no-store'])) { |
|
| 89 | - return false; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - // Pragma: no-cache is an http request directive, but is occasionally |
|
| 93 | - // used as a response header incorrectly. |
|
| 94 | - $pragma = $resp->getResponseHeader('pragma'); |
|
| 95 | - if ($pragma == 'no-cache' || strpos($pragma, 'no-cache') !== false) { |
|
| 96 | - return false; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - // [rfc2616-14.44] Vary: * is extremely difficult to cache. "It implies that |
|
| 100 | - // a cache cannot determine from the request headers of a subsequent request |
|
| 101 | - // whether this response is the appropriate representation." |
|
| 102 | - // Given this, we deem responses with the Vary header as uncacheable. |
|
| 103 | - $vary = $resp->getResponseHeader('vary'); |
|
| 104 | - if ($vary) { |
|
| 105 | - return false; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - return true; |
|
| 67 | + // First, check if the HTTP request was cacheable before inspecting the |
|
| 68 | + // HTTP response. |
|
| 69 | + if (false == self::isRequestCacheable($resp)) { |
|
| 70 | + return false; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + $code = $resp->getResponseHttpCode(); |
|
| 74 | + if (! in_array($code, self::$CACHEABLE_STATUS_CODES)) { |
|
| 75 | + return false; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + // The resource is uncacheable if the resource is already expired and |
|
| 79 | + // the resource doesn't have an ETag for revalidation. |
|
| 80 | + $etag = $resp->getResponseHeader("etag"); |
|
| 81 | + if (self::isExpired($resp) && $etag == false) { |
|
| 82 | + return false; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + // [rfc2616-14.9.2] If [no-store is] sent in a response, a cache MUST NOT |
|
| 86 | + // store any part of either this response or the request that elicited it. |
|
| 87 | + $cacheControl = $resp->getParsedCacheControl(); |
|
| 88 | + if (isset($cacheControl['no-store'])) { |
|
| 89 | + return false; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + // Pragma: no-cache is an http request directive, but is occasionally |
|
| 93 | + // used as a response header incorrectly. |
|
| 94 | + $pragma = $resp->getResponseHeader('pragma'); |
|
| 95 | + if ($pragma == 'no-cache' || strpos($pragma, 'no-cache') !== false) { |
|
| 96 | + return false; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + // [rfc2616-14.44] Vary: * is extremely difficult to cache. "It implies that |
|
| 100 | + // a cache cannot determine from the request headers of a subsequent request |
|
| 101 | + // whether this response is the appropriate representation." |
|
| 102 | + // Given this, we deem responses with the Vary header as uncacheable. |
|
| 103 | + $vary = $resp->getResponseHeader('vary'); |
|
| 104 | + if ($vary) { |
|
| 105 | + return false; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + return true; |
|
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | /** |
@@ -116,57 +116,57 @@ discard block |
||
| 116 | 116 | */ |
| 117 | 117 | public static function isExpired(Google_Http_Request $resp) |
| 118 | 118 | { |
| 119 | - // HTTP/1.1 clients and caches MUST treat other invalid date formats, |
|
| 120 | - // especially including the value “0”, as in the past. |
|
| 121 | - $parsedExpires = false; |
|
| 122 | - $responseHeaders = $resp->getResponseHeaders(); |
|
| 123 | - |
|
| 124 | - if (isset($responseHeaders['expires'])) { |
|
| 125 | - $rawExpires = $responseHeaders['expires']; |
|
| 126 | - // Check for a malformed expires header first. |
|
| 127 | - if (empty($rawExpires) || (is_numeric($rawExpires) && $rawExpires <= 0)) { |
|
| 128 | - return true; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - // See if we can parse the expires header. |
|
| 132 | - $parsedExpires = strtotime($rawExpires); |
|
| 133 | - if (false == $parsedExpires || $parsedExpires <= 0) { |
|
| 134 | - return true; |
|
| 135 | - } |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - // Calculate the freshness of an http response. |
|
| 139 | - $freshnessLifetime = false; |
|
| 140 | - $cacheControl = $resp->getParsedCacheControl(); |
|
| 141 | - if (isset($cacheControl['max-age'])) { |
|
| 142 | - $freshnessLifetime = $cacheControl['max-age']; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - $rawDate = $resp->getResponseHeader('date'); |
|
| 146 | - $parsedDate = strtotime($rawDate); |
|
| 147 | - |
|
| 148 | - if (empty($rawDate) || false == $parsedDate) { |
|
| 149 | - // We can't default this to now, as that means future cache reads |
|
| 150 | - // will always pass with the logic below, so we will require a |
|
| 151 | - // date be injected if not supplied. |
|
| 152 | - throw new Google_Exception("All cacheable requests must have creation dates."); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - if (false == $freshnessLifetime && isset($responseHeaders['expires'])) { |
|
| 156 | - $freshnessLifetime = $parsedExpires - $parsedDate; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - if (false == $freshnessLifetime) { |
|
| 160 | - return true; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - // Calculate the age of an http response. |
|
| 164 | - $age = max(0, time() - $parsedDate); |
|
| 165 | - if (isset($responseHeaders['age'])) { |
|
| 166 | - $age = max($age, strtotime($responseHeaders['age'])); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - return $freshnessLifetime <= $age; |
|
| 119 | + // HTTP/1.1 clients and caches MUST treat other invalid date formats, |
|
| 120 | + // especially including the value “0”, as in the past. |
|
| 121 | + $parsedExpires = false; |
|
| 122 | + $responseHeaders = $resp->getResponseHeaders(); |
|
| 123 | + |
|
| 124 | + if (isset($responseHeaders['expires'])) { |
|
| 125 | + $rawExpires = $responseHeaders['expires']; |
|
| 126 | + // Check for a malformed expires header first. |
|
| 127 | + if (empty($rawExpires) || (is_numeric($rawExpires) && $rawExpires <= 0)) { |
|
| 128 | + return true; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + // See if we can parse the expires header. |
|
| 132 | + $parsedExpires = strtotime($rawExpires); |
|
| 133 | + if (false == $parsedExpires || $parsedExpires <= 0) { |
|
| 134 | + return true; |
|
| 135 | + } |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + // Calculate the freshness of an http response. |
|
| 139 | + $freshnessLifetime = false; |
|
| 140 | + $cacheControl = $resp->getParsedCacheControl(); |
|
| 141 | + if (isset($cacheControl['max-age'])) { |
|
| 142 | + $freshnessLifetime = $cacheControl['max-age']; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + $rawDate = $resp->getResponseHeader('date'); |
|
| 146 | + $parsedDate = strtotime($rawDate); |
|
| 147 | + |
|
| 148 | + if (empty($rawDate) || false == $parsedDate) { |
|
| 149 | + // We can't default this to now, as that means future cache reads |
|
| 150 | + // will always pass with the logic below, so we will require a |
|
| 151 | + // date be injected if not supplied. |
|
| 152 | + throw new Google_Exception("All cacheable requests must have creation dates."); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + if (false == $freshnessLifetime && isset($responseHeaders['expires'])) { |
|
| 156 | + $freshnessLifetime = $parsedExpires - $parsedDate; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + if (false == $freshnessLifetime) { |
|
| 160 | + return true; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + // Calculate the age of an http response. |
|
| 164 | + $age = max(0, time() - $parsedDate); |
|
| 165 | + if (isset($responseHeaders['age'])) { |
|
| 166 | + $age = max($age, strtotime($responseHeaders['age'])); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + return $freshnessLifetime <= $age; |
|
| 170 | 170 | } |
| 171 | 171 | |
| 172 | 172 | /** |
@@ -177,9 +177,9 @@ discard block |
||
| 177 | 177 | */ |
| 178 | 178 | public static function mustRevalidate(Google_Http_Request $response) |
| 179 | 179 | { |
| 180 | - // [13.3] When a cache has a stale entry that it would like to use as a |
|
| 181 | - // response to a client's request, it first has to check with the origin |
|
| 182 | - // server to see if its cached entry is still usable. |
|
| 183 | - return self::isExpired($response); |
|
| 180 | + // [13.3] When a cache has a stale entry that it would like to use as a |
|
| 181 | + // response to a client's request, it first has to check with the origin |
|
| 182 | + // server to see if its cached entry is still usable. |
|
| 183 | + return self::isExpired($response); |
|
| 184 | 184 | } |
| 185 | 185 | } |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | |
| 18 | 18 | if (!class_exists('Google_Client')) { |
| 19 | - require_once dirname(__FILE__) . '/../autoload.php'; |
|
| 19 | + require_once dirname(__FILE__).'/../autoload.php'; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | /** |
@@ -39,7 +39,7 @@ discard block |
||
| 39 | 39 | public static function isRequestCacheable(Google_Http_Request $resp) |
| 40 | 40 | { |
| 41 | 41 | $method = $resp->getRequestMethod(); |
| 42 | - if (! in_array($method, self::$CACHEABLE_HTTP_METHODS)) { |
|
| 42 | + if (!in_array($method, self::$CACHEABLE_HTTP_METHODS)) { |
|
| 43 | 43 | return false; |
| 44 | 44 | } |
| 45 | 45 | |
@@ -71,7 +71,7 @@ discard block |
||
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | $code = $resp->getResponseHttpCode(); |
| 74 | - if (! in_array($code, self::$CACHEABLE_STATUS_CODES)) { |
|
| 74 | + if (!in_array($code, self::$CACHEABLE_STATUS_CODES)) { |
|
| 75 | 75 | return false; |
| 76 | 76 | } |
| 77 | 77 | |
@@ -73,29 +73,29 @@ discard block |
||
| 73 | 73 | * only used if resumable=True |
| 74 | 74 | */ |
| 75 | 75 | public function __construct( |
| 76 | - Google_Client $client, |
|
| 77 | - Google_Http_Request $request, |
|
| 78 | - $mimeType, |
|
| 79 | - $data, |
|
| 80 | - $resumable = false, |
|
| 81 | - $chunkSize = false, |
|
| 82 | - $boundary = false |
|
| 76 | + Google_Client $client, |
|
| 77 | + Google_Http_Request $request, |
|
| 78 | + $mimeType, |
|
| 79 | + $data, |
|
| 80 | + $resumable = false, |
|
| 81 | + $chunkSize = false, |
|
| 82 | + $boundary = false |
|
| 83 | 83 | ) { |
| 84 | - $this->client = $client; |
|
| 85 | - $this->request = $request; |
|
| 86 | - $this->mimeType = $mimeType; |
|
| 87 | - $this->data = $data; |
|
| 88 | - $this->size = strlen($this->data); |
|
| 89 | - $this->resumable = $resumable; |
|
| 90 | - if (!$chunkSize) { |
|
| 91 | - $chunkSize = 256 * 1024; |
|
| 92 | - } |
|
| 93 | - $this->chunkSize = $chunkSize; |
|
| 94 | - $this->progress = 0; |
|
| 95 | - $this->boundary = $boundary; |
|
| 96 | - |
|
| 97 | - // Process Media Request |
|
| 98 | - $this->process(); |
|
| 84 | + $this->client = $client; |
|
| 85 | + $this->request = $request; |
|
| 86 | + $this->mimeType = $mimeType; |
|
| 87 | + $this->data = $data; |
|
| 88 | + $this->size = strlen($this->data); |
|
| 89 | + $this->resumable = $resumable; |
|
| 90 | + if (!$chunkSize) { |
|
| 91 | + $chunkSize = 256 * 1024; |
|
| 92 | + } |
|
| 93 | + $this->chunkSize = $chunkSize; |
|
| 94 | + $this->progress = 0; |
|
| 95 | + $this->boundary = $boundary; |
|
| 96 | + |
|
| 97 | + // Process Media Request |
|
| 98 | + $this->process(); |
|
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | /** |
@@ -104,7 +104,7 @@ discard block |
||
| 104 | 104 | */ |
| 105 | 105 | public function setFileSize($size) |
| 106 | 106 | { |
| 107 | - $this->size = $size; |
|
| 107 | + $this->size = $size; |
|
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | /** |
@@ -113,7 +113,7 @@ discard block |
||
| 113 | 113 | */ |
| 114 | 114 | public function getProgress() |
| 115 | 115 | { |
| 116 | - return $this->progress; |
|
| 116 | + return $this->progress; |
|
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | /** |
@@ -122,7 +122,7 @@ discard block |
||
| 122 | 122 | */ |
| 123 | 123 | public function getHttpResultCode() |
| 124 | 124 | { |
| 125 | - return $this->httpResultCode; |
|
| 125 | + return $this->httpResultCode; |
|
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | /** |
@@ -132,56 +132,56 @@ discard block |
||
| 132 | 132 | */ |
| 133 | 133 | public function nextChunk($chunk = false) |
| 134 | 134 | { |
| 135 | - if (false == $this->resumeUri) { |
|
| 136 | - $this->resumeUri = $this->getResumeUri(); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - if (false == $chunk) { |
|
| 140 | - $chunk = substr($this->data, $this->progress, $this->chunkSize); |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - $lastBytePos = $this->progress + strlen($chunk) - 1; |
|
| 144 | - $headers = array( |
|
| 145 | - 'content-range' => "bytes $this->progress-$lastBytePos/$this->size", |
|
| 146 | - 'content-type' => $this->request->getRequestHeader('content-type'), |
|
| 147 | - 'content-length' => $this->chunkSize, |
|
| 148 | - 'expect' => '', |
|
| 149 | - ); |
|
| 150 | - |
|
| 151 | - $httpRequest = new Google_Http_Request( |
|
| 152 | - $this->resumeUri, |
|
| 153 | - 'PUT', |
|
| 154 | - $headers, |
|
| 155 | - $chunk |
|
| 156 | - ); |
|
| 157 | - |
|
| 158 | - if ($this->client->getClassConfig("Google_Http_Request", "enable_gzip_for_uploads")) { |
|
| 159 | - $httpRequest->enableGzip(); |
|
| 160 | - } else { |
|
| 161 | - $httpRequest->disableGzip(); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - $response = $this->client->getIo()->makeRequest($httpRequest); |
|
| 165 | - $response->setExpectedClass($this->request->getExpectedClass()); |
|
| 166 | - $code = $response->getResponseHttpCode(); |
|
| 167 | - $this->httpResultCode = $code; |
|
| 168 | - |
|
| 169 | - if (308 == $code) { |
|
| 170 | - // Track the amount uploaded. |
|
| 171 | - $range = explode('-', $response->getResponseHeader('range')); |
|
| 172 | - $this->progress = $range[1] + 1; |
|
| 173 | - |
|
| 174 | - // Allow for changing upload URLs. |
|
| 175 | - $location = $response->getResponseHeader('location'); |
|
| 176 | - if ($location) { |
|
| 177 | - $this->resumeUri = $location; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - // No problems, but upload not complete. |
|
| 181 | - return false; |
|
| 182 | - } else { |
|
| 183 | - return Google_Http_REST::decodeHttpResponse($response, $this->client); |
|
| 184 | - } |
|
| 135 | + if (false == $this->resumeUri) { |
|
| 136 | + $this->resumeUri = $this->getResumeUri(); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + if (false == $chunk) { |
|
| 140 | + $chunk = substr($this->data, $this->progress, $this->chunkSize); |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + $lastBytePos = $this->progress + strlen($chunk) - 1; |
|
| 144 | + $headers = array( |
|
| 145 | + 'content-range' => "bytes $this->progress-$lastBytePos/$this->size", |
|
| 146 | + 'content-type' => $this->request->getRequestHeader('content-type'), |
|
| 147 | + 'content-length' => $this->chunkSize, |
|
| 148 | + 'expect' => '', |
|
| 149 | + ); |
|
| 150 | + |
|
| 151 | + $httpRequest = new Google_Http_Request( |
|
| 152 | + $this->resumeUri, |
|
| 153 | + 'PUT', |
|
| 154 | + $headers, |
|
| 155 | + $chunk |
|
| 156 | + ); |
|
| 157 | + |
|
| 158 | + if ($this->client->getClassConfig("Google_Http_Request", "enable_gzip_for_uploads")) { |
|
| 159 | + $httpRequest->enableGzip(); |
|
| 160 | + } else { |
|
| 161 | + $httpRequest->disableGzip(); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + $response = $this->client->getIo()->makeRequest($httpRequest); |
|
| 165 | + $response->setExpectedClass($this->request->getExpectedClass()); |
|
| 166 | + $code = $response->getResponseHttpCode(); |
|
| 167 | + $this->httpResultCode = $code; |
|
| 168 | + |
|
| 169 | + if (308 == $code) { |
|
| 170 | + // Track the amount uploaded. |
|
| 171 | + $range = explode('-', $response->getResponseHeader('range')); |
|
| 172 | + $this->progress = $range[1] + 1; |
|
| 173 | + |
|
| 174 | + // Allow for changing upload URLs. |
|
| 175 | + $location = $response->getResponseHeader('location'); |
|
| 176 | + if ($location) { |
|
| 177 | + $this->resumeUri = $location; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + // No problems, but upload not complete. |
|
| 181 | + return false; |
|
| 182 | + } else { |
|
| 183 | + return Google_Http_REST::decodeHttpResponse($response, $this->client); |
|
| 184 | + } |
|
| 185 | 185 | } |
| 186 | 186 | |
| 187 | 187 | /** |
@@ -192,53 +192,53 @@ discard block |
||
| 192 | 192 | */ |
| 193 | 193 | private function process() |
| 194 | 194 | { |
| 195 | - $postBody = false; |
|
| 196 | - $contentType = false; |
|
| 197 | - |
|
| 198 | - $meta = $this->request->getPostBody(); |
|
| 199 | - $meta = is_string($meta) ? json_decode($meta, true) : $meta; |
|
| 200 | - |
|
| 201 | - $uploadType = $this->getUploadType($meta); |
|
| 202 | - $this->request->setQueryParam('uploadType', $uploadType); |
|
| 203 | - $this->transformToUploadUrl(); |
|
| 204 | - $mimeType = $this->mimeType ? |
|
| 205 | - $this->mimeType : |
|
| 206 | - $this->request->getRequestHeader('content-type'); |
|
| 207 | - |
|
| 208 | - if (self::UPLOAD_RESUMABLE_TYPE == $uploadType) { |
|
| 209 | - $contentType = $mimeType; |
|
| 210 | - $postBody = is_string($meta) ? $meta : json_encode($meta); |
|
| 211 | - } else if (self::UPLOAD_MEDIA_TYPE == $uploadType) { |
|
| 212 | - $contentType = $mimeType; |
|
| 213 | - $postBody = $this->data; |
|
| 214 | - } else if (self::UPLOAD_MULTIPART_TYPE == $uploadType) { |
|
| 215 | - // This is a multipart/related upload. |
|
| 216 | - $boundary = $this->boundary ? $this->boundary : mt_rand(); |
|
| 217 | - $boundary = str_replace('"', '', $boundary); |
|
| 218 | - $contentType = 'multipart/related; boundary=' . $boundary; |
|
| 219 | - $related = "--$boundary\r\n"; |
|
| 220 | - $related .= "Content-Type: application/json; charset=UTF-8\r\n"; |
|
| 221 | - $related .= "\r\n" . json_encode($meta) . "\r\n"; |
|
| 222 | - $related .= "--$boundary\r\n"; |
|
| 223 | - $related .= "Content-Type: $mimeType\r\n"; |
|
| 224 | - $related .= "Content-Transfer-Encoding: base64\r\n"; |
|
| 225 | - $related .= "\r\n" . base64_encode($this->data) . "\r\n"; |
|
| 226 | - $related .= "--$boundary--"; |
|
| 227 | - $postBody = $related; |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - $this->request->setPostBody($postBody); |
|
| 231 | - |
|
| 232 | - if (isset($contentType) && $contentType) { |
|
| 233 | - $contentTypeHeader['content-type'] = $contentType; |
|
| 234 | - $this->request->setRequestHeaders($contentTypeHeader); |
|
| 235 | - } |
|
| 195 | + $postBody = false; |
|
| 196 | + $contentType = false; |
|
| 197 | + |
|
| 198 | + $meta = $this->request->getPostBody(); |
|
| 199 | + $meta = is_string($meta) ? json_decode($meta, true) : $meta; |
|
| 200 | + |
|
| 201 | + $uploadType = $this->getUploadType($meta); |
|
| 202 | + $this->request->setQueryParam('uploadType', $uploadType); |
|
| 203 | + $this->transformToUploadUrl(); |
|
| 204 | + $mimeType = $this->mimeType ? |
|
| 205 | + $this->mimeType : |
|
| 206 | + $this->request->getRequestHeader('content-type'); |
|
| 207 | + |
|
| 208 | + if (self::UPLOAD_RESUMABLE_TYPE == $uploadType) { |
|
| 209 | + $contentType = $mimeType; |
|
| 210 | + $postBody = is_string($meta) ? $meta : json_encode($meta); |
|
| 211 | + } else if (self::UPLOAD_MEDIA_TYPE == $uploadType) { |
|
| 212 | + $contentType = $mimeType; |
|
| 213 | + $postBody = $this->data; |
|
| 214 | + } else if (self::UPLOAD_MULTIPART_TYPE == $uploadType) { |
|
| 215 | + // This is a multipart/related upload. |
|
| 216 | + $boundary = $this->boundary ? $this->boundary : mt_rand(); |
|
| 217 | + $boundary = str_replace('"', '', $boundary); |
|
| 218 | + $contentType = 'multipart/related; boundary=' . $boundary; |
|
| 219 | + $related = "--$boundary\r\n"; |
|
| 220 | + $related .= "Content-Type: application/json; charset=UTF-8\r\n"; |
|
| 221 | + $related .= "\r\n" . json_encode($meta) . "\r\n"; |
|
| 222 | + $related .= "--$boundary\r\n"; |
|
| 223 | + $related .= "Content-Type: $mimeType\r\n"; |
|
| 224 | + $related .= "Content-Transfer-Encoding: base64\r\n"; |
|
| 225 | + $related .= "\r\n" . base64_encode($this->data) . "\r\n"; |
|
| 226 | + $related .= "--$boundary--"; |
|
| 227 | + $postBody = $related; |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + $this->request->setPostBody($postBody); |
|
| 231 | + |
|
| 232 | + if (isset($contentType) && $contentType) { |
|
| 233 | + $contentTypeHeader['content-type'] = $contentType; |
|
| 234 | + $this->request->setRequestHeaders($contentTypeHeader); |
|
| 235 | + } |
|
| 236 | 236 | } |
| 237 | 237 | |
| 238 | 238 | private function transformToUploadUrl() |
| 239 | 239 | { |
| 240 | - $base = $this->request->getBaseComponent(); |
|
| 241 | - $this->request->setBaseComponent($base . '/upload'); |
|
| 240 | + $base = $this->request->getBaseComponent(); |
|
| 241 | + $this->request->setBaseComponent($base . '/upload'); |
|
| 242 | 242 | } |
| 243 | 243 | |
| 244 | 244 | /** |
@@ -252,56 +252,56 @@ discard block |
||
| 252 | 252 | */ |
| 253 | 253 | public function getUploadType($meta) |
| 254 | 254 | { |
| 255 | - if ($this->resumable) { |
|
| 256 | - return self::UPLOAD_RESUMABLE_TYPE; |
|
| 257 | - } |
|
| 255 | + if ($this->resumable) { |
|
| 256 | + return self::UPLOAD_RESUMABLE_TYPE; |
|
| 257 | + } |
|
| 258 | 258 | |
| 259 | - if (false == $meta && $this->data) { |
|
| 260 | - return self::UPLOAD_MEDIA_TYPE; |
|
| 261 | - } |
|
| 259 | + if (false == $meta && $this->data) { |
|
| 260 | + return self::UPLOAD_MEDIA_TYPE; |
|
| 261 | + } |
|
| 262 | 262 | |
| 263 | - return self::UPLOAD_MULTIPART_TYPE; |
|
| 263 | + return self::UPLOAD_MULTIPART_TYPE; |
|
| 264 | 264 | } |
| 265 | 265 | |
| 266 | 266 | private function getResumeUri() |
| 267 | 267 | { |
| 268 | - $result = null; |
|
| 269 | - $body = $this->request->getPostBody(); |
|
| 270 | - if ($body) { |
|
| 271 | - $headers = array( |
|
| 272 | - 'content-type' => 'application/json; charset=UTF-8', |
|
| 273 | - 'content-length' => Google_Utils::getStrLen($body), |
|
| 274 | - 'x-upload-content-type' => $this->mimeType, |
|
| 275 | - 'x-upload-content-length' => $this->size, |
|
| 276 | - 'expect' => '', |
|
| 277 | - ); |
|
| 278 | - $this->request->setRequestHeaders($headers); |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - $response = $this->client->getIo()->makeRequest($this->request); |
|
| 282 | - $location = $response->getResponseHeader('location'); |
|
| 283 | - $code = $response->getResponseHttpCode(); |
|
| 284 | - |
|
| 285 | - if (200 == $code && true == $location) { |
|
| 286 | - return $location; |
|
| 287 | - } |
|
| 288 | - $message = $code; |
|
| 289 | - $body = @json_decode($response->getResponseBody()); |
|
| 290 | - if (!empty($body->error->errors) ) { |
|
| 291 | - $message .= ': '; |
|
| 292 | - foreach ($body->error->errors as $error) { |
|
| 293 | - $message .= "{$error->domain}, {$error->message};"; |
|
| 294 | - } |
|
| 295 | - $message = rtrim($message, ';'); |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - $error = "Failed to start the resumable upload (HTTP {$message})"; |
|
| 299 | - $this->client->getLogger()->error($error); |
|
| 300 | - throw new Google_Exception($error); |
|
| 268 | + $result = null; |
|
| 269 | + $body = $this->request->getPostBody(); |
|
| 270 | + if ($body) { |
|
| 271 | + $headers = array( |
|
| 272 | + 'content-type' => 'application/json; charset=UTF-8', |
|
| 273 | + 'content-length' => Google_Utils::getStrLen($body), |
|
| 274 | + 'x-upload-content-type' => $this->mimeType, |
|
| 275 | + 'x-upload-content-length' => $this->size, |
|
| 276 | + 'expect' => '', |
|
| 277 | + ); |
|
| 278 | + $this->request->setRequestHeaders($headers); |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + $response = $this->client->getIo()->makeRequest($this->request); |
|
| 282 | + $location = $response->getResponseHeader('location'); |
|
| 283 | + $code = $response->getResponseHttpCode(); |
|
| 284 | + |
|
| 285 | + if (200 == $code && true == $location) { |
|
| 286 | + return $location; |
|
| 287 | + } |
|
| 288 | + $message = $code; |
|
| 289 | + $body = @json_decode($response->getResponseBody()); |
|
| 290 | + if (!empty($body->error->errors) ) { |
|
| 291 | + $message .= ': '; |
|
| 292 | + foreach ($body->error->errors as $error) { |
|
| 293 | + $message .= "{$error->domain}, {$error->message};"; |
|
| 294 | + } |
|
| 295 | + $message = rtrim($message, ';'); |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + $error = "Failed to start the resumable upload (HTTP {$message})"; |
|
| 299 | + $this->client->getLogger()->error($error); |
|
| 300 | + throw new Google_Exception($error); |
|
| 301 | 301 | } |
| 302 | 302 | |
| 303 | 303 | public function setChunkSize($chunkSize) |
| 304 | 304 | { |
| 305 | - $this->chunkSize = $chunkSize; |
|
| 305 | + $this->chunkSize = $chunkSize; |
|
| 306 | 306 | } |
| 307 | 307 | } |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | |
| 18 | 18 | if (!class_exists('Google_Client')) { |
| 19 | - require_once dirname(__FILE__) . '/../autoload.php'; |
|
| 19 | + require_once dirname(__FILE__).'/../autoload.php'; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | /** |
@@ -202,8 +202,7 @@ discard block |
||
| 202 | 202 | $this->request->setQueryParam('uploadType', $uploadType); |
| 203 | 203 | $this->transformToUploadUrl(); |
| 204 | 204 | $mimeType = $this->mimeType ? |
| 205 | - $this->mimeType : |
|
| 206 | - $this->request->getRequestHeader('content-type'); |
|
| 205 | + $this->mimeType : $this->request->getRequestHeader('content-type'); |
|
| 207 | 206 | |
| 208 | 207 | if (self::UPLOAD_RESUMABLE_TYPE == $uploadType) { |
| 209 | 208 | $contentType = $mimeType; |
@@ -215,14 +214,14 @@ discard block |
||
| 215 | 214 | // This is a multipart/related upload. |
| 216 | 215 | $boundary = $this->boundary ? $this->boundary : mt_rand(); |
| 217 | 216 | $boundary = str_replace('"', '', $boundary); |
| 218 | - $contentType = 'multipart/related; boundary=' . $boundary; |
|
| 217 | + $contentType = 'multipart/related; boundary='.$boundary; |
|
| 219 | 218 | $related = "--$boundary\r\n"; |
| 220 | 219 | $related .= "Content-Type: application/json; charset=UTF-8\r\n"; |
| 221 | - $related .= "\r\n" . json_encode($meta) . "\r\n"; |
|
| 220 | + $related .= "\r\n".json_encode($meta)."\r\n"; |
|
| 222 | 221 | $related .= "--$boundary\r\n"; |
| 223 | 222 | $related .= "Content-Type: $mimeType\r\n"; |
| 224 | 223 | $related .= "Content-Transfer-Encoding: base64\r\n"; |
| 225 | - $related .= "\r\n" . base64_encode($this->data) . "\r\n"; |
|
| 224 | + $related .= "\r\n".base64_encode($this->data)."\r\n"; |
|
| 226 | 225 | $related .= "--$boundary--"; |
| 227 | 226 | $postBody = $related; |
| 228 | 227 | } |
@@ -238,7 +237,7 @@ discard block |
||
| 238 | 237 | private function transformToUploadUrl() |
| 239 | 238 | { |
| 240 | 239 | $base = $this->request->getBaseComponent(); |
| 241 | - $this->request->setBaseComponent($base . '/upload'); |
|
| 240 | + $this->request->setBaseComponent($base.'/upload'); |
|
| 242 | 241 | } |
| 243 | 242 | |
| 244 | 243 | /** |
@@ -287,7 +286,7 @@ discard block |
||
| 287 | 286 | } |
| 288 | 287 | $message = $code; |
| 289 | 288 | $body = @json_decode($response->getResponseBody()); |
| 290 | - if (!empty($body->error->errors) ) { |
|
| 289 | + if (!empty($body->error->errors)) { |
|
| 291 | 290 | $message .= ': '; |
| 292 | 291 | foreach ($body->error->errors as $error) { |
| 293 | 292 | $message .= "{$error->domain}, {$error->message};"; |
@@ -41,105 +41,105 @@ |
||
| 41 | 41 | |
| 42 | 42 | public function __construct(Google_Client $client, $boundary = false, $rootUrl = '', $batchPath = '') |
| 43 | 43 | { |
| 44 | - $this->client = $client; |
|
| 45 | - $this->root_url = rtrim($rootUrl ? $rootUrl : $this->client->getBasePath(), '/'); |
|
| 46 | - $this->batch_path = $batchPath ? $batchPath : 'batch'; |
|
| 47 | - $this->expected_classes = array(); |
|
| 48 | - $boundary = (false == $boundary) ? mt_rand() : $boundary; |
|
| 49 | - $this->boundary = str_replace('"', '', $boundary); |
|
| 44 | + $this->client = $client; |
|
| 45 | + $this->root_url = rtrim($rootUrl ? $rootUrl : $this->client->getBasePath(), '/'); |
|
| 46 | + $this->batch_path = $batchPath ? $batchPath : 'batch'; |
|
| 47 | + $this->expected_classes = array(); |
|
| 48 | + $boundary = (false == $boundary) ? mt_rand() : $boundary; |
|
| 49 | + $this->boundary = str_replace('"', '', $boundary); |
|
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | public function add(Google_Http_Request $request, $key = false) |
| 53 | 53 | { |
| 54 | - if (false == $key) { |
|
| 55 | - $key = mt_rand(); |
|
| 56 | - } |
|
| 54 | + if (false == $key) { |
|
| 55 | + $key = mt_rand(); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - $this->requests[$key] = $request; |
|
| 58 | + $this->requests[$key] = $request; |
|
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | public function execute() |
| 62 | 62 | { |
| 63 | - $body = ''; |
|
| 63 | + $body = ''; |
|
| 64 | 64 | |
| 65 | - /** @var Google_Http_Request $req */ |
|
| 66 | - foreach ($this->requests as $key => $req) { |
|
| 67 | - $body .= "--{$this->boundary}\n"; |
|
| 68 | - $body .= $req->toBatchString($key) . "\n\n"; |
|
| 69 | - $this->expected_classes["response-" . $key] = $req->getExpectedClass(); |
|
| 70 | - } |
|
| 65 | + /** @var Google_Http_Request $req */ |
|
| 66 | + foreach ($this->requests as $key => $req) { |
|
| 67 | + $body .= "--{$this->boundary}\n"; |
|
| 68 | + $body .= $req->toBatchString($key) . "\n\n"; |
|
| 69 | + $this->expected_classes["response-" . $key] = $req->getExpectedClass(); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - $body .= "--{$this->boundary}--"; |
|
| 72 | + $body .= "--{$this->boundary}--"; |
|
| 73 | 73 | |
| 74 | - $url = $this->root_url . '/' . $this->batch_path; |
|
| 75 | - $httpRequest = new Google_Http_Request($url, 'POST'); |
|
| 76 | - $httpRequest->setRequestHeaders( |
|
| 77 | - array('Content-Type' => 'multipart/mixed; boundary=' . $this->boundary) |
|
| 78 | - ); |
|
| 74 | + $url = $this->root_url . '/' . $this->batch_path; |
|
| 75 | + $httpRequest = new Google_Http_Request($url, 'POST'); |
|
| 76 | + $httpRequest->setRequestHeaders( |
|
| 77 | + array('Content-Type' => 'multipart/mixed; boundary=' . $this->boundary) |
|
| 78 | + ); |
|
| 79 | 79 | |
| 80 | - $httpRequest->setPostBody($body); |
|
| 81 | - $response = $this->client->getIo()->makeRequest($httpRequest); |
|
| 80 | + $httpRequest->setPostBody($body); |
|
| 81 | + $response = $this->client->getIo()->makeRequest($httpRequest); |
|
| 82 | 82 | |
| 83 | - return $this->parseResponse($response); |
|
| 83 | + return $this->parseResponse($response); |
|
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | public function parseResponse(Google_Http_Request $response) |
| 87 | 87 | { |
| 88 | - $contentType = $response->getResponseHeader('content-type'); |
|
| 89 | - $contentType = explode(';', $contentType); |
|
| 90 | - $boundary = false; |
|
| 91 | - foreach ($contentType as $part) { |
|
| 92 | - $part = (explode('=', $part, 2)); |
|
| 93 | - if (isset($part[0]) && 'boundary' == trim($part[0])) { |
|
| 94 | - $boundary = $part[1]; |
|
| 95 | - } |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - $body = $response->getResponseBody(); |
|
| 99 | - if ($body) { |
|
| 100 | - $body = str_replace("--$boundary--", "--$boundary", $body); |
|
| 101 | - $parts = explode("--$boundary", $body); |
|
| 102 | - $responses = array(); |
|
| 103 | - |
|
| 104 | - foreach ($parts as $part) { |
|
| 105 | - $part = trim($part); |
|
| 106 | - if (!empty($part)) { |
|
| 107 | - list($metaHeaders, $part) = explode("\r\n\r\n", $part, 2); |
|
| 108 | - $metaHeaders = $this->client->getIo()->getHttpResponseHeaders($metaHeaders); |
|
| 109 | - |
|
| 110 | - $status = substr($part, 0, strpos($part, "\n")); |
|
| 111 | - $status = explode(" ", $status); |
|
| 112 | - $status = $status[1]; |
|
| 113 | - |
|
| 114 | - list($partHeaders, $partBody) = $this->client->getIo()->ParseHttpResponse($part, false); |
|
| 115 | - $response = new Google_Http_Request(""); |
|
| 116 | - $response->setResponseHttpCode($status); |
|
| 117 | - $response->setResponseHeaders($partHeaders); |
|
| 118 | - $response->setResponseBody($partBody); |
|
| 119 | - |
|
| 120 | - // Need content id. |
|
| 121 | - $key = $metaHeaders['content-id']; |
|
| 122 | - |
|
| 123 | - if (isset($this->expected_classes[$key]) && |
|
| 124 | - strlen($this->expected_classes[$key]) > 0) { |
|
| 125 | - $class = $this->expected_classes[$key]; |
|
| 126 | - $response->setExpectedClass($class); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - try { |
|
| 130 | - $response = Google_Http_REST::decodeHttpResponse($response, $this->client); |
|
| 131 | - $responses[$key] = $response; |
|
| 132 | - } catch (Google_Service_Exception $e) { |
|
| 133 | - // Store the exception as the response, so successful responses |
|
| 134 | - // can be processed. |
|
| 135 | - $responses[$key] = $e; |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - return $responses; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - return null; |
|
| 88 | + $contentType = $response->getResponseHeader('content-type'); |
|
| 89 | + $contentType = explode(';', $contentType); |
|
| 90 | + $boundary = false; |
|
| 91 | + foreach ($contentType as $part) { |
|
| 92 | + $part = (explode('=', $part, 2)); |
|
| 93 | + if (isset($part[0]) && 'boundary' == trim($part[0])) { |
|
| 94 | + $boundary = $part[1]; |
|
| 95 | + } |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + $body = $response->getResponseBody(); |
|
| 99 | + if ($body) { |
|
| 100 | + $body = str_replace("--$boundary--", "--$boundary", $body); |
|
| 101 | + $parts = explode("--$boundary", $body); |
|
| 102 | + $responses = array(); |
|
| 103 | + |
|
| 104 | + foreach ($parts as $part) { |
|
| 105 | + $part = trim($part); |
|
| 106 | + if (!empty($part)) { |
|
| 107 | + list($metaHeaders, $part) = explode("\r\n\r\n", $part, 2); |
|
| 108 | + $metaHeaders = $this->client->getIo()->getHttpResponseHeaders($metaHeaders); |
|
| 109 | + |
|
| 110 | + $status = substr($part, 0, strpos($part, "\n")); |
|
| 111 | + $status = explode(" ", $status); |
|
| 112 | + $status = $status[1]; |
|
| 113 | + |
|
| 114 | + list($partHeaders, $partBody) = $this->client->getIo()->ParseHttpResponse($part, false); |
|
| 115 | + $response = new Google_Http_Request(""); |
|
| 116 | + $response->setResponseHttpCode($status); |
|
| 117 | + $response->setResponseHeaders($partHeaders); |
|
| 118 | + $response->setResponseBody($partBody); |
|
| 119 | + |
|
| 120 | + // Need content id. |
|
| 121 | + $key = $metaHeaders['content-id']; |
|
| 122 | + |
|
| 123 | + if (isset($this->expected_classes[$key]) && |
|
| 124 | + strlen($this->expected_classes[$key]) > 0) { |
|
| 125 | + $class = $this->expected_classes[$key]; |
|
| 126 | + $response->setExpectedClass($class); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + try { |
|
| 130 | + $response = Google_Http_REST::decodeHttpResponse($response, $this->client); |
|
| 131 | + $responses[$key] = $response; |
|
| 132 | + } catch (Google_Service_Exception $e) { |
|
| 133 | + // Store the exception as the response, so successful responses |
|
| 134 | + // can be processed. |
|
| 135 | + $responses[$key] = $e; |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + return $responses; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + return null; |
|
| 144 | 144 | } |
| 145 | 145 | } |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | |
| 18 | 18 | if (!class_exists('Google_Client')) { |
| 19 | - require_once dirname(__FILE__) . '/../autoload.php'; |
|
| 19 | + require_once dirname(__FILE__).'/../autoload.php'; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | /** |
@@ -65,16 +65,16 @@ discard block |
||
| 65 | 65 | /** @var Google_Http_Request $req */ |
| 66 | 66 | foreach ($this->requests as $key => $req) { |
| 67 | 67 | $body .= "--{$this->boundary}\n"; |
| 68 | - $body .= $req->toBatchString($key) . "\n\n"; |
|
| 69 | - $this->expected_classes["response-" . $key] = $req->getExpectedClass(); |
|
| 68 | + $body .= $req->toBatchString($key)."\n\n"; |
|
| 69 | + $this->expected_classes["response-".$key] = $req->getExpectedClass(); |
|
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | $body .= "--{$this->boundary}--"; |
| 73 | 73 | |
| 74 | - $url = $this->root_url . '/' . $this->batch_path; |
|
| 74 | + $url = $this->root_url.'/'.$this->batch_path; |
|
| 75 | 75 | $httpRequest = new Google_Http_Request($url, 'POST'); |
| 76 | 76 | $httpRequest->setRequestHeaders( |
| 77 | - array('Content-Type' => 'multipart/mixed; boundary=' . $this->boundary) |
|
| 77 | + array('Content-Type' => 'multipart/mixed; boundary='.$this->boundary) |
|
| 78 | 78 | ); |
| 79 | 79 | |
| 80 | 80 | $httpRequest->setPostBody($body); |
@@ -36,14 +36,14 @@ discard block |
||
| 36 | 36 | */ |
| 37 | 37 | public static function execute(Google_Client $client, Google_Http_Request $req) |
| 38 | 38 | { |
| 39 | - $runner = new Google_Task_Runner( |
|
| 40 | - $client, |
|
| 41 | - sprintf('%s %s', $req->getRequestMethod(), $req->getUrl()), |
|
| 42 | - array(get_class(), 'doExecute'), |
|
| 43 | - array($client, $req) |
|
| 44 | - ); |
|
| 39 | + $runner = new Google_Task_Runner( |
|
| 40 | + $client, |
|
| 41 | + sprintf('%s %s', $req->getRequestMethod(), $req->getUrl()), |
|
| 42 | + array(get_class(), 'doExecute'), |
|
| 43 | + array($client, $req) |
|
| 44 | + ); |
|
| 45 | 45 | |
| 46 | - return $runner->run(); |
|
| 46 | + return $runner->run(); |
|
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | /** |
@@ -57,9 +57,9 @@ discard block |
||
| 57 | 57 | */ |
| 58 | 58 | public static function doExecute(Google_Client $client, Google_Http_Request $req) |
| 59 | 59 | { |
| 60 | - $httpRequest = $client->getIo()->makeRequest($req); |
|
| 61 | - $httpRequest->setExpectedClass($req->getExpectedClass()); |
|
| 62 | - return self::decodeHttpResponse($httpRequest, $client); |
|
| 60 | + $httpRequest = $client->getIo()->makeRequest($req); |
|
| 61 | + $httpRequest->setExpectedClass($req->getExpectedClass()); |
|
| 62 | + return self::decodeHttpResponse($httpRequest, $client); |
|
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | /** |
@@ -72,65 +72,65 @@ discard block |
||
| 72 | 72 | */ |
| 73 | 73 | public static function decodeHttpResponse($response, Google_Client $client = null) |
| 74 | 74 | { |
| 75 | - $code = $response->getResponseHttpCode(); |
|
| 76 | - $body = $response->getResponseBody(); |
|
| 77 | - $decoded = null; |
|
| 75 | + $code = $response->getResponseHttpCode(); |
|
| 76 | + $body = $response->getResponseBody(); |
|
| 77 | + $decoded = null; |
|
| 78 | 78 | |
| 79 | - if ((intVal($code)) >= 300) { |
|
| 80 | - $decoded = json_decode($body, true); |
|
| 81 | - $err = 'Error calling ' . $response->getRequestMethod() . ' ' . $response->getUrl(); |
|
| 82 | - if (isset($decoded['error']) && |
|
| 83 | - isset($decoded['error']['message']) && |
|
| 84 | - isset($decoded['error']['code'])) { |
|
| 85 | - // if we're getting a json encoded error definition, use that instead of the raw response |
|
| 86 | - // body for improved readability |
|
| 87 | - $err .= ": ({$decoded['error']['code']}) {$decoded['error']['message']}"; |
|
| 88 | - } else { |
|
| 89 | - $err .= ": ($code) $body"; |
|
| 90 | - } |
|
| 79 | + if ((intVal($code)) >= 300) { |
|
| 80 | + $decoded = json_decode($body, true); |
|
| 81 | + $err = 'Error calling ' . $response->getRequestMethod() . ' ' . $response->getUrl(); |
|
| 82 | + if (isset($decoded['error']) && |
|
| 83 | + isset($decoded['error']['message']) && |
|
| 84 | + isset($decoded['error']['code'])) { |
|
| 85 | + // if we're getting a json encoded error definition, use that instead of the raw response |
|
| 86 | + // body for improved readability |
|
| 87 | + $err .= ": ({$decoded['error']['code']}) {$decoded['error']['message']}"; |
|
| 88 | + } else { |
|
| 89 | + $err .= ": ($code) $body"; |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - $errors = null; |
|
| 93 | - // Specific check for APIs which don't return error details, such as Blogger. |
|
| 94 | - if (isset($decoded['error']) && isset($decoded['error']['errors'])) { |
|
| 95 | - $errors = $decoded['error']['errors']; |
|
| 96 | - } |
|
| 92 | + $errors = null; |
|
| 93 | + // Specific check for APIs which don't return error details, such as Blogger. |
|
| 94 | + if (isset($decoded['error']) && isset($decoded['error']['errors'])) { |
|
| 95 | + $errors = $decoded['error']['errors']; |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - $map = null; |
|
| 99 | - if ($client) { |
|
| 100 | - $client->getLogger()->error( |
|
| 101 | - $err, |
|
| 102 | - array('code' => $code, 'errors' => $errors) |
|
| 103 | - ); |
|
| 98 | + $map = null; |
|
| 99 | + if ($client) { |
|
| 100 | + $client->getLogger()->error( |
|
| 101 | + $err, |
|
| 102 | + array('code' => $code, 'errors' => $errors) |
|
| 103 | + ); |
|
| 104 | 104 | |
| 105 | - $map = $client->getClassConfig( |
|
| 106 | - 'Google_Service_Exception', |
|
| 107 | - 'retry_map' |
|
| 108 | - ); |
|
| 109 | - } |
|
| 110 | - throw new Google_Service_Exception($err, $code, null, $errors, $map); |
|
| 111 | - } |
|
| 105 | + $map = $client->getClassConfig( |
|
| 106 | + 'Google_Service_Exception', |
|
| 107 | + 'retry_map' |
|
| 108 | + ); |
|
| 109 | + } |
|
| 110 | + throw new Google_Service_Exception($err, $code, null, $errors, $map); |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - // Only attempt to decode the response, if the response code wasn't (204) 'no content' |
|
| 114 | - if ($code != '204') { |
|
| 115 | - if ($response->getExpectedRaw()) { |
|
| 116 | - return $body; |
|
| 117 | - } |
|
| 113 | + // Only attempt to decode the response, if the response code wasn't (204) 'no content' |
|
| 114 | + if ($code != '204') { |
|
| 115 | + if ($response->getExpectedRaw()) { |
|
| 116 | + return $body; |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | - $decoded = json_decode($body, true); |
|
| 120 | - if ($decoded === null || $decoded === "") { |
|
| 121 | - $error = "Invalid json in service response: $body"; |
|
| 122 | - if ($client) { |
|
| 123 | - $client->getLogger()->error($error); |
|
| 124 | - } |
|
| 125 | - throw new Google_Service_Exception($error); |
|
| 126 | - } |
|
| 119 | + $decoded = json_decode($body, true); |
|
| 120 | + if ($decoded === null || $decoded === "") { |
|
| 121 | + $error = "Invalid json in service response: $body"; |
|
| 122 | + if ($client) { |
|
| 123 | + $client->getLogger()->error($error); |
|
| 124 | + } |
|
| 125 | + throw new Google_Service_Exception($error); |
|
| 126 | + } |
|
| 127 | 127 | |
| 128 | - if ($response->getExpectedClass()) { |
|
| 129 | - $class = $response->getExpectedClass(); |
|
| 130 | - $decoded = new $class($decoded); |
|
| 131 | - } |
|
| 132 | - } |
|
| 133 | - return $decoded; |
|
| 128 | + if ($response->getExpectedClass()) { |
|
| 129 | + $class = $response->getExpectedClass(); |
|
| 130 | + $decoded = new $class($decoded); |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | + return $decoded; |
|
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | /** |
@@ -144,35 +144,35 @@ discard block |
||
| 144 | 144 | */ |
| 145 | 145 | public static function createRequestUri($servicePath, $restPath, $params) |
| 146 | 146 | { |
| 147 | - $requestUrl = $servicePath . $restPath; |
|
| 148 | - $uriTemplateVars = array(); |
|
| 149 | - $queryVars = array(); |
|
| 150 | - foreach ($params as $paramName => $paramSpec) { |
|
| 151 | - if ($paramSpec['type'] == 'boolean') { |
|
| 152 | - $paramSpec['value'] = ($paramSpec['value']) ? 'true' : 'false'; |
|
| 153 | - } |
|
| 154 | - if ($paramSpec['location'] == 'path') { |
|
| 155 | - $uriTemplateVars[$paramName] = $paramSpec['value']; |
|
| 156 | - } else if ($paramSpec['location'] == 'query') { |
|
| 157 | - if (isset($paramSpec['repeated']) && is_array($paramSpec['value'])) { |
|
| 158 | - foreach ($paramSpec['value'] as $value) { |
|
| 159 | - $queryVars[] = $paramName . '=' . rawurlencode(rawurldecode($value)); |
|
| 160 | - } |
|
| 161 | - } else { |
|
| 162 | - $queryVars[] = $paramName . '=' . rawurlencode(rawurldecode($paramSpec['value'])); |
|
| 163 | - } |
|
| 164 | - } |
|
| 165 | - } |
|
| 147 | + $requestUrl = $servicePath . $restPath; |
|
| 148 | + $uriTemplateVars = array(); |
|
| 149 | + $queryVars = array(); |
|
| 150 | + foreach ($params as $paramName => $paramSpec) { |
|
| 151 | + if ($paramSpec['type'] == 'boolean') { |
|
| 152 | + $paramSpec['value'] = ($paramSpec['value']) ? 'true' : 'false'; |
|
| 153 | + } |
|
| 154 | + if ($paramSpec['location'] == 'path') { |
|
| 155 | + $uriTemplateVars[$paramName] = $paramSpec['value']; |
|
| 156 | + } else if ($paramSpec['location'] == 'query') { |
|
| 157 | + if (isset($paramSpec['repeated']) && is_array($paramSpec['value'])) { |
|
| 158 | + foreach ($paramSpec['value'] as $value) { |
|
| 159 | + $queryVars[] = $paramName . '=' . rawurlencode(rawurldecode($value)); |
|
| 160 | + } |
|
| 161 | + } else { |
|
| 162 | + $queryVars[] = $paramName . '=' . rawurlencode(rawurldecode($paramSpec['value'])); |
|
| 163 | + } |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - if (count($uriTemplateVars)) { |
|
| 168 | - $uriTemplateParser = new Google_Utils_URITemplate(); |
|
| 169 | - $requestUrl = $uriTemplateParser->parse($requestUrl, $uriTemplateVars); |
|
| 170 | - } |
|
| 167 | + if (count($uriTemplateVars)) { |
|
| 168 | + $uriTemplateParser = new Google_Utils_URITemplate(); |
|
| 169 | + $requestUrl = $uriTemplateParser->parse($requestUrl, $uriTemplateVars); |
|
| 170 | + } |
|
| 171 | 171 | |
| 172 | - if (count($queryVars)) { |
|
| 173 | - $requestUrl .= '?' . implode($queryVars, '&'); |
|
| 174 | - } |
|
| 172 | + if (count($queryVars)) { |
|
| 173 | + $requestUrl .= '?' . implode($queryVars, '&'); |
|
| 174 | + } |
|
| 175 | 175 | |
| 176 | - return $requestUrl; |
|
| 176 | + return $requestUrl; |
|
| 177 | 177 | } |
| 178 | 178 | } |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | |
| 18 | 18 | if (!class_exists('Google_Client')) { |
| 19 | - require_once dirname(__FILE__) . '/../autoload.php'; |
|
| 19 | + require_once dirname(__FILE__).'/../autoload.php'; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | /** |
@@ -78,9 +78,9 @@ discard block |
||
| 78 | 78 | |
| 79 | 79 | if ((intVal($code)) >= 300) { |
| 80 | 80 | $decoded = json_decode($body, true); |
| 81 | - $err = 'Error calling ' . $response->getRequestMethod() . ' ' . $response->getUrl(); |
|
| 81 | + $err = 'Error calling '.$response->getRequestMethod().' '.$response->getUrl(); |
|
| 82 | 82 | if (isset($decoded['error']) && |
| 83 | - isset($decoded['error']['message']) && |
|
| 83 | + isset($decoded['error']['message']) && |
|
| 84 | 84 | isset($decoded['error']['code'])) { |
| 85 | 85 | // if we're getting a json encoded error definition, use that instead of the raw response |
| 86 | 86 | // body for improved readability |
@@ -144,7 +144,7 @@ discard block |
||
| 144 | 144 | */ |
| 145 | 145 | public static function createRequestUri($servicePath, $restPath, $params) |
| 146 | 146 | { |
| 147 | - $requestUrl = $servicePath . $restPath; |
|
| 147 | + $requestUrl = $servicePath.$restPath; |
|
| 148 | 148 | $uriTemplateVars = array(); |
| 149 | 149 | $queryVars = array(); |
| 150 | 150 | foreach ($params as $paramName => $paramSpec) { |
@@ -156,10 +156,10 @@ discard block |
||
| 156 | 156 | } else if ($paramSpec['location'] == 'query') { |
| 157 | 157 | if (isset($paramSpec['repeated']) && is_array($paramSpec['value'])) { |
| 158 | 158 | foreach ($paramSpec['value'] as $value) { |
| 159 | - $queryVars[] = $paramName . '=' . rawurlencode(rawurldecode($value)); |
|
| 159 | + $queryVars[] = $paramName.'='.rawurlencode(rawurldecode($value)); |
|
| 160 | 160 | } |
| 161 | 161 | } else { |
| 162 | - $queryVars[] = $paramName . '=' . rawurlencode(rawurldecode($paramSpec['value'])); |
|
| 162 | + $queryVars[] = $paramName.'='.rawurlencode(rawurldecode($paramSpec['value'])); |
|
| 163 | 163 | } |
| 164 | 164 | } |
| 165 | 165 | } |
@@ -170,7 +170,7 @@ discard block |
||
| 170 | 170 | } |
| 171 | 171 | |
| 172 | 172 | if (count($queryVars)) { |
| 173 | - $requestUrl .= '?' . implode($queryVars, '&'); |
|
| 173 | + $requestUrl .= '?'.implode($queryVars, '&'); |
|
| 174 | 174 | } |
| 175 | 175 | |
| 176 | 176 | return $requestUrl; |
@@ -34,13 +34,13 @@ discard block |
||
| 34 | 34 | |
| 35 | 35 | public function __construct(Google_Client $client) |
| 36 | 36 | { |
| 37 | - if (!extension_loaded('curl')) { |
|
| 38 | - $error = 'The cURL IO handler requires the cURL extension to be enabled'; |
|
| 39 | - $client->getLogger()->critical($error); |
|
| 40 | - throw new Google_IO_Exception($error); |
|
| 41 | - } |
|
| 37 | + if (!extension_loaded('curl')) { |
|
| 38 | + $error = 'The cURL IO handler requires the cURL extension to be enabled'; |
|
| 39 | + $client->getLogger()->critical($error); |
|
| 40 | + throw new Google_IO_Exception($error); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - parent::__construct($client); |
|
| 43 | + parent::__construct($client); |
|
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | /** |
@@ -52,83 +52,83 @@ discard block |
||
| 52 | 52 | */ |
| 53 | 53 | public function executeRequest(Google_Http_Request $request) |
| 54 | 54 | { |
| 55 | - $curl = curl_init(); |
|
| 56 | - |
|
| 57 | - if ($request->getPostBody()) { |
|
| 58 | - curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getPostBody()); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - $requestHeaders = $request->getRequestHeaders(); |
|
| 62 | - if ($requestHeaders && is_array($requestHeaders)) { |
|
| 63 | - $curlHeaders = array(); |
|
| 64 | - foreach ($requestHeaders as $k => $v) { |
|
| 65 | - $curlHeaders[] = "$k: $v"; |
|
| 66 | - } |
|
| 67 | - curl_setopt($curl, CURLOPT_HTTPHEADER, $curlHeaders); |
|
| 68 | - } |
|
| 69 | - curl_setopt($curl, CURLOPT_URL, $request->getUrl()); |
|
| 70 | - |
|
| 71 | - curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $request->getRequestMethod()); |
|
| 72 | - curl_setopt($curl, CURLOPT_USERAGENT, $request->getUserAgent()); |
|
| 73 | - |
|
| 74 | - curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false); |
|
| 75 | - curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); |
|
| 76 | - // 1 is CURL_SSLVERSION_TLSv1, which is not always defined in PHP. |
|
| 77 | - curl_setopt($curl, CURLOPT_SSLVERSION, 1); |
|
| 78 | - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
|
| 79 | - curl_setopt($curl, CURLOPT_HEADER, true); |
|
| 80 | - |
|
| 81 | - if ($request->canGzip()) { |
|
| 82 | - curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate'); |
|
| 83 | - } |
|
| 55 | + $curl = curl_init(); |
|
| 56 | + |
|
| 57 | + if ($request->getPostBody()) { |
|
| 58 | + curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getPostBody()); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + $requestHeaders = $request->getRequestHeaders(); |
|
| 62 | + if ($requestHeaders && is_array($requestHeaders)) { |
|
| 63 | + $curlHeaders = array(); |
|
| 64 | + foreach ($requestHeaders as $k => $v) { |
|
| 65 | + $curlHeaders[] = "$k: $v"; |
|
| 66 | + } |
|
| 67 | + curl_setopt($curl, CURLOPT_HTTPHEADER, $curlHeaders); |
|
| 68 | + } |
|
| 69 | + curl_setopt($curl, CURLOPT_URL, $request->getUrl()); |
|
| 70 | + |
|
| 71 | + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $request->getRequestMethod()); |
|
| 72 | + curl_setopt($curl, CURLOPT_USERAGENT, $request->getUserAgent()); |
|
| 73 | + |
|
| 74 | + curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false); |
|
| 75 | + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); |
|
| 76 | + // 1 is CURL_SSLVERSION_TLSv1, which is not always defined in PHP. |
|
| 77 | + curl_setopt($curl, CURLOPT_SSLVERSION, 1); |
|
| 78 | + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
|
| 79 | + curl_setopt($curl, CURLOPT_HEADER, true); |
|
| 80 | + |
|
| 81 | + if ($request->canGzip()) { |
|
| 82 | + curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate'); |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - $options = $this->client->getClassConfig('Google_IO_Curl', 'options'); |
|
| 86 | - if (is_array($options)) { |
|
| 87 | - $this->setOptions($options); |
|
| 88 | - } |
|
| 85 | + $options = $this->client->getClassConfig('Google_IO_Curl', 'options'); |
|
| 86 | + if (is_array($options)) { |
|
| 87 | + $this->setOptions($options); |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - foreach ($this->options as $key => $var) { |
|
| 91 | - curl_setopt($curl, $key, $var); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - if (!isset($this->options[CURLOPT_CAINFO])) { |
|
| 95 | - curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/cacerts.pem'); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - $this->client->getLogger()->debug( |
|
| 99 | - 'cURL request', |
|
| 100 | - array( |
|
| 101 | - 'url' => $request->getUrl(), |
|
| 102 | - 'method' => $request->getRequestMethod(), |
|
| 103 | - 'headers' => $requestHeaders, |
|
| 104 | - 'body' => $request->getPostBody() |
|
| 105 | - ) |
|
| 106 | - ); |
|
| 107 | - |
|
| 108 | - $response = curl_exec($curl); |
|
| 109 | - if ($response === false) { |
|
| 110 | - $error = curl_error($curl); |
|
| 111 | - $code = curl_errno($curl); |
|
| 112 | - $map = $this->client->getClassConfig('Google_IO_Exception', 'retry_map'); |
|
| 113 | - |
|
| 114 | - $this->client->getLogger()->error('cURL ' . $error); |
|
| 115 | - throw new Google_IO_Exception($error, $code, null, $map); |
|
| 116 | - } |
|
| 117 | - $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); |
|
| 118 | - |
|
| 119 | - list($responseHeaders, $responseBody) = $this->parseHttpResponse($response, $headerSize); |
|
| 120 | - $responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
|
| 121 | - |
|
| 122 | - $this->client->getLogger()->debug( |
|
| 123 | - 'cURL response', |
|
| 124 | - array( |
|
| 125 | - 'code' => $responseCode, |
|
| 126 | - 'headers' => $responseHeaders, |
|
| 127 | - 'body' => $responseBody, |
|
| 128 | - ) |
|
| 129 | - ); |
|
| 130 | - |
|
| 131 | - return array($responseBody, $responseHeaders, $responseCode); |
|
| 90 | + foreach ($this->options as $key => $var) { |
|
| 91 | + curl_setopt($curl, $key, $var); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + if (!isset($this->options[CURLOPT_CAINFO])) { |
|
| 95 | + curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/cacerts.pem'); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + $this->client->getLogger()->debug( |
|
| 99 | + 'cURL request', |
|
| 100 | + array( |
|
| 101 | + 'url' => $request->getUrl(), |
|
| 102 | + 'method' => $request->getRequestMethod(), |
|
| 103 | + 'headers' => $requestHeaders, |
|
| 104 | + 'body' => $request->getPostBody() |
|
| 105 | + ) |
|
| 106 | + ); |
|
| 107 | + |
|
| 108 | + $response = curl_exec($curl); |
|
| 109 | + if ($response === false) { |
|
| 110 | + $error = curl_error($curl); |
|
| 111 | + $code = curl_errno($curl); |
|
| 112 | + $map = $this->client->getClassConfig('Google_IO_Exception', 'retry_map'); |
|
| 113 | + |
|
| 114 | + $this->client->getLogger()->error('cURL ' . $error); |
|
| 115 | + throw new Google_IO_Exception($error, $code, null, $map); |
|
| 116 | + } |
|
| 117 | + $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); |
|
| 118 | + |
|
| 119 | + list($responseHeaders, $responseBody) = $this->parseHttpResponse($response, $headerSize); |
|
| 120 | + $responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
|
| 121 | + |
|
| 122 | + $this->client->getLogger()->debug( |
|
| 123 | + 'cURL response', |
|
| 124 | + array( |
|
| 125 | + 'code' => $responseCode, |
|
| 126 | + 'headers' => $responseHeaders, |
|
| 127 | + 'body' => $responseBody, |
|
| 128 | + ) |
|
| 129 | + ); |
|
| 130 | + |
|
| 131 | + return array($responseBody, $responseHeaders, $responseCode); |
|
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | /** |
@@ -137,7 +137,7 @@ discard block |
||
| 137 | 137 | */ |
| 138 | 138 | public function setOptions($options) |
| 139 | 139 | { |
| 140 | - $this->options = $options + $this->options; |
|
| 140 | + $this->options = $options + $this->options; |
|
| 141 | 141 | } |
| 142 | 142 | |
| 143 | 143 | /** |
@@ -146,12 +146,12 @@ discard block |
||
| 146 | 146 | */ |
| 147 | 147 | public function setTimeout($timeout) |
| 148 | 148 | { |
| 149 | - // Since this timeout is really for putting a bound on the time |
|
| 150 | - // we'll set them both to the same. If you need to specify a longer |
|
| 151 | - // CURLOPT_TIMEOUT, or a higher CONNECTTIMEOUT, the best thing to |
|
| 152 | - // do is use the setOptions method for the values individually. |
|
| 153 | - $this->options[CURLOPT_CONNECTTIMEOUT] = $timeout; |
|
| 154 | - $this->options[CURLOPT_TIMEOUT] = $timeout; |
|
| 149 | + // Since this timeout is really for putting a bound on the time |
|
| 150 | + // we'll set them both to the same. If you need to specify a longer |
|
| 151 | + // CURLOPT_TIMEOUT, or a higher CONNECTTIMEOUT, the best thing to |
|
| 152 | + // do is use the setOptions method for the values individually. |
|
| 153 | + $this->options[CURLOPT_CONNECTTIMEOUT] = $timeout; |
|
| 154 | + $this->options[CURLOPT_TIMEOUT] = $timeout; |
|
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | /** |
@@ -160,7 +160,7 @@ discard block |
||
| 160 | 160 | */ |
| 161 | 161 | public function getTimeout() |
| 162 | 162 | { |
| 163 | - return $this->options[CURLOPT_TIMEOUT]; |
|
| 163 | + return $this->options[CURLOPT_TIMEOUT]; |
|
| 164 | 164 | } |
| 165 | 165 | |
| 166 | 166 | /** |
@@ -172,8 +172,8 @@ discard block |
||
| 172 | 172 | */ |
| 173 | 173 | protected function needsQuirk() |
| 174 | 174 | { |
| 175 | - $ver = curl_version(); |
|
| 176 | - $versionNum = $ver['version_number']; |
|
| 177 | - return $versionNum < Google_IO_Curl::NO_QUIRK_VERSION; |
|
| 175 | + $ver = curl_version(); |
|
| 176 | + $versionNum = $ver['version_number']; |
|
| 177 | + return $versionNum < Google_IO_Curl::NO_QUIRK_VERSION; |
|
| 178 | 178 | } |
| 179 | 179 | } |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | */ |
| 23 | 23 | |
| 24 | 24 | if (!class_exists('Google_Client')) { |
| 25 | - require_once dirname(__FILE__) . '/../autoload.php'; |
|
| 25 | + require_once dirname(__FILE__).'/../autoload.php'; |
|
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | class Google_IO_Curl extends Google_IO_Abstract |
@@ -92,7 +92,7 @@ discard block |
||
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | if (!isset($this->options[CURLOPT_CAINFO])) { |
| 95 | - curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/cacerts.pem'); |
|
| 95 | + curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__).'/cacerts.pem'); |
|
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | $this->client->getLogger()->debug( |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | $code = curl_errno($curl); |
| 112 | 112 | $map = $this->client->getClassConfig('Google_IO_Exception', 'retry_map'); |
| 113 | 113 | |
| 114 | - $this->client->getLogger()->error('cURL ' . $error); |
|
| 114 | + $this->client->getLogger()->error('cURL '.$error); |
|
| 115 | 115 | throw new Google_IO_Exception($error, $code, null, $map); |
| 116 | 116 | } |
| 117 | 117 | $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); |
@@ -34,24 +34,24 @@ discard block |
||
| 34 | 34 | private $trappedErrorString; |
| 35 | 35 | |
| 36 | 36 | private static $DEFAULT_HTTP_CONTEXT = array( |
| 37 | - "follow_location" => 0, |
|
| 38 | - "ignore_errors" => 1, |
|
| 37 | + "follow_location" => 0, |
|
| 38 | + "ignore_errors" => 1, |
|
| 39 | 39 | ); |
| 40 | 40 | |
| 41 | 41 | private static $DEFAULT_SSL_CONTEXT = array( |
| 42 | - "verify_peer" => true, |
|
| 42 | + "verify_peer" => true, |
|
| 43 | 43 | ); |
| 44 | 44 | |
| 45 | 45 | public function __construct(Google_Client $client) |
| 46 | 46 | { |
| 47 | - if (!ini_get('allow_url_fopen')) { |
|
| 48 | - $error = 'The stream IO handler requires the allow_url_fopen runtime ' . |
|
| 49 | - 'configuration to be enabled'; |
|
| 50 | - $client->getLogger()->critical($error); |
|
| 51 | - throw new Google_IO_Exception($error); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - parent::__construct($client); |
|
| 47 | + if (!ini_get('allow_url_fopen')) { |
|
| 48 | + $error = 'The stream IO handler requires the allow_url_fopen runtime ' . |
|
| 49 | + 'configuration to be enabled'; |
|
| 50 | + $client->getLogger()->critical($error); |
|
| 51 | + throw new Google_IO_Exception($error); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + parent::__construct($client); |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | /** |
@@ -63,119 +63,119 @@ discard block |
||
| 63 | 63 | */ |
| 64 | 64 | public function executeRequest(Google_Http_Request $request) |
| 65 | 65 | { |
| 66 | - $default_options = stream_context_get_options(stream_context_get_default()); |
|
| 67 | - |
|
| 68 | - $requestHttpContext = array_key_exists('http', $default_options) ? |
|
| 69 | - $default_options['http'] : array(); |
|
| 70 | - |
|
| 71 | - if ($request->getPostBody()) { |
|
| 72 | - $requestHttpContext["content"] = $request->getPostBody(); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - $requestHeaders = $request->getRequestHeaders(); |
|
| 76 | - if ($requestHeaders && is_array($requestHeaders)) { |
|
| 77 | - $headers = ""; |
|
| 78 | - foreach ($requestHeaders as $k => $v) { |
|
| 79 | - $headers .= "$k: $v\r\n"; |
|
| 80 | - } |
|
| 81 | - $requestHttpContext["header"] = $headers; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - $requestHttpContext["method"] = $request->getRequestMethod(); |
|
| 85 | - $requestHttpContext["user_agent"] = $request->getUserAgent(); |
|
| 86 | - |
|
| 87 | - $requestSslContext = array_key_exists('ssl', $default_options) ? |
|
| 88 | - $default_options['ssl'] : array(); |
|
| 89 | - |
|
| 90 | - if (!array_key_exists("cafile", $requestSslContext)) { |
|
| 91 | - $requestSslContext["cafile"] = dirname(__FILE__) . '/cacerts.pem'; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - $options = array( |
|
| 95 | - "http" => array_merge( |
|
| 96 | - self::$DEFAULT_HTTP_CONTEXT, |
|
| 97 | - $requestHttpContext |
|
| 98 | - ), |
|
| 99 | - "ssl" => array_merge( |
|
| 100 | - self::$DEFAULT_SSL_CONTEXT, |
|
| 101 | - $requestSslContext |
|
| 102 | - ) |
|
| 103 | - ); |
|
| 104 | - |
|
| 105 | - $context = stream_context_create($options); |
|
| 106 | - |
|
| 107 | - $url = $request->getUrl(); |
|
| 108 | - |
|
| 109 | - if ($request->canGzip()) { |
|
| 110 | - $url = self::ZLIB . $url; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - $this->client->getLogger()->debug( |
|
| 114 | - 'Stream request', |
|
| 115 | - array( |
|
| 116 | - 'url' => $url, |
|
| 117 | - 'method' => $request->getRequestMethod(), |
|
| 118 | - 'headers' => $requestHeaders, |
|
| 119 | - 'body' => $request->getPostBody() |
|
| 120 | - ) |
|
| 121 | - ); |
|
| 122 | - |
|
| 123 | - // We are trapping any thrown errors in this method only and |
|
| 124 | - // throwing an exception. |
|
| 125 | - $this->trappedErrorNumber = null; |
|
| 126 | - $this->trappedErrorString = null; |
|
| 127 | - |
|
| 128 | - // START - error trap. |
|
| 129 | - set_error_handler(array($this, 'trapError')); |
|
| 130 | - $fh = fopen($url, 'r', false, $context); |
|
| 131 | - restore_error_handler(); |
|
| 132 | - // END - error trap. |
|
| 133 | - |
|
| 134 | - if ($this->trappedErrorNumber) { |
|
| 135 | - $error = sprintf( |
|
| 136 | - "HTTP Error: Unable to connect: '%s'", |
|
| 137 | - $this->trappedErrorString |
|
| 138 | - ); |
|
| 139 | - |
|
| 140 | - $this->client->getLogger()->error('Stream ' . $error); |
|
| 141 | - throw new Google_IO_Exception($error, $this->trappedErrorNumber); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - $response_data = false; |
|
| 145 | - $respHttpCode = self::UNKNOWN_CODE; |
|
| 146 | - if ($fh) { |
|
| 147 | - if (isset($this->options[self::TIMEOUT])) { |
|
| 148 | - stream_set_timeout($fh, $this->options[self::TIMEOUT]); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - $response_data = stream_get_contents($fh); |
|
| 152 | - fclose($fh); |
|
| 153 | - |
|
| 154 | - $respHttpCode = $this->getHttpResponseCode($http_response_header); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - if (false === $response_data) { |
|
| 158 | - $error = sprintf( |
|
| 159 | - "HTTP Error: Unable to connect: '%s'", |
|
| 160 | - $respHttpCode |
|
| 161 | - ); |
|
| 162 | - |
|
| 163 | - $this->client->getLogger()->error('Stream ' . $error); |
|
| 164 | - throw new Google_IO_Exception($error, $respHttpCode); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - $responseHeaders = $this->getHttpResponseHeaders($http_response_header); |
|
| 168 | - |
|
| 169 | - $this->client->getLogger()->debug( |
|
| 170 | - 'Stream response', |
|
| 171 | - array( |
|
| 172 | - 'code' => $respHttpCode, |
|
| 173 | - 'headers' => $responseHeaders, |
|
| 174 | - 'body' => $response_data, |
|
| 175 | - ) |
|
| 176 | - ); |
|
| 177 | - |
|
| 178 | - return array($response_data, $responseHeaders, $respHttpCode); |
|
| 66 | + $default_options = stream_context_get_options(stream_context_get_default()); |
|
| 67 | + |
|
| 68 | + $requestHttpContext = array_key_exists('http', $default_options) ? |
|
| 69 | + $default_options['http'] : array(); |
|
| 70 | + |
|
| 71 | + if ($request->getPostBody()) { |
|
| 72 | + $requestHttpContext["content"] = $request->getPostBody(); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + $requestHeaders = $request->getRequestHeaders(); |
|
| 76 | + if ($requestHeaders && is_array($requestHeaders)) { |
|
| 77 | + $headers = ""; |
|
| 78 | + foreach ($requestHeaders as $k => $v) { |
|
| 79 | + $headers .= "$k: $v\r\n"; |
|
| 80 | + } |
|
| 81 | + $requestHttpContext["header"] = $headers; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + $requestHttpContext["method"] = $request->getRequestMethod(); |
|
| 85 | + $requestHttpContext["user_agent"] = $request->getUserAgent(); |
|
| 86 | + |
|
| 87 | + $requestSslContext = array_key_exists('ssl', $default_options) ? |
|
| 88 | + $default_options['ssl'] : array(); |
|
| 89 | + |
|
| 90 | + if (!array_key_exists("cafile", $requestSslContext)) { |
|
| 91 | + $requestSslContext["cafile"] = dirname(__FILE__) . '/cacerts.pem'; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + $options = array( |
|
| 95 | + "http" => array_merge( |
|
| 96 | + self::$DEFAULT_HTTP_CONTEXT, |
|
| 97 | + $requestHttpContext |
|
| 98 | + ), |
|
| 99 | + "ssl" => array_merge( |
|
| 100 | + self::$DEFAULT_SSL_CONTEXT, |
|
| 101 | + $requestSslContext |
|
| 102 | + ) |
|
| 103 | + ); |
|
| 104 | + |
|
| 105 | + $context = stream_context_create($options); |
|
| 106 | + |
|
| 107 | + $url = $request->getUrl(); |
|
| 108 | + |
|
| 109 | + if ($request->canGzip()) { |
|
| 110 | + $url = self::ZLIB . $url; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + $this->client->getLogger()->debug( |
|
| 114 | + 'Stream request', |
|
| 115 | + array( |
|
| 116 | + 'url' => $url, |
|
| 117 | + 'method' => $request->getRequestMethod(), |
|
| 118 | + 'headers' => $requestHeaders, |
|
| 119 | + 'body' => $request->getPostBody() |
|
| 120 | + ) |
|
| 121 | + ); |
|
| 122 | + |
|
| 123 | + // We are trapping any thrown errors in this method only and |
|
| 124 | + // throwing an exception. |
|
| 125 | + $this->trappedErrorNumber = null; |
|
| 126 | + $this->trappedErrorString = null; |
|
| 127 | + |
|
| 128 | + // START - error trap. |
|
| 129 | + set_error_handler(array($this, 'trapError')); |
|
| 130 | + $fh = fopen($url, 'r', false, $context); |
|
| 131 | + restore_error_handler(); |
|
| 132 | + // END - error trap. |
|
| 133 | + |
|
| 134 | + if ($this->trappedErrorNumber) { |
|
| 135 | + $error = sprintf( |
|
| 136 | + "HTTP Error: Unable to connect: '%s'", |
|
| 137 | + $this->trappedErrorString |
|
| 138 | + ); |
|
| 139 | + |
|
| 140 | + $this->client->getLogger()->error('Stream ' . $error); |
|
| 141 | + throw new Google_IO_Exception($error, $this->trappedErrorNumber); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + $response_data = false; |
|
| 145 | + $respHttpCode = self::UNKNOWN_CODE; |
|
| 146 | + if ($fh) { |
|
| 147 | + if (isset($this->options[self::TIMEOUT])) { |
|
| 148 | + stream_set_timeout($fh, $this->options[self::TIMEOUT]); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + $response_data = stream_get_contents($fh); |
|
| 152 | + fclose($fh); |
|
| 153 | + |
|
| 154 | + $respHttpCode = $this->getHttpResponseCode($http_response_header); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + if (false === $response_data) { |
|
| 158 | + $error = sprintf( |
|
| 159 | + "HTTP Error: Unable to connect: '%s'", |
|
| 160 | + $respHttpCode |
|
| 161 | + ); |
|
| 162 | + |
|
| 163 | + $this->client->getLogger()->error('Stream ' . $error); |
|
| 164 | + throw new Google_IO_Exception($error, $respHttpCode); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + $responseHeaders = $this->getHttpResponseHeaders($http_response_header); |
|
| 168 | + |
|
| 169 | + $this->client->getLogger()->debug( |
|
| 170 | + 'Stream response', |
|
| 171 | + array( |
|
| 172 | + 'code' => $respHttpCode, |
|
| 173 | + 'headers' => $responseHeaders, |
|
| 174 | + 'body' => $response_data, |
|
| 175 | + ) |
|
| 176 | + ); |
|
| 177 | + |
|
| 178 | + return array($response_data, $responseHeaders, $respHttpCode); |
|
| 179 | 179 | } |
| 180 | 180 | |
| 181 | 181 | /** |
@@ -184,7 +184,7 @@ discard block |
||
| 184 | 184 | */ |
| 185 | 185 | public function setOptions($options) |
| 186 | 186 | { |
| 187 | - $this->options = $options + $this->options; |
|
| 187 | + $this->options = $options + $this->options; |
|
| 188 | 188 | } |
| 189 | 189 | |
| 190 | 190 | /** |
@@ -193,8 +193,8 @@ discard block |
||
| 193 | 193 | */ |
| 194 | 194 | public function trapError($errno, $errstr) |
| 195 | 195 | { |
| 196 | - $this->trappedErrorNumber = $errno; |
|
| 197 | - $this->trappedErrorString = $errstr; |
|
| 196 | + $this->trappedErrorNumber = $errno; |
|
| 197 | + $this->trappedErrorString = $errstr; |
|
| 198 | 198 | } |
| 199 | 199 | |
| 200 | 200 | /** |
@@ -203,7 +203,7 @@ discard block |
||
| 203 | 203 | */ |
| 204 | 204 | public function setTimeout($timeout) |
| 205 | 205 | { |
| 206 | - $this->options[self::TIMEOUT] = $timeout; |
|
| 206 | + $this->options[self::TIMEOUT] = $timeout; |
|
| 207 | 207 | } |
| 208 | 208 | |
| 209 | 209 | /** |
@@ -212,7 +212,7 @@ discard block |
||
| 212 | 212 | */ |
| 213 | 213 | public function getTimeout() |
| 214 | 214 | { |
| 215 | - return $this->options[self::TIMEOUT]; |
|
| 215 | + return $this->options[self::TIMEOUT]; |
|
| 216 | 216 | } |
| 217 | 217 | |
| 218 | 218 | /** |
@@ -224,20 +224,20 @@ discard block |
||
| 224 | 224 | */ |
| 225 | 225 | protected function needsQuirk() |
| 226 | 226 | { |
| 227 | - return false; |
|
| 227 | + return false; |
|
| 228 | 228 | } |
| 229 | 229 | |
| 230 | 230 | protected function getHttpResponseCode($response_headers) |
| 231 | 231 | { |
| 232 | - $header_count = count($response_headers); |
|
| 233 | - |
|
| 234 | - for ($i = 0; $i < $header_count; $i++) { |
|
| 235 | - $header = $response_headers[$i]; |
|
| 236 | - if (strncasecmp("HTTP", $header, strlen("HTTP")) == 0) { |
|
| 237 | - $response = explode(' ', $header); |
|
| 238 | - return $response[1]; |
|
| 239 | - } |
|
| 240 | - } |
|
| 241 | - return self::UNKNOWN_CODE; |
|
| 232 | + $header_count = count($response_headers); |
|
| 233 | + |
|
| 234 | + for ($i = 0; $i < $header_count; $i++) { |
|
| 235 | + $header = $response_headers[$i]; |
|
| 236 | + if (strncasecmp("HTTP", $header, strlen("HTTP")) == 0) { |
|
| 237 | + $response = explode(' ', $header); |
|
| 238 | + return $response[1]; |
|
| 239 | + } |
|
| 240 | + } |
|
| 241 | + return self::UNKNOWN_CODE; |
|
| 242 | 242 | } |
| 243 | 243 | } |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | */ |
| 23 | 23 | |
| 24 | 24 | if (!class_exists('Google_Client')) { |
| 25 | - require_once dirname(__FILE__) . '/../autoload.php'; |
|
| 25 | + require_once dirname(__FILE__).'/../autoload.php'; |
|
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | class Google_IO_Stream extends Google_IO_Abstract |
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | public function __construct(Google_Client $client) |
| 46 | 46 | { |
| 47 | 47 | if (!ini_get('allow_url_fopen')) { |
| 48 | - $error = 'The stream IO handler requires the allow_url_fopen runtime ' . |
|
| 48 | + $error = 'The stream IO handler requires the allow_url_fopen runtime '. |
|
| 49 | 49 | 'configuration to be enabled'; |
| 50 | 50 | $client->getLogger()->critical($error); |
| 51 | 51 | throw new Google_IO_Exception($error); |
@@ -88,7 +88,7 @@ discard block |
||
| 88 | 88 | $default_options['ssl'] : array(); |
| 89 | 89 | |
| 90 | 90 | if (!array_key_exists("cafile", $requestSslContext)) { |
| 91 | - $requestSslContext["cafile"] = dirname(__FILE__) . '/cacerts.pem'; |
|
| 91 | + $requestSslContext["cafile"] = dirname(__FILE__).'/cacerts.pem'; |
|
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | $options = array( |
@@ -107,7 +107,7 @@ discard block |
||
| 107 | 107 | $url = $request->getUrl(); |
| 108 | 108 | |
| 109 | 109 | if ($request->canGzip()) { |
| 110 | - $url = self::ZLIB . $url; |
|
| 110 | + $url = self::ZLIB.$url; |
|
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | $this->client->getLogger()->debug( |
@@ -137,7 +137,7 @@ discard block |
||
| 137 | 137 | $this->trappedErrorString |
| 138 | 138 | ); |
| 139 | 139 | |
| 140 | - $this->client->getLogger()->error('Stream ' . $error); |
|
| 140 | + $this->client->getLogger()->error('Stream '.$error); |
|
| 141 | 141 | throw new Google_IO_Exception($error, $this->trappedErrorNumber); |
| 142 | 142 | } |
| 143 | 143 | |
@@ -160,7 +160,7 @@ discard block |
||
| 160 | 160 | $respHttpCode |
| 161 | 161 | ); |
| 162 | 162 | |
| 163 | - $this->client->getLogger()->error('Stream ' . $error); |
|
| 163 | + $this->client->getLogger()->error('Stream '.$error); |
|
| 164 | 164 | throw new Google_IO_Exception($error, $respHttpCode); |
| 165 | 165 | } |
| 166 | 166 | |
@@ -35,20 +35,20 @@ discard block |
||
| 35 | 35 | * @param array|null $retryMap Map of errors with retry counts. |
| 36 | 36 | */ |
| 37 | 37 | public function __construct( |
| 38 | - $message, |
|
| 39 | - $code = 0, |
|
| 40 | - Exception $previous = null, |
|
| 41 | - array $retryMap = null |
|
| 38 | + $message, |
|
| 39 | + $code = 0, |
|
| 40 | + Exception $previous = null, |
|
| 41 | + array $retryMap = null |
|
| 42 | 42 | ) { |
| 43 | - if (version_compare(PHP_VERSION, '5.3.0') >= 0) { |
|
| 44 | - parent::__construct($message, $code, $previous); |
|
| 45 | - } else { |
|
| 46 | - parent::__construct($message, $code); |
|
| 47 | - } |
|
| 43 | + if (version_compare(PHP_VERSION, '5.3.0') >= 0) { |
|
| 44 | + parent::__construct($message, $code, $previous); |
|
| 45 | + } else { |
|
| 46 | + parent::__construct($message, $code); |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - if (is_array($retryMap)) { |
|
| 50 | - $this->retryMap = $retryMap; |
|
| 51 | - } |
|
| 49 | + if (is_array($retryMap)) { |
|
| 50 | + $this->retryMap = $retryMap; |
|
| 51 | + } |
|
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | /** |
@@ -60,10 +60,10 @@ discard block |
||
| 60 | 60 | */ |
| 61 | 61 | public function allowedRetries() |
| 62 | 62 | { |
| 63 | - if (isset($this->retryMap[$this->code])) { |
|
| 64 | - return $this->retryMap[$this->code]; |
|
| 65 | - } |
|
| 63 | + if (isset($this->retryMap[$this->code])) { |
|
| 64 | + return $this->retryMap[$this->code]; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - return 0; |
|
| 67 | + return 0; |
|
| 68 | 68 | } |
| 69 | 69 | } |
@@ -16,7 +16,7 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | |
| 18 | 18 | if (!class_exists('Google_Client')) { |
| 19 | - require_once dirname(__FILE__) . '/../autoload.php'; |
|
| 19 | + require_once dirname(__FILE__).'/../autoload.php'; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | class Google_IO_Exception extends Google_Exception implements Google_Task_Retryable |