private function getResumeUri() { $result = null; $body = $this->request->getPostBody(); if ($body) { $headers = array('content-type' => 'application/json; charset=UTF-8', 'content-length' => Postman_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 Postman_Google_Exception($error); }
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 Postman_Google_Http_Request($url); $request->setExpectedClass("Postman_Google_Client"); $this->assertEquals(2, count($request->getQueryParams())); $request->setQueryParam("hi", "there"); $this->assertEquals($url2, $request->getUrl()); $this->assertEquals("Postman_Google_Client", $request->getExpectedClass()); $urlPath = "/foo/bar"; $request = new Postman_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 Postman_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 Postman_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()); }
/** * Execute an HTTP Request * * @param Postman_Google_HttpRequest $request the http request to be executed * @return Postman_Google_HttpRequest http request with the response http code, * response headers and response body filled in * @throws Postman_Google_IO_Exception on curl or IO error */ public function executeRequest(Postman_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_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'); } 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); $this->client->getLogger()->error('cURL ' . $error); throw new Postman_Google_IO_Exception($error); } $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 testProcess() { $client = $this->getClient(); $data = 'foo'; // Test data *only* uploads. $request = new Postman_Google_Http_Request('http://www.example.com', 'POST'); $media = new Postman_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 Postman_Google_Http_Request('http://www.example.com', 'POST'); $reqData = json_encode("hello"); $request->setPostBody($reqData); $media = new Postman_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 Postman_Google_Http_Request('http://www.example.com', 'POST'); $reqData = json_encode("hello"); $request->setPostBody($reqData); $media = new Postman_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 Postman_Google_HttpRequest $request the http request to be executed * @return Postman_Google_HttpRequest http request with the response http code, * response headers and response body filled in * @throws Postman_Google_IO_Exception on curl or IO error */ public function executeRequest(Postman_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; } $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); 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 Postman_Google_IO_Exception($error, $this->trappedErrorNumber); } $response_data = false; $respHttpCode = self::UNKNOWN_CODE; if ($fh) { if (isset($this->options[self::TIMEOUT])) { // @jason: added @ to hide PHP warnings if the host has disabled stream_set_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 Postman_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); }
/** * @visible for testing * Process an http request that contains an enclosed entity. * @param Postman_Google_Http_Request $request * @return Postman_Google_Http_Request Processed request with the enclosed entity. */ public function processEntityRequest(Postman_Google_Http_Request $request) { $postBody = $request->getPostBody(); $contentType = $request->getRequestHeader("content-type"); // Set the default content-type as application/x-www-form-urlencoded. if (false == $contentType) { $contentType = self::FORM_URLENCODED; $request->setRequestHeaders(array('content-type' => $contentType)); } // Force the payload to match the content-type asserted in the header. if ($contentType == self::FORM_URLENCODED && is_array($postBody)) { $postBody = http_build_query($postBody, '', '&'); $request->setPostBody($postBody); } // Make sure the content-length header is set. if (!$postBody || is_string($postBody)) { $postsLength = strlen($postBody); $request->setRequestHeaders(array('content-length' => $postsLength)); } return $request; }