PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/lib/google/io/Google_HttpRequest.php

https://gitlab.com/x33n/platform
PHP | 302 lines | 155 code | 39 blank | 108 comment | 14 complexity | 6cac4e327ed8ddc73eee81727681fe01 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 Google_HttpRequest {
  26. const USER_AGENT_SUFFIX = "google-api-php-client/0.6.0";
  27. private $batchHeaders = array(
  28. 'Content-Type' => 'application/http',
  29. 'Content-Transfer-Encoding' => 'binary',
  30. 'MIME-Version' => '1.0',
  31. 'Content-Length' => ''
  32. );
  33. protected $url;
  34. protected $requestMethod;
  35. protected $requestHeaders;
  36. protected $postBody;
  37. protected $userAgent;
  38. protected $responseHttpCode;
  39. protected $responseHeaders;
  40. protected $responseBody;
  41. public $accessKey;
  42. public function __construct($url, $method = 'GET', $headers = array(), $postBody = null) {
  43. $this->setUrl($url);
  44. $this->setRequestMethod($method);
  45. $this->setRequestHeaders($headers);
  46. $this->setPostBody($postBody);
  47. if (empty($GLOBALS['googleApiConfig']['application_name'])) {
  48. $this->userAgent = self::USER_AGENT_SUFFIX;
  49. } else {
  50. $this->userAgent = $GLOBALS['googleApiConfig']['application_name'] . " " . self::USER_AGENT_SUFFIX;
  51. }
  52. }
  53. /**
  54. * Misc function that returns the base url component of the $url
  55. * used by the OAuth signing class to calculate the base string
  56. * @return string The base url component of the $url.
  57. * @see http://oauth.net/core/1.0a/#anchor13
  58. */
  59. public function getBaseUrl() {
  60. if ($pos = strpos($this->url, '?')) {
  61. return substr($this->url, 0, $pos);
  62. }
  63. return $this->url;
  64. }
  65. /**
  66. * Misc function that returns an array of the query parameters of the current
  67. * url used by the OAuth signing class to calculate the signature
  68. * @return array Query parameters in the query string.
  69. */
  70. public function getQueryParams() {
  71. if ($pos = strpos($this->url, '?')) {
  72. $queryStr = substr($this->url, $pos + 1);
  73. $params = array();
  74. parse_str($queryStr, $params);
  75. return $params;
  76. }
  77. return array();
  78. }
  79. /**
  80. * @return string HTTP Response Code.
  81. */
  82. public function getResponseHttpCode() {
  83. return (int) $this->responseHttpCode;
  84. }
  85. /**
  86. * @param int $responseHttpCode HTTP Response Code.
  87. */
  88. public function setResponseHttpCode($responseHttpCode) {
  89. $this->responseHttpCode = $responseHttpCode;
  90. }
  91. /**
  92. * @return $responseHeaders (array) HTTP Response Headers.
  93. */
  94. public function getResponseHeaders() {
  95. return $this->responseHeaders;
  96. }
  97. /**
  98. * @return string HTTP Response Body
  99. */
  100. public function getResponseBody() {
  101. return $this->responseBody;
  102. }
  103. /**
  104. * @param array $headers The HTTP response headers
  105. * to be normalized.
  106. */
  107. public function setResponseHeaders($headers) {
  108. $headers = Google_Utils::normalize($headers);
  109. if ($this->responseHeaders) {
  110. $headers = array_merge($this->responseHeaders, $headers);
  111. }
  112. $this->responseHeaders = $headers;
  113. }
  114. /**
  115. * @param string $key
  116. * @return array|boolean Returns the requested HTTP header or
  117. * false if unavailable.
  118. */
  119. public function getResponseHeader($key) {
  120. return isset($this->responseHeaders[$key])
  121. ? $this->responseHeaders[$key]
  122. : false;
  123. }
  124. /**
  125. * @param string $responseBody The HTTP response body.
  126. */
  127. public function setResponseBody($responseBody) {
  128. $this->responseBody = $responseBody;
  129. }
  130. /**
  131. * @return string $url The request URL.
  132. */
  133. public function getUrl() {
  134. return $this->url;
  135. }
  136. /**
  137. * @return string $method HTTP Request Method.
  138. */
  139. public function getRequestMethod() {
  140. return $this->requestMethod;
  141. }
  142. /**
  143. * @return array $headers HTTP Request Headers.
  144. */
  145. public function getRequestHeaders() {
  146. return $this->requestHeaders;
  147. }
  148. /**
  149. * @param string $key
  150. * @return array|boolean Returns the requested HTTP header or
  151. * false if unavailable.
  152. */
  153. public function getRequestHeader($key) {
  154. return isset($this->requestHeaders[$key])
  155. ? $this->requestHeaders[$key]
  156. : false;
  157. }
  158. /**
  159. * @return string $postBody HTTP Request Body.
  160. */
  161. public function getPostBody() {
  162. return $this->postBody;
  163. }
  164. /**
  165. * @param string $url the url to set
  166. */
  167. public function setUrl($url) {
  168. if (substr($url, 0, 4) == 'http') {
  169. $this->url = $url;
  170. } else {
  171. // Force the path become relative.
  172. if (substr($url, 0, 1) !== '/') {
  173. $url = '/' . $url;
  174. }
  175. $this->url = $GLOBALS['googleApiConfig']['basePath'] . $url;
  176. }
  177. }
  178. /**
  179. * @param string $method Set he HTTP Method and normalize
  180. * it to upper-case, as required by HTTP.
  181. *
  182. */
  183. public function setRequestMethod($method) {
  184. $this->requestMethod = strtoupper($method);
  185. }
  186. /**
  187. * @param array $headers The HTTP request headers
  188. * to be set and normalized.
  189. */
  190. public function setRequestHeaders($headers) {
  191. $headers = Google_Utils::normalize($headers);
  192. if ($this->requestHeaders) {
  193. $headers = array_merge($this->requestHeaders, $headers);
  194. }
  195. $this->requestHeaders = $headers;
  196. }
  197. /**
  198. * @param string $postBody the postBody to set
  199. */
  200. public function setPostBody($postBody) {
  201. $this->postBody = $postBody;
  202. }
  203. /**
  204. * Set the User-Agent Header.
  205. * @param string $userAgent The User-Agent.
  206. */
  207. public function setUserAgent($userAgent) {
  208. $this->userAgent = $userAgent;
  209. }
  210. /**
  211. * @return string The User-Agent.
  212. */
  213. public function getUserAgent() {
  214. return $this->userAgent;
  215. }
  216. /**
  217. * Returns a cache key depending on if this was an OAuth signed request
  218. * in which case it will use the non-signed url and access key to make this
  219. * cache key unique per authenticated user, else use the plain request url
  220. * @return string The md5 hash of the request cache key.
  221. */
  222. public function getCacheKey() {
  223. $key = $this->getUrl();
  224. if (isset($this->accessKey)) {
  225. $key .= $this->accessKey;
  226. }
  227. if (isset($this->requestHeaders['authorization'])) {
  228. $key .= $this->requestHeaders['authorization'];
  229. }
  230. return md5($key);
  231. }
  232. public function getParsedCacheControl() {
  233. $parsed = array();
  234. $rawCacheControl = $this->getResponseHeader('cache-control');
  235. if ($rawCacheControl) {
  236. $rawCacheControl = str_replace(', ', '&', $rawCacheControl);
  237. parse_str($rawCacheControl, $parsed);
  238. }
  239. return $parsed;
  240. }
  241. /**
  242. * @param string $id
  243. * @return string A string representation of the HTTP Request.
  244. */
  245. public function toBatchString($id) {
  246. $str = '';
  247. foreach($this->batchHeaders as $key => $val) {
  248. $str .= $key . ': ' . $val . "\n";
  249. }
  250. $str .= "Content-ID: $id\n";
  251. $str .= "\n";
  252. $path = parse_url($this->getUrl(), PHP_URL_PATH);
  253. $str .= $this->getRequestMethod() . ' ' . $path . " HTTP/1.1\n";
  254. foreach($this->getRequestHeaders() as $key => $val) {
  255. $str .= $key . ': ' . $val . "\n";
  256. }
  257. if ($this->getPostBody()) {
  258. $str .= "\n";
  259. $str .= $this->getPostBody();
  260. }
  261. return $str;
  262. }
  263. }