Google_IO_Stream

The SZ - Google Google IO Stream class.

Defined (1)

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

/lib/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.  
  6. private static $DEFAULT_HTTP_CONTEXT = array( 
  7. "follow_location" => 0,  
  8. "ignore_errors" => 1,  
  9. ); 
  10.  
  11. private static $DEFAULT_SSL_CONTEXT = array( 
  12. "verify_peer" => true,  
  13. ); 
  14.  
  15. /** 
  16. * Execute an HTTP Request 
  17. * @param Google_HttpRequest $request the http request to be executed 
  18. * @return Google_HttpRequest http request with the response http code,  
  19. * response headers and response body filled in 
  20. * @throws Google_IO_Exception on curl or IO error 
  21. */ 
  22. public function executeRequest(Google_Http_Request $request) 
  23. $default_options = stream_context_get_options(stream_context_get_default()); 
  24.  
  25. $requestHttpContext = array_key_exists('http', $default_options) ? 
  26. $default_options['http'] : array(); 
  27.  
  28. if ($request->getPostBody()) { 
  29. $requestHttpContext["content"] = $request->getPostBody(); 
  30.  
  31. $requestHeaders = $request->getRequestHeaders(); 
  32. if ($requestHeaders && is_array($requestHeaders)) { 
  33. $headers = ""; 
  34. foreach ($requestHeaders as $k => $v) { 
  35. $headers .= "$k: $v\r\n"; 
  36. $requestHttpContext["header"] = $headers; 
  37.  
  38. $requestHttpContext["method"] = $request->getRequestMethod(); 
  39. $requestHttpContext["user_agent"] = $request->getUserAgent(); 
  40.  
  41. $requestSslContext = array_key_exists('ssl', $default_options) ? 
  42. $default_options['ssl'] : array(); 
  43.  
  44. if (!array_key_exists("cafile", $requestSslContext)) { 
  45. $requestSslContext["cafile"] = dirname(__FILE__) . '/cacerts.pem'; 
  46.  
  47. $options = array( 
  48. "http" => array_merge( 
  49. self::$DEFAULT_HTTP_CONTEXT,  
  50. $requestHttpContext 
  51. ),  
  52. "ssl" => array_merge( 
  53. self::$DEFAULT_SSL_CONTEXT,  
  54. $requestSslContext 
  55. ); 
  56.  
  57. $context = stream_context_create($options); 
  58.  
  59. $url = $request->getUrl(); 
  60.  
  61. if ($request->canGzip()) { 
  62. $url = self::ZLIB . $url; 
  63.  
  64. // Not entirely happy about this, but supressing the warning from the 
  65. // fopen seems like the best situation here - we can't do anything 
  66. // useful with it, and failure to connect is a legitimate run 
  67. // time situation. 
  68. @$fh = fopen($url, 'r', false, $context); 
  69.  
  70. $response_data = false; 
  71. $respHttpCode = self::UNKNOWN_CODE; 
  72. if ($fh) { 
  73. if (isset($this->options[self::TIMEOUT])) { 
  74. stream_set_timeout($fh, $this->options[self::TIMEOUT]); 
  75.  
  76. $response_data = stream_get_contents($fh); 
  77. fclose($fh); 
  78.  
  79. $respHttpCode = $this->getHttpResponseCode($http_response_header); 
  80.  
  81. if (false === $response_data) { 
  82. throw new Google_IO_Exception( 
  83. sprintf( 
  84. "HTTP Error: Unable to connect: '%s'",  
  85. $respHttpCode 
  86. ),  
  87. $respHttpCode 
  88. ); 
  89.  
  90. $responseHeaders = $this->getHttpResponseHeaders($http_response_header); 
  91.  
  92. return array($response_data, $responseHeaders, $respHttpCode); 
  93.  
  94. /** 
  95. * Set options that update the transport implementation's behavior. 
  96. * @param $options 
  97. */ 
  98. public function setOptions($options) 
  99. $this->options = $options + $this->options; 
  100.  
  101. /** 
  102. * Set the maximum request time in seconds. 
  103. * @param $timeout in seconds 
  104. */ 
  105. public function setTimeout($timeout) 
  106. $this->options[self::TIMEOUT] = $timeout; 
  107.  
  108. /** 
  109. * Get the maximum request time in seconds. 
  110. * @return timeout in seconds 
  111. */ 
  112. public function getTimeout() 
  113. return $this->options[self::TIMEOUT]; 
  114.  
  115. /** 
  116. * Test for the presence of a cURL header processing bug 
  117. * {@inheritDoc} 
  118. * @return boolean 
  119. */ 
  120. protected function needsQuirk() 
  121. return false; 
  122.  
  123. protected function getHttpResponseCode($response_headers) 
  124. $header_count = count($response_headers); 
  125.  
  126. for ($i = 0; $i < $header_count; $i++) { 
  127. $header = $response_headers[$i]; 
  128. if (strncasecmp("HTTP", $header, strlen("HTTP")) == 0) { 
  129. $response = explode(' ', $header); 
  130. return $response[1]; 
  131. return self::UNKNOWN_CODE;