Skip to content

Commit

Permalink
Merge pull request #65 from ianbarber/master
Browse files Browse the repository at this point in the history
Fix upload issues
  • Loading branch information
silvolu committed Jan 24, 2014
2 parents 7b7030e + 02ee4e9 commit 792e5ec
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ See the examples/ directory for examples of the key client features.

## Frequently Asked Questions ##

### What do I do if something isn't woring? ###
### What do I do if something isn't working? ###

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

Expand Down
12 changes: 4 additions & 8 deletions src/Google/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@
*/
class Google_Client
{
const LIBVER = "1.0.1-beta";
const LIBVER = "1.0.2-beta";
const USER_AGENT_SUFFIX = "google-api-php-client/";
const GZIP_UA = " (gzip)";
/**
* @var Google_Auth_Abstract $auth
*/
Expand Down Expand Up @@ -476,17 +475,14 @@ public function setDefer($defer)
public function execute($request)
{
if ($request instanceof Google_Http_Request) {
$userAgentGzipSuffix = "";
if (!$this->getClassConfig("Google_Http_Request", "disable_gzip")) {
$request->setRequestHeaders(array("Accept-Encoding" => "gzip"));
$userAgentGzipSuffix = self::GZIP_UA;
}
$request->setUserAgent(
$this->getApplicationName()
. " " . self::USER_AGENT_SUFFIX
. $this->getLibraryVersion()
. $userAgentGzipSuffix
);
if (!$this->getClassConfig("Google_Http_Request", "disable_gzip")) {
$request->enableGzip();
}
$request->maybeMoveParametersToBody();
return Google_Http_REST::execute($this, $request);
} else if ($request instanceof Google_Http_Batch) {
Expand Down
1 change: 1 addition & 0 deletions src/Google/Http/MediaFileUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ public function nextChunk($chunk = false)
$headers,
$chunk
);
$httpRequest->disableGzip(); // Disable gzip for uploads.
$response = $this->client->getIo()->makeRequest($httpRequest);
$response->setExpectedClass($this->request->getExpectedClass());
$code = $response->getResponseHttpCode();
Expand Down
40 changes: 40 additions & 0 deletions src/Google/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
*/
class Google_Http_Request
{
const GZIP_UA = " (gzip)";

private $batchHeaders = array(
'Content-Type' => 'application/http',
'Content-Transfer-Encoding' => 'binary',
Expand All @@ -40,6 +42,7 @@ class Google_Http_Request
protected $path;
protected $postBody;
protected $userAgent;
protected $canGzip = null;

protected $responseHttpCode;
protected $responseHeaders;
Expand Down Expand Up @@ -79,6 +82,40 @@ public function setBaseComponent($baseComponent)
{
$this->baseComponent = $baseComponent;
}

/**
* Enable support for gzipped responses with this request.
*/
public function enableGzip()
{
$this->setRequestHeaders(array("Accept-Encoding" => "gzip"));
$this->canGzip = true;
$this->setUserAgent($this->userAgent);
}

/**
* Disable support for gzip responses with this request.
*/
public function disableGzip()
{
if (
isset($this->requestHeaders['accept-encoding']) &&
$this->requestHeaders['accept-encoding'] == "gzip"
) {
unset($this->requestHeaders['accept-encoding']);
}
$this->canGzip = false;
$this->userAgent = str_replace(self::GZIP_UA, "", $this->userAgent);
}

/**
* Can this request accept a gzip response?
* @return bool
*/
public function canGzip()
{
return $this->canGzip;
}

/**
* Misc function that returns an array of the query parameters of the current
Expand Down Expand Up @@ -297,6 +334,9 @@ public function setPostBody($postBody)
public function setUserAgent($userAgent)
{
$this->userAgent = $userAgent;
if ($this->canGzip) {
$this->userAgent = $userAgent . self::GZIP_UA;
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Google/IO/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function makeRequest(Google_Http_Request $request)

$url = $request->getUrl();

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

Expand Down
14 changes: 14 additions & 0 deletions tests/general/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,18 @@ public function testRequestParameters()
$request->setBaseComponent($base . '/upload');
$this->assertEquals($url4, $request->getUrl());
}

public function testGzipSupport()
{
$url = 'http://localhost:8080/foo/bar?foo=a&foo=b&wowee=oh+my';
$request = new Google_Http_Request($url);
$request->enableGzip();
$this->assertStringEndsWith(Google_Http_Request::GZIP_UA, $request->getUserAgent());
$this->assertArrayHasKey('accept-encoding', $request->getRequestHeaders());
$this->assertTrue($request->canGzip());
$request->disableGzip();
$this->assertStringEndsNotWith(Google_Http_Request::GZIP_UA, $request->getUserAgent());
$this->assertArrayNotHasKey('accept-encoding', $request->getRequestHeaders());
$this->assertFalse($request->canGzip());
}
}

0 comments on commit 792e5ec

Please sign in to comment.