From 61da51c2d51342bb338cba67191b50442488eeb3 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 18 Oct 2020 22:15:20 +0200 Subject: [PATCH] Add HTTPRequest::head() function --- mod/parse_url.php | 2 +- src/Network/HTTPRequest.php | 11 +++++++++++ src/Network/IHTTPRequest.php | 21 ++++++++++++++++----- src/Util/HTTPSignature.php | 6 +++++- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/mod/parse_url.php b/mod/parse_url.php index ede557ff22..3c931fdb1e 100644 --- a/mod/parse_url.php +++ b/mod/parse_url.php @@ -85,7 +85,7 @@ function parse_url_content(App $a) // Check if the URL is an image, video or audio file. If so format // the URL with the corresponding BBCode media tag // Fetch the header of the URL - $curlResponse = DI::httpRequest()->get($url, ['novalidate' => true, 'nobody' => true]); + $curlResponse = DI::httpRequest()->head($url, ['novalidate' => true]); if ($curlResponse->isSuccess()) { // Convert the header fields into an array diff --git a/src/Network/HTTPRequest.php b/src/Network/HTTPRequest.php index 6442295273..df62ea4544 100644 --- a/src/Network/HTTPRequest.php +++ b/src/Network/HTTPRequest.php @@ -52,6 +52,17 @@ class HTTPRequest implements IHTTPRequest $this->baseUrl = $baseUrl->get(); } + /** {@inheritDoc} + * + * @throws HTTPException\InternalServerErrorException + */ + public function head(string $url, array $opts = []) + { + $opts['nobody'] = true; + + return $this->get($url, $opts); + } + /** * {@inheritDoc} * diff --git a/src/Network/IHTTPRequest.php b/src/Network/IHTTPRequest.php index d6bf981052..a1c7730caf 100644 --- a/src/Network/IHTTPRequest.php +++ b/src/Network/IHTTPRequest.php @@ -29,12 +29,10 @@ interface IHTTPRequest /** * Fetches the content of an URL * - * If binary flag is true, return binary results. * Set the cookiejar argument to a string (e.g. "/tmp/friendica-cookies.txt") * to preserve cookies from one request to the next. * * @param string $url URL to fetch - * TRUE if asked to return binary results (file download) * @param int $timeout Timeout in seconds, default system config value or 60 seconds * @param string $accept_content supply Accept: header with 'accept_content' as the value * @param string $cookiejar Path to cookie jar file @@ -50,7 +48,6 @@ interface IHTTPRequest * all the information collected during the fetch. * * @param string $url URL to fetch - * TRUE if asked to return binary results (file download) * @param int $timeout Timeout in seconds, default system config value or 60 seconds * @param string $accept_content supply Accept: header with 'accept_content' as the value * @param string $cookiejar Path to cookie jar file @@ -59,17 +56,31 @@ interface IHTTPRequest */ public function fetchFull(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = ''); + /** + * Send a HEAD to an URL. + * + * @param string $url URL to fetch + * @param array $opts (optional parameters) assoziative array with: + * 'accept_content' => supply Accept: header with 'accept_content' as the value + * 'timeout' => int Timeout in seconds, default system config value or 60 seconds + * 'http_auth' => username:password + * 'novalidate' => do not validate SSL certs, default is to validate using our CA list + * 'cookiejar' => path to cookie jar file + * 'header' => header array + * + * @return CurlResult + */ + public function head(string $url, array $opts = []); + /** * Send a GET to an URL. * * @param string $url URL to fetch - * TRUE if asked to return binary results (file download) * @param array $opts (optional parameters) assoziative array with: * 'accept_content' => supply Accept: header with 'accept_content' as the value * 'timeout' => int Timeout in seconds, default system config value or 60 seconds * 'http_auth' => username:password * 'novalidate' => do not validate SSL certs, default is to validate using our CA list - * 'nobody' => only return the header * 'cookiejar' => path to cookie jar file * 'header' => header array * diff --git a/src/Util/HTTPSignature.php b/src/Util/HTTPSignature.php index e3ed5d0799..61e95a52cc 100644 --- a/src/Util/HTTPSignature.php +++ b/src/Util/HTTPSignature.php @@ -449,7 +449,11 @@ class HTTPSignature $curl_opts = $opts; $curl_opts['header'] = $headers; - $curlResult = DI::httpRequest()->get($request, $curl_opts); + if ($opts['nobody']) { + $curlResult = DI::httpRequest()->head($request, $curl_opts); + } else { + $curlResult = DI::httpRequest()->get($request, $curl_opts); + } $return_code = $curlResult->getReturnCode(); Logger::log('Fetched for user ' . $uid . ' from ' . $request . ' returned ' . $return_code, Logger::DEBUG); -- 2.20.1