diff --git a/libraries/http_request.inc b/libraries/http_request.inc index 4ed75fd..9a8310b 100644 --- a/libraries/http_request.inc +++ b/libraries/http_request.inc @@ -47,6 +47,11 @@ define('HTTP_REQUEST_PCRE_TAG_ATTRIBUTES', '/[\x09\x0A\x0B\x0C\x0D\x20]+([^\x09\ class HRCurlException extends Exception {} /** + * For HTTP requests that do not return a 2xx code. + */ +class FeedsHTTPRequestException extends Exception {} + +/** * Discovers RSS or atom feeds at the given URL. * * If document in given URL is an HTML document, function attempts to discover @@ -311,6 +316,57 @@ function http_request_get($url, $username = NULL, $password = NULL, $accept_inva } /** + * Checks the result of the HTTP Request. + * + * @param string $url + * The URL that was requested. + * @param object $result + * The HTTP Request result. + * + * @throws FeedsHTTPRequestException + * In case the result code of the HTTP request is not in the 2xx series. + */ +function http_request_check_result($url, $result) { + if (!in_array($result->code, array(200, 201, 202, 203, 204, 205, 206))) { + $vars = array( + '@url' => $url, + '@code' => $result->code, + '@error' => isset($result->error) ? $result->error : 'Unknown error', + ); + + switch ($result->code) { + case FEEDS_ERROR_PARSE_ERROR: + $message = t('Download of @url failed because it could not be parsed.', $vars); + break; + + case FEEDS_ERROR_NO_SCHEME: + $message = t("Download of @url failed because its scheme could not be determined. The URL is expected to start with something like '@example'.", $vars + array( + '@example' => 'http://', + )); + break; + + case FEEDS_ERROR_INVALID_SCHEME: + $message = t('Download of @url failed because its scheme is not supported: @error. Examples of supported schemes are: @supported.', $vars + array( + '@scheme' => $result->uri['scheme'], + '@supported' => implode(', ', array('http', 'https')), + )); + break; + + default: + if (isset($result->error)) { + $message = t('Download of @url failed with code @code and the following error: @error.', $vars); + } + else { + $message = t('Download of @url failed with code @code.', $vars); + } + break; + } + + throw new FeedsHTTPRequestException($message); + } +} + +/** * Decides if it's possible to use cURL or not. * * @return bool diff --git a/plugins/FeedsHTTPFetcher.inc b/plugins/FeedsHTTPFetcher.inc index cd91b63..93a89f8 100644 --- a/plugins/FeedsHTTPFetcher.inc +++ b/plugins/FeedsHTTPFetcher.inc @@ -42,15 +42,16 @@ class FeedsHTTPFetcherResult extends FeedsFetcherResult { } /** - * Overrides FeedsFetcherResult::getRaw(); + * Overrides FeedsFetcherResult::getRaw(). + * + * @throws FeedsHTTPRequestException + * In case the result code of the HTTP request is not in the 2xx series. */ public function getRaw() { if (!isset($this->raw)) { feeds_include_library('http_request.inc', 'http_request'); $result = http_request_get($this->url, NULL, NULL, $this->acceptInvalidCert, $this->timeout); - if (!in_array($result->code, array(200, 201, 202, 203, 204, 205, 206))) { - throw new Exception(t('Download of @url failed with code !code.', array('@url' => $this->url, '!code' => $result->code))); - } + http_request_check_result($result); $this->raw = $result->data; } diff --git a/plugins/FeedsParser.inc b/plugins/FeedsParser.inc index 058903a..5de55ee 100644 --- a/plugins/FeedsParser.inc +++ b/plugins/FeedsParser.inc @@ -430,47 +430,14 @@ class FeedsEnclosure extends FeedsElement { * * @return string * The content of the referenced resource. + * + * @throws FeedsHTTPRequestException + * In case the result code of the HTTP request is not in the 2xx series. */ public function getContent() { feeds_include_library('http_request.inc', 'http_request'); $result = http_request_get($this->getUrlEncodedValue()); - if ($result->code != 200) { - $vars = array( - '@url' => $this->getUrlEncodedValue(), - '@code' => $result->code, - '@error' => isset($result->error) ? $result->error : 'Unknown error', - ); - - switch ($result->code) { - case FEEDS_ERROR_PARSE_ERROR: - $message = t('Download of @url failed because it could not be parsed.', $vars); - break; - - case FEEDS_ERROR_NO_SCHEME: - $message = t("Download of @url failed because its scheme could not be determined. The URL is expected to start with something like '@example'.", $vars + array( - '@example' => 'http://', - )); - break; - - case FEEDS_ERROR_INVALID_SCHEME: - $message = t('Download of @url failed because its scheme is not supported: @error. Examples of supported schemes are: @supported.', $vars + array( - '@scheme' => $result->uri['scheme'], - '@supported' => implode(', ', array('http', 'https')), - )); - break; - - default: - if (isset($result->error)) { - $message = t('Download of @url failed with code @code and the following error: @error.', $vars); - } - else { - $message = t('Download of @url failed with code @code.', $vars); - } - break; - } - - throw new Exception($message); - } + http_request_check_result($this->getUrlEncodedValue(), $result); return $result->data; }