Exception
8 years ago
WebAuthException
8 years ago
certs
8 years ago
AppInfo.php
8 years ago
AppInfoLoadException.php
8 years ago
ArrayEntryStore.php
8 years ago
AuthBase.php
8 years ago
AuthInfo.php
8 years ago
AuthInfoLoadException.php
8 years ago
Checker.php
8 years ago
Client.php
8 years ago
Curl.php
8 years ago
CurlStreamRelay.php
8 years ago
DeserializeException.php
8 years ago
DropboxMetadataHeaderCatcher.php
8 years ago
Exception.php
8 years ago
Host.php
8 years ago
HttpResponse.php
8 years ago
OAuth1AccessToken.php
8 years ago
OAuth1Upgrader.php
8 years ago
Path.php
8 years ago
RequestUtil.php
8 years ago
RootCertificates.php
8 years ago
SSLTester.php
8 years ago
Security.php
8 years ago
StreamReadException.php
8 years ago
Util.php
8 years ago
ValueStore.php
8 years ago
WebAuth.php
5 years ago
WebAuthBase.php
8 years ago
WebAuthNoRedirect.php
8 years ago
WriteMode.php
8 years ago
autoload.php
8 years ago
strict.php
8 years ago
RequestUtil.php
348 lines
| 1 | <?php |
| 2 | namespace Dropbox; |
| 3 | |
| 4 | if (!function_exists('curl_init')) { |
| 5 | throw new \Exception("The Dropbox SDK requires the cURL PHP extension, but it looks like you don't have it (couldn't find function \"curl_init\"). Library: \"" . __FILE__ . "\"."); |
| 6 | } |
| 7 | |
| 8 | if (!function_exists('json_decode')) { |
| 9 | throw new \Exception("The Dropbox SDK requires the JSON PHP extension, but it looks like you don't have it (couldn't find function \"json_decode\"). Library: \"" . __FILE__ . "\"."); |
| 10 | } |
| 11 | |
| 12 | // If mbstring.func_overload is set, it changes the behavior of the standard string functions in |
| 13 | // ways that makes this library break. |
| 14 | $mbstring_func_overload = ini_get("mbstring.func_overload"); |
| 15 | if ($mbstring_func_overload & 2 == 2) { |
| 16 | throw new \Exception("The Dropbox SDK doesn't work when mbstring.func_overload is set to overload the standard string functions (value = ".var_export($mbstring_func_overload, true)."). Library: \"" . __FILE__ . "\"."); |
| 17 | } |
| 18 | |
| 19 | if (strlen((string) PHP_INT_MAX) < 19) { |
| 20 | // Looks like we're running on a 32-bit build of PHP. This could cause problems because some of the numbers |
| 21 | // we use (file sizes, quota, etc) can be larger than 32-bit ints can handle. |
| 22 | throw new \Exception("The Dropbox SDK uses 64-bit integers, but it looks like we're running on a version of PHP that doesn't support 64-bit integers (PHP_INT_MAX=" . ((string) PHP_INT_MAX) . "). Library: \"" . __FILE__ . "\""); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * @internal |
| 27 | */ |
| 28 | final class RequestUtil |
| 29 | { |
| 30 | /** |
| 31 | * @param string $userLocale |
| 32 | * @param string $host |
| 33 | * @param string $path |
| 34 | * @param array $params |
| 35 | * @return string |
| 36 | */ |
| 37 | // static function buildUrlForGetOrPut($userLocale, $host, $path, $params = null) |
| 38 | // { |
| 39 | // $url = self::buildUri($host, $path); |
| 40 | // $url .= "?locale=" . rawurlencode($userLocale); |
| 41 | |
| 42 | // if ($params !== null) { |
| 43 | // foreach ($params as $key => $value) { |
| 44 | // Checker::argStringNonEmpty("key in 'params'", $key); |
| 45 | // if ($value !== null) { |
| 46 | // if (is_bool($value)) { |
| 47 | // $value = $value ? "true" : "false"; |
| 48 | // } |
| 49 | // else if (is_int($value)) { |
| 50 | // $value = (string) $value; |
| 51 | // } |
| 52 | // else if (!is_string($value)) { |
| 53 | // throw new \InvalidArgumentException("params['$key'] is not a string, int, or bool"); |
| 54 | // } |
| 55 | // $url .= "&" . rawurlencode($key) . "=" . rawurlencode($value); |
| 56 | // } |
| 57 | // } |
| 58 | // } |
| 59 | // return $url; |
| 60 | // } |
| 61 | |
| 62 | /*Dropbox api 2*/ |
| 63 | static function buildUrlForGetOrPut($userLocale, $host, $path, $params = null) |
| 64 | { |
| 65 | $url = self::buildUri($host, $path); |
| 66 | |
| 67 | if ($params !== null) { |
| 68 | $url .= "?"; |
| 69 | foreach ($params as $key => $value) { |
| 70 | Checker::argStringNonEmpty("key in 'params'", $key); |
| 71 | if ($value !== null) { |
| 72 | if (is_bool($value)) { |
| 73 | $value = $value ? "true" : "false"; |
| 74 | } |
| 75 | else if (is_int($value)) { |
| 76 | $value = (string) $value; |
| 77 | } |
| 78 | else if (!is_string($value)) { |
| 79 | throw new \InvalidArgumentException("params['$key'] is not a string, int, or bool"); |
| 80 | } |
| 81 | $url .= "&" . rawurlencode($key) . "=" . rawurlencode($value); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | return $url; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @param string $host |
| 90 | * @param string $path |
| 91 | * @return string |
| 92 | */ |
| 93 | static function buildUri($host, $path) |
| 94 | { |
| 95 | Checker::argStringNonEmpty("host", $host); |
| 96 | Checker::argStringNonEmpty("path", $path); |
| 97 | return "https://" . $host . "/" . $path; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @param string $clientIdentifier |
| 102 | * @param string $url |
| 103 | * @return Curl |
| 104 | */ |
| 105 | static function mkCurl($clientIdentifier, $url) |
| 106 | { |
| 107 | $curl = new Curl($url); |
| 108 | |
| 109 | $curl->set(CURLOPT_CONNECTTIMEOUT, 10); |
| 110 | |
| 111 | // If the transfer speed is below 1kB/sec for 10 sec, abort. |
| 112 | $curl->set(CURLOPT_LOW_SPEED_LIMIT, 0); |
| 113 | $curl->set(CURLOPT_LOW_SPEED_TIME, 0); |
| 114 | |
| 115 | //$curl->set(CURLOPT_VERBOSE, true); // For debugging. |
| 116 | // TODO: Figure out how to encode clientIdentifier (urlencode?) |
| 117 | $curl->addHeader("User-Agent: ".$clientIdentifier." Dropbox-PHP-SDK"); |
| 118 | |
| 119 | return $curl; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @param string $clientIdentifier |
| 124 | * @param string $url |
| 125 | * @param string $authHeaderValue |
| 126 | * @return Curl |
| 127 | */ |
| 128 | static function mkCurlWithAuth($clientIdentifier, $url, $authHeaderValue) |
| 129 | { |
| 130 | $curl = self::mkCurl($clientIdentifier, $url); |
| 131 | $curl->addHeader("Authorization: $authHeaderValue"); |
| 132 | return $curl; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * @param string $clientIdentifier |
| 137 | * @param string $url |
| 138 | * @param string $accessToken |
| 139 | * @return Curl |
| 140 | */ |
| 141 | static function mkCurlWithOAuth($clientIdentifier, $url, $accessToken) |
| 142 | { |
| 143 | return self::mkCurlWithAuth($clientIdentifier, $url, "Bearer $accessToken"); |
| 144 | } |
| 145 | |
| 146 | static function buildPostBody($params) |
| 147 | { |
| 148 | if ($params === null) return ""; |
| 149 | |
| 150 | $pairs = array(); |
| 151 | foreach ($params as $key => $value) { |
| 152 | Checker::argStringNonEmpty("key in 'params'", $key); |
| 153 | if ($value !== null) { |
| 154 | if (is_bool($value)) { |
| 155 | $value = $value ? "true" : "false"; |
| 156 | } |
| 157 | else if (is_int($value)) { |
| 158 | $value = (string) $value; |
| 159 | } |
| 160 | else if (!is_string($value)) { |
| 161 | throw new \InvalidArgumentException("params['$key'] is not a string, int, or bool"); |
| 162 | } |
| 163 | $pairs[] = rawurlencode($key) . "=" . rawurlencode((string) $value); |
| 164 | } |
| 165 | } |
| 166 | return implode("&", $pairs); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * @param string $clientIdentifier |
| 171 | * @param string $accessToken |
| 172 | * @param string $userLocale |
| 173 | * @param string $host |
| 174 | * @param string $path |
| 175 | * @param array|null $params |
| 176 | * |
| 177 | * @return HttpResponse |
| 178 | * |
| 179 | * @throws Exception |
| 180 | */ |
| 181 | // static function doPost($clientIdentifier, $accessToken, $userLocale, $host, $path, $params = null) |
| 182 | // { |
| 183 | // Checker::argStringNonEmpty("accessToken", $accessToken); |
| 184 | |
| 185 | // $url = self::buildUri($host, $path); |
| 186 | |
| 187 | // if ($params === null) $params = array(); |
| 188 | // $params['locale'] = $userLocale; |
| 189 | |
| 190 | // $curl = self::mkCurlWithOAuth($clientIdentifier, $url, $accessToken); |
| 191 | // $curl->set(CURLOPT_POST, true); |
| 192 | // $curl->set(CURLOPT_POSTFIELDS, self::buildPostBody($params)); |
| 193 | |
| 194 | // $curl->set(CURLOPT_RETURNTRANSFER, true); |
| 195 | |
| 196 | // return $curl->exec(); |
| 197 | // } |
| 198 | |
| 199 | /*Dropbox api 2*/ |
| 200 | static function doPost($clientIdentifier, $accessToken, $userLocale, $host, $path, $params = null, $headers = null) |
| 201 | { |
| 202 | Checker::argStringNonEmpty("accessToken", $accessToken); |
| 203 | |
| 204 | $url = self::buildUri($host, $path); |
| 205 | |
| 206 | $curl = self::mkCurlWithOAuth($clientIdentifier, $url, $accessToken); |
| 207 | $curl->set(CURLOPT_POST, true); |
| 208 | $curl->set(CURLOPT_POSTFIELDS, json_encode($params)); |
| 209 | $curl->set(CURLOPT_RETURNTRANSFER, true); |
| 210 | |
| 211 | if ($headers) { |
| 212 | $curl->addHeader($headers); |
| 213 | } |
| 214 | |
| 215 | return $curl->exec(); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * @param string $clientIdentifier |
| 220 | * @param string $authHeaderValue |
| 221 | * @param string $userLocale |
| 222 | * @param string $host |
| 223 | * @param string $path |
| 224 | * @param array|null $params |
| 225 | * |
| 226 | * @return HttpResponse |
| 227 | * |
| 228 | * @throws Exception |
| 229 | */ |
| 230 | static function doPostWithSpecificAuth($clientIdentifier, $authHeaderValue, $userLocale, $host, $path, $params = null) |
| 231 | { |
| 232 | Checker::argStringNonEmpty("authHeaderValue", $authHeaderValue); |
| 233 | |
| 234 | $url = self::buildUri($host, $path); |
| 235 | |
| 236 | if ($params === null) $params = array(); |
| 237 | $params['locale'] = $userLocale; |
| 238 | |
| 239 | $curl = self::mkCurlWithAuth($clientIdentifier, $url, $authHeaderValue); |
| 240 | $curl->set(CURLOPT_POST, true); |
| 241 | $curl->set(CURLOPT_POSTFIELDS, self::buildPostBody($params)); |
| 242 | |
| 243 | $curl->set(CURLOPT_RETURNTRANSFER, true); |
| 244 | return $curl->exec(); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * @param string $clientIdentifier |
| 249 | * @param string $accessToken |
| 250 | * @param string $userLocale |
| 251 | * @param string $host |
| 252 | * @param string $path |
| 253 | * @param array|null $params |
| 254 | * |
| 255 | * @return HttpResponse |
| 256 | * |
| 257 | * @throws Exception |
| 258 | */ |
| 259 | static function doGet($clientIdentifier, $accessToken, $userLocale, $host, $path, $params = null) |
| 260 | { |
| 261 | Checker::argStringNonEmpty("accessToken", $accessToken); |
| 262 | |
| 263 | $url = self::buildUrlForGetOrPut($userLocale, $host, $path, $params); |
| 264 | |
| 265 | $curl = self::mkCurlWithOAuth($clientIdentifier, $url, $accessToken); |
| 266 | $curl->set(CURLOPT_HTTPGET, true); |
| 267 | $curl->set(CURLOPT_RETURNTRANSFER, true); |
| 268 | |
| 269 | return $curl->exec(); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * @param string $responseBody |
| 274 | * @return mixed |
| 275 | * @throws Exception_BadResponse |
| 276 | */ |
| 277 | static function parseResponseJson($responseBody) |
| 278 | { |
| 279 | $obj = json_decode($responseBody, true, 10); |
| 280 | if ($obj === null) { |
| 281 | throw new Exception_BadResponse("Got bad JSON from server: $responseBody"); |
| 282 | } |
| 283 | return $obj; |
| 284 | } |
| 285 | |
| 286 | static function unexpectedStatus($httpResponse) |
| 287 | { |
| 288 | $sc = $httpResponse->statusCode; |
| 289 | |
| 290 | $message = "HTTP status $sc"; |
| 291 | if (is_string($httpResponse->body)) { |
| 292 | // TODO: Maybe only include the first ~200 chars of the body? |
| 293 | $message .= "\n".$httpResponse->body; |
| 294 | } |
| 295 | |
| 296 | if ($sc === 400) return new Exception_BadRequest($message); |
| 297 | if ($sc === 401) return new Exception_InvalidAccessToken($message); |
| 298 | if ($sc === 500 || $sc === 502) return new Exception_ServerError($message); |
| 299 | if ($sc === 503) return new Exception_RetryLater($message); |
| 300 | |
| 301 | return new Exception_BadResponseCode("Unexpected $message", $sc); |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * @param int $maxRetries |
| 306 | * The number of times to retry it the action if it fails with one of the transient |
| 307 | * API errors. A value of 1 means we'll try the action once and if it fails, we |
| 308 | * will retry once. |
| 309 | * |
| 310 | * @param callable $action |
| 311 | * The the action you want to retry. |
| 312 | * |
| 313 | * @return mixed |
| 314 | * Whatever is returned by the $action callable. |
| 315 | */ |
| 316 | static function runWithRetry($maxRetries, $action) |
| 317 | { |
| 318 | Checker::argNat("maxRetries", $maxRetries); |
| 319 | |
| 320 | $retryDelay = 1; |
| 321 | $numRetries = 0; |
| 322 | while (true) { |
| 323 | try { |
| 324 | return $action(); |
| 325 | } |
| 326 | // These exception types are the ones we think are possibly transient errors. |
| 327 | catch (Exception_NetworkIO $ex) { |
| 328 | $savedEx = $ex; |
| 329 | } |
| 330 | catch (Exception_ServerError $ex) { |
| 331 | $savedEx = $ex; |
| 332 | } |
| 333 | catch (Exception_RetryLater $ex) { |
| 334 | $savedEx = $ex; |
| 335 | } |
| 336 | |
| 337 | // We maxed out our retries. Propagate the last exception we got. |
| 338 | if ($numRetries >= $maxRetries) throw $savedEx; |
| 339 | |
| 340 | $numRetries++; |
| 341 | sleep($retryDelay); |
| 342 | $retryDelay *= 2; // Exponential back-off. |
| 343 | } |
| 344 | throw new \RuntimeException("unreachable"); |
| 345 | } |
| 346 | |
| 347 | } |
| 348 |