Skip to content

Commit 792e5ec

Browse files
committed
Merge pull request googleapis#65 from ianbarber/master
Fix upload issues
2 parents 7b7030e + 02ee4e9 commit 792e5ec

File tree

6 files changed

+61
-10
lines changed

6 files changed

+61
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ See the examples/ directory for examples of the key client features.
3030

3131
## Frequently Asked Questions ##
3232

33-
### What do I do if something isn't woring? ###
33+
### What do I do if something isn't working? ###
3434

3535
For support with the library the best place to ask is via the google-api-php-client tag on StackOverflow: http://stackoverflow.com/questions/tagged/google-api-php-client
3636

src/Google/Client.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@
3535
*/
3636
class Google_Client
3737
{
38-
const LIBVER = "1.0.1-beta";
38+
const LIBVER = "1.0.2-beta";
3939
const USER_AGENT_SUFFIX = "google-api-php-client/";
40-
const GZIP_UA = " (gzip)";
4140
/**
4241
* @var Google_Auth_Abstract $auth
4342
*/
@@ -476,17 +475,14 @@ public function setDefer($defer)
476475
public function execute($request)
477476
{
478477
if ($request instanceof Google_Http_Request) {
479-
$userAgentGzipSuffix = "";
480-
if (!$this->getClassConfig("Google_Http_Request", "disable_gzip")) {
481-
$request->setRequestHeaders(array("Accept-Encoding" => "gzip"));
482-
$userAgentGzipSuffix = self::GZIP_UA;
483-
}
484478
$request->setUserAgent(
485479
$this->getApplicationName()
486480
. " " . self::USER_AGENT_SUFFIX
487481
. $this->getLibraryVersion()
488-
. $userAgentGzipSuffix
489482
);
483+
if (!$this->getClassConfig("Google_Http_Request", "disable_gzip")) {
484+
$request->enableGzip();
485+
}
490486
$request->maybeMoveParametersToBody();
491487
return Google_Http_REST::execute($this, $request);
492488
} else if ($request instanceof Google_Http_Batch) {

src/Google/Http/MediaFileUpload.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ public function nextChunk($chunk = false)
141141
$headers,
142142
$chunk
143143
);
144+
$httpRequest->disableGzip(); // Disable gzip for uploads.
144145
$response = $this->client->getIo()->makeRequest($httpRequest);
145146
$response->setExpectedClass($this->request->getExpectedClass());
146147
$code = $response->getResponseHttpCode();

src/Google/Http/Request.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
*/
2828
class Google_Http_Request
2929
{
30+
const GZIP_UA = " (gzip)";
31+
3032
private $batchHeaders = array(
3133
'Content-Type' => 'application/http',
3234
'Content-Transfer-Encoding' => 'binary',
@@ -40,6 +42,7 @@ class Google_Http_Request
4042
protected $path;
4143
protected $postBody;
4244
protected $userAgent;
45+
protected $canGzip = null;
4346

4447
protected $responseHttpCode;
4548
protected $responseHeaders;
@@ -79,6 +82,40 @@ public function setBaseComponent($baseComponent)
7982
{
8083
$this->baseComponent = $baseComponent;
8184
}
85+
86+
/**
87+
* Enable support for gzipped responses with this request.
88+
*/
89+
public function enableGzip()
90+
{
91+
$this->setRequestHeaders(array("Accept-Encoding" => "gzip"));
92+
$this->canGzip = true;
93+
$this->setUserAgent($this->userAgent);
94+
}
95+
96+
/**
97+
* Disable support for gzip responses with this request.
98+
*/
99+
public function disableGzip()
100+
{
101+
if (
102+
isset($this->requestHeaders['accept-encoding']) &&
103+
$this->requestHeaders['accept-encoding'] == "gzip"
104+
) {
105+
unset($this->requestHeaders['accept-encoding']);
106+
}
107+
$this->canGzip = false;
108+
$this->userAgent = str_replace(self::GZIP_UA, "", $this->userAgent);
109+
}
110+
111+
/**
112+
* Can this request accept a gzip response?
113+
* @return bool
114+
*/
115+
public function canGzip()
116+
{
117+
return $this->canGzip;
118+
}
82119

83120
/**
84121
* Misc function that returns an array of the query parameters of the current
@@ -297,6 +334,9 @@ public function setPostBody($postBody)
297334
public function setUserAgent($userAgent)
298335
{
299336
$this->userAgent = $userAgent;
337+
if ($this->canGzip) {
338+
$this->userAgent = $userAgent . self::GZIP_UA;
339+
}
300340
}
301341

302342
/**

src/Google/IO/Stream.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public function makeRequest(Google_Http_Request $request)
104104

105105
$url = $request->getUrl();
106106

107-
if (!$this->client->getClassConfig("Google_Http_Request", "disable_gzip")) {
107+
if ($request->canGzip()) {
108108
$url = self::ZLIB . $url;
109109
}
110110

tests/general/RequestTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,18 @@ public function testRequestParameters()
5757
$request->setBaseComponent($base . '/upload');
5858
$this->assertEquals($url4, $request->getUrl());
5959
}
60+
61+
public function testGzipSupport()
62+
{
63+
$url = 'http://localhost:8080/foo/bar?foo=a&foo=b&wowee=oh+my';
64+
$request = new Google_Http_Request($url);
65+
$request->enableGzip();
66+
$this->assertStringEndsWith(Google_Http_Request::GZIP_UA, $request->getUserAgent());
67+
$this->assertArrayHasKey('accept-encoding', $request->getRequestHeaders());
68+
$this->assertTrue($request->canGzip());
69+
$request->disableGzip();
70+
$this->assertStringEndsNotWith(Google_Http_Request::GZIP_UA, $request->getUserAgent());
71+
$this->assertArrayNotHasKey('accept-encoding', $request->getRequestHeaders());
72+
$this->assertFalse($request->canGzip());
73+
}
6074
}

0 commit comments

Comments
 (0)