]> git.mxchange.org Git - friendica.git/commitdiff
Fixup HTTP headers for httpClient requests
authorPhilipp <admin@philipp.info>
Tue, 24 Aug 2021 12:17:42 +0000 (14:17 +0200)
committerPhilipp <admin@philipp.info>
Wed, 25 Aug 2021 12:22:43 +0000 (14:22 +0200)
12 files changed:
src/Core/Search.php
src/Model/GServer.php
src/Model/Storage/ExternalResource.php
src/Module/Proxy.php
src/Network/HTTPClient.php
src/Network/Probe.php
src/Protocol/DFRN.php
src/Protocol/Diaspora.php
src/Protocol/OStatus.php
src/Protocol/Salmon.php
src/Util/HTTPSignature.php
src/Worker/PubSubPublish.php

index a3588b3bd3d53443f5bdac1d57e21f8f7070d3a7..efe8331b856f3a583aa7ebce4f366ac13a22f02e 100644 (file)
@@ -227,7 +227,7 @@ class Search
                        $return = Contact::searchByName($search, $mode);
                } else {
                        $p = $page > 1 ? 'p=' . $page : '';
-                       $curlResult = DI::httpRequest()->get(self::getGlobalDirectory() . '/search/people?' . $p . '&q=' . urlencode($search), ['accept_content' => 'application/json']);
+                       $curlResult = DI::httpRequest()->get(self::getGlobalDirectory() . '/search/people?' . $p . '&q=' . urlencode($search), ['accept_content' => ['application/json']]);
                        if ($curlResult->isSuccess()) {
                                $searchResult = json_decode($curlResult->getBody(), true);
                                if (!empty($searchResult['profiles'])) {
index eb99b1bbc8fa06677adb74d6aa1b8fb4881f9ac9..15bf727e527d17e17edfcbbc9f4bc5e3e028f78e 100644 (file)
@@ -1728,8 +1728,7 @@ class GServer
 
                if (!empty($accesstoken)) {
                        $api = 'https://instances.social/api/1.0/instances/list?count=0';
-                       $header = ['Authorization: Bearer '.$accesstoken];
-                       $curlResult = DI::httpRequest()->get($api, ['header' => $header]);
+                       $curlResult = DI::httpRequest()->get($api, ['header' => ['Authorization' => ['Bearer ' . $accesstoken]]]);
 
                        if ($curlResult->isSuccess()) {
                                $servers = json_decode($curlResult->getBody(), true);
index 918bcf8ac315b9d1f867e93ad2c1a38ccdd9ee26..413050c306aa563aa71ea91d6129db07bdfa242b 100644 (file)
@@ -50,7 +50,7 @@ class ExternalResource implements IStorage
                }
 
                try {
-                       $fetchResult = HTTPSignature::fetchRaw($data->url, $data->uid, ['accept_content' => '']);
+                       $fetchResult = HTTPSignature::fetchRaw($data->url, $data->uid, ['accept_content' => []]);
                } catch (Exception $exception) {
                        throw new ReferenceStorageException(sprintf('External resource failed to get %s', $reference), $exception->getCode(), $exception);
                }
index 04fe00db1248a8ed07e726c525b256f9b75986a7..cd11d9e3abb2c25cfd19f4c853908acf08a8fb0d 100644 (file)
@@ -75,7 +75,7 @@ class Proxy extends BaseModule
                $request['url'] = str_replace(' ', '+', $request['url']);
 
                // Fetch the content with the local user
-               $fetchResult = HTTPSignature::fetchRaw($request['url'], local_user(), ['accept_content' => '', 'timeout' => 10]);
+               $fetchResult = HTTPSignature::fetchRaw($request['url'], local_user(), ['accept_content' => [], 'timeout' => 10]);
                $img_str = $fetchResult->getBody();
 
                if (!$fetchResult->isSuccess() || empty($img_str)) {
index d2ea596f16eb3421fb00715bf8fb039a31532338..cd1d9e46b30a3fe3fa21306e12fe07c279d42a3b 100644 (file)
@@ -103,7 +103,7 @@ class HTTPClient implements IHTTPClient
                $header = [];
 
                if (!empty($opts['accept_content'])) {
-                       array_push($header, 'Accept: ' . $opts['accept_content']);
+                       $header['Accept'] = $opts['accept_content'];
                }
 
                if (!empty($opts['header'])) {
index 4cacfedf408f81d8f2b5f0f0d019e8ffc60d37d9..bdc12aadc1a967c6346480c3dd66470881ab21f3 100644 (file)
@@ -170,7 +170,7 @@ class Probe
                Logger::info('Probing', ['host' => $host, 'ssl_url' => $ssl_url, 'url' => $url, 'callstack' => System::callstack(20)]);
                $xrd = null;
 
-               $curlResult = DI::httpRequest()->get($ssl_url, ['timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml']);
+               $curlResult = DI::httpRequest()->get($ssl_url, ['timeout' => $xrd_timeout, 'accept_content' => ['application/xrd+xml']]);
                $ssl_connection_error = ($curlResult->getErrorNumber() == CURLE_COULDNT_CONNECT) || ($curlResult->getReturnCode() == 0);
                if ($curlResult->isSuccess()) {
                        $xml = $curlResult->getBody();
@@ -187,7 +187,7 @@ class Probe
                }
 
                if (!is_object($xrd) && !empty($url)) {
-                       $curlResult = DI::httpRequest()->get($url, ['timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml']);
+                       $curlResult = DI::httpRequest()->get($url, ['timeout' => $xrd_timeout, 'accept_content' => ['application/xrd+xml']]);
                        $connection_error = ($curlResult->getErrorNumber() == CURLE_COULDNT_CONNECT) || ($curlResult->getReturnCode() == 0);
                        if ($curlResult->isTimeout()) {
                                Logger::info('Probing timeout', ['url' => $url]);
@@ -940,7 +940,7 @@ class Probe
        {
                $xrd_timeout = DI::config()->get('system', 'xrd_timeout', 20);
 
-               $curlResult = DI::httpRequest()->get($url, ['timeout' => $xrd_timeout, 'accept_content' => $type]);
+               $curlResult = DI::httpRequest()->get($url, ['timeout' => $xrd_timeout, 'accept_content' => [$type]]);
                if ($curlResult->isTimeout()) {
                        self::$istimeout = true;
                        return [];
index cd63291eed16bda98f3ce376f3bd8d85bdeb709e..141bce54270561107a4234b5a2102f7327baacc7 100644 (file)
@@ -976,7 +976,7 @@ class DFRN
 
                $content_type = ($public_batch ? "application/magic-envelope+xml" : "application/json");
 
-               $postResult = DI::httpRequest()->post($dest_url, $envelope, ["Content-Type: " . $content_type]);
+               $postResult = DI::httpRequest()->post($dest_url, $envelope, ['Content-Type' => $content_type]);
                $xml = $postResult->getBody();
 
                $curl_stat = $postResult->getReturnCode();
index b38e0506b9dd33c128beb5b83ef4cf917a2908b8..32c0ce0428d9cfed6ad9913fbef29a2ecb3b27ab 100644 (file)
@@ -3022,7 +3022,7 @@ class Diaspora
                if (!intval(DI::config()->get("system", "diaspora_test"))) {
                        $content_type = (($public_batch) ? "application/magic-envelope+xml" : "application/json");
 
-                       $postResult = DI::httpRequest()->post($dest_url . "/", $envelope, ["Content-Type: " . $content_type]);
+                       $postResult = DI::httpRequest()->post($dest_url . "/", $envelope, ['Content-Type' => $content_type]);
                        $return_code = $postResult->getReturnCode();
                } else {
                        Logger::log("test_mode");
index 215c08fa08dca9d1749b37e73f8cbf15e474e4fe..5a0135f9cb4b90c454b11d6af1990e5bd1ee7b9c 100644 (file)
@@ -727,7 +727,7 @@ class OStatus
 
                self::$conv_list[$conversation] = true;
 
-               $curlResult = DI::httpRequest()->get($conversation, ['accept_content' => 'application/atom+xml, text/html']);
+               $curlResult = DI::httpRequest()->get($conversation, ['accept_content' => ['application/atom+xml', 'text/html']]);
 
                if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
                        return;
@@ -921,7 +921,7 @@ class OStatus
                }
 
                $stored = false;
-               $curlResult = DI::httpRequest()->get($related, ['accept_content' => 'application/atom+xml, text/html']);
+               $curlResult = DI::httpRequest()->get($related, ['accept_content' => ['application/atom+xml', 'text/html']]);
 
                if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
                        return;
index 53367f6d092976eb632a5a644d51e57e57025747..8d17f96783238c76242d0bd29d4194b9f627d35f 100644 (file)
@@ -156,8 +156,8 @@ class Salmon
 
                // slap them
                $postResult = DI::httpRequest()->post($url, $salmon, [
-                       'Content-typeapplication/magic-envelope+xml',
-                       'Content-length: ' . strlen($salmon)
+                       'Content-type' => 'application/magic-envelope+xml',
+                       'Content-length' => strlen($salmon),
                ]);
 
                $return_code = $postResult->getReturnCode();
@@ -181,8 +181,8 @@ class Salmon
 
                        // slap them
                        $postResult = DI::httpRequest()->post($url, $salmon, [
-                               'Content-typeapplication/magic-envelope+xml',
-                               'Content-length: ' . strlen($salmon)
+                               'Content-type' => 'application/magic-envelope+xml',
+                               'Content-length' => strlen($salmon),
                        ]);
                        $return_code = $postResult->getReturnCode();
                }
@@ -204,8 +204,8 @@ class Salmon
 
                        // slap them
                        $postResult = DI::httpRequest()->post($url, $salmon, [
-                               'Content-typeapplication/magic-envelope+xml',
-                               'Content-length: ' . strlen($salmon)]);
+                               'Content-type' => 'application/magic-envelope+xml',
+                               'Content-length' => strlen($salmon)]);
                        $return_code = $postResult->getReturnCode();
                }
 
index e2de810a606a32c8b33c2ac812e6e859accada65..cf3e1294f2054b3f10a1a34615b3a063284ba735 100644 (file)
@@ -29,6 +29,7 @@ use Friendica\Model\APContact;
 use Friendica\Model\Contact;
 use Friendica\Model\User;
 use Friendica\Network\CurlResult;
+use Friendica\Network\IHTTPResult;
 
 /**
  * Implements HTTP Signatures per draft-cavage-http-signatures-07.
@@ -290,15 +291,20 @@ class HTTPSignature
                $content_length = strlen($content);
                $date = DateTimeFormat::utcNow(DateTimeFormat::HTTP);
 
-               $headers = ['Date: ' . $date, 'Content-Length: ' . $content_length, 'Digest: ' . $digest, 'Host: ' . $host];
+               $headers = [
+                       'Date' => $date,
+                       'Content-Length' => $content_length,
+                       'Digest' => $digest,
+                       'Host' => $host
+               ];
 
                $signed_data = "(request-target): post " . $path . "\ndate: ". $date . "\ncontent-length: " . $content_length . "\ndigest: " . $digest . "\nhost: " . $host;
 
                $signature = base64_encode(Crypto::rsaSign($signed_data, $owner['uprvkey'], 'sha256'));
 
-               $headers[] = 'Signature: keyId="' . $owner['url'] . '#main-key' . '",algorithm="rsa-sha256",headers="(request-target) date content-length digest host",signature="' . $signature . '"';
+               $headers['Signature'] = 'keyId="' . $owner['url'] . '#main-key' . '",algorithm="rsa-sha256",headers="(request-target) date content-length digest host",signature="' . $signature . '"';
 
-               $headers[] = 'Content-Type: application/activity+json';
+               $headers['Content-Type'] = 'application/activity+json';
 
                $postResult = DI::httpRequest()->post($target, $content, $headers);
                $return_code = $postResult->getReturnCode();
@@ -409,10 +415,10 @@ class HTTPSignature
         *                         'nobody' => only return the header
         *                         'cookiejar' => path to cookie jar file
         *
-        * @return CurlResult CurlResult
+        * @return IHTTPResult CurlResult
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       public static function fetchRaw($request, $uid = 0, $opts = ['accept_content' => 'application/activity+json, application/ld+json'])
+       public static function fetchRaw($request, $uid = 0, $opts = ['accept_content' => ['application/activity+json', 'application/ld+json']])
        {
                $header = [];
 
@@ -434,17 +440,14 @@ class HTTPSignature
                        $path = parse_url($request, PHP_URL_PATH);
                        $date = DateTimeFormat::utcNow(DateTimeFormat::HTTP);
 
-                       $header = ['Date: ' . $date, 'Host: ' . $host];
+                       $header['Date'] = $date;
+                       $header['Host'] = $host;
 
                        $signed_data = "(request-target): get " . $path . "\ndate: ". $date . "\nhost: " . $host;
 
                        $signature = base64_encode(Crypto::rsaSign($signed_data, $owner['uprvkey'], 'sha256'));
 
-                       $header[] = 'Signature: keyId="' . $owner['url'] . '#main-key' . '",algorithm="rsa-sha256",headers="(request-target) date host",signature="' . $signature . '"';
-               }
-
-               if (!empty($opts['accept_content'])) {
-                       $header[] = 'Accept: ' . $opts['accept_content'];
+                       $header['Signature'] = 'keyId="' . $owner['url'] . '#main-key' . '",algorithm="rsa-sha256",headers="(request-target) date host",signature="' . $signature . '"';
                }
 
                $curl_opts = $opts;
index a5381c18d16a6af23684cb01cfc73b451deaee5e..a3e2ee4ad09130d8961ac68e114374e998d09192 100644 (file)
@@ -59,11 +59,12 @@ class PubSubPublish
 
                $hmac_sig = hash_hmac("sha1", $params, $subscriber['secret']);
 
-               $headers = ["Content-type: application/atom+xml",
-                               sprintf("Link: <%s>;rel=hub,<%s>;rel=self",
+               $headers = [
+                       'Content-type' => 'application/atom+xml',
+                       'Link' => sprintf("<%s>;rel=hub,<%s>;rel=self",
                                        DI::baseUrl() . '/pubsubhubbub/' . $subscriber['nickname'],
                                        $subscriber['topic']),
-                               "X-Hub-Signature: sha1=" . $hmac_sig];
+                       'X-Hub-Signature' => 'sha1=' . $hmac_sig];
 
                Logger::log('POST ' . print_r($headers, true) . "\n" . $params, Logger::DATA);