public function parseResponse(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 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 = REST::decodeHttpResponse($response, $this->client); $responses[$key] = $response; } catch (Exception $e) { // Store the exception as the response, so successful responses // can be processed. $responses[$key] = $e; } } } return $responses; } return null; }
/** * Save the poster/cover photo to disk * @param string $path where to store the file * @param boolean $thumb get the thumbnail (100x140, default) or the * bigger variant (400x600 - FALSE) * @return boolean success * @see IMDB page / (TitlePage) */ public function savephoto($path, $thumb = true) { $photo_url = $this->photo($thumb); if (!$photo_url) { return false; } $req = new Request($photo_url, $this->config); $req->sendRequest(); if (strpos($req->getResponseHeader("Content-Type"), 'image/jpeg') === 0 || strpos($req->getResponseHeader("Content-Type"), 'image/gif') === 0 || strpos($req->getResponseHeader("Content-Type"), 'image/bmp') === 0) { $fp = $req->getResponseBody(); } else { $ctype = $req->getResponseHeader("Content-Type"); $this->debug_scalar("<BR>*photoerror* at " . __FILE__ . " line " . __LINE__ . ": " . $photo_url . ": Content Type is '{$ctype}'<BR>"); if (substr($ctype, 0, 4) == 'text') { $this->debug_scalar("Details: <PRE>" . $req->getResponseBody() . "</PRE>\n"); } return false; } $fp2 = fopen($path, "w"); if (!$fp || !$fp2) { $this->debug_scalar("image error at " . __FILE__ . " line " . __LINE__ . "...<BR>"); return false; } fputs($fp2, $fp); return true; }
/** * @static * @param 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(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 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; }
/** * Save the photo to disk * @param string path where to store the file * @param optional boolean thumb get the thumbnail (100x140, default) or the * bigger variant (400x600 - FALSE) * @return boolean success * @see IMDB person page / (Main page) */ public function savephoto($path, $thumb = TRUE, $rerun = FALSE) { $photo_url = $this->photo($thumb); if (!$photo_url) { return FALSE; } $req = new Request($photo_url, $this->config); $req->sendRequest(); if (strpos($req->getResponseHeader("Content-Type"), 'image/jpeg') === 0 || strpos($req->getResponseHeader("Content-Type"), 'image/gif') === 0 || strpos($req->getResponseHeader("Content-Type"), 'image/bmp') === 0) { $fp = $req->getResponseBody(); } else { if ($rerun) { $this->debug_scalar("<BR>*photoerror* at " . __FILE__ . " line " . __LINE__ . ": " . $photo_url . ": Content Type is '" . $req->getResponseHeader("Content-Type") . "'<BR>"); return FALSE; } else { $this->debug_scalar("<BR>Initiate second run for photo '{$path}'<BR>"); return $this->savephoto($path, $thumb, TRUE); } } $fp2 = fopen($path, "w"); if (!$fp || !$fp2) { $this->debug_scalar("image error...<BR>"); return false; } fputs($fp2, $fp); return TRUE; }