Google_IO_Stream

The Google Photos & Picasa Viewer Google IO Stream class.

Defined (1)

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

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