public function testEmptyResponseExceptionRaisesException() { require_once 'Zend/Gdata/App/HttpException.php'; $e = new Zend_Gdata_App_HttpException(); $e->setResponse(null); $success = false; try { $this->gdata->throwServiceExceptionIfDetected($e); } catch (Zend_Gdata_App_IOException $f) { $success = true; } $this->assertTrue($success, 'Zend_Gdata_App_IOException not thrown'); }
/** * Performs a HTTP request using the specified method * * @param string $method The HTTP method for the request - 'GET', 'POST', * 'PUT', 'DELETE' * @param string $url The URL to which this request is being performed * @param array $headers An associative array of HTTP headers * for this request * @param string $body The body of the HTTP request * @param string $contentType The value for the content type * of the request body * @param int $remainingRedirects Number of redirects to follow if request * s results in one * @return Zend_Http_Response The response object */ public function performHttpRequest($method, $url, $headers = null, $body = null, $contentType = null, $remainingRedirects = null) { require_once 'Zend/Http/Client/Exception.php'; if ($remainingRedirects === null) { $remainingRedirects = self::getMaxRedirects(); } if ($headers === null) { $headers = array(); } // Append a Gdata version header if protocol v2 or higher is in use. // (Protocol v1 does not use this header.) $major = $this->getMajorProtocolVersion(); $minor = $this->getMinorProtocolVersion(); if ($major >= 2) { $headers['GData-Version'] = $major + ($minor === null ? '.' + $minor : ''); } // check the overridden method if (($method == 'POST' || $method == 'PUT') && $body === null && $headers['x-http-method-override'] != 'DELETE') { require_once 'Zend/Gdata/App/InvalidArgumentException.php'; throw new Zend_Gdata_App_InvalidArgumentException('You must specify the data to post as either a ' . 'string or a child of Zend_Gdata_App_Entry'); } if ($url === null) { require_once 'Zend/Gdata/App/InvalidArgumentException.php'; throw new Zend_Gdata_App_InvalidArgumentException('You must specify an URI to which to post.'); } $headers['Content-Type'] = $contentType; if (Zend_Gdata_App::getGzipEnabled()) { // some services require the word 'gzip' to be in the user-agent // header in addition to the accept-encoding header if (strpos($this->_httpClient->getHeader('User-Agent'), 'gzip') === false) { $headers['User-Agent'] = $this->_httpClient->getHeader('User-Agent') . ' (gzip)'; } $headers['Accept-encoding'] = 'gzip, deflate'; } else { $headers['Accept-encoding'] = 'identity'; } // Make sure the HTTP client object is 'clean' before making a request // In addition to standard headers to reset via resetParameters(), // also reset the Slug and If-Match headers $this->_httpClient->resetParameters(); $this->_httpClient->setHeaders(array('Slug', 'If-Match')); // Set the params for the new request to be performed $this->_httpClient->setHeaders($headers); require_once 'Zend/Uri/Http.php'; $uri = Zend_Uri_Http::fromString($url); preg_match("/^(.*?)(\\?.*)?\$/", $url, $matches); $this->_httpClient->setUri($matches[1]); $queryArray = $uri->getQueryAsArray(); foreach ($queryArray as $name => $value) { $this->_httpClient->setParameterGet($name, $value); } $this->_httpClient->setConfig(array('maxredirects' => 0)); // Set the proper adapter if we are handling a streaming upload $usingMimeStream = false; $oldHttpAdapter = null; if ($body instanceof Zend_Gdata_MediaMimeStream) { $usingMimeStream = true; $this->_httpClient->setRawDataStream($body, $contentType); $oldHttpAdapter = $this->_httpClient->getAdapter(); if ($oldHttpAdapter instanceof Zend_Http_Client_Adapter_Proxy) { require_once 'Zend/Gdata/HttpAdapterStreamingProxy.php'; $newAdapter = new Zend_Gdata_HttpAdapterStreamingProxy(); } else { require_once 'Zend/Gdata/HttpAdapterStreamingSocket.php'; $newAdapter = new Zend_Gdata_HttpAdapterStreamingSocket(); } $this->_httpClient->setAdapter($newAdapter); } else { $this->_httpClient->setRawData($body, $contentType); } try { $response = $this->_httpClient->request($method); // reset adapter if ($usingMimeStream) { $this->_httpClient->setAdapter($oldHttpAdapter); } } catch (Zend_Http_Client_Exception $e) { // reset adapter if ($usingMimeStream) { $this->_httpClient->setAdapter($oldHttpAdapter); } require_once 'Zend/Gdata/App/HttpException.php'; throw new Zend_Gdata_App_HttpException($e->getMessage(), $e); } if ($response->isRedirect() && $response->getStatus() != '304') { if ($remainingRedirects > 0) { $newUrl = $response->getHeader('Location'); $response = $this->performHttpRequest($method, $newUrl, $headers, $body, $contentType, $remainingRedirects); } else { require_once 'Zend/Gdata/App/HttpException.php'; throw new Zend_Gdata_App_HttpException('Number of redirects exceeds maximum', null, $response); } } if (!$response->isSuccessful()) { require_once 'Zend/Gdata/App/HttpException.php'; $exceptionMessage = 'Expected response code 200, got ' . $response->getStatus(); if (self::getVerboseExceptionMessages()) { $exceptionMessage .= "\n" . $response->getBody(); } $exception = new Zend_Gdata_App_HttpException($exceptionMessage); $exception->setResponse($response); throw $exception; } return $response; }
/** * Set the Zend_Http_Response. * * @param Zend_Http_Response $response */ public function setResponse($response) { $this->_parseResponse($response); return parent::setResponse($response); }
/** * Performs a HTTP request using the specified method * * @param string $method The HTTP method for the request - 'GET', 'POST', * 'PUT', 'DELETE' * @param string $url The URL to which this request is being performed * @param array $headers An associative array of HTTP headers * for this request * @param string $body The body of the HTTP request * @param string $contentType The value for the content type * of the request body * @param int $remainingRedirects Number of redirects to follow if request * s results in one * @return Zend_Http_Response The response object */ public function performHttpRequest($method, $url, $headers = null, $body = null, $contentType = null, $remainingRedirects = null) { require_once 'Zend/Http/Client/Exception.php'; if ($remainingRedirects === null) { $remainingRedirects = self::getMaxRedirects(); } if ($headers === null) { $headers = array(); } // Append a Gdata version header if protocol v2 or higher is in use. // (Protocol v1 does not use this header.) $major = $this->getMajorProtocolVersion(); $minor = $this->getMinorProtocolVersion(); if ($major >= 2) { $headers['GData-Version'] = $major + (is_null($minor) ? '.' + $minor : ''); } // check the overridden method if (($method == 'POST' || $method == 'PUT') && $body === null && $headers['x-http-method-override'] != 'DELETE') { require_once 'Zend/Gdata/App/InvalidArgumentException.php'; throw new Zend_Gdata_App_InvalidArgumentException('You must specify the data to post as either a ' . 'string or a child of Zend_Gdata_App_Entry'); } if ($url === null) { require_once 'Zend/Gdata/App/InvalidArgumentException.php'; throw new Zend_Gdata_App_InvalidArgumentException('You must specify an URI to which to post.'); } $headers['Content-Type'] = $contentType; if (Zend_Gdata_App::getGzipEnabled()) { // some services require the word 'gzip' to be in the user-agent header // in addition to the accept-encoding header if (strpos($this->_httpClient->getHeader('User-Agent'), 'gzip') === false) { $headers['User-Agent'] = $this->_httpClient->getHeader('User-Agent') . ' (gzip)'; } $headers['Accept-encoding'] = 'gzip, deflate'; } else { $headers['Accept-encoding'] = 'identity'; } // Make sure the HTTP client object is 'clean' before making a request // In addition to standard headers to reset via resetParameters(), // also reset the Slug header $this->_httpClient->resetParameters(); $this->_httpClient->setHeaders('Slug', null); // Set the params for the new request to be performed $this->_httpClient->setHeaders($headers); $this->_httpClient->setUri($url); $this->_httpClient->setConfig(array('maxredirects' => 0)); $this->_httpClient->setRawData($body, $contentType); try { $response = $this->_httpClient->request($method); } catch (Zend_Http_Client_Exception $e) { require_once 'Zend/Gdata/App/HttpException.php'; throw new Zend_Gdata_App_HttpException($e->getMessage(), $e); } if ($response->isRedirect() && $response->getStatus() != '304') { if ($remainingRedirects > 0) { $newUrl = $response->getHeader('Location'); $response = $this->performHttpRequest($method, $newUrl, $headers, $body, $contentType, $remainingRedirects); } else { require_once 'Zend/Gdata/App/HttpException.php'; throw new Zend_Gdata_App_HttpException('Number of redirects exceeds maximum', null, $response); } } if (!$response->isSuccessful()) { require_once 'Zend/Gdata/App/HttpException.php'; $exceptionMessage = 'Expected response code 200, got ' . $response->getStatus(); if (self::getVerboseExceptionMessages()) { $exceptionMessage .= "\n" . $response->getBody(); } $exception = new Zend_Gdata_App_HttpException($exceptionMessage); $exception->setResponse($response); throw $exception; } return $response; }
/** * DELETE entry with client object * * @param mixed $data The Zend_Gdata_App_Entry or URL to delete * @return void * @throws Zend_Gdata_App_Exception * @throws Zend_Gdata_App_HttpException * @throws Zend_Gdata_App_InvalidArgumentException */ public function delete($data, $remainingRedirects = null) { require_once 'Zend/Http/Client/Exception.php'; if ($remainingRedirects === null) { $remainingRedirects = self::getMaxRedirects(); } if (is_string($data)) { $uri = $data; } elseif ($data instanceof Zend_Gdata_App_Entry) { $editLink = $data->getEditLink(); if ($editLink != null) { $uri = $editLink->getHref(); } } else { require_once 'Zend/Gdata/App/InvalidArgumentException.php'; throw new Zend_Gdata_App_InvalidArgumentException('You must specify the data to post as either a string or a child of Zend_Gdata_App_Entry'); } if ($uri === null) { require_once 'Zend/Gdata/App/InvalidArgumentException.php'; throw new Zend_Gdata_App_InvalidArgumentException('You must specify an URI which needs deleted.'); } $this->_httpClient->resetParameters(); $this->_httpClient->setHeaders('x-http-method-override', null); $this->_httpClient->setUri($uri); $this->_httpClient->setConfig(array('maxredirects' => 0)); try { if (Zend_Gdata_App::getHttpMethodOverride()) { $this->_httpClient->setHeaders(array('X-HTTP-Method-Override: DELETE')); $this->_httpClient->setRawData(''); $response = $this->_httpClient->request('POST'); } else { $response = $this->_httpClient->request('DELETE'); } } catch (Zend_Http_Client_Exception $e) { require_once 'Zend/Gdata/App/HttpException.php'; throw new Zend_Gdata_App_HttpException($e->getMessage(), $e); } if ($response->isRedirect()) { if ($remainingRedirects > 0) { $newUri = $response->getHeader('Location'); $response = $this->delete($newUri, $remainingRedirects - 1); } else { require_once 'Zend/Gdata/App/HttpException.php'; throw new Zend_Gdata_App_HttpException('No more redirects allowed', null, $response); } } if (!$response->isSuccessful()) { require_once 'Zend/Gdata/App/HttpException.php'; $exception = new Zend_Gdata_App_HttpException('Expected response code 200, got ' . $response->getStatus()); $exception->setResponse($response); throw $exception; } return $response; }
/** * POST data to Google with authorization headers set * * @param mixed $data The Zend_Gdata_App_Entry or XML to post * @param string $uri POST URI * @return Zend_Http_Response * @throws Zend_Gdata_App_Exception * @throws Zend_Gdata_App_HttpException * @throws Zend_Gdata_App_InvalidArgumentException */ public function post($data, $uri = null) { require_once 'Zend/Http/Client/Exception.php'; if (is_string($data)) { $rawData = $data; } elseif ($data instanceof Zend_Gdata_App_Entry) { $rawData = $data->saveXML(); } else { require_once 'Zend/Gdata/App/InvalidArgumentException.php'; throw new Zend_Gdata_App_InvalidArgumentException('You must specify the data to post as either a string or a child of Zend_Gdata_App_Entry'); } if ($uri == null) { $uri = $this->_defaultPostUri; } if ($uri == null) { require_once 'Zend/Gdata/App/InvalidArgumentException.php'; throw new Zend_Gdata_App_InvalidArgumentException('You must specify an URI to which to post.'); } $this->_httpClient->setUri($uri); $this->_httpClient->setConfig(array('maxredirects' => 0)); $this->_httpClient->setRawData($rawData, 'application/atom+xml'); try { $response = $this->_httpClient->request('POST'); } catch (Zend_Http_Client_Exception $e) { require_once 'Zend/Gdata/App/HttpException.php'; throw new Zend_Gdata_App_HttpException($e->getMessage(), $e); } /** * set "S" cookie to avoid future redirects. */ if ($cookie = $response->getHeader('Set-cookie')) { list($cookieName, $cookieValue) = explode('=', $cookie, 2); $this->_httpClient->setCookie($cookieName, $cookieValue); } if ($response->isRedirect()) { /** * Re-POST with redirected URI. * This happens frequently. */ $this->_httpClient->setUri($response->getHeader('Location')); $this->_httpClient->setRawData($rawData, 'application/atom+xml'); try { $response = $this->_httpClient->request('POST'); } catch (Zend_Http_Client_Exception $e) { throw new Zend_Gdata_App_HttpException($e->getMessage(), $e); } } if (!$response->isSuccessful()) { require_once 'Zend/Gdata/App/HttpException.php'; $exception = new Zend_Gdata_App_HttpException('Expected response code 200, got ' . $response->getStatus()); $exception->setResponse($response); throw $exception; } return $response; }
/** * Deletes this entry to the server using the referenced * Zend_Http_Client to do a HTTP DELETE to the edit link stored in this * entry's link collection. * * @return boolean The success of the delete operation * @throws Zend_Gdata_App_Exception */ public function delete() { if ($this->id) { // If id is set, look for link rel="edit" in the // entry object and DELETE. $editLink = $this->getLink('edit'); $editUri = $editLink->href; if (!$editUri) { throw new Zend_Gdata_App_Exception('Cannot delete entry; no link rel="edit" is present.'); } $client = $this->getHttpClient(); if (is_null($client)) { $client = Zend_Gdata_App::getStaticHttpClient(); } $client->resetParameters(); $client->setUri($editUri); $client->setHeaders('Content-Type', null); $client->setRawData(''); if (Zend_Gdata_App::getHttpMethodOverride()) { $client->setHeaders('X-HTTP-Method-Override', 'DELETE'); $response = $client->request('DELETE'); } else { $response = $client->request('DELETE'); } if ($response->getStatus() !== 200) { require_once 'Zend/Gdata/App/HttpException.php'; $exception = new Zend_Gdata_App_HttpException('Expected response code 200, got ' . $response->getStatus()); $exception->setResponse($response); throw $exception; } return true; } else { throw new Zend_Gdata_App_Exception('Cannot edit entry; no id is present'); } }