Skip to content

Commit 0b0ffa2

Browse files
author
Ian Barber
committed
Adding GZIP support
Automatically handle gzip responses, and transform requests to request gzip responses.
1 parent 46f1554 commit 0b0ffa2

4 files changed

Lines changed: 21 additions & 5 deletions

File tree

src/Google/Client.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class Google_Client
3737
{
3838
const LIBVER = "1.0.1-alpha";
3939
const USER_AGENT_SUFFIX = "google-api-php-client/";
40+
const GZIP_UA = " (gzip)";
4041
/**
4142
* @var Google_Auth_Abstract $auth
4243
*/
@@ -473,10 +474,16 @@ public function setDefer($defer)
473474
public function execute($request)
474475
{
475476
if ($request instanceof Google_Http_Request) {
477+
$userAgentGzipSuffix = "";
478+
if (!$this->getClassConfig("Google_Http_Request", "disable_gzip")) {
479+
$request->setRequestHeaders(array("Accept-Encoding" => "gzip"));
480+
$userAgentGzipSuffix = self::GZIP_UA;
481+
}
476482
$request->setUserAgent(
477483
$this->getApplicationName()
478484
. " " . self::USER_AGENT_SUFFIX
479485
. $this->getLibraryVersion()
486+
. $userAgentGzipSuffix
480487
);
481488
$request->maybeMoveParametersToBody();
482489
return Google_Http_REST::execute($this, $request);

src/Google/Config.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public function __construct($ini_file_location = null)
4848
'classes' => array(
4949
// If you want to pass in OAuth 2.0 settings, they will need to be
5050
// structured like this.
51+
'Google_Http_Request' => array(
52+
// Disable the use of gzip on calls if set to true.
53+
'disable_gzip' => false
54+
),
5155
'Google_Auth_OAuth2' => array(
5256
// Keys for OAuth 2.0 access, see the API console at
5357
// https://developers.google.com/console

src/Google/Http/REST.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
class Google_Http_REST
3030
{
3131
/**
32-
* Executes a apiServiceRequest using a RESTful call by transforming it into
33-
* an apiHttpRequest, and executed via apiIO::authenticatedRequest().
32+
* Executes a Google_Http_Request
3433
*
3534
* @param Google_Client $client
3635
* @param Google_Http_Request $req

src/Google/IO/Stream.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
class Google_IO_Stream extends Google_IO_Abstract
2727
{
28-
28+
const ZLIB = "compress.zlib://";
2929
private static $ENTITY_HTTP_METHODS = array("POST" => null, "PUT" => null);
3030

3131
private static $DEFAULT_HTTP_CONTEXT = array(
@@ -74,7 +74,7 @@ public function makeRequest(Google_Http_Request $request)
7474
if ($requestHeaders && is_array($requestHeaders)) {
7575
$headers = "";
7676
foreach ($requestHeaders as $k => $v) {
77-
$headers .= "$k: $v\n";
77+
$headers .= "$k: $v\r\n";
7878
}
7979
$requestHttpContext["header"] = $headers;
8080
}
@@ -101,9 +101,15 @@ public function makeRequest(Google_Http_Request $request)
101101
);
102102

103103
$context = stream_context_create($options);
104+
105+
$url = $request->getUrl();
106+
107+
if (!$this->client->getClassConfig("Google_Http_Request", "disable_gzip")) {
108+
$url = self::ZLIB . $url;
109+
}
104110

105111
$response_data = file_get_contents(
106-
$request->getUrl(),
112+
$url,
107113
false,
108114
$context
109115
);

0 commit comments

Comments
 (0)