Google_IO

Abstract IO class.

Defined (1)

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

/gdwpm-api/io/Google_IO.php  
  1. abstract class Google_IO { 
  2. const CONNECTION_ESTABLISHED = "HTTP/1.0 200 Connection established\r\n\r\n"; 
  3. const FORM_URLENCODED = 'application/x-www-form-urlencoded'; 
  4. /** 
  5. * An utility function that first calls $this->auth->sign($request) and then executes makeRequest() 
  6. * on that signed request. Used for when a request should be authenticated 
  7. * @param Google_HttpRequest $request 
  8. * @return Google_HttpRequest $request 
  9. */ 
  10. abstract function authenticatedRequest(Google_HttpRequest $request); 
  11.  
  12. /** 
  13. * Executes a apIHttpRequest and returns the resulting populated httpRequest 
  14. * @param Google_HttpRequest $request 
  15. * @return Google_HttpRequest $request 
  16. */ 
  17. abstract function makeRequest(Google_HttpRequest $request); 
  18.  
  19. /** 
  20. * Set options that update the transport implementation's behavior. 
  21. * @param $options 
  22. */ 
  23. abstract function setOptions($options); 
  24.  
  25. /** 
  26. * @visible for testing. 
  27. * Cache the response to an HTTP request if it is cacheable. 
  28. * @param Google_HttpRequest $request 
  29. * @return bool Returns true if the insertion was successful. 
  30. * Otherwise, return false. 
  31. */ 
  32. protected function setCachedRequest(Google_HttpRequest $request) { 
  33. // Determine if the request is cacheable. 
  34. if (Google_CacheParser::isResponseCacheable($request)) { 
  35. Google_Client::$cache->set($request->getCacheKey(), $request); 
  36. return true; 
  37.  
  38. return false; 
  39.  
  40. /** 
  41. * @visible for testing. 
  42. * @param Google_HttpRequest $request 
  43. * @return Google_HttpRequest|bool Returns the cached object or 
  44. * false if the operation was unsuccessful. 
  45. */ 
  46. protected function getCachedRequest(Google_HttpRequest $request) { 
  47. if (false == Google_CacheParser::isRequestCacheable($request)) { 
  48. false; 
  49.  
  50. return Google_Client::$cache->get($request->getCacheKey()); 
  51.  
  52. /** 
  53. * @visible for testing 
  54. * Process an http request that contains an enclosed entity. 
  55. * @param Google_HttpRequest $request 
  56. * @return Google_HttpRequest Processed request with the enclosed entity. 
  57. */ 
  58. protected function processEntityRequest(Google_HttpRequest $request) { 
  59. $postBody = $request->getPostBody(); 
  60. $contentType = $request->getRequestHeader("content-type"); 
  61.  
  62. // Set the default content-type as application/x-www-form-urlencoded. 
  63. if (false == $contentType) { 
  64. $contentType = self::FORM_URLENCODED; 
  65. $request->setRequestHeaders(array('content-type' => $contentType)); 
  66.  
  67. // Force the payload to match the content-type asserted in the header. 
  68. if ($contentType == self::FORM_URLENCODED && is_array($postBody)) { 
  69. $postBody = http_build_query($postBody, '', '&'); 
  70. $request->setPostBody($postBody); 
  71.  
  72. // Make sure the content-length header is set. 
  73. if (!$postBody || is_string($postBody)) { 
  74. $postsLength = strlen($postBody); 
  75. $request->setRequestHeaders(array('content-length' => $postsLength)); 
  76.  
  77. return $request; 
  78.  
  79. /** 
  80. * Check if an already cached request must be revalidated, and if so update 
  81. * the request with the correct ETag headers. 
  82. * @param Google_HttpRequest $cached A previously cached response. 
  83. * @param Google_HttpRequest $request The outbound request. 
  84. * return bool If the cached object needs to be revalidated, false if it is 
  85. * still current and can be re-used. 
  86. */ 
  87. protected function checkMustRevaliadateCachedRequest($cached, $request) { 
  88. if (Google_CacheParser::mustRevalidate($cached)) { 
  89. $addHeaders = array(); 
  90. if ($cached->getResponseHeader('etag')) { 
  91. // [13.3.4] If an entity tag has been provided by the origin server,  
  92. // we must use that entity tag in any cache-conditional request. 
  93. $addHeaders['If-None-Match'] = $cached->getResponseHeader('etag'); 
  94. } elseif ($cached->getResponseHeader('date')) { 
  95. $addHeaders['If-Modified-Since'] = $cached->getResponseHeader('date'); 
  96.  
  97. $request->setRequestHeaders($addHeaders); 
  98. return true; 
  99. } else { 
  100. return false; 
  101.  
  102. /** 
  103. * Update a cached request, using the headers from the last response. 
  104. * @param Google_HttpRequest $cached A previously cached response. 
  105. * @param mixed Associative array of response headers from the last request. 
  106. */ 
  107. protected function updateCachedRequest($cached, $responseHeaders) { 
  108. if (isset($responseHeaders['connection'])) { 
  109. $hopByHop = array_merge( 
  110. self::$HOP_BY_HOP,  
  111. explode(', ', $responseHeaders['connection']) 
  112. ); 
  113.  
  114. $endToEnd = array(); 
  115. foreach($hopByHop as $key) { 
  116. if (isset($responseHeaders[$key])) { 
  117. $endToEnd[$key] = $responseHeaders[$key]; 
  118. $cached->setResponseHeaders($endToEnd);