/** * Decode an HTTP Response. * @static * @throws displetretsidx_Google_ServiceException * @param displetretsidx_Google_HttpRequest $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 ($decoded != null && 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}"; } throw new displetretsidx_Google_ServiceException($err, $code, null, $decoded['error']['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 displetretsidx_Google_ServiceException("Invalid json in service response: {$body}"); } } return $decoded; }
/** * Check if an HTTP request can be cached by a private local cache. * * @static * @param displetretsidx_Google_HttpRequest $resp * @return bool True if the request is cacheable. * False if the request is uncacheable. */ public static function isRequestCacheable(displetretsidx_Google_HttpRequest $resp) { $method = $resp->getRequestMethod(); if (!in_array($method, self::$CACHEABLE_HTTP_METHODS)) { return false; } // Don't cache authorized requests/responses. // [rfc2616-14.8] When a shared cache receives a request containing an // Authorization field, it MUST NOT return the corresponding response // as a reply to any other request... if ($resp->getRequestHeader("authorization")) { return false; } return true; }