Example #1
0
 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);
 }
 /**
  * @expectedException Google_Service_Exception
  */
 public function tesProperErrorFormatting()
 {
     $stream = Stream::factory('{
       error: {
        errors: [
         {
          "domain": "global",
          "reason": "authError",
          "message": "Invalid Credentials",
          "locationType": "header",
          "location": "Authorization",
         }
        ],
       "code": 401,
       "message": "Invalid Credentials"
     }');
     $response = new Response(401, array(), $stream);
     $this->rest->decodeHttpResponse($response);
 }
Example #3
0
 /**
  * Send the next part of the file to upload.
  * @param [$chunk] the next set of bytes to send. If false will used $data passed
  * at construct time.
  */
 public function nextChunk($chunk = false)
 {
     if (false == $this->resumeUri) {
         $this->resumeUri = $this->getResumeUri();
     }
     if (false == $chunk) {
         $chunk = substr($this->data, $this->progress, $this->chunkSize);
     }
     $lastBytePos = $this->progress + strlen($chunk) - 1;
     $headers = array('content-range' => "bytes {$this->progress}-{$lastBytePos}/{$this->size}", 'content-type' => $this->request->getRequestHeader('content-type'), 'content-length' => $this->chunkSize, 'expect' => '');
     $httpRequest = new Google_Http_Request($this->resumeUri, 'PUT', $headers, $chunk);
     if ($this->client->getClassConfig("Google_Http_Request", "enable_gzip_for_uploads")) {
         $httpRequest->enableGzip();
     } else {
         $httpRequest->disableGzip();
     }
     $response = $this->client->getIo()->makeRequest($httpRequest);
     $response->setExpectedClass($this->request->getExpectedClass());
     $code = $response->getResponseHttpCode();
     $this->httpResultCode = $code;
     if (308 == $code) {
         // Track the amount uploaded.
         $range = explode('-', $response->getResponseHeader('range'));
         $this->progress = $range[1] + 1;
         // Allow for changing upload URLs.
         $location = $response->getResponseHeader('location');
         if ($location) {
             $this->resumeUri = $location;
         }
         // No problems, but upload not complete.
         return false;
     } else {
         return Google_Http_REST::decodeHttpResponse($response);
     }
 }
Example #4
0
 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;
 }
 /**
  * @expectedException Google_Service_Exception
  */
 public function testNotJson404Error()
 {
     $stream = Psr7\stream_for('Not Found');
     $response = new Response(404, array(), $stream);
     $this->rest->decodeHttpResponse($response, $this->request);
 }
 /**
  * @expectedException Google_Service_Exception
  */
 public function testNotJson404Error()
 {
     $stream = Stream::factory('Not Found');
     $response = new Response(404, array(), $stream);
     $this->rest->decodeHttpResponse($response);
 }
Example #7
0
 public function parseResponse(ResponseInterface $response, $classes = array())
 {
     $contentType = $response->getHeaderLine('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 = (string) $response->getBody();
     if (!empty($body)) {
         $body = str_replace("--{$boundary}--", "--{$boundary}", $body);
         $parts = explode("--{$boundary}", $body);
         $responses = array();
         $requests = array_values($this->requests);
         foreach ($parts as $i => $part) {
             $part = trim($part);
             if (!empty($part)) {
                 list($rawHeaders, $part) = explode("\r\n\r\n", $part, 2);
                 $headers = $this->parseRawHeaders($rawHeaders);
                 $status = substr($part, 0, strpos($part, "\n"));
                 $status = explode(" ", $status);
                 $status = $status[1];
                 list($partHeaders, $partBody) = $this->parseHttpResponse($part, false);
                 $response = new Response($status, $partHeaders, Psr7\stream_for($partBody));
                 // Need content id.
                 $key = $headers['content-id'];
                 $class = '';
                 if (!empty($this->expected_classes[$key])) {
                     $class = $this->expected_classes[$key];
                 }
                 try {
                     $response = Google_Http_REST::decodeHttpResponse($response, $requests[$i - 1]);
                 } catch (Google_Service_Exception $e) {
                     // Store the exception as the response, so successful responses
                     // can be processed.
                     $response = $e;
                 }
                 $responses[$key] = $response;
             }
         }
         return $responses;
     }
     return null;
 }
 /**
  * Sends a PUT-Request to google drive and parses the response,
  * setting the appropiate variables from the response()
  *
  * @param Google_Http_Request $httpRequest the Reuqest which will be send
  *
  * @return false|mixed false when the upload is unfinished or the decoded http response
  *
  */
 private function makePutRequest(RequestInterface $request)
 {
     $response = $this->client->execute($request);
     $this->httpResultCode = $response->getStatusCode();
     if (308 == $this->httpResultCode) {
         // Track the amount uploaded.
         $range = explode('-', $response->getHeaderLine('range'));
         $this->progress = $range[1] + 1;
         // Allow for changing upload URLs.
         $location = $response->getHeaderLine('location');
         if ($location) {
             $this->resumeUri = $location;
         }
         // No problems, but upload not complete.
         return false;
     }
     return Google_Http_REST::decodeHttpResponse($response, $this->request);
 }
Example #9
0
 /**
  * Sends a PUT-Request to google drive and parses the response,
  * setting the appropiate variables from the response()
  *
  * @param Google_Http_Request $httpRequest the Reuqest which will be send
  *
  * @return false|mixed false when the upload is unfinished or the decoded http response
  *
  */
 private function makePutRequest(Google_Http_Request $httpRequest)
 {
     if ($this->client->getClassConfig("Google_Http_Request", "enable_gzip_for_uploads")) {
         $httpRequest->enableGzip();
     } else {
         $httpRequest->disableGzip();
     }
     $response = $this->client->getIo()->makeRequest($httpRequest);
     $response->setExpectedClass($this->request->getExpectedClass());
     $code = $response->getResponseHttpCode();
     $this->httpResultCode = $code;
     if (308 == $code) {
         // Track the amount uploaded.
         $range = explode('-', $response->getResponseHeader('range'));
         $this->progress = $range[1] + 1;
         // Allow for changing upload URLs.
         $location = $response->getResponseHeader('location');
         if ($location) {
             $this->resumeUri = $location;
         }
         // No problems, but upload not complete.
         return false;
     } else {
         return Google_Http_REST::decodeHttpResponse($response, $this->client);
     }
 }