public function sign(Google_Http_Request $request) { $this->key = $this->client->getClassConfig($this, 'key'); if ($this->key) { $request->setRequestHeaders(array('Authorization' => $this->key)); } return $request; }
public function sign(Google_Http_Request $request) { $key = $this->client->getClassConfig($this, 'developer_key'); if ($key) { $request->setQueryParam('key', $key); } return $request; }
public function sign(Google_Http_Request $request) { $key = $this->client->getClassConfig($this, 'developer_key'); if ($key) { $this->client->getLogger()->debug('Simple API Access developer key authentication'); $request->setQueryParam('key', $key); } return $request; }
public function testDecodeEmptyResponse() { $url = 'http://localhost'; $response = new Google_Http_Request($url, 'GET', array()); $response->setResponseBody('{}'); $response->setResponseHttpCode(200); $decoded = $this->rest->decodeHttpResponse($response); $this->assertEquals(array(), $decoded); }
public function sign(Google_Http_Request $request) { if (!$this->token) { // No token, so nothing to do. return $request; } // Add the OAuth2 header to the request $request->setRequestHeaders(array('Authorization' => 'Bearer ' . $this->token['access_token'])); return $request; }
public function sign(Google_Http_Request $request) { if (!$this->token) { // No token, so nothing to do. return $request; } $this->client->getLogger()->debug('App Identity authentication'); // Add the OAuth2 header to the request $request->setRequestHeaders(array('Authorization' => 'Bearer ' . $this->token['access_token'])); return $request; }
/** * Execute an HTTP Request * * @param Google_Http_Request $request the http request to be executed * @return array containing response headers, body, and http code * @throws Google_IO_Exception on curl or IO error */ public function executeRequest(Google_Http_Request $request) { $curl = curl_init(); if ($request->getPostBody()) { curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getPostBody()); } $requestHeaders = $request->getRequestHeaders(); if ($requestHeaders && is_array($requestHeaders)) { $curlHeaders = array(); foreach ($requestHeaders as $k => $v) { $curlHeaders[] = "{$k}: {$v}"; } if (isset($this->options[CURLOPT_HTTPHEADER])) { if (is_array($this->options[CURLOPT_HTTPHEADER])) { foreach ($this->options[CURLOPT_HTTPHEADER] as $optionHeader) { $curlHeaders[] = $optionHeader; } } unset($this->options[CURLOPT_HTTPHEADER]); } curl_setopt($curl, CURLOPT_HTTPHEADER, $curlHeaders); } curl_setopt($curl, CURLOPT_URL, $request->getUrl()); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $request->getRequestMethod()); curl_setopt($curl, CURLOPT_USERAGENT, $request->getUserAgent()); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); // 1 is CURL_SSLVERSION_TLSv1, which is not always defined in PHP. curl_setopt($curl, CURLOPT_SSLVERSION, 1); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HEADER, true); if ($request->canGzip()) { curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate'); } $options = $this->client->getClassConfig('Google_IO_Curl', 'options'); if (is_array($options)) { $this->setOptions($options); } foreach ($this->options as $key => $var) { curl_setopt($curl, $key, $var); } if (!isset($this->options[CURLOPT_CAINFO])) { curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/cacerts.pem'); } $this->client->getLogger()->debug('cURL request', array('url' => $request->getUrl(), 'method' => $request->getRequestMethod(), 'headers' => $requestHeaders, 'body' => $request->getPostBody())); $response = curl_exec($curl); if ($response === false) { $error = curl_error($curl); $code = curl_errno($curl); $map = $this->client->getClassConfig('Google_IO_Exception', 'retry_map'); $this->client->getLogger()->error('cURL ' . $error); throw new Google_IO_Exception($error, $code, null, $map); } $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); list($responseHeaders, $responseBody) = $this->parseHttpResponse($response, $headerSize); $responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); $this->client->getLogger()->debug('cURL response', array('code' => $responseCode, 'headers' => $responseHeaders, 'body' => $responseBody)); return array($responseBody, $responseHeaders, $responseCode); }
public function parseResponse(Google_Http_Request $response) { $contentType = $response->getResponseHeader('content-type'); $contentType = explode(';', $contentType); $boundary = false; foreach ($contentType as $part) { $part = explode('=', $part, 2); if (isset($part[0]) && 'boundary' == trim($part[0])) { $boundary = $part[1]; } } $body = $response->getResponseBody(); if ($body) { $body = str_replace("--{$boundary}--", "--{$boundary}", $body); $parts = explode("--{$boundary}", $body); $responses = array(); foreach ($parts as $part) { $part = trim($part); if (!empty($part)) { list($metaHeaders, $part) = explode("\r\n\r\n", $part, 2); $metaHeaders = $this->client->getIo()->getHttpResponseHeaders($metaHeaders); $status = substr($part, 0, strpos($part, "\n")); $status = explode(" ", $status); $status = $status[1]; list($partHeaders, $partBody) = $this->client->getIo()->ParseHttpResponse($part, false); $response = new Google_Http_Request(""); $response->setResponseHttpCode($status); $response->setResponseHeaders($partHeaders); $response->setResponseBody($partBody); // Need content id. $key = $metaHeaders['content-id']; if (isset($this->expected_classes[$key]) && strlen($this->expected_classes[$key]) > 0) { $class = $this->expected_classes[$key]; $response->setExpectedClass($class); } try { $response = Google_Http_REST::decodeHttpResponse($response); $responses[$key] = $response; } catch (Google_Service_Exception $e) { // Store the exception as the response, so succesful responses // can be processed. $responses[$key] = $e; } } } return $responses; } return null; }
/** * Decode an HTTP Response. * @static * @throws Google_Service_Exception * @param Google_Http_Request $response The http response to be decoded. * @param Google_Client $client * @return mixed|null */ public static function decodeHttpResponse($response, Google_Client $client = null) { $code = $response->getResponseHttpCode(); $body = $response->getResponseBody(); $decoded = null; if (intVal($code) >= 300) { $decoded = json_decode($body, true); $err = 'Error calling ' . $response->getRequestMethod() . ' ' . $response->getUrl(); if (isset($decoded['error']) && isset($decoded['error']['message']) && isset($decoded['error']['code'])) { // if we're getting a json encoded error definition, use that instead of the raw response // body for improved readability $err .= ": ({$decoded['error']['code']}) {$decoded['error']['message']}"; } else { $err .= ": ({$code}) {$body}"; } $errors = null; // Specific check for APIs which don't return error details, such as Blogger. if (isset($decoded['error']) && isset($decoded['error']['errors'])) { $errors = $decoded['error']['errors']; } $map = null; if ($client) { $client->getLogger()->error($err, array('code' => $code, 'errors' => $errors)); $map = $client->getClassConfig('Google_Service_Exception', 'retry_map'); } //throw new Google_Service_Exception($err, $code, null, $errors, $map); echo json_encode(array('status' => FALSE, 'response-code' => $code, 'response' => $err, 'message' => $errors, "map" => $map)); die; } // Only attempt to decode the response, if the response code wasn't (204) 'no content' if ($code != '204') { $decoded = json_decode($body, true); if ($decoded === null || $decoded === "") { $error = "Invalid json in service response: {$body}"; if ($client) { $client->getLogger()->error($error); } //throw new Google_Service_Exception($error); echo json_encode(array('status' => FALSE, 'response-code' => $code, 'response' => $error, 'message' => $body)); die; } if ($response->getExpectedClass()) { $class = $response->getExpectedClass(); $decoded = new $class($decoded); } } return $decoded; }
private function getResumeUri() { $result = null; $body = $this->request->getPostBody(); if ($body) { $headers = array('content-type' => 'application/json; charset=UTF-8', 'content-length' => Google_Utils::getStrLen($body), 'x-upload-content-type' => $this->mimeType, 'x-upload-content-length' => $this->size, 'expect' => ''); $this->request->setRequestHeaders($headers); } $response = $this->client->getIo()->makeRequest($this->request); $location = $response->getResponseHeader('location'); $code = $response->getResponseHttpCode(); if (200 == $code && true == $location) { return $location; } $message = $code; $body = @json_decode($response->getResponseBody()); if (!empty($body->error->errors)) { $message .= ': '; foreach ($body->error->errors as $error) { $message .= "{$error->domain}, {$error->message};"; } $message = rtrim($message, ';'); } $error = "Failed to start the resumable upload (HTTP {$message})"; $this->client->getLogger()->error($error); throw new Google_Exception($error); }
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()); }
/** * Execute an HTTP Request * * @param Google_HttpRequest $request the http request to be executed * @return Google_HttpRequest http request with the response http code, * response headers and response body filled in * @throws Google_IO_Exception on curl or IO error */ public function executeRequest(Google_Http_Request $request) { $default_options = stream_context_get_options(stream_context_get_default()); $requestHttpContext = array_key_exists('http', $default_options) ? $default_options['http'] : array(); if ($request->getPostBody()) { $requestHttpContext["content"] = $request->getPostBody(); } $requestHeaders = $request->getRequestHeaders(); if ($requestHeaders && is_array($requestHeaders)) { $headers = ""; foreach ($requestHeaders as $k => $v) { $headers .= "{$k}: {$v}\r\n"; } $requestHttpContext["header"] = $headers; } $requestHttpContext["method"] = $request->getRequestMethod(); $requestHttpContext["user_agent"] = $request->getUserAgent(); $requestSslContext = array_key_exists('ssl', $default_options) ? $default_options['ssl'] : array(); if (!array_key_exists("cafile", $requestSslContext)) { $requestSslContext["cafile"] = dirname(__FILE__) . '/cacerts.pem'; } $options = array("http" => array_merge(self::$DEFAULT_HTTP_CONTEXT, $requestHttpContext), "ssl" => array_merge(self::$DEFAULT_SSL_CONTEXT, $requestSslContext)); $context = stream_context_create($options); $url = $request->getUrl(); if ($request->canGzip()) { $url = self::ZLIB . $url; } // We are trapping any thrown errors in this method only and // throwing an exception. $this->trappedErrorNumber = null; $this->trappedErrorString = null; // START - error trap. set_error_handler(array($this, 'trapError')); $fh = fopen($url, 'r', false, $context); restore_error_handler(); // END - error trap. if ($this->trappedErrorNumber) { throw new Google_IO_Exception(sprintf("HTTP Error: Unable to connect: '%s'", $this->trappedErrorString), $this->trappedErrorNumber); } $response_data = false; $respHttpCode = self::UNKNOWN_CODE; if ($fh) { if (isset($this->options[self::TIMEOUT])) { stream_set_timeout($fh, $this->options[self::TIMEOUT]); } $response_data = stream_get_contents($fh); fclose($fh); $respHttpCode = $this->getHttpResponseCode($http_response_header); } if (false === $response_data) { throw new Google_IO_Exception(sprintf("HTTP Error: Unable to connect: '%s'", $respHttpCode), $respHttpCode); } $responseHeaders = $this->getHttpResponseHeaders($http_response_header); return array($response_data, $responseHeaders, $respHttpCode); }
/** * Decode an HTTP Response. * @static * @throws Google_Service_Exception * @param Google_Http_Request $response The http response to be decoded. * @return mixed|null */ public static function decodeHttpResponse($response) { $code = $response->getResponseHttpCode(); $body = $response->getResponseBody(); $decoded = null; if (intVal($code) >= 300) { $decoded = json_decode($body, true); $err = 'Error calling ' . $response->getRequestMethod() . ' ' . $response->getUrl(); if (isset($decoded['error']) && isset($decoded['error']['message']) && isset($decoded['error']['code'])) { // if we're getting a json encoded error definition, use that instead of the raw response // body for improved readability $err .= ": ({$decoded['error']['code']}) {$decoded['error']['message']}"; } else { $err .= ": ({$code}) {$body}"; } $errors = null; // Specific check for APIs which don't return error details, such as Blogger. if (isset($decoded['error']) && isset($decoded['error']['errors'])) { $errors = $decoded['error']['errors']; } throw new Google_Service_Exception($err, $code, null, $errors); } // Only attempt to decode the response, if the response code wasn't (204) 'no content' if ($code != '204') { $decoded = json_decode($body, true); if ($decoded === null || $decoded === "") { throw new Google_Service_Exception("Invalid json in service response: {$body}"); } if ($response->getExpectedClass()) { $class = $response->getExpectedClass(); $decoded = new $class($decoded); } } return $decoded; }
private function transformToUploadUrl() { $parts = parse_url($this->request->getUrl()); if (!isset($parts['path'])) { $parts['path'] = ''; } $parts['path'] = '/upload' . $parts['path']; $url = Url::fromString(Url::buildUrl($parts)); $this->request->setUrl($url); }
public function testRequestParameters() { $url = 'http://localhost:8080/foo/bar?foo=a&foo=b&wowee=oh+my'; $url2 = 'http://localhost:8080/foo/bar?foo=a&foo=b&wowee=oh+my&hi=there'; $request = new Google_Http_Request($this->getClient(), $url); $request->setExpectedClass("Google_Client"); $this->assertEquals(2, count($request->getQueryParams())); $request->setQueryParam("hi", "there"); $this->assertEquals($url2, $request->getUrl()); $this->assertEquals("Google_Client", $request->getExpectedClass()); $url3a = 'http://localhost:8080/foo/bar'; $url3b = 'foo=a&foo=b&wowee=oh+my'; $url3c = 'foo=a&foo=b&wowee=oh+my&hi=there'; $request = new Google_Http_Request($this->getClient(), $url3a . "?" . $url3b, "POST"); $request->setQueryParam("hi", "there"); $request->maybeMoveParametersToBody(); $this->assertEquals($url3a, $request->getUrl()); $this->assertEquals($url3c, $request->getPostBody()); }
public function testRequestParameters() { $url = 'http://localhost:8080/foo/bar?foo=a&foo=b&wowee=oh+my'; $url2 = 'http://localhost:8080/foo/bar?foo=a&foo=b&wowee=oh+my&hi=there'; $request = new Google_Http_Request($url); $request->setExpectedClass("Google_Client"); $this->assertEquals(2, count($request->getQueryParams())); $request->setQueryParam("hi", "there"); $this->assertEquals($url2, $request->getUrl()); $this->assertEquals("Google_Client", $request->getExpectedClass()); $urlPath = "/foo/bar"; $request = new Google_Http_Request($urlPath); $this->assertEquals($urlPath, $request->getUrl()); $request->setBaseComponent("http://example.com"); $this->assertEquals("http://example.com" . $urlPath, $request->getUrl()); $url3a = 'http://localhost:8080/foo/bar'; $url3b = 'foo=a&foo=b&wowee=oh+my'; $url3c = 'foo=a&foo=b&wowee=oh+my&hi=there'; $request = new Google_Http_Request($url3a . "?" . $url3b, "POST"); $request->setQueryParam("hi", "there"); $request->maybeMoveParametersToBody(); $this->assertEquals($url3a, $request->getUrl()); $this->assertEquals($url3c, $request->getPostBody()); $url4 = 'http://localhost:8080/upload/foo/bar?foo=a&foo=b&wowee=oh+my&hi=there'; $request = new Google_Http_Request($url); $this->assertEquals(2, count($request->getQueryParams())); $request->setQueryParam("hi", "there"); $base = $request->getBaseComponent(); $request->setBaseComponent($base . '/upload'); $this->assertEquals($url4, $request->getUrl()); }
public function testProcess() { $client = $this->getClient(); $data = 'foo'; // Test data *only* uploads. $request = new Google_Http_Request('http://www.example.com', 'POST'); $media = new Google_Http_MediaFileUpload($client, $request, 'image/png', $data, false); $this->assertEquals($data, $request->getPostBody()); // Test resumable (meta data) - we want to send the metadata, not the app data. $request = new Google_Http_Request('http://www.example.com', 'POST'); $reqData = json_encode("hello"); $request->setPostBody($reqData); $media = new Google_Http_MediaFileUpload($client, $request, 'image/png', $data, true); $this->assertEquals(json_decode($reqData), $request->getPostBody()); // Test multipart - we are sending encoded meta data and post data $request = new Google_Http_Request('http://www.example.com', 'POST'); $reqData = json_encode("hello"); $request->setPostBody($reqData); $media = new Google_Http_MediaFileUpload($client, $request, 'image/png', $data, false); $this->assertContains($reqData, $request->getPostBody()); $this->assertContains(base64_encode($data), $request->getPostBody()); }
/** * Execute an HTTP Request * * @param Google_HttpRequest $request the http request to be executed * * @return Google_HttpRequest http request with the response http code, * response headers and response body filled in * @throws Google_IO_Exception on curl or IO error */ public function executeRequest(Google_Http_Request $request) { $default_options = stream_context_get_options(stream_context_get_default()); $requestHttpContext = array_key_exists('http', $default_options) ? $default_options['http'] : array(); if ($request->getPostBody()) { $requestHttpContext["content"] = $request->getPostBody(); } $requestHeaders = $request->getRequestHeaders(); if ($requestHeaders && is_array($requestHeaders)) { $headers = ""; foreach ($requestHeaders as $k => $v) { $headers .= "{$k}: {$v}\r\n"; } $requestHttpContext["header"] = $headers; } $requestHttpContext["method"] = $request->getRequestMethod(); $requestHttpContext["user_agent"] = $request->getUserAgent(); $requestSslContext = array_key_exists('ssl', $default_options) ? $default_options['ssl'] : array(); if (!array_key_exists("cafile", $requestSslContext)) { $requestSslContext["cafile"] = dirname(__FILE__) . '/cacerts.pem'; } $options = array("http" => array_merge(self::$DEFAULT_HTTP_CONTEXT, $requestHttpContext), "ssl" => array_merge(self::$DEFAULT_SSL_CONTEXT, $requestSslContext)); $context = stream_context_create($options); $url = $request->getUrl(); if ($request->canGzip()) { $url = self::ZLIB . $url; } // Not entirely happy about this, but supressing the warning from the // fopen seems like the best situation here - we can't do anything // useful with it, and failure to connect is a legitimate run // time situation. @($fh = fopen($url, 'r', false, $context)); $response_data = false; $respHttpCode = self::UNKNOWN_CODE; if ($fh) { if (isset($this->options[self::TIMEOUT])) { stream_set_timeout($fh, $this->options[self::TIMEOUT]); } $response_data = stream_get_contents($fh); fclose($fh); $respHttpCode = $this->getHttpResponseCode($http_response_header); } if (false === $response_data) { throw new Google_IO_Exception(sprintf("HTTP Error: Unable to connect: '%s'", $respHttpCode), $respHttpCode); } $responseHeaders = $this->getHttpResponseHeaders($http_response_header); return array($response_data, $responseHeaders, $respHttpCode); }
/** * Execute an API request. * * This is a copy/paste from the parent class that uses Moodle's implementation * of curl. Portions have been removed or altered. * * @param Google_Http_Request $request the http request to be executed * @return Google_Http_Request http request with the response http code, response * headers and response body filled in * @throws Google_IO_Exception on curl or IO error */ public function executeRequest(Google_Http_Request $request) { $curl = new curl(); if ($request->getPostBody()) { $curl->setopt(array('CURLOPT_POSTFIELDS' => $request->getPostBody())); } $requestHeaders = $request->getRequestHeaders(); if ($requestHeaders && is_array($requestHeaders)) { $curlHeaders = array(); foreach ($requestHeaders as $k => $v) { $curlHeaders[] = "{$k}: {$v}"; } $curl->setopt(array('CURLOPT_HTTPHEADER' => $curlHeaders)); } $curl->setopt(array('CURLOPT_URL' => $request->getUrl())); $curl->setopt(array('CURLOPT_CUSTOMREQUEST' => $request->getRequestMethod())); $curl->setopt(array('CURLOPT_USERAGENT' => $request->getUserAgent())); $curl->setopt(array('CURLOPT_FOLLOWLOCATION' => false)); $curl->setopt(array('CURLOPT_SSL_VERIFYPEER' => true)); $curl->setopt(array('CURLOPT_RETURNTRANSFER' => true)); $curl->setopt(array('CURLOPT_HEADER' => true)); if ($request->canGzip()) { $curl->setopt(array('CURLOPT_ENCODING' => 'gzip,deflate')); } $curl->setopt($this->options); $respdata = $this->do_request($curl, $request); $infos = $curl->get_info(); $respheadersize = $infos['header_size']; $resphttpcode = (int) $infos['http_code']; $curlerrornum = $curl->get_errno(); $curlerror = $curl->error; if ($respdata != CURLE_OK) { throw new Google_IO_Exception($curlerror); } list($responseHeaders, $responseBody) = $this->parseHttpResponse($respdata, $respheadersize); return array($responseBody, $responseHeaders, $resphttpcode); }
private function getResumeUri() { $result = null; $body = $this->request->getPostBody(); if ($body) { $headers = array('content-type' => 'application/json; charset=UTF-8', 'content-length' => Google_Utils::getStrLen($body), 'x-upload-content-type' => $this->mimeType, 'x-upload-content-length' => $this->size, 'expect' => ''); $this->request->setRequestHeaders($headers); } $response = $this->client->getIo()->makeRequest($this->request); $location = $response->getResponseHeader('location'); $code = $response->getResponseHttpCode(); if (200 == $code && true == $location) { return $location; } throw new Google_Exception("Failed to start the resumable upload"); }
/** * Execute an HTTP Request * * @param Google_HttpRequest $request the http request to be executed * @return Google_HttpRequest http request with the response http code, * response headers and response body filled in * @throws Google_IO_Exception on curl or IO error */ public function executeRequest(Google_Http_Request $request) { $curl = curl_init(); if ($request->getPostBody()) { curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getPostBody()); } $requestHeaders = $request->getRequestHeaders(); if ($requestHeaders && is_array($requestHeaders)) { $curlHeaders = array(); foreach ($requestHeaders as $k => $v) { $curlHeaders[] = "{$k}: {$v}"; } curl_setopt($curl, CURLOPT_HTTPHEADER, $curlHeaders); } curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $request->getRequestMethod()); curl_setopt($curl, CURLOPT_USERAGENT, $request->getUserAgent()); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HEADER, true); curl_setopt($curl, CURLOPT_URL, $request->getUrl()); if ($request->canGzip()) { curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate'); } foreach ($this->options as $key => $var) { curl_setopt($curl, $key, $var); } if (!isset($this->options[CURLOPT_CAINFO])) { curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/cacerts.pem'); } $response = curl_exec($curl); if ($response === false) { throw new Google_IO_Exception(curl_error($curl)); } $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); $responseBody = substr($response, $headerSize); $responseHeaderString = substr($response, 0, $headerSize); $responseHeaders = $this->getHttpResponseHeaders($responseHeaderString); $responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); return array($responseBody, $responseHeaders, $responseCode); }
/** * TODO(ianbarber): This function needs simplifying. * @param $name * @param $arguments * @param $expected_class - optional, the expected class name * @return Google_Http_Request|expected_class * @throws Google_Exception */ public function call($name, $arguments, $expected_class = null) { if (!isset($this->methods[$name])) { $this->client->getLogger()->error('Service method unknown', array('service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name)); throw new Google_Exception("Unknown function: " . "{$this->serviceName}->{$this->resourceName}->{$name}()"); } $method = $this->methods[$name]; $parameters = $arguments[0]; // postBody is a special case since it's not defined in the discovery // document as parameter, but we abuse the param entry for storing it. $postBody = null; if (isset($parameters['postBody'])) { if ($parameters['postBody'] instanceof Google_Model) { // In the cases the post body is an existing object, we want // to use the smart method to create a simple object for // for JSONification. $parameters['postBody'] = $parameters['postBody']->toSimpleObject(); } else { if (is_object($parameters['postBody'])) { // If the post body is another kind of object, we will try and // wrangle it into a sensible format. $parameters['postBody'] = $this->convertToArrayAndStripNulls($parameters['postBody']); } } $postBody = json_encode($parameters['postBody']); unset($parameters['postBody']); } // TODO(ianbarber): optParams here probably should have been // handled already - this may well be redundant code. if (isset($parameters['optParams'])) { $optParams = $parameters['optParams']; unset($parameters['optParams']); $parameters = array_merge($parameters, $optParams); } if (!isset($method['parameters'])) { $method['parameters'] = array(); } $method['parameters'] = array_merge($method['parameters'], $this->stackParameters); foreach ($parameters as $key => $val) { if ($key != 'postBody' && !isset($method['parameters'][$key])) { $this->client->getLogger()->error('Service parameter unknown', array('service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name, 'parameter' => $key)); throw new Google_Exception("({$name}) unknown parameter: '{$key}'"); } } foreach ($method['parameters'] as $paramName => $paramSpec) { if (isset($paramSpec['required']) && $paramSpec['required'] && !isset($parameters[$paramName])) { $this->client->getLogger()->error('Service parameter missing', array('service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name, 'parameter' => $paramName)); throw new Google_Exception("({$name}) missing required param: '{$paramName}'"); } if (isset($parameters[$paramName])) { $value = $parameters[$paramName]; $parameters[$paramName] = $paramSpec; $parameters[$paramName]['value'] = $value; unset($parameters[$paramName]['required']); } else { // Ensure we don't pass nulls. unset($parameters[$paramName]); } } $this->client->getLogger()->info('Service Call', array('service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name, 'arguments' => $parameters)); $url = Google_Http_REST::createRequestUri($this->servicePath, $method['path'], $parameters); $httpRequest = new Google_Http_Request($url, $method['httpMethod'], null, $postBody); if ($this->rootUrl) { $httpRequest->setBaseComponent($this->rootUrl); } else { $httpRequest->setBaseComponent($this->client->getBasePath()); } if ($postBody) { $contentTypeHeader = array(); $contentTypeHeader['content-type'] = 'application/json; charset=UTF-8'; $httpRequest->setRequestHeaders($contentTypeHeader); $httpRequest->setPostBody($postBody); } $httpRequest = $this->client->getAuth()->sign($httpRequest); $httpRequest->setExpectedClass($expected_class); if (isset($parameters['data']) && ($parameters['uploadType']['value'] == 'media' || $parameters['uploadType']['value'] == 'multipart')) { // If we are doing a simple media upload, trigger that as a convenience. $mfu = new Google_Http_MediaFileUpload($this->client, $httpRequest, isset($parameters['mimeType']) ? $parameters['mimeType']['value'] : 'application/octet-stream', $parameters['data']['value']); } if (isset($parameters['alt']) && $parameters['alt']['value'] == 'media') { $httpRequest->enableExpectedRaw(); } if ($this->client->shouldDefer()) { // If we are in batch or upload mode, return the raw request. return $httpRequest; } return $this->client->execute($httpRequest); }
/** * @expectedException Google_Service_Exception */ public function tesProperErrorFormatting() { $request = new Google_Http_Request("/a/b"); $request->setResponseHttpCode(401); $request->setResponseBody('{ error: { errors: [ { "domain": "global", "reason": "authError", "message": "Invalid Credentials", "locationType": "header", "location": "Authorization", } ], "code": 401, "message": "Invalid Credentials" }'); Google_Http_Rest::decodeHttpResponse($request); }
/** * @static * @param Google_Http_Request $resp * @return bool True if the HTTP response is considered to be expired. * False if it is considered to be fresh. */ public static function isExpired(Google_Http_Request $resp) { // HTTP/1.1 clients and caches MUST treat other invalid date formats, // especially including the value “0”, as in the past. $parsedExpires = false; $responseHeaders = $resp->getResponseHeaders(); if (isset($responseHeaders['expires'])) { $rawExpires = $responseHeaders['expires']; // Check for a malformed expires header first. if (empty($rawExpires) || is_numeric($rawExpires) && $rawExpires <= 0) { return true; } // See if we can parse the expires header. $parsedExpires = strtotime($rawExpires); if (false == $parsedExpires || $parsedExpires <= 0) { return true; } } // Calculate the freshness of an http response. $freshnessLifetime = false; $cacheControl = $resp->getParsedCacheControl(); if (isset($cacheControl['max-age'])) { $freshnessLifetime = $cacheControl['max-age']; } $rawDate = $resp->getResponseHeader('date'); $parsedDate = strtotime($rawDate); if (empty($rawDate) || false == $parsedDate) { // We can't default this to now, as that means future cache reads // will always pass with the logic below, so we will require a // date be injected if not supplied. throw new Google_Exception("All cacheable requests must have creation dates."); } if (false == $freshnessLifetime && isset($responseHeaders['expires'])) { $freshnessLifetime = $parsedExpires - $parsedDate; } if (false == $freshnessLifetime) { return true; } // Calculate the age of an http response. $age = max(0, time() - $parsedDate); if (isset($responseHeaders['age'])) { $age = max($age, strtotime($responseHeaders['age'])); } return $freshnessLifetime <= $age; }
public function processEntityRequest($io, $client) { $req = new Google_Http_Request("http://localhost.com"); $req->setRequestMethod("POST"); // Verify that the content-length is calculated. $req->setPostBody("{}"); $io->processEntityRequest($req); $this->assertEquals(2, $req->getRequestHeader("content-length")); // Test an empty post body. $req->setPostBody(""); $io->processEntityRequest($req); $this->assertEquals(0, $req->getRequestHeader("content-length")); // Test a null post body. $req->setPostBody(null); $io->processEntityRequest($req); $this->assertEquals(0, $req->getRequestHeader("content-length")); // Set an array in the postbody, and verify that it is url-encoded. $req->setPostBody(array("a" => "1", "b" => 2)); $io->processEntityRequest($req); $this->assertEquals(7, $req->getRequestHeader("content-length")); $this->assertEquals(Google_IO_Abstract::FORM_URLENCODED, $req->getRequestHeader("content-type")); $this->assertEquals("a=1&b=2", $req->getPostBody()); // Verify that the content-type isn't reset. $payload = array("a" => "1", "b" => 2); $req->setPostBody($payload); $req->setRequestHeaders(array("content-type" => "multipart/form-data")); $io->processEntityRequest($req); $this->assertEquals("multipart/form-data", $req->getRequestHeader("content-type")); $this->assertEquals($payload, $req->getPostBody()); }
/** * Revoke an OAuth2 access token or refresh token. This method will revoke the current access * token, if a token isn't provided. * @throws Google_Auth_Exception * @param string|null $token The token (access token or a refresh token) that should be revoked. * @return boolean Returns True if the revocation was successful, otherwise False. */ public function revokeToken($token = null) { if (!$token) { if (!$this->token) { // Not initialized, no token to actually revoke return false; } elseif (array_key_exists('refresh_token', $this->token)) { $token = $this->token['refresh_token']; } else { $token = $this->token['access_token']; } } $request = new Google_Http_Request(self::OAUTH2_REVOKE_URI, 'POST', array(), "token={$token}"); $request->disableGzip(); $response = $this->client->getIo()->makeRequest($request); $code = $response->getResponseHttpCode(); if ($code == 200) { $this->token = null; return true; } return false; }
/** * Sends the authenticated request to Gitkit API. The request contains an * OAuth2 access_token generated from service account. * * @param string $method the API method name * @param array $data http post data for the api * @return array server response * @throws Gitkit_ClientException if input is invalid * @throws Gitkit_ServerException if there is server error */ public function invokeGitkitApiWithServiceAccount($method, $data) { $httpRequest = new Google_Http_Request($this->gitkitApisUrl . $method, 'POST', null, json_encode($data)); $contentTypeHeader = array(); $contentTypeHeader['content-type'] = 'application/json; charset=UTF-8'; $httpRequest->setRequestHeaders($contentTypeHeader); $response = $this->oauth2Client->authenticatedRequest($httpRequest)->getResponseBody(); return $this->checkGitkitError(json_decode($response, true)); }
/** * Check if an already cached request must be revalidated, and if so update * the request with the correct ETag headers. * @param Google_Http_Request $cached A previously cached response. * @param Google_Http_Request $request The outbound request. * return bool If the cached object needs to be revalidated, false if it is * still current and can be re-used. */ protected function checkMustRevalidateCachedRequest($cached, $request) { if (Google_Http_CacheParser::mustRevalidate($cached)) { $addHeaders = array(); if ($cached->getResponseHeader('etag')) { // [13.3.4] If an entity tag has been provided by the origin server, // we must use that entity tag in any cache-conditional request. $addHeaders['If-None-Match'] = $cached->getResponseHeader('etag'); } elseif ($cached->getResponseHeader('date')) { $addHeaders['If-Modified-Since'] = $cached->getResponseHeader('date'); } $request->setRequestHeaders($addHeaders); return true; } else { return false; } }
/** * Execute an HTTP Request * * @param Google_Http_Request $request the http request to be executed * @return array containing response headers, body, and http code * @throws Google_IO_Exception on curl or IO error */ public function executeRequest(Google_Http_Request $request) { $client = $this->getGuzzleClient(); $options = array('exceptions' => true); $method = $request->getRequestMethod(); if (strcasecmp($method, 'get') == 0) { $req = $client->get($request->getUrl(), $request->getRequestHeaders(), $options); } elseif (strcasecmp($method, 'post') == 0) { $req = $client->post($request->getUrl(), $request->getRequestHeaders(), $request->getPostBody(), $options); } elseif (strcasecmp($method, 'delete') == 0) { $req = $client->delete($request->getUrl(), $request->getRequestHeaders(), null, $options); } elseif (strcasecmp($method, 'put') == 0) { $req = $client->put($request->getUrl(), $request->getRequestHeaders(), $request->getPostBody(), $options); } else { throw new Google_IO_Exception(__CLASS__ . '#' . __LINE__ . ' Request method not supported'); } if ($request->canGzip()) { $req->addHeader('Accept-Encoding', 'gzip,deflate'); } $this->client->getLogger()->debug('guzzle request', array('url' => $request->getUrl(), 'method' => $request->getRequestMethod(), 'headers' => $req->getHeaders(), 'body' => $request->getPostBody())); try { $result = $client->send($req); $rawHeaders = $result->getRawHeaders(); $responseHeaders = $this->getHttpResponseHeaders($rawHeaders); $responseBody = $result->getBody(); $responseCode = $result->getStatusCode(); } catch (Exception $ex) { // check if http response info can be retrieved from exception // this gives calling service opportunity to handle failures if ($ex instanceof Guzzle\Http\Exception\BadResponseException) { $response = $ex->getResponse(); $responseBody = $response->getBody(); $rawHeaders = $response->getRawHeaders(); $responseHeaders = $this->getHttpResponseHeaders($rawHeaders); $responseCode = $response->getStatusCode(); } else { throw $ex; } } $this->client->getLogger()->debug('guzzle response', array('code' => $responseCode, 'headers' => $responseHeaders, 'body' => $responseBody)); return array($responseBody, $responseHeaders, $responseCode); }
/** * Execute an HTTP Request * * @param Google_Http_Request $request the http request to be executed * @return array containing response headers, body, and http code * @throws Google_IO_Exception on curl or IO error */ public function executeRequest(Google_Http_Request $request) { $default_options = stream_context_get_options(stream_context_get_default()); $requestHttpContext = array_key_exists('http', $default_options) ? $default_options['http'] : array(); if ($request->getPostBody()) { $requestHttpContext["content"] = $request->getPostBody(); } $requestHeaders = $request->getRequestHeaders(); if ($requestHeaders && is_array($requestHeaders)) { $headers = ""; foreach ($requestHeaders as $k => $v) { $headers .= "{$k}: {$v}\r\n"; } $requestHttpContext["header"] = $headers; } $requestHttpContext["method"] = $request->getRequestMethod(); $requestHttpContext["user_agent"] = $request->getUserAgent(); $requestSslContext = array_key_exists('ssl', $default_options) ? $default_options['ssl'] : array(); # UpdraftPlus patch // if (!array_key_exists("cafile", $requestSslContext)) { // $requestSslContext["cafile"] = dirname(__FILE__) . '/cacerts.pem'; // } $url = $request->getUrl(); if (preg_match('#^https?://([^/]+)/#', $url, $umatches)) { $cname = $umatches[1]; } else { $cname = false; } # UpdraftPlus patch // Added if (empty($this->options['disable_verify_peer'])) { $requestSslContext['verify_peer'] = true; if (version_compare(PHP_VERSION, '5.6.0', '>=')) { if (!empty($cname)) { $requestSslContext['peer_name'] = $cname; } } else { if (!empty($cname)) { $requestSslContext['CN_match'] = $cname; $retry_on_fail = true; } } } else { $requestSslContext['allow_self_signed'] = true; } if (!empty($this->options['cafile'])) { $requestSslContext['cafile'] = $this->options['cafile']; } $options = array("http" => array_merge(self::$DEFAULT_HTTP_CONTEXT, $requestHttpContext), "ssl" => array_merge($requestSslContext)); $context = stream_context_create($options); # UpdraftPlus patch // $url = $request->getUrl(); if ($request->canGzip()) { $url = self::ZLIB . $url; } $this->client->getLogger()->debug('Stream request', array('url' => $url, 'method' => $request->getRequestMethod(), 'headers' => $requestHeaders, 'body' => $request->getPostBody())); // We are trapping any thrown errors in this method only and // throwing an exception. $this->trappedErrorNumber = null; $this->trappedErrorString = null; // START - error trap. set_error_handler(array($this, 'trapError')); $fh = fopen($url, 'r', false, $context); # UpdraftPLus patch if (!$fh && isset($retry_on_fail) && !empty($cname) && 'www.googleapis.com' == $cname) { // Reset $this->trappedErrorNumber = null; $this->trappedErrorString = null; global $updraftplus; $updraftplus->log("Using Stream, and fopen failed; retrying different CN match to try to overcome"); // www.googleapis.com does not match the cert now being presented - *.storage.googleapis.com; presumably, PHP's stream handler isn't handling alternative names properly. Rather than turn off all verification, let's retry with a new name to match. $options['ssl']['CN_match'] = 'www.storage.googleapis.com'; $context = stream_context_create($options); $fh = fopen($url, 'r', false, $context); } restore_error_handler(); // END - error trap. if ($this->trappedErrorNumber) { $error = sprintf("HTTP Error: Unable to connect: '%s'", $this->trappedErrorString); $this->client->getLogger()->error('Stream ' . $error); throw new Google_IO_Exception($error, $this->trappedErrorNumber); } $response_data = false; $respHttpCode = self::UNKNOWN_CODE; if ($fh) { if (isset($this->options[self::TIMEOUT])) { stream_set_timeout($fh, $this->options[self::TIMEOUT]); } $response_data = stream_get_contents($fh); fclose($fh); $respHttpCode = $this->getHttpResponseCode($http_response_header); } if (false === $response_data) { $error = sprintf("HTTP Error: Unable to connect: '%s'", $respHttpCode); $this->client->getLogger()->error('Stream ' . $error); throw new Google_IO_Exception($error, $respHttpCode); } $responseHeaders = $this->getHttpResponseHeaders($http_response_header); $this->client->getLogger()->debug('Stream response', array('code' => $respHttpCode, 'headers' => $responseHeaders, 'body' => $response_data)); return array($response_data, $responseHeaders, $respHttpCode); }