PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/apps/files_external/3rdparty/google-api-php-client/src/Google/IO/Stream.php

https://gitlab.com/wuhang2003/core
PHP | 211 lines | 124 code | 32 blank | 55 comment | 12 complexity | 4a960f03e6c1dccf913a31662ab7e434 MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright 2013 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 Streams based implementation of Google_IO.
  19. *
  20. * @author Stuart Langley <slangley@google.com>
  21. */
  22. require_once 'Google/IO/Abstract.php';
  23. class Google_IO_Stream extends Google_IO_Abstract
  24. {
  25. const TIMEOUT = "timeout";
  26. const ZLIB = "compress.zlib://";
  27. private $options = array();
  28. private $trappedErrorNumber;
  29. private $trappedErrorString;
  30. private static $DEFAULT_HTTP_CONTEXT = array(
  31. "follow_location" => 0,
  32. "ignore_errors" => 1,
  33. );
  34. private static $DEFAULT_SSL_CONTEXT = array(
  35. "verify_peer" => true,
  36. );
  37. /**
  38. * Execute an HTTP Request
  39. *
  40. * @param Google_HttpRequest $request the http request to be executed
  41. * @return Google_HttpRequest http request with the response http code,
  42. * response headers and response body filled in
  43. * @throws Google_IO_Exception on curl or IO error
  44. */
  45. public function executeRequest(Google_Http_Request $request)
  46. {
  47. $default_options = stream_context_get_options(stream_context_get_default());
  48. $requestHttpContext = array_key_exists('http', $default_options) ?
  49. $default_options['http'] : array();
  50. if ($request->getPostBody()) {
  51. $requestHttpContext["content"] = $request->getPostBody();
  52. }
  53. $requestHeaders = $request->getRequestHeaders();
  54. if ($requestHeaders && is_array($requestHeaders)) {
  55. $headers = "";
  56. foreach ($requestHeaders as $k => $v) {
  57. $headers .= "$k: $v\r\n";
  58. }
  59. $requestHttpContext["header"] = $headers;
  60. }
  61. $requestHttpContext["method"] = $request->getRequestMethod();
  62. $requestHttpContext["user_agent"] = $request->getUserAgent();
  63. $requestSslContext = array_key_exists('ssl', $default_options) ?
  64. $default_options['ssl'] : array();
  65. if (!array_key_exists("cafile", $requestSslContext)) {
  66. $requestSslContext["cafile"] = dirname(__FILE__) . '/cacerts.pem';
  67. }
  68. $options = array(
  69. "http" => array_merge(
  70. self::$DEFAULT_HTTP_CONTEXT,
  71. $requestHttpContext
  72. ),
  73. "ssl" => array_merge(
  74. self::$DEFAULT_SSL_CONTEXT,
  75. $requestSslContext
  76. )
  77. );
  78. $context = stream_context_create($options);
  79. $url = $request->getUrl();
  80. if ($request->canGzip()) {
  81. $url = self::ZLIB . $url;
  82. }
  83. // We are trapping any thrown errors in this method only and
  84. // throwing an exception.
  85. $this->trappedErrorNumber = null;
  86. $this->trappedErrorString = null;
  87. // START - error trap.
  88. set_error_handler(array($this, 'trapError'));
  89. $fh = fopen($url, 'r', false, $context);
  90. restore_error_handler();
  91. // END - error trap.
  92. if ($this->trappedErrorNumber) {
  93. throw new Google_IO_Exception(
  94. sprintf(
  95. "HTTP Error: Unable to connect: '%s'",
  96. $this->trappedErrorString
  97. ),
  98. $this->trappedErrorNumber
  99. );
  100. }
  101. $response_data = false;
  102. $respHttpCode = self::UNKNOWN_CODE;
  103. if ($fh) {
  104. if (isset($this->options[self::TIMEOUT])) {
  105. stream_set_timeout($fh, $this->options[self::TIMEOUT]);
  106. }
  107. $response_data = stream_get_contents($fh);
  108. fclose($fh);
  109. $respHttpCode = $this->getHttpResponseCode($http_response_header);
  110. }
  111. if (false === $response_data) {
  112. throw new Google_IO_Exception(
  113. sprintf(
  114. "HTTP Error: Unable to connect: '%s'",
  115. $respHttpCode
  116. ),
  117. $respHttpCode
  118. );
  119. }
  120. $responseHeaders = $this->getHttpResponseHeaders($http_response_header);
  121. return array($response_data, $responseHeaders, $respHttpCode);
  122. }
  123. /**
  124. * Set options that update the transport implementation's behavior.
  125. * @param $options
  126. */
  127. public function setOptions($options)
  128. {
  129. $this->options = $options + $this->options;
  130. }
  131. /**
  132. * Method to handle errors, used for error handling around
  133. * stream connection methods.
  134. */
  135. public function trapError($errno, $errstr)
  136. {
  137. $this->trappedErrorNumber = $errno;
  138. $this->trappedErrorString = $errstr;
  139. }
  140. /**
  141. * Set the maximum request time in seconds.
  142. * @param $timeout in seconds
  143. */
  144. public function setTimeout($timeout)
  145. {
  146. $this->options[self::TIMEOUT] = $timeout;
  147. }
  148. /**
  149. * Get the maximum request time in seconds.
  150. * @return timeout in seconds
  151. */
  152. public function getTimeout()
  153. {
  154. return $this->options[self::TIMEOUT];
  155. }
  156. /**
  157. * Test for the presence of a cURL header processing bug
  158. *
  159. * {@inheritDoc}
  160. *
  161. * @return boolean
  162. */
  163. protected function needsQuirk()
  164. {
  165. return false;
  166. }
  167. protected function getHttpResponseCode($response_headers)
  168. {
  169. $header_count = count($response_headers);
  170. for ($i = 0; $i < $header_count; $i++) {
  171. $header = $response_headers[$i];
  172. if (strncasecmp("HTTP", $header, strlen("HTTP")) == 0) {
  173. $response = explode(' ', $header);
  174. return $response[1];
  175. }
  176. }
  177. return self::UNKNOWN_CODE;
  178. }
  179. }