PageRenderTime 47ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/wordpress-seo/vendor/yoast/api-libs/google/io/Google_HttpRequest.php

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