Google_IO_Stream

The Yakadanda Google+ Hangout Events Google IO Stream class.

Defined (1)

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

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