Google_HttpStreamIO

The BestWebSoft Google Analytics Google HttpStreamIO class.

Defined (1)

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

/google-api-php-client/api-code/io/Google_HttpStreamIO.php  
  1. class Google_HttpStreamIO extends Google_IO { 
  2.  
  3. private static $ENTITY_HTTP_METHODS = array("POST" => null, "PUT" => null); 
  4.  
  5. private static $DEFAULT_HTTP_CONTEXT = array( 
  6. "follow_location" => 0,  
  7. "ignore_errors" => 1,  
  8. ); 
  9.  
  10. private static $DEFAULT_SSL_CONTEXT = array( 
  11. "verify_peer" => true,  
  12. ); 
  13.  
  14. /** 
  15. * Perform an authenticated / signed apiHttpRequest. 
  16. * This function takes the apiHttpRequest, calls apiAuth->sign on it 
  17. * (which can modify the request in what ever way fits the auth mechanism) 
  18. * and then calls Google_HttpStreamIO::makeRequest on the signed request 
  19. * @param Google_HttpRequest $request 
  20. * @return Google_HttpRequest The resulting HTTP response including the 
  21. * responseHttpCode, responseHeaders and responseBody. 
  22. */ 
  23. public function authenticatedRequest(Google_HttpRequest $request) { 
  24. $request = Google_Client::$auth->sign($request); 
  25. return $this->makeRequest($request); 
  26.  
  27. /** 
  28. * Execute a apiHttpRequest 
  29. * @param Google_HttpRequest $request the http request to be executed 
  30. * @return Google_HttpRequest http request with the response http code,  
  31. * response headers and response body filled in 
  32. * @throws Google_IOException on curl or IO error 
  33. */ 
  34. public function makeRequest(Google_HttpRequest $request) { 
  35. // First, check to see if we have a valid cached version. 
  36. $cached = $this->getCachedRequest($request); 
  37. if ($cached !== false) { 
  38. if (!$this->checkMustRevaliadateCachedRequest($cached, $request)) { 
  39. return $cached; 
  40.  
  41. $default_options = stream_context_get_options(stream_context_get_default()); 
  42.  
  43. $requestHttpContext = array_key_exists('http', $default_options) ? 
  44. $default_options['http'] : array(); 
  45. if (array_key_exists($request->getRequestMethod(),  
  46. self::$ENTITY_HTTP_METHODS)) { 
  47. $request = $this->processEntityRequest($request); 
  48.  
  49. if ($request->getPostBody()) { 
  50. $requestHttpContext["content"] = $request->getPostBody(); 
  51.  
  52. $requestHeaders = $request->getRequestHeaders(); 
  53. if ($requestHeaders && is_array($requestHeaders)) { 
  54. $headers = ""; 
  55. foreach($requestHeaders as $k => $v) { 
  56. $headers .= "$k: $v\n"; 
  57. $requestHttpContext["header"] = $headers; 
  58.  
  59. $requestHttpContext["method"] = $request->getRequestMethod(); 
  60. $requestHttpContext["user_agent"] = $request->getUserAgent(); 
  61.  
  62. $requestSslContext = array_key_exists('ssl', $default_options) ? 
  63. $default_options['ssl'] : array(); 
  64.  
  65. if (!array_key_exists("cafile", $requestSslContext)) { 
  66. $requestSslContext["cafile"] = dirname(__FILE__) . '/cacerts.pem'; 
  67.  
  68. $options = array("http" => array_merge(self::$DEFAULT_HTTP_CONTEXT,  
  69. $requestHttpContext),  
  70. "ssl" => array_merge(self::$DEFAULT_SSL_CONTEXT,  
  71. $requestSslContext)); 
  72.  
  73. $context = stream_context_create($options); 
  74.  
  75. $response_data = file_get_contents($request->getUrl(),  
  76. false,  
  77. $context); 
  78.  
  79. if (false === $response_data) { 
  80. throw new Google_IOException("HTTP Error: Unable to connect"); 
  81.  
  82. $respHttpCode = $this->getHttpResponseCode($http_response_header); 
  83. $responseHeaders = $this->getHttpResponseHeaders($http_response_header); 
  84.  
  85. if ($respHttpCode == 304 && $cached) { 
  86. // If the server responded NOT_MODIFIED, return the cached request. 
  87. $this->updateCachedRequest($cached, $responseHeaders); 
  88. return $cached; 
  89.  
  90. $request->setResponseHttpCode($respHttpCode); 
  91. $request->setResponseHeaders($responseHeaders); 
  92. $request->setResponseBody($response_data); 
  93. // Store the request in cache (the function checks to see if the request 
  94. // can actually be cached) 
  95. $this->setCachedRequest($request); 
  96. return $request; 
  97.  
  98. /** 
  99. * Set options that update the transport implementation's behavior. 
  100. * @param $options 
  101. */ 
  102. public function setOptions($options) { 
  103.  
  104. private function getHttpResponseCode($response_headers) { 
  105. $header_count = count($response_headers); 
  106.  
  107. for ($i = 0; $i < $header_count; $i++) { 
  108. $header = $response_headers[$i]; 
  109. if (strncasecmp("HTTP", $header, strlen("HTTP")) == 0) { 
  110. $response = explode(' ', $header); 
  111. return $response[1]; 
  112. return 'UNKNOWN'; 
  113.  
  114. private function getHttpResponseHeaders($response_headers) { 
  115. $header_count = count($response_headers); 
  116. $headers = array(); 
  117.  
  118. for ($i = 0; $i < $header_count; $i++) { 
  119. $header = $response_headers[$i]; 
  120. $header_parts = explode(':', $header); 
  121. if (count($header_parts) == 2) { 
  122. $headers[$header_parts[0]] = $header_parts[1]; 
  123.  
  124. return $headers;