/google-api-php-client/src/io/apiHttpRequest.php

https://bitbucket.org/baddog/google-latitude-history · PHP · 262 lines · 125 code · 34 blank · 103 comment · 9 complexity · 0bdee921f6a8c44ed7962d07ea40a35a MD5 · raw file

  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. * HTTP Request to be executed by apiIO classes. Upon execution, the
  19. * responseHttpCode, responseHeaders and responseBody will be filled in.
  20. *
  21. * @author Chris Chabot <chabotc@google.com>
  22. * @author Chirag Shah <chirags@google.com>
  23. *
  24. */
  25. class apiHttpRequest {
  26. const USER_AGENT_SUFFIX = "google-api-php-client/0.4.8";
  27. protected $url;
  28. protected $requestMethod;
  29. protected $requestHeaders;
  30. protected $postBody;
  31. protected $userAgent;
  32. protected $responseHttpCode;
  33. protected $responseHeaders;
  34. protected $responseBody;
  35. public $accessKey;
  36. public function __construct($url, $method = 'GET', $headers = array(), $postBody = null) {
  37. $this->url = $url;
  38. $this->setRequestMethod($method);
  39. $this->setRequestHeaders($headers);
  40. $this->setPostBody($postBody);
  41. global $apiConfig;
  42. if (empty($apiConfig['application_name'])) {
  43. $this->userAgent = self::USER_AGENT_SUFFIX;
  44. } else {
  45. $this->userAgent = $apiConfig['application_name'] . " " . self::USER_AGENT_SUFFIX;
  46. }
  47. }
  48. /**
  49. * Misc function that returns the base url component of the $url
  50. * used by the OAuth signing class to calculate the base string
  51. * @return string The base url component of the $url.
  52. * @see http://oauth.net/core/1.0a/#anchor13
  53. */
  54. public function getBaseUrl() {
  55. if ($pos = strpos($this->url, '?')) {
  56. return substr($this->url, 0, $pos);
  57. }
  58. return $this->url;
  59. }
  60. /**
  61. * Misc function that returns an array of the query parameters of the current
  62. * url used by the OAuth signing class to calculate the signature
  63. * @return array Query parameters in the query string.
  64. */
  65. public function getQueryParams() {
  66. if ($pos = strpos($this->url, '?')) {
  67. $queryStr = substr($this->url, $pos + 1);
  68. $params = array();
  69. parse_str($queryStr, $params);
  70. return $params;
  71. }
  72. return array();
  73. }
  74. /**
  75. * @return string HTTP Response Code.
  76. */
  77. public function getResponseHttpCode() {
  78. return (int) $this->responseHttpCode;
  79. }
  80. /**
  81. * @param int $responseHttpCode HTTP Response Code.
  82. */
  83. public function setResponseHttpCode($responseHttpCode) {
  84. $this->responseHttpCode = $responseHttpCode;
  85. }
  86. /**
  87. * @return $responseHeaders (array) HTTP Response Headers.
  88. */
  89. public function getResponseHeaders() {
  90. return $this->responseHeaders;
  91. }
  92. /**
  93. * @return string HTTP Response Body
  94. */
  95. public function getResponseBody() {
  96. return $this->responseBody;
  97. }
  98. /**
  99. * @param array $headers The HTTP response headers
  100. * to be normalized.
  101. */
  102. public function setResponseHeaders($headers) {
  103. $headers = apiUtils::normalize($headers);
  104. if ($this->responseHeaders) {
  105. $headers = array_merge($this->responseHeaders, $headers);
  106. }
  107. $this->responseHeaders = $headers;
  108. }
  109. /**
  110. * @param string $key
  111. * @return array|boolean Returns the requested HTTP header or
  112. * false if unavailable.
  113. */
  114. public function getResponseHeader($key) {
  115. return isset($this->responseHeaders[$key])
  116. ? $this->responseHeaders[$key]
  117. : false;
  118. }
  119. /**
  120. * @param string $responseBody The HTTP response body.
  121. */
  122. public function setResponseBody($responseBody) {
  123. $this->responseBody = $responseBody;
  124. }
  125. /**
  126. * @return string $url The request URL.
  127. */
  128. public function getUrl() {
  129. return $this->url;
  130. }
  131. /**
  132. * @return string $method HTTP Request Method.
  133. */
  134. public function getRequestMethod() {
  135. return $this->requestMethod;
  136. }
  137. /**
  138. * @return array $headers HTTP Request Headers.
  139. */
  140. public function getRequestHeaders() {
  141. return $this->requestHeaders;
  142. }
  143. /**
  144. * @param string $key
  145. * @return array|boolean Returns the requested HTTP header or
  146. * false if unavailable.
  147. */
  148. public function getRequestHeader($key) {
  149. return isset($this->requestHeaders[$key])
  150. ? $this->requestHeaders[$key]
  151. : false;
  152. }
  153. /**
  154. * @return string $postBody HTTP Request Body.
  155. */
  156. public function getPostBody() {
  157. return $this->postBody;
  158. }
  159. /**
  160. * @param string $url the url to set
  161. */
  162. public function setUrl($url) {
  163. $this->url = $url;
  164. }
  165. /**
  166. * @param string $method Set he HTTP Method and normalize
  167. * it to upper-case, as required by HTTP.
  168. *
  169. */
  170. public function setRequestMethod($method) {
  171. $this->requestMethod = strtoupper($method);
  172. }
  173. /**
  174. * @param array $headers The HTTP request headers
  175. * to be set and normalized.
  176. */
  177. public function setRequestHeaders($headers) {
  178. $headers = apiUtils::normalize($headers);
  179. if ($this->requestHeaders) {
  180. $headers = array_merge($this->requestHeaders, $headers);
  181. }
  182. $this->requestHeaders = $headers;
  183. }
  184. /**
  185. * @param string $postBody the postBody to set
  186. */
  187. public function setPostBody($postBody) {
  188. $this->postBody = $postBody;
  189. }
  190. /**
  191. * Set the User-Agent Header.
  192. * @param string $userAgent The User-Agent.
  193. */
  194. public function setUserAgent($userAgent) {
  195. $this->userAgent = $userAgent;
  196. }
  197. /**
  198. * @return string The User-Agent.
  199. */
  200. public function getUserAgent() {
  201. return $this->userAgent;
  202. }
  203. /**
  204. * Returns a cache key depending on if this was an OAuth signed request
  205. * in which case it will use the non-signed url and access key to make this
  206. * cache key unique per authenticated user, else use the plain request url
  207. * @return The md5 hash of the request cache key.
  208. */
  209. public function getCacheKey() {
  210. $key = $this->getUrl();
  211. if (isset($this->accessKey)) {
  212. $key .= $this->accessKey;
  213. }
  214. if (isset($this->requestHeaders['authorization'])) {
  215. $key .= $this->requestHeaders['authorization'];
  216. }
  217. return md5($key);
  218. }
  219. public function getParsedCacheControl() {
  220. $parsed = array();
  221. $rawCacheControl = $this->getResponseHeader('cache-control');
  222. if ($rawCacheControl) {
  223. $rawCacheControl = str_replace(", ", "&", $rawCacheControl);
  224. parse_str($rawCacheControl, $parsed);
  225. }
  226. return $parsed;
  227. }
  228. }