Google_HttpRequest

HTTP Request to be executed by apiIO classes.

Defined (1)

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

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