| 1 |
<?php |
| 2 |
/* |
| 3 |
* Copyright 2010 Google Inc. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
|
| 18 |
require_once 'Google/Utils.php'; |
| 19 |
|
| 20 |
/** |
| 21 |
* HTTP Request to be executed by IO classes. Upon execution, the |
| 22 |
* responseHttpCode, responseHeaders and responseBody will be filled in. |
| 23 |
* |
| 24 |
* @author Chris Chabot <chabotc@google.com> |
| 25 |
* @author Chirag Shah <chirags@google.com> |
| 26 |
* |
| 27 |
*/ |
| 28 |
class Google_Http_Request |
| 29 |
{ |
| 30 |
const GZIP_UA = " (gzip)"; |
| 31 |
|
| 32 |
private $batchHeaders = array( |
| 33 |
'Content-Type' => 'application/http', |
| 34 |
'Content-Transfer-Encoding' => 'binary', |
| 35 |
'MIME-Version' => '1.0', |
| 36 |
); |
| 37 |
|
| 38 |
protected $queryParams; |
| 39 |
protected $requestMethod; |
| 40 |
protected $requestHeaders; |
| 41 |
protected $baseComponent = null; |
| 42 |
protected $path; |
| 43 |
protected $postBody; |
| 44 |
protected $userAgent; |
| 45 |
protected $canGzip = null; |
| 46 |
|
| 47 |
protected $responseHttpCode; |
| 48 |
protected $responseHeaders; |
| 49 |
protected $responseBody; |
| 50 |
|
| 51 |
protected $expectedClass; |
| 52 |
|
| 53 |
public $accessKey; |
| 54 |
|
| 55 |
public function __construct( |
| 56 |
$url, |
| 57 |
$method = 'GET', |
| 58 |
$headers = array(), |
| 59 |
$postBody = null |
| 60 |
) { |
| 61 |
$this->setUrl($url); |
| 62 |
$this->setRequestMethod($method); |
| 63 |
$this->setRequestHeaders($headers); |
| 64 |
$this->setPostBody($postBody); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Misc function that returns the base url component of the $url |
| 69 |
* used by the OAuth signing class to calculate the base string |
| 70 |
* @return string The base url component of the $url. |
| 71 |
*/ |
| 72 |
public function getBaseComponent() |
| 73 |
{ |
| 74 |
return $this->baseComponent; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Set the base URL that path and query parameters will be added to. |
| 79 |
* @param $baseComponent string |
| 80 |
*/ |
| 81 |
public function setBaseComponent($baseComponent) |
| 82 |
{ |
| 83 |
$this->baseComponent = $baseComponent; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Enable support for gzipped responses with this request. |
| 88 |
*/ |
| 89 |
public function enableGzip() |
| 90 |
{ |
| 91 |
$this->setRequestHeaders(array("Accept-Encoding" => "gzip")); |
| 92 |
$this->canGzip = true; |
| 93 |
$this->setUserAgent($this->userAgent); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Disable support for gzip responses with this request. |
| 98 |
*/ |
| 99 |
public function disableGzip() |
| 100 |
{ |
| 101 |
if ( |
| 102 |
isset($this->requestHeaders['accept-encoding']) && |
| 103 |
$this->requestHeaders['accept-encoding'] == "gzip" |
| 104 |
) { |
| 105 |
unset($this->requestHeaders['accept-encoding']); |
| 106 |
} |
| 107 |
$this->canGzip = false; |
| 108 |
$this->userAgent = str_replace(self::GZIP_UA, "", $this->userAgent); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Can this request accept a gzip response? |
| 113 |
* @return bool |
| 114 |
*/ |
| 115 |
public function canGzip() |
| 116 |
{ |
| 117 |
return $this->canGzip; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Misc function that returns an array of the query parameters of the current |
| 122 |
* url used by the OAuth signing class to calculate the signature |
| 123 |
* @return array Query parameters in the query string. |
| 124 |
*/ |
| 125 |
public function getQueryParams() |
| 126 |
{ |
| 127 |
return $this->queryParams; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Set a new query parameter. |
| 132 |
* @param $key - string to set, does not need to be URL encoded |
| 133 |
* @param $value - string to set, does not need to be URL encoded |
| 134 |
*/ |
| 135 |
public function setQueryParam($key, $value) |
| 136 |
{ |
| 137 |
$this->queryParams[$key] = $value; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* @return string HTTP Response Code. |
| 142 |
*/ |
| 143 |
public function getResponseHttpCode() |
| 144 |
{ |
| 145 |
return (int) $this->responseHttpCode; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* @param int $responseHttpCode HTTP Response Code. |
| 150 |
*/ |
| 151 |
public function setResponseHttpCode($responseHttpCode) |
| 152 |
{ |
| 153 |
$this->responseHttpCode = $responseHttpCode; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* @return $responseHeaders (array) HTTP Response Headers. |
| 158 |
*/ |
| 159 |
public function getResponseHeaders() |
| 160 |
{ |
| 161 |
return $this->responseHeaders; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* @return string HTTP Response Body |
| 166 |
*/ |
| 167 |
public function getResponseBody() |
| 168 |
{ |
| 169 |
return $this->responseBody; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Set the class the response to this request should expect. |
| 174 |
* |
| 175 |
* @param $class string the class name |
| 176 |
*/ |
| 177 |
public function setExpectedClass($class) |
| 178 |
{ |
| 179 |
$this->expectedClass = $class; |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Retrieve the expected class the response should expect. |
| 184 |
* @return string class name |
| 185 |
*/ |
| 186 |
public function getExpectedClass() |
| 187 |
{ |
| 188 |
return $this->expectedClass; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* @param array $headers The HTTP response headers |
| 193 |
* to be normalized. |
| 194 |
*/ |
| 195 |
public function setResponseHeaders($headers) |
| 196 |
{ |
| 197 |
$headers = Google_Utils::normalize($headers); |
| 198 |
if ($this->responseHeaders) { |
| 199 |
$headers = array_merge($this->responseHeaders, $headers); |
| 200 |
} |
| 201 |
|
| 202 |
$this->responseHeaders = $headers; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* @param string $key |
| 207 |
* @return array|boolean Returns the requested HTTP header or |
| 208 |
* false if unavailable. |
| 209 |
*/ |
| 210 |
public function getResponseHeader($key) |
| 211 |
{ |
| 212 |
return isset($this->responseHeaders[$key]) |
| 213 |
? $this->responseHeaders[$key] |
| 214 |
: false; |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* @param string $responseBody The HTTP response body. |
| 219 |
*/ |
| 220 |
public function setResponseBody($responseBody) |
| 221 |
{ |
| 222 |
$this->responseBody = $responseBody; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* @return string $url The request URL. |
| 227 |
*/ |
| 228 |
public function getUrl() |
| 229 |
{ |
| 230 |
return $this->baseComponent . $this->path . |
| 231 |
(count($this->queryParams) ? |
| 232 |
"?" . $this->buildQuery($this->queryParams) : |
| 233 |
''); |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* @return string $method HTTP Request Method. |
| 238 |
*/ |
| 239 |
public function getRequestMethod() |
| 240 |
{ |
| 241 |
return $this->requestMethod; |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* @return array $headers HTTP Request Headers. |
| 246 |
*/ |
| 247 |
public function getRequestHeaders() |
| 248 |
{ |
| 249 |
return $this->requestHeaders; |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* @param string $key |
| 254 |
* @return array|boolean Returns the requested HTTP header or |
| 255 |
* false if unavailable. |
| 256 |
*/ |
| 257 |
public function getRequestHeader($key) |
| 258 |
{ |
| 259 |
return isset($this->requestHeaders[$key]) |
| 260 |
? $this->requestHeaders[$key] |
| 261 |
: false; |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* @return string $postBody HTTP Request Body. |
| 266 |
*/ |
| 267 |
public function getPostBody() |
| 268 |
{ |
| 269 |
return $this->postBody; |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* @param string $url the url to set |
| 274 |
*/ |
| 275 |
public function setUrl($url) |
| 276 |
{ |
| 277 |
if (substr($url, 0, 4) != 'http') { |
| 278 |
// Force the path become relative. |
| 279 |
if (substr($url, 0, 1) !== '/') { |
| 280 |
$url = '/' . $url; |
| 281 |
} |
| 282 |
} |
| 283 |
$parts = parse_url($url); |
| 284 |
if (isset($parts['host'])) { |
| 285 |
$this->baseComponent = sprintf( |
| 286 |
"%s%s%s", |
| 287 |
isset($parts['scheme']) ? $parts['scheme'] . "://" : '', |
| 288 |
isset($parts['host']) ? $parts['host'] : '', |
| 289 |
isset($parts['port']) ? ":" . $parts['port'] : '' |
| 290 |
); |
| 291 |
} |
| 292 |
$this->path = isset($parts['path']) ? $parts['path'] : ''; |
| 293 |
$this->queryParams = array(); |
| 294 |
if (isset($parts['query'])) { |
| 295 |
$this->queryParams = $this->parseQuery($parts['query']); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* @param string $method Set he HTTP Method and normalize |
| 301 |
* it to upper-case, as required by HTTP. |
| 302 |
* |
| 303 |
*/ |
| 304 |
public function setRequestMethod($method) |
| 305 |
{ |
| 306 |
$this->requestMethod = strtoupper($method); |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* @param array $headers The HTTP request headers |
| 311 |
* to be set and normalized. |
| 312 |
*/ |
| 313 |
public function setRequestHeaders($headers) |
| 314 |
{ |
| 315 |
$headers = Google_Utils::normalize($headers); |
| 316 |
if ($this->requestHeaders) { |
| 317 |
$headers = array_merge($this->requestHeaders, $headers); |
| 318 |
} |
| 319 |
$this->requestHeaders = $headers; |
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
* @param string $postBody the postBody to set |
| 324 |
*/ |
| 325 |
public function setPostBody($postBody) |
| 326 |
{ |
| 327 |
$this->postBody = $postBody; |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* Set the User-Agent Header. |
| 332 |
* @param string $userAgent The User-Agent. |
| 333 |
*/ |
| 334 |
public function setUserAgent($userAgent) |
| 335 |
{ |
| 336 |
$this->userAgent = $userAgent; |
| 337 |
if ($this->canGzip) { |
| 338 |
$this->userAgent = $userAgent . self::GZIP_UA; |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
/** |
| 343 |
* @return string The User-Agent. |
| 344 |
*/ |
| 345 |
public function getUserAgent() |
| 346 |
{ |
| 347 |
return $this->userAgent; |
| 348 |
} |
| 349 |
|
| 350 |
/** |
| 351 |
* Returns a cache key depending on if this was an OAuth signed request |
| 352 |
* in which case it will use the non-signed url and access key to make this |
| 353 |
* cache key unique per authenticated user, else use the plain request url |
| 354 |
* @return string The md5 hash of the request cache key. |
| 355 |
*/ |
| 356 |
public function getCacheKey() |
| 357 |
{ |
| 358 |
$key = $this->getUrl(); |
| 359 |
|
| 360 |
if (isset($this->accessKey)) { |
| 361 |
$key .= $this->accessKey; |
| 362 |
} |
| 363 |
|
| 364 |
if (isset($this->requestHeaders['authorization'])) { |
| 365 |
$key .= $this->requestHeaders['authorization']; |
| 366 |
} |
| 367 |
|
| 368 |
return md5($key); |
| 369 |
} |
| 370 |
|
| 371 |
public function getParsedCacheControl() |
| 372 |
{ |
| 373 |
$parsed = array(); |
| 374 |
$rawCacheControl = $this->getResponseHeader('cache-control'); |
| 375 |
if ($rawCacheControl) { |
| 376 |
$rawCacheControl = str_replace(', ', '&', $rawCacheControl); |
| 377 |
parse_str($rawCacheControl, $parsed); |
| 378 |
} |
| 379 |
|
| 380 |
return $parsed; |
| 381 |
} |
| 382 |
|
| 383 |
/** |
| 384 |
* @param string $id |
| 385 |
* @return string A string representation of the HTTP Request. |
| 386 |
*/ |
| 387 |
public function toBatchString($id) |
| 388 |
{ |
| 389 |
$str = ''; |
| 390 |
$path = parse_url($this->getUrl(), PHP_URL_PATH) . "?" . |
| 391 |
http_build_query($this->queryParams); |
| 392 |
$str .= $this->getRequestMethod() . ' ' . $path . " HTTP/1.1\n"; |
| 393 |
|
| 394 |
foreach ($this->getRequestHeaders() as $key => $val) { |
| 395 |
$str .= $key . ': ' . $val . "\n"; |
| 396 |
} |
| 397 |
|
| 398 |
if ($this->getPostBody()) { |
| 399 |
$str .= "\n"; |
| 400 |
$str .= $this->getPostBody(); |
| 401 |
} |
| 402 |
|
| 403 |
$headers = ''; |
| 404 |
foreach ($this->batchHeaders as $key => $val) { |
| 405 |
$headers .= $key . ': ' . $val . "\n"; |
| 406 |
} |
| 407 |
|
| 408 |
$headers .= "Content-ID: $id\n"; |
| 409 |
$str = $headers . "\n" . $str; |
| 410 |
|
| 411 |
return $str; |
| 412 |
} |
| 413 |
|
| 414 |
/** |
| 415 |
* Our own version of parse_str that allows for multiple variables |
| 416 |
* with the same name. |
| 417 |
* @param $string - the query string to parse |
| 418 |
*/ |
| 419 |
private function parseQuery($string) |
| 420 |
{ |
| 421 |
$return = array(); |
| 422 |
$parts = explode("&", $string); |
| 423 |
foreach ($parts as $part) { |
| 424 |
list($key, $value) = explode('=', $part, 2); |
| 425 |
$value = urldecode($value); |
| 426 |
if (isset($return[$key])) { |
| 427 |
if (!is_array($return[$key])) { |
| 428 |
$return[$key] = array($return[$key]); |
| 429 |
} |
| 430 |
$return[$key][] = $value; |
| 431 |
} else { |
| 432 |
$return[$key] = $value; |
| 433 |
} |
| 434 |
} |
| 435 |
return $return; |
| 436 |
} |
| 437 |
|
| 438 |
/** |
| 439 |
* A version of build query that allows for multiple |
| 440 |
* duplicate keys. |
| 441 |
* @param $parts array of key value pairs |
| 442 |
*/ |
| 443 |
private function buildQuery($parts) |
| 444 |
{ |
| 445 |
$return = array(); |
| 446 |
foreach ($parts as $key => $value) { |
| 447 |
if (is_array($value)) { |
| 448 |
foreach ($value as $v) { |
| 449 |
$return[] = urlencode($key) . "=" . urlencode($v); |
| 450 |
} |
| 451 |
} else { |
| 452 |
$return[] = urlencode($key) . "=" . urlencode($value); |
| 453 |
} |
| 454 |
} |
| 455 |
return implode('&', $return); |
| 456 |
} |
| 457 |
|
| 458 |
/** |
| 459 |
* If we're POSTing and have no body to send, we can send the query |
| 460 |
* parameters in there, which avoids length issues with longer query |
| 461 |
* params. |
| 462 |
*/ |
| 463 |
public function maybeMoveParametersToBody() |
| 464 |
{ |
| 465 |
if ($this->getRequestMethod() == "POST" && empty($this->postBody)) { |
| 466 |
$this->setRequestHeaders( |
| 467 |
array( |
| 468 |
"content-type" => |
| 469 |
"application/x-www-form-urlencoded; charset=UTF-8" |
| 470 |
) |
| 471 |
); |
| 472 |
$this->setPostBody($this->buildQuery($this->queryParams)); |
| 473 |
$this->queryParams = array(); |
| 474 |
} |
| 475 |
} |
| 476 |
} |
| 477 |
|