Google_HttpRequest

HTTP Request to be executed by apiIO classes.

Defined (1)

The class is defined in the following location(s).

/includes/api-libs/Google/Http/Request.php  
  1. class Google_Http_Request 
  2. const GZIP_UA = " (gzip)"; 
  3.  
  4. private $batchHeaders = array( 
  5. 'Content-Type' => 'application/http',  
  6. 'Content-Transfer-Encoding' => 'binary',  
  7. 'MIME-Version' => '1.0',  
  8. ); 
  9.  
  10. protected $queryParams; 
  11. protected $requestMethod; 
  12. protected $requestHeaders; 
  13. protected $baseComponent = null; 
  14. protected $path; 
  15. protected $postBody; 
  16. protected $userAgent; 
  17. protected $canGzip = null; 
  18.  
  19. protected $responseHttpCode; 
  20. protected $responseHeaders; 
  21. protected $responseBody; 
  22.  
  23. protected $expectedClass; 
  24. protected $expectedRaw = false; 
  25.  
  26. public $accessKey; 
  27.  
  28. public function __construct( 
  29. $url,  
  30. $method = 'GET',  
  31. $headers = array(),  
  32. $postBody = null 
  33. ) { 
  34. $this->setUrl($url); 
  35. $this->setRequestMethod($method); 
  36. $this->setRequestHeaders($headers); 
  37. $this->setPostBody($postBody); 
  38.  
  39. /** 
  40. * Misc function that returns the base url component of the $url 
  41. * used by the OAuth signing class to calculate the base string 
  42. * @return string The base url component of the $url. 
  43. */ 
  44. public function getBaseComponent() 
  45. return $this->baseComponent; 
  46.  
  47. /** 
  48. * Set the base URL that path and query parameters will be added to. 
  49. * @param $baseComponent string 
  50. */ 
  51. public function setBaseComponent($baseComponent) 
  52. $this->baseComponent = $baseComponent; 
  53.  
  54. /** 
  55. * Enable support for gzipped responses with this request. 
  56. */ 
  57. public function enableGzip() 
  58. $this->setRequestHeaders(array("Accept-Encoding" => "gzip")); 
  59. $this->canGzip = true; 
  60. $this->setUserAgent($this->userAgent); 
  61.  
  62. /** 
  63. * Disable support for gzip responses with this request. 
  64. */ 
  65. public function disableGzip() 
  66. if ( 
  67. isset($this->requestHeaders['accept-encoding']) && 
  68. $this->requestHeaders['accept-encoding'] == "gzip" 
  69. ) { 
  70. unset($this->requestHeaders['accept-encoding']); 
  71. $this->canGzip = false; 
  72. $this->userAgent = str_replace(self::GZIP_UA, "", $this->userAgent); 
  73.  
  74. /** 
  75. * Can this request accept a gzip response? 
  76. * @return bool 
  77. */ 
  78. public function canGzip() 
  79. return $this->canGzip; 
  80.  
  81. /** 
  82. * Misc function that returns an array of the query parameters of the current 
  83. * url used by the OAuth signing class to calculate the signature 
  84. * @return array Query parameters in the query string. 
  85. */ 
  86. public function getQueryParams() 
  87. return $this->queryParams; 
  88.  
  89. /**  
  90. * Set a new query parameter. 
  91. * @param $key - string to set, does not need to be URL encoded 
  92. * @param $value - string to set, does not need to be URL encoded 
  93. */ 
  94. public function setQueryParam($key, $value) 
  95. $this->queryParams[$key] = $value; 
  96.  
  97. /** 
  98. * @return string HTTP Response Code. 
  99. */ 
  100. public function getResponseHttpCode() 
  101. return (int) $this->responseHttpCode; 
  102.  
  103. /** 
  104. * @param int $responseHttpCode HTTP Response Code. 
  105. */ 
  106. public function setResponseHttpCode($responseHttpCode) 
  107. $this->responseHttpCode = $responseHttpCode; 
  108.  
  109. /** 
  110. * @return $responseHeaders (array) HTTP Response Headers. 
  111. */ 
  112. public function getResponseHeaders() 
  113. return $this->responseHeaders; 
  114.  
  115. /** 
  116. * @return string HTTP Response Body 
  117. */ 
  118. public function getResponseBody() 
  119. return $this->responseBody; 
  120.  
  121. /** 
  122. * Set the class the response to this request should expect. 
  123. * @param $class string the class name 
  124. */ 
  125. public function setExpectedClass($class) 
  126. $this->expectedClass = $class; 
  127.  
  128. /** 
  129. * Retrieve the expected class the response should expect. 
  130. * @return string class name 
  131. */ 
  132. public function getExpectedClass() 
  133. return $this->expectedClass; 
  134.  
  135. /** 
  136. * Enable expected raw response 
  137. */ 
  138. public function enableExpectedRaw() 
  139. $this->expectedRaw = true; 
  140.  
  141. /** 
  142. * Disable expected raw response 
  143. */ 
  144. public function disableExpectedRaw() 
  145. $this->expectedRaw = false; 
  146.  
  147. /** 
  148. * Expected raw response or not. 
  149. * @return boolean expected raw response 
  150. */ 
  151. public function getExpectedRaw() 
  152. return $this->expectedRaw; 
  153.  
  154. /** 
  155. * @param array $headers The HTTP response headers 
  156. * to be normalized. 
  157. */ 
  158. public function setResponseHeaders($headers) 
  159. $headers = Google_Utils::normalize($headers); 
  160. if ($this->responseHeaders) { 
  161. $headers = array_merge($this->responseHeaders, $headers); 
  162.  
  163. $this->responseHeaders = $headers; 
  164.  
  165. /** 
  166. * @param string $key 
  167. * @return array|boolean Returns the requested HTTP header or 
  168. * false if unavailable. 
  169. */ 
  170. public function getResponseHeader($key) 
  171. return isset($this->responseHeaders[$key]) 
  172. ? $this->responseHeaders[$key] 
  173. : false; 
  174.  
  175. /** 
  176. * @param string $responseBody The HTTP response body. 
  177. */ 
  178. public function setResponseBody($responseBody) 
  179. $this->responseBody = $responseBody; 
  180.  
  181. /** 
  182. * @return string $url The request URL. 
  183. */ 
  184. public function getUrl() 
  185. return $this->baseComponent . $this->path . 
  186. (count($this->queryParams) ? 
  187. "?" . $this->buildQuery($this->queryParams) : 
  188. ''); 
  189.  
  190. /** 
  191. * @return string $method HTTP Request Method. 
  192. */ 
  193. public function getRequestMethod() 
  194. return $this->requestMethod; 
  195.  
  196. /** 
  197. * @return array $headers HTTP Request Headers. 
  198. */ 
  199. public function getRequestHeaders() 
  200. return $this->requestHeaders; 
  201.  
  202. /** 
  203. * @param string $key 
  204. * @return array|boolean Returns the requested HTTP header or 
  205. * false if unavailable. 
  206. */ 
  207. public function getRequestHeader($key) 
  208. return isset($this->requestHeaders[$key]) 
  209. ? $this->requestHeaders[$key] 
  210. : false; 
  211.  
  212. /** 
  213. * @return string $postBody HTTP Request Body. 
  214. */ 
  215. public function getPostBody() 
  216. return $this->postBody; 
  217.  
  218. /** 
  219. * @param string $url the url to set 
  220. */ 
  221. public function setUrl($url) 
  222. if (substr($url, 0, 4) != 'http') { 
  223. // Force the path become relative. 
  224. if (substr($url, 0, 1) !== '/') { 
  225. $url = '/' . $url; 
  226. $parts = parse_url($url); 
  227. if (isset($parts['host'])) { 
  228. $this->baseComponent = sprintf( 
  229. "%s%s%s",  
  230. isset($parts['scheme']) ? $parts['scheme'] . "://" : '',  
  231. isset($parts['host']) ? $parts['host'] : '',  
  232. isset($parts['port']) ? ":" . $parts['port'] : '' 
  233. ); 
  234. $this->path = isset($parts['path']) ? $parts['path'] : ''; 
  235. $this->queryParams = array(); 
  236. if (isset($parts['query'])) { 
  237. $this->queryParams = $this->parseQuery($parts['query']); 
  238.  
  239. /** 
  240. * @param string $method Set he HTTP Method and normalize 
  241. * it to upper-case, as required by HTTP. 
  242. */ 
  243. public function setRequestMethod($method) 
  244. $this->requestMethod = strtoupper($method); 
  245.  
  246. /** 
  247. * @param array $headers The HTTP request headers 
  248. * to be set and normalized. 
  249. */ 
  250. public function setRequestHeaders($headers) 
  251. $headers = Google_Utils::normalize($headers); 
  252. if ($this->requestHeaders) { 
  253. $headers = array_merge($this->requestHeaders, $headers); 
  254. $this->requestHeaders = $headers; 
  255.  
  256. /** 
  257. * @param string $postBody the postBody to set 
  258. */ 
  259. public function setPostBody($postBody) 
  260. $this->postBody = $postBody; 
  261.  
  262. /** 
  263. * Set the User-Agent Header. 
  264. * @param string $userAgent The User-Agent. 
  265. */ 
  266. public function setUserAgent($userAgent) 
  267. $this->userAgent = $userAgent; 
  268. if ($this->canGzip) { 
  269. $this->userAgent = $userAgent . self::GZIP_UA; 
  270.  
  271. /** 
  272. * @return string The User-Agent. 
  273. */ 
  274. public function getUserAgent() 
  275. return $this->userAgent; 
  276.  
  277. /** 
  278. * Returns a cache key depending on if this was an OAuth signed request 
  279. * in which case it will use the non-signed url and access key to make this 
  280. * cache key unique per authenticated user, else use the plain request url 
  281. * @return string The md5 hash of the request cache key. 
  282. */ 
  283. public function getCacheKey() 
  284. $key = $this->getUrl(); 
  285.  
  286. if (isset($this->accessKey)) { 
  287. $key .= $this->accessKey; 
  288.  
  289. if (isset($this->requestHeaders['authorization'])) { 
  290. $key .= $this->requestHeaders['authorization']; 
  291.  
  292. return md5($key); 
  293.  
  294. public function getParsedCacheControl() 
  295. $parsed = array(); 
  296. $rawCacheControl = $this->getResponseHeader('cache-control'); 
  297. if ($rawCacheControl) { 
  298. $rawCacheControl = str_replace(', ', '&', $rawCacheControl); 
  299. parse_str($rawCacheControl, $parsed); 
  300.  
  301. return $parsed; 
  302.  
  303. /** 
  304. * @param string $id 
  305. * @return string A string representation of the HTTP Request. 
  306. */ 
  307. public function toBatchString($id) 
  308. $str = ''; 
  309. $path = parse_url($this->getUrl(), PHP_URL_PATH) . "?" . 
  310. http_build_query($this->queryParams); 
  311. $str .= $this->getRequestMethod() . ' ' . $path . " HTTP/1.1\n"; 
  312.  
  313. foreach ($this->getRequestHeaders() as $key => $val) { 
  314. $str .= $key . ': ' . $val . "\n"; 
  315.  
  316. if ($this->getPostBody()) { 
  317. $str .= "\n"; 
  318. $str .= $this->getPostBody(); 
  319.  
  320. $headers = ''; 
  321. foreach ($this->batchHeaders as $key => $val) { 
  322. $headers .= $key . ': ' . $val . "\n"; 
  323.  
  324. $headers .= "Content-ID: $id\n"; 
  325. $str = $headers . "\n" . $str; 
  326.  
  327. return $str; 
  328.  
  329. /** 
  330. * Our own version of parse_str that allows for multiple variables 
  331. * with the same name.  
  332. * @param $string - the query string to parse 
  333. */ 
  334. private function parseQuery($string) 
  335. $return = array(); 
  336. $parts = explode("&", $string); 
  337. foreach ($parts as $part) { 
  338. list($key, $value) = explode('=', $part, 2); 
  339. $value = urldecode($value); 
  340. if (isset($return[$key])) { 
  341. if (!is_array($return[$key])) { 
  342. $return[$key] = array($return[$key]); 
  343. $return[$key][] = $value; 
  344. } else { 
  345. $return[$key] = $value; 
  346. return $return; 
  347.  
  348. /** 
  349. * A version of build query that allows for multiple 
  350. * duplicate keys.  
  351. * @param $parts array of key value pairs 
  352. */ 
  353. private function buildQuery($parts) 
  354. $return = array(); 
  355. foreach ($parts as $key => $value) { 
  356. if (is_array($value)) { 
  357. foreach ($value as $v) { 
  358. $return[] = urlencode($key) . "=" . urlencode($v); 
  359. } else { 
  360. $return[] = urlencode($key) . "=" . urlencode($value); 
  361. return implode('&', $return); 
  362.  
  363. /**  
  364. * If we're POSTing and have no body to send, we can send the query 
  365. * parameters in there, which avoids length issues with longer query 
  366. * params. 
  367. */ 
  368. public function maybeMoveParametersToBody() 
  369. if ($this->getRequestMethod() == "POST" && empty($this->postBody)) { 
  370. $this->setRequestHeaders( 
  371. array( 
  372. "content-type" => 
  373. "application/x-www-form-urlencoded; charset=UTF-8" 
  374. ); 
  375. $this->setPostBody($this->buildQuery($this->queryParams)); 
  376. $this->queryParams = array();