Merge pull request #9437 from nupplaphil/task/httprequest_head
Add HTTPRequest::head() function
This commit is contained in:
commit
feabf80476
6 changed files with 45 additions and 26 deletions
|
@ -85,7 +85,7 @@ function parse_url_content(App $a)
|
||||||
// Check if the URL is an image, video or audio file. If so format
|
// Check if the URL is an image, video or audio file. If so format
|
||||||
// the URL with the corresponding BBCode media tag
|
// the URL with the corresponding BBCode media tag
|
||||||
// Fetch the header of the URL
|
// Fetch the header of the URL
|
||||||
$curlResponse = DI::httpRequest()->get($url, ['novalidate' => true, 'nobody' => true]);
|
$curlResponse = DI::httpRequest()->head($url);
|
||||||
|
|
||||||
if ($curlResponse->isSuccess()) {
|
if ($curlResponse->isSuccess()) {
|
||||||
// Convert the header fields into an array
|
// Convert the header fields into an array
|
||||||
|
|
|
@ -1639,7 +1639,7 @@ class GServer
|
||||||
if (!empty($accesstoken)) {
|
if (!empty($accesstoken)) {
|
||||||
$api = 'https://instances.social/api/1.0/instances/list?count=0';
|
$api = 'https://instances.social/api/1.0/instances/list?count=0';
|
||||||
$header = ['Authorization: Bearer '.$accesstoken];
|
$header = ['Authorization: Bearer '.$accesstoken];
|
||||||
$curlResult = DI::httpRequest()->get($api, ['headers' => $header]);
|
$curlResult = DI::httpRequest()->get($api, ['header' => $header]);
|
||||||
|
|
||||||
if ($curlResult->isSuccess()) {
|
if ($curlResult->isSuccess()) {
|
||||||
$servers = json_decode($curlResult->getBody(), true);
|
$servers = json_decode($curlResult->getBody(), true);
|
||||||
|
|
|
@ -88,19 +88,19 @@ class Magic extends BaseModule
|
||||||
$exp = explode('/profile/', $contact['url']);
|
$exp = explode('/profile/', $contact['url']);
|
||||||
$basepath = $exp[0];
|
$basepath = $exp[0];
|
||||||
|
|
||||||
$headers = [];
|
$header = [];
|
||||||
$headers['Accept'] = 'application/x-dfrn+json, application/x-zot+json';
|
$header['Accept'] = 'application/x-dfrn+json, application/x-zot+json';
|
||||||
$headers['X-Open-Web-Auth'] = Strings::getRandomHex();
|
$header['X-Open-Web-Auth'] = Strings::getRandomHex();
|
||||||
|
|
||||||
// Create a header that is signed with the local users private key.
|
// Create a header that is signed with the local users private key.
|
||||||
$headers = HTTPSignature::createSig(
|
$header = HTTPSignature::createSig(
|
||||||
$headers,
|
$header,
|
||||||
$user['prvkey'],
|
$user['prvkey'],
|
||||||
'acct:' . $user['nickname'] . '@' . DI::baseUrl()->getHostname() . (DI::baseUrl()->getUrlPath() ? '/' . DI::baseUrl()->getUrlPath() : '')
|
'acct:' . $user['nickname'] . '@' . DI::baseUrl()->getHostname() . (DI::baseUrl()->getUrlPath() ? '/' . DI::baseUrl()->getUrlPath() : '')
|
||||||
);
|
);
|
||||||
|
|
||||||
// Try to get an authentication token from the other instance.
|
// Try to get an authentication token from the other instance.
|
||||||
$curlResult = DI::httpRequest()->get($basepath . '/owa', ['headers' => $headers]);
|
$curlResult = DI::httpRequest()->get($basepath . '/owa', ['header' => $header]);
|
||||||
|
|
||||||
if ($curlResult->isSuccess()) {
|
if ($curlResult->isSuccess()) {
|
||||||
$j = json_decode($curlResult->getBody(), true);
|
$j = json_decode($curlResult->getBody(), true);
|
||||||
|
|
|
@ -52,6 +52,17 @@ class HTTPRequest implements IHTTPRequest
|
||||||
$this->baseUrl = $baseUrl->get();
|
$this->baseUrl = $baseUrl->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
*
|
||||||
|
* @throws HTTPException\InternalServerErrorException
|
||||||
|
*/
|
||||||
|
public function head(string $url, array $opts = [])
|
||||||
|
{
|
||||||
|
$opts['nobody'] = true;
|
||||||
|
|
||||||
|
return $this->get($url, $opts);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*
|
*
|
||||||
|
@ -130,6 +141,7 @@ class HTTPRequest implements IHTTPRequest
|
||||||
curl_setopt($ch, CURLOPT_ENCODING, '');
|
curl_setopt($ch, CURLOPT_ENCODING, '');
|
||||||
|
|
||||||
if (!empty($opts['headers'])) {
|
if (!empty($opts['headers'])) {
|
||||||
|
$this->logger->notice('Wrong option \'headers\' used.');
|
||||||
@curl_setopt($ch, CURLOPT_HTTPHEADER, $opts['headers']);
|
@curl_setopt($ch, CURLOPT_HTTPHEADER, $opts['headers']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -172,8 +184,6 @@ class HTTPRequest implements IHTTPRequest
|
||||||
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
|
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
|
||||||
}
|
}
|
||||||
|
|
||||||
$logger = $this->logger;
|
|
||||||
|
|
||||||
$s = @curl_exec($ch);
|
$s = @curl_exec($ch);
|
||||||
$curl_info = @curl_getinfo($ch);
|
$curl_info = @curl_getinfo($ch);
|
||||||
|
|
||||||
|
|
|
@ -29,12 +29,10 @@ interface IHTTPRequest
|
||||||
/**
|
/**
|
||||||
* Fetches the content of an URL
|
* 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")
|
* Set the cookiejar argument to a string (e.g. "/tmp/friendica-cookies.txt")
|
||||||
* to preserve cookies from one request to the next.
|
* to preserve cookies from one request to the next.
|
||||||
*
|
*
|
||||||
* @param string $url URL to 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 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 $accept_content supply Accept: header with 'accept_content' as the value
|
||||||
* @param string $cookiejar Path to cookie jar file
|
* @param string $cookiejar Path to cookie jar file
|
||||||
|
@ -50,7 +48,6 @@ interface IHTTPRequest
|
||||||
* all the information collected during the fetch.
|
* all the information collected during the fetch.
|
||||||
*
|
*
|
||||||
* @param string $url URL to 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 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 $accept_content supply Accept: header with 'accept_content' as the value
|
||||||
* @param string $cookiejar Path to cookie jar file
|
* @param string $cookiejar Path to cookie jar file
|
||||||
|
@ -60,16 +57,26 @@ interface IHTTPRequest
|
||||||
public function fetchFull(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = '');
|
public function fetchFull(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = '');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a GET to an URL.
|
* 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
|
||||||
|
* '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
|
* @param string $url URL to fetch
|
||||||
* TRUE if asked to return binary results (file download)
|
|
||||||
* @param array $opts (optional parameters) assoziative array with:
|
* @param array $opts (optional parameters) assoziative array with:
|
||||||
* 'accept_content' => supply Accept: header with 'accept_content' as the value
|
* 'accept_content' => supply Accept: header with 'accept_content' as the value
|
||||||
* 'timeout' => int Timeout in seconds, default system config value or 60 seconds
|
* '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
|
* 'cookiejar' => path to cookie jar file
|
||||||
* 'header' => header array
|
* 'header' => header array
|
||||||
*
|
*
|
||||||
|
|
|
@ -403,8 +403,6 @@ class HTTPSignature
|
||||||
* @param array $opts (optional parameters) assoziative array with:
|
* @param array $opts (optional parameters) assoziative array with:
|
||||||
* 'accept_content' => supply Accept: header with 'accept_content' as the value
|
* 'accept_content' => supply Accept: header with 'accept_content' as the value
|
||||||
* 'timeout' => int Timeout in seconds, default system config value or 60 seconds
|
* '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
|
* 'nobody' => only return the header
|
||||||
* 'cookiejar' => path to cookie jar file
|
* 'cookiejar' => path to cookie jar file
|
||||||
*
|
*
|
||||||
|
@ -413,7 +411,7 @@ class HTTPSignature
|
||||||
*/
|
*/
|
||||||
public static function fetchRaw($request, $uid = 0, $binary = false, $opts = [])
|
public static function fetchRaw($request, $uid = 0, $binary = false, $opts = [])
|
||||||
{
|
{
|
||||||
$headers = [];
|
$header = [];
|
||||||
|
|
||||||
if (!empty($uid)) {
|
if (!empty($uid)) {
|
||||||
$owner = User::getOwnerDataById($uid);
|
$owner = User::getOwnerDataById($uid);
|
||||||
|
@ -433,23 +431,27 @@ class HTTPSignature
|
||||||
$path = parse_url($request, PHP_URL_PATH);
|
$path = parse_url($request, PHP_URL_PATH);
|
||||||
$date = DateTimeFormat::utcNow(DateTimeFormat::HTTP);
|
$date = DateTimeFormat::utcNow(DateTimeFormat::HTTP);
|
||||||
|
|
||||||
$headers = ['Date: ' . $date, 'Host: ' . $host];
|
$header = ['Date: ' . $date, 'Host: ' . $host];
|
||||||
|
|
||||||
$signed_data = "(request-target): get " . $path . "\ndate: ". $date . "\nhost: " . $host;
|
$signed_data = "(request-target): get " . $path . "\ndate: ". $date . "\nhost: " . $host;
|
||||||
|
|
||||||
$signature = base64_encode(Crypto::rsaSign($signed_data, $owner['uprvkey'], 'sha256'));
|
$signature = base64_encode(Crypto::rsaSign($signed_data, $owner['uprvkey'], 'sha256'));
|
||||||
|
|
||||||
$headers[] = 'Signature: keyId="' . $owner['url'] . '#main-key' . '",algorithm="rsa-sha256",headers="(request-target) date host",signature="' . $signature . '"';
|
$header[] = 'Signature: keyId="' . $owner['url'] . '#main-key' . '",algorithm="rsa-sha256",headers="(request-target) date host",signature="' . $signature . '"';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($opts['accept_content'])) {
|
if (!empty($opts['accept_content'])) {
|
||||||
$headers[] = 'Accept: ' . $opts['accept_content'];
|
$header[] = 'Accept: ' . $opts['accept_content'];
|
||||||
}
|
}
|
||||||
|
|
||||||
$curl_opts = $opts;
|
$curl_opts = $opts;
|
||||||
$curl_opts['header'] = $headers;
|
$curl_opts['header'] = $header;
|
||||||
|
|
||||||
|
if ($opts['nobody']) {
|
||||||
|
$curlResult = DI::httpRequest()->head($request, $curl_opts);
|
||||||
|
} else {
|
||||||
$curlResult = DI::httpRequest()->get($request, $curl_opts);
|
$curlResult = DI::httpRequest()->get($request, $curl_opts);
|
||||||
|
}
|
||||||
$return_code = $curlResult->getReturnCode();
|
$return_code = $curlResult->getReturnCode();
|
||||||
|
|
||||||
Logger::log('Fetched for user ' . $uid . ' from ' . $request . ' returned ' . $return_code, Logger::DEBUG);
|
Logger::log('Fetched for user ' . $uid . ' from ' . $request . ' returned ' . $return_code, Logger::DEBUG);
|
||||||
|
|
Loading…
Reference in a new issue