PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/backwpup/sdk/WindowsAzure/Common/Internal/Http/HttpClient.php

https://bitbucket.org/cesarmedrano/cesarmedrano
PHP | 387 lines | 158 code | 41 blank | 188 comment | 15 complexity | e72d7d8aa539a82504aef595073b4101 MD5 | raw file
  1. <?php
  2. /**
  3. * LICENSE: Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. *
  14. * PHP version 5
  15. *
  16. * @category Microsoft
  17. * @package WindowsAzure\Common\Internal\Http
  18. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  19. * @copyright 2012 Microsoft Corporation
  20. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  21. * @link https://github.com/windowsazure/azure-sdk-for-php
  22. */
  23. namespace WindowsAzure\Common\Internal\Http;
  24. use WindowsAzure\Common\Internal\Http\IHttpClient;
  25. use WindowsAzure\Common\Internal\IServiceFilter;
  26. use WindowsAzure\Common\Internal\Resources;
  27. use WindowsAzure\Common\ServiceException;
  28. use WindowsAzure\Common\Internal\Validate;
  29. use WindowsAzure\Common\Internal\Http\IUrl;
  30. require_once 'HTTP/Request2.php';
  31. /**
  32. * HTTP client which sends and receives HTTP requests and responses.
  33. *
  34. * @category Microsoft
  35. * @package WindowsAzure\Common\Internal\Http
  36. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  37. * @copyright 2012 Microsoft Corporation
  38. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  39. * @version Release: @package_version@
  40. * @link https://github.com/windowsazure/azure-sdk-for-php
  41. */
  42. class HttpClient implements IHttpClient
  43. {
  44. /**
  45. * @var \HTTP_Request2
  46. */
  47. private $_request;
  48. /**
  49. * @var WindowsAzure\Common\Internal\Http\IUrl
  50. */
  51. private $_requestUrl;
  52. /**
  53. * Holds the latest response object
  54. *
  55. * @var \HTTP_Request2_Response
  56. */
  57. private $_response;
  58. /**
  59. * Holds expected status code after sending the request.
  60. *
  61. * @var array
  62. */
  63. private $_expectedStatusCodes;
  64. /**
  65. * Initializes new HttpClient object.
  66. *
  67. * @param string $certificatePath The certificate path.
  68. * @param string $certificateAuthorityPath The path of the certificate authority.
  69. *
  70. * @return WindowsAzure\Common\Internal\Http\HttpClient
  71. */
  72. function __construct(
  73. $certificatePath = Resources::EMPTY_STRING,
  74. $certificateAuthorityPath = Resources::EMPTY_STRING
  75. ) {
  76. $config = array(
  77. Resources::USE_BRACKETS => true,
  78. Resources::SSL_VERIFY_PEER => false,
  79. Resources::SSL_VERIFY_HOST => false
  80. );
  81. if (!empty($certificatePath)) {
  82. $config[Resources::SSL_LOCAL_CERT] = $certificatePath;
  83. $config[Resources::SSL_VERIFY_HOST] = true;
  84. }
  85. if (!empty($certificateAuthorityPath)) {
  86. $config[Resources::SSL_CAFILE] = $certificateAuthorityPath;
  87. $config[Resources::SSL_VERIFY_PEER] = true;
  88. }
  89. $this->_request = new \HTTP_Request2(
  90. null, null, $config
  91. );
  92. $this->setHeader('user-agent', null);
  93. $this->_requestUrl = null;
  94. $this->_response = null;
  95. $this->_expectedStatusCodes = array();
  96. }
  97. /**
  98. * Makes deep copy from the current object.
  99. *
  100. * @return WindowsAzure\Common\Internal\Http\HttpClient
  101. */
  102. public function __clone()
  103. {
  104. $this->_request = clone $this->_request;
  105. if (!is_null($this->_requestUrl)) {
  106. $this->_requestUrl = clone $this->_requestUrl;
  107. }
  108. }
  109. /**
  110. * Sets the request url.
  111. *
  112. * @param WindowsAzure\Common\Internal\Http\IUrl $url request url.
  113. *
  114. * @return none.
  115. */
  116. public function setUrl($url)
  117. {
  118. $this->_requestUrl = $url;
  119. }
  120. /**
  121. * Gets request url. Note that you must check if the returned object is null or
  122. * not.
  123. *
  124. * @return WindowsAzure\Common\Internal\Http\IUrl
  125. */
  126. public function getUrl()
  127. {
  128. return $this->_requestUrl;
  129. }
  130. /**
  131. * Sets request's HTTP method. You can use \HTTP_Request2 constants like
  132. * Resources::HTTP_GET or strings like 'GET'.
  133. *
  134. * @param string $method request's HTTP method.
  135. *
  136. * @return none
  137. */
  138. public function setMethod($method)
  139. {
  140. $this->_request->setMethod($method);
  141. }
  142. /**
  143. * Gets request's HTTP method.
  144. *
  145. * @return string
  146. */
  147. public function getMethod()
  148. {
  149. return $this->_request->getMethod();
  150. }
  151. /**
  152. * Gets request's headers. The returned array key (header names) are all in
  153. * lower case even if they were set having some upper letters.
  154. *
  155. * @return array
  156. */
  157. public function getHeaders()
  158. {
  159. return $this->_request->getHeaders();
  160. }
  161. /**
  162. * Sets a an existing request header to value or creates a new one if the $header
  163. * doesn't exist.
  164. *
  165. * @param string $header header name.
  166. * @param string $value header value.
  167. * @param bool $replace whether to replace previous header with the same name
  168. * or append to its value (comma separated)
  169. *
  170. * @return none
  171. */
  172. public function setHeader($header, $value, $replace = false)
  173. {
  174. Validate::isString($value, 'value');
  175. $this->_request->setHeader($header, $value, $replace);
  176. }
  177. /**
  178. * Sets request headers using array
  179. *
  180. * @param array $headers headers key-value array
  181. *
  182. * @return none
  183. */
  184. public function setHeaders($headers)
  185. {
  186. foreach ($headers as $key => $value) {
  187. $this->setHeader($key, $value);
  188. }
  189. }
  190. /**
  191. * Sets HTTP POST parameters.
  192. *
  193. * @param array $postParameters The HTTP POST parameters.
  194. *
  195. * @return none
  196. */
  197. public function setPostParameters($postParameters)
  198. {
  199. $this->_request->addPostParameter($postParameters);
  200. }
  201. /**
  202. * Processes the reuqest through HTTP pipeline with passed $filters,
  203. * sends HTTP request to the wire and process the response in the HTTP pipeline.
  204. *
  205. * @param array $filters HTTP filters which will be applied to the request before
  206. * send and then applied to the response.
  207. * @param IUrl $url Request url.
  208. *
  209. * @throws WindowsAzure\Common\ServiceException
  210. *
  211. * @return string The response body
  212. */
  213. public function send($filters, $url = null)
  214. {
  215. if (isset($url)) {
  216. $this->setUrl($url);
  217. $this->_request->setUrl($this->_requestUrl->getUrl());
  218. }
  219. $contentLength = Resources::EMPTY_STRING;
  220. if ( strtoupper($this->getMethod()) != Resources::HTTP_GET
  221. && strtoupper($this->getMethod()) != Resources::HTTP_DELETE
  222. && strtoupper($this->getMethod()) != Resources::HTTP_HEAD
  223. ) {
  224. $contentLength = 0;
  225. if (!is_null($this->getBody())) {
  226. $contentLength = strlen($this->getBody());
  227. }
  228. $this->_request->setHeader(Resources::CONTENT_LENGTH, $contentLength);
  229. }
  230. foreach ($filters as $filter) {
  231. $this->_request = $filter->handleRequest($this)->_request;
  232. }
  233. $this->_response = $this->_request->send();
  234. $start = count($filters) - 1;
  235. for ($index = $start; $index >= 0; $index--) {
  236. $this->_response = $filters[$index]->handleResponse(
  237. $this, $this->_response
  238. );
  239. }
  240. self::throwIfError(
  241. $this->_response->getStatus(),
  242. $this->_response->getReasonPhrase(),
  243. $this->_response->getBody(),
  244. $this->_expectedStatusCodes
  245. );
  246. return $this->_response->getBody();
  247. }
  248. /**
  249. * Sets successful status code
  250. *
  251. * @param array|string $statusCodes successful status code.
  252. *
  253. * @return none
  254. */
  255. public function setExpectedStatusCode($statusCodes)
  256. {
  257. if (!is_array($statusCodes)) {
  258. $this->_expectedStatusCodes[] = $statusCodes;
  259. } else {
  260. $this->_expectedStatusCodes = $statusCodes;
  261. }
  262. }
  263. /**
  264. * Gets successful status code
  265. *
  266. * @return array
  267. */
  268. public function getSuccessfulStatusCode()
  269. {
  270. return $this->_expectedStatusCodes;
  271. }
  272. /**
  273. * Sets configuration parameter.
  274. *
  275. * @param string $name The configuration parameter name.
  276. * @param mix $value The configuration parameter value.
  277. *
  278. * @return none
  279. */
  280. public function setConfig($name, $value = null)
  281. {
  282. $this->_request->setConfig($name, $value);
  283. }
  284. /**
  285. * Gets value for configuration parameter.
  286. *
  287. * @param string $name configuration parameter name.
  288. *
  289. * @return string
  290. */
  291. public function getConfig($name)
  292. {
  293. return $this->_request->getConfig($name);
  294. }
  295. /**
  296. * Sets the request body.
  297. *
  298. * @param string $body body to use.
  299. *
  300. * @return none
  301. */
  302. public function setBody($body)
  303. {
  304. Validate::isString($body, 'body');
  305. $this->_request->setBody($body);
  306. }
  307. /**
  308. * Gets the request body.
  309. *
  310. * @return string
  311. */
  312. public function getBody()
  313. {
  314. return $this->_request->getBody();
  315. }
  316. /**
  317. * Gets the response object.
  318. *
  319. * @return \HTTP_Request2_Response
  320. */
  321. public function getResponse()
  322. {
  323. return $this->_response;
  324. }
  325. /**
  326. * Throws ServiceException if the recieved status code is not expected.
  327. *
  328. * @param string $actual The received status code.
  329. * @param string $reason The reason phrase.
  330. * @param string $message The detailed message (if any).
  331. * @param array $expected The expected status codes.
  332. *
  333. * @return none
  334. *
  335. * @static
  336. *
  337. * @throws ServiceException
  338. */
  339. public static function throwIfError($actual, $reason, $message, $expected)
  340. {
  341. if (!in_array($actual, $expected)) {
  342. throw new ServiceException($actual, $reason, $message);
  343. }
  344. }
  345. }
  346. ?>