Google_IO_Stream

The Simple Calendar Google IO Stream class.

Defined (1)

The class is defined in the following location(s).

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