Skip to content

Commit 600050a

Browse files
committed
Merge pull request #107 from ianbarber/master
Requests clean up
2 parents e82267d + f5d0d49 commit 600050a

8 files changed

Lines changed: 230 additions & 46 deletions

File tree

src/Google/Cache/Null.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/*
3+
* Copyright 2014 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
require_once "Google/Cache/Abstract.php";
19+
require_once "Google/Cache/Exception.php";
20+
21+
/**
22+
* A blank storage class, for cases where caching is not
23+
* required.
24+
*/
25+
class Google_Cache_Null extends Google_Cache_Abstract
26+
{
27+
public function __construct(Google_Client $client)
28+
{
29+
30+
}
31+
32+
/**
33+
* @inheritDoc
34+
*/
35+
public function get($key, $expiration = false)
36+
{
37+
return false;
38+
}
39+
40+
/**
41+
* @inheritDoc
42+
*/
43+
public function set($key, $value)
44+
{
45+
// Nop.
46+
}
47+
48+
/**
49+
* @inheritDoc
50+
* @param String $key
51+
*/
52+
public function delete($key)
53+
{
54+
// Nop.
55+
}
56+
}

src/Google/Config.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
*/
2121
class Google_Config
2222
{
23+
const GZIP_DISABLED = true;
24+
const GZIP_ENABLED = false;
25+
const GZIP_UPLOADS_ENABLED = true;
26+
const GZIP_UPLOADS_DISABLED = false;
2327
const USE_AUTO_IO_SELECTION = "auto";
2428
private $configuration;
2529

@@ -47,12 +51,21 @@ public function __construct($ini_file_location = null)
4751

4852
// Definition of class specific values, like file paths and so on.
4953
'classes' => array(
50-
// If you want to pass in OAuth 2.0 settings, they will need to be
51-
// structured like this.
54+
'Google_IO_Abstract' => array(
55+
'request_timeout_seconds' => 100,
56+
),
5257
'Google_Http_Request' => array(
53-
// Disable the use of gzip on calls if set to true.
54-
'disable_gzip' => false
58+
// Disable the use of gzip on calls if set to true. Defaults to false.
59+
'disable_gzip' => self::GZIP_ENABLED,
60+
61+
// We default gzip to disabled on uploads even if gzip is otherwise
62+
// enabled, due to some issues seen with small packet sizes for uploads.
63+
// Please test with this option before enabling gzip for uploads in
64+
// a production environment.
65+
'enable_gzip_for_uploads' => self::GZIP_UPLOADS_DISABLED,
5566
),
67+
// If you want to pass in OAuth 2.0 settings, they will need to be
68+
// structured like this.
5669
'Google_Auth_OAuth2' => array(
5770
// Keys for OAuth 2.0 access, see the API console at
5871
// https://developers.google.com/console
@@ -109,7 +122,7 @@ public function setClassConfig($class, $config, $value = null)
109122
$this->configuration['classes'][$class] = $config;
110123
}
111124
}
112-
125+
113126
public function getClassConfig($class, $key = null)
114127
{
115128
if (!isset($this->configuration['classes'][$class])) {
@@ -139,7 +152,7 @@ public function getAuthClass()
139152
{
140153
return $this->configuration['auth_class'];
141154
}
142-
155+
143156
/**
144157
* Set the auth class.
145158
*
@@ -155,7 +168,7 @@ public function setAuthClass($class)
155168
}
156169
$this->configuration['auth_class'] = $class;
157170
}
158-
171+
159172
/**
160173
* Set the IO class.
161174
*
@@ -268,7 +281,7 @@ public function setApprovalPrompt($approval)
268281
{
269282
$this->setAuthConfig('approval_prompt', $approval);
270283
}
271-
284+
272285
/**
273286
* Set the developer key for the auth class. Note that this is separate value
274287
* from the client ID - if it looks like a URL, its a client ID!
@@ -286,7 +299,7 @@ public function getBasePath()
286299
{
287300
return $this->configuration['base_path'];
288301
}
289-
302+
290303
/**
291304
* Set the auth configuration for the current auth class.
292305
* @param $key - the key to set

src/Google/Http/MediaFileUpload.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ class Google_Http_MediaFileUpload
5151

5252
/** @var int $progress */
5353
private $progress;
54-
54+
5555
/** @var Google_Client */
5656
private $client;
57-
57+
5858
/** @var Google_Http_Request */
5959
private $request;
60-
60+
6161
/** @var string */
6262
private $boundary;
6363

@@ -89,7 +89,7 @@ public function __construct(
8989
$this->chunkSize = $chunkSize;
9090
$this->progress = 0;
9191
$this->boundary = $boundary;
92-
92+
9393
// Process Media Request
9494
$this->process();
9595
}
@@ -102,16 +102,16 @@ public function setFileSize($size)
102102
{
103103
$this->size = $size;
104104
}
105-
106-
/**
105+
106+
/**
107107
* Return the progress on the upload
108108
* @return int progress in bytes uploaded.
109109
*/
110110
public function getProgress()
111111
{
112112
return $this->progress;
113113
}
114-
114+
115115
/**
116116
* Send the next part of the file to upload.
117117
* @param [$chunk] the next set of bytes to send. If false will used $data passed
@@ -141,7 +141,13 @@ public function nextChunk($chunk = false)
141141
$headers,
142142
$chunk
143143
);
144-
$httpRequest->disableGzip(); // Disable gzip for uploads.
144+
145+
if ($client->getClassConfig("Google_Http_Request", "enable_gzip_for_uploads")) {
146+
$httpRequest->enableGzip();
147+
} else {
148+
$httpRequest->disableGzip();
149+
}
150+
145151
$response = $this->client->getIo()->makeRequest($httpRequest);
146152
$response->setExpectedClass($this->request->getExpectedClass());
147153
$code = $response->getResponseHttpCode();
@@ -150,13 +156,13 @@ public function nextChunk($chunk = false)
150156
// Track the amount uploaded.
151157
$range = explode('-', $response->getResponseHeader('range'));
152158
$this->progress = $range[1] + 1;
153-
159+
154160
// Allow for changing upload URLs.
155161
$location = $response->getResponseHeader('location');
156162
if ($location) {
157163
$this->resumeUri = $location;
158164
}
159-
165+
160166
// No problems, but upload not complete.
161167
return false;
162168
} else {
@@ -177,7 +183,7 @@ private function process()
177183

178184
$meta = $this->request->getPostBody();
179185
$meta = is_string($meta) ? json_decode($meta, true) : $meta;
180-
186+
181187
$uploadType = $this->getUploadType($meta);
182188
$this->request->setQueryParam('uploadType', $uploadType);
183189
$this->transformToUploadUrl();
@@ -214,7 +220,7 @@ private function process()
214220
$this->request->setRequestHeaders($contentTypeHeader);
215221
}
216222
}
217-
223+
218224
private function transformToUploadUrl()
219225
{
220226
$base = $this->request->getBaseComponent();

src/Google/IO/Abstract.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ abstract class Google_IO_Abstract
3737
public function __construct(Google_Client $client)
3838
{
3939
$this->client = $client;
40+
$timeout = $client->getClassConfig('Google_IO_Abstract', 'request_timeout_seconds');
41+
if ($timeout > 0) {
42+
$this->setTimeout($timeout);
43+
}
4044
}
4145

4246
/**
@@ -51,6 +55,18 @@ abstract public function executeRequest(Google_Http_Request $request);
5155
* @param $options
5256
*/
5357
abstract public function setOptions($options);
58+
59+
/**
60+
* Set the maximum request time in seconds.
61+
* @param $timeout in seconds
62+
*/
63+
abstract public function setTimeout($timeout);
64+
65+
/**
66+
* Get the maximum request time in seconds.
67+
* @return timeout in seconds
68+
*/
69+
abstract public function getTimeout();
5470

5571
/**
5672
* @visible for testing.

src/Google/IO/Curl.php

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ class Google_IO_Curl extends Google_IO_Abstract
3737
public function executeRequest(Google_Http_Request $request)
3838
{
3939
$curl = curl_init();
40-
40+
4141
if ($request->getPostBody()) {
4242
curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getPostBody());
4343
}
44-
44+
4545
$requestHeaders = $request->getRequestHeaders();
4646
if ($requestHeaders && is_array($requestHeaders)) {
4747
$curlHeaders = array();
@@ -60,11 +60,11 @@ public function executeRequest(Google_Http_Request $request)
6060
curl_setopt($curl, CURLOPT_HEADER, true);
6161

6262
curl_setopt($curl, CURLOPT_URL, $request->getUrl());
63-
63+
6464
if ($request->canGzip()) {
6565
curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
6666
}
67-
67+
6868
foreach ($this->options as $key => $var) {
6969
curl_setopt($curl, $key, $var);
7070
}
@@ -74,6 +74,9 @@ public function executeRequest(Google_Http_Request $request)
7474
}
7575

7676
$response = curl_exec($curl);
77+
if ($response === false) {
78+
throw new Google_IO_Exception(curl_error($curl));
79+
}
7780
$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
7881

7982
$responseBody = substr($response, $headerSize);
@@ -90,6 +93,29 @@ public function executeRequest(Google_Http_Request $request)
9093
*/
9194
public function setOptions($options)
9295
{
93-
$this->options = $options;
96+
$this->options = array_merge($this->options, $options);
97+
}
98+
99+
/**
100+
* Set the maximum request time in seconds.
101+
* @param $timeout in seconds
102+
*/
103+
public function setTimeout($timeout)
104+
{
105+
// Since this timeout is really for putting a bound on the time
106+
// we'll set them both to the same. If you need to specify a longer
107+
// CURLOPT_TIMEOUT, or a tigher CONNECTTIMEOUT, the best thing to
108+
// do is use the setOptions method for the values individually.
109+
$this->options[CURLOPT_CONNECTTIMEOUT] = $timeout;
110+
$this->options[CURLOPT_TIMEOUT] = $timeout;
111+
}
112+
113+
/**
114+
* Get the maximum request time in seconds.
115+
* @return timeout in seconds
116+
*/
117+
public function getTimeout()
118+
{
119+
return $this->options[CURLOPT_TIMEOUT];
94120
}
95121
}

src/Google/IO/Stream.php

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

2626
class Google_IO_Stream extends Google_IO_Abstract
2727
{
28+
const TIMEOUT = "timeout";
2829
const ZLIB = "compress.zlib://";
30+
private $options = array();
2931

3032
private static $DEFAULT_HTTP_CONTEXT = array(
3133
"follow_location" => 0,
@@ -102,6 +104,10 @@ public function executeRequest(Google_Http_Request $request)
102104
$response_data = false;
103105
$respHttpCode = self::UNKNOWN_CODE;
104106
if ($fh) {
107+
if (isset($this->options[self::TIMEOUT])) {
108+
stream_set_timeout($fh, $this->options[self::TIMEOUT]);
109+
}
110+
105111
$response_data = stream_get_contents($fh);
106112
fclose($fh);
107113

@@ -119,7 +125,7 @@ public function executeRequest(Google_Http_Request $request)
119125
}
120126

121127
$responseHeaders = $this->getHttpResponseHeaders($http_response_header);
122-
128+
123129
return array($response_data, $responseHeaders, $respHttpCode);
124130
}
125131

@@ -129,9 +135,27 @@ public function executeRequest(Google_Http_Request $request)
129135
*/
130136
public function setOptions($options)
131137
{
132-
// NO-OP
138+
$this->options = array_merge($this->options, $options);
139+
}
140+
141+
/**
142+
* Set the maximum request time in seconds.
143+
* @param $timeout in seconds
144+
*/
145+
public function setTimeout($timeout)
146+
{
147+
$this->options[self::TIMEOUT] = $timeout;
133148
}
134-
149+
150+
/**
151+
* Get the maximum request time in seconds.
152+
* @return timeout in seconds
153+
*/
154+
public function getTimeout()
155+
{
156+
return $this->options[self::TIMEOUT];
157+
}
158+
135159
protected function getHttpResponseCode($response_headers)
136160
{
137161
$header_count = count($response_headers);

0 commit comments

Comments
 (0)