From 95485cc75f148210ebc9a6bd5d1ebeee385a672c Mon Sep 17 00:00:00 2001 From: swentel Date: Sat, 11 Apr 2026 11:52:21 +0200 Subject: [PATCH 1/4] Allow passing options to Helper::fetch --- src/ActivityPhp/Server/Helper.php | 5 +++-- src/ActivityPhp/Server/Http/Request.php | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/ActivityPhp/Server/Helper.php b/src/ActivityPhp/Server/Helper.php index 030d0f6..8958ea0 100644 --- a/src/ActivityPhp/Server/Helper.php +++ b/src/ActivityPhp/Server/Helper.php @@ -77,9 +77,10 @@ public static function validateAcceptHeader($accept, $strict = false) * * @param string $url * @param float|int $timeout + * @param array $options * @return array */ - public static function fetch($url, $timeout = 10.0) + public static function fetch($url, $timeout = 10.0, array $options = []) { return Util::decodeJson( (new HttpRequest($timeout)) @@ -87,7 +88,7 @@ public static function fetch($url, $timeout = 10.0) Server::server()->config('http')->get('retries'), Server::server()->config('http')->get('sleep') ) - ->get($url) + ->get($url, $options) ); } } diff --git a/src/ActivityPhp/Server/Http/Request.php b/src/ActivityPhp/Server/Http/Request.php index 121ad92..55c5904 100644 --- a/src/ActivityPhp/Server/Http/Request.php +++ b/src/ActivityPhp/Server/Http/Request.php @@ -120,16 +120,17 @@ protected function getMethod() * Execute a GET request * * @param string $url + * @param array $options * @return string */ - public function get(string $url) + public function get(string $url, array $options = []) { if (CacheHelper::has($url)) { return CacheHelper::get($url); } try { - $content = $this->client->get($url)->getBody()->getContents(); + $content = $this->client->get($url, $options)->getBody()->getContents(); } catch (Exception $e) { Server::server()->logger()->error( __METHOD__ . ':failure', From eef7cebc13be452e32861f03603683d3194a0698 Mon Sep 17 00:00:00 2001 From: swentel Date: Sat, 11 Apr 2026 12:46:53 +0200 Subject: [PATCH 2/4] Add static function to set options --- src/ActivityPhp/Server/Http/Request.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/ActivityPhp/Server/Http/Request.php b/src/ActivityPhp/Server/Http/Request.php index 55c5904..f15dbd3 100644 --- a/src/ActivityPhp/Server/Http/Request.php +++ b/src/ActivityPhp/Server/Http/Request.php @@ -63,6 +63,13 @@ class Request */ protected $retryCounter = 0; + /** + * HTTP options + * + * @var array + */ + protected static $options = []; + /** * Set HTTP client * @@ -116,6 +123,16 @@ protected function getMethod() return $this->method; } + /** + * Set HTTP options + * + * @param array $options + */ + public static function setOptions(array $options) + { + self::$options = $options; + } + /** * Execute a GET request * @@ -129,6 +146,10 @@ public function get(string $url, array $options = []) return CacheHelper::get($url); } + if (empty($options)) { + $options = self::$options; + } + try { $content = $this->client->get($url, $options)->getBody()->getContents(); } catch (Exception $e) { From d89abb7a4d88350324f1ba5a8db66faf4c6d6943 Mon Sep 17 00:00:00 2001 From: swentel Date: Sat, 11 Apr 2026 13:29:09 +0200 Subject: [PATCH 3/4] Add options in retry --- src/ActivityPhp/Server/Http/Request.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ActivityPhp/Server/Http/Request.php b/src/ActivityPhp/Server/Http/Request.php index f15dbd3..651f2e7 100644 --- a/src/ActivityPhp/Server/Http/Request.php +++ b/src/ActivityPhp/Server/Http/Request.php @@ -166,7 +166,7 @@ public function get(string $url, array $options = []) ['url' => $url] ); sleep($this->sleepBeforeRetry); - return $this->get($url); + return $this->get($url, $options); } throw new Exception($e->getMessage()); From d5b7ef3c33031ebf0c87446c31d96d6c8a281625 Mon Sep 17 00:00:00 2001 From: swentel Date: Fri, 1 May 2026 20:05:09 +0200 Subject: [PATCH 4/4] Add a setter on config --- src/ActivityPhp/Server/Actor/ActorFactory.php | 3 +- .../Configuration/AbstractConfiguration.php | 28 ++++++++++++--- .../Configuration/HttpConfiguration.php | 5 +++ src/ActivityPhp/Server/Helper.php | 10 ++---- src/ActivityPhp/Server/Http/Request.php | 35 +++++-------------- .../Server/Http/WebFingerFactory.php | 3 +- .../ActivityPhp/Server/ConfigurationTest.php | 15 ++++++++ 7 files changed, 60 insertions(+), 39 deletions(-) diff --git a/src/ActivityPhp/Server/Actor/ActorFactory.php b/src/ActivityPhp/Server/Actor/ActorFactory.php index b162045..b7802fe 100644 --- a/src/ActivityPhp/Server/Actor/ActorFactory.php +++ b/src/ActivityPhp/Server/Actor/ActorFactory.php @@ -46,7 +46,8 @@ public static function create(string $url) $content = json_decode( (new Request( self::$server->config('http.timeout'), - self::$server->config('http.agent') + self::$server->config('http.agent'), + self::$server->config('http.headers') ))->get($url), true ); diff --git a/src/ActivityPhp/Server/Configuration/AbstractConfiguration.php b/src/ActivityPhp/Server/Configuration/AbstractConfiguration.php index 9e3910f..5f8ce78 100644 --- a/src/ActivityPhp/Server/Configuration/AbstractConfiguration.php +++ b/src/ActivityPhp/Server/Configuration/AbstractConfiguration.php @@ -15,7 +15,7 @@ /** * Abstract methods for configurations classes - */ + */ abstract class AbstractConfiguration { /** @@ -25,7 +25,7 @@ public function __construct(array $params = []) { $this->setArray($params); } - + /** * Get a config value * @@ -41,6 +41,26 @@ public function get($key) throw new Exception("'$key' parameter does not exist"); } + /** + * Set a config value + * + * @param string $key + * @param mixed $value + */ + public function set($key, $value) { + if (!is_string($key)) { + throw new Exception( + "Configuration key must be a string" + ); + } elseif (!isset($this->$key) && !property_exists($this, $key)) { + throw new Exception( + "Configuration parameter '$key' does not exist" + ); + } else { + $this->$key = $value; + } + } + /** * Set configuration values by array * @@ -56,7 +76,7 @@ public function setArray(array $settings) } elseif (!isset($this->$key) && !property_exists($this, $key)) { throw new Exception( "Configuration parameter '$key' does not exist" - ); + ); } else { // @todo Should be validated $this->$key = $value; @@ -64,5 +84,5 @@ public function setArray(array $settings) } } - + } diff --git a/src/ActivityPhp/Server/Configuration/HttpConfiguration.php b/src/ActivityPhp/Server/Configuration/HttpConfiguration.php index 43e7389..bc5e64a 100644 --- a/src/ActivityPhp/Server/Configuration/HttpConfiguration.php +++ b/src/ActivityPhp/Server/Configuration/HttpConfiguration.php @@ -39,6 +39,11 @@ class HttpConfiguration extends AbstractConfiguration */ protected $sleep = 5; + /** + * @var array HTTP headers to send with each request + */ + protected $headers = []; + /** * Dispatch configuration parameters * diff --git a/src/ActivityPhp/Server/Helper.php b/src/ActivityPhp/Server/Helper.php index 8958ea0..9168b00 100644 --- a/src/ActivityPhp/Server/Helper.php +++ b/src/ActivityPhp/Server/Helper.php @@ -11,10 +11,7 @@ namespace ActivityPhp\Server; -use DateTime; use Exception; -use DateInterval; -use ActivityPhp\Type; use ActivityPhp\Server; use ActivityPhp\Type\Util; use ActivityPhp\Server\Http\Request as HttpRequest; @@ -77,18 +74,17 @@ public static function validateAcceptHeader($accept, $strict = false) * * @param string $url * @param float|int $timeout - * @param array $options * @return array */ - public static function fetch($url, $timeout = 10.0, array $options = []) + public static function fetch($url, $timeout = 10.0) { return Util::decodeJson( - (new HttpRequest($timeout)) + (new HttpRequest($timeout, Server::server()->config('http.agent'), Server::server()->config('http.headers'))) ->setMaxRetries( Server::server()->config('http')->get('retries'), Server::server()->config('http')->get('sleep') ) - ->get($url, $options) + ->get($url) ); } } diff --git a/src/ActivityPhp/Server/Http/Request.php b/src/ActivityPhp/Server/Http/Request.php index 651f2e7..c6b7800 100644 --- a/src/ActivityPhp/Server/Http/Request.php +++ b/src/ActivityPhp/Server/Http/Request.php @@ -63,20 +63,14 @@ class Request */ protected $retryCounter = 0; - /** - * HTTP options - * - * @var array - */ - protected static $options = []; - /** * Set HTTP client * * @param float|int $timeout * @param string $agent + * @param array $extra_headers */ - public function __construct($timeout = 10.0, $agent = '') + public function __construct($timeout = 10.0, $agent = '', $extra_headers = []) { $headers = ['Accept' => self::HTTP_HEADER_ACCEPT]; @@ -84,6 +78,10 @@ public function __construct($timeout = 10.0, $agent = '') $headers['User-Agent'] = $agent; } + if (!empty($extra_headers)) { + $headers = array_merge($headers, $extra_headers); + } + $this->client = new Client([ 'timeout' => $timeout, 'headers' => $headers @@ -123,35 +121,20 @@ protected function getMethod() return $this->method; } - /** - * Set HTTP options - * - * @param array $options - */ - public static function setOptions(array $options) - { - self::$options = $options; - } - /** * Execute a GET request * * @param string $url - * @param array $options * @return string */ - public function get(string $url, array $options = []) + public function get(string $url) { if (CacheHelper::has($url)) { return CacheHelper::get($url); } - if (empty($options)) { - $options = self::$options; - } - try { - $content = $this->client->get($url, $options)->getBody()->getContents(); + $content = $this->client->get($url)->getBody()->getContents(); } catch (Exception $e) { Server::server()->logger()->error( __METHOD__ . ':failure', @@ -166,7 +149,7 @@ public function get(string $url, array $options = []) ['url' => $url] ); sleep($this->sleepBeforeRetry); - return $this->get($url, $options); + return $this->get($url); } throw new Exception($e->getMessage()); diff --git a/src/ActivityPhp/Server/Http/WebFingerFactory.php b/src/ActivityPhp/Server/Http/WebFingerFactory.php index cde19d5..1687bf7 100644 --- a/src/ActivityPhp/Server/Http/WebFingerFactory.php +++ b/src/ActivityPhp/Server/Http/WebFingerFactory.php @@ -70,7 +70,8 @@ public static function get(string $handle, string $scheme = 'https') $content = Util::decodeJson( (new Request( self::$server->config('http.timeout'), - self::$server->config('http.agent') + self::$server->config('http.agent'), + self::$server->config('http.headers'), ))->get($url) ); diff --git a/tests/ActivityPhp/Server/ConfigurationTest.php b/tests/ActivityPhp/Server/ConfigurationTest.php index 15d0592..e86ca4d 100644 --- a/tests/ActivityPhp/Server/ConfigurationTest.php +++ b/tests/ActivityPhp/Server/ConfigurationTest.php @@ -44,4 +44,19 @@ public function testFailingOnNonExistingParameter() $config->getConfig('https'); } + + /** + * Check a call of set() on a configuration key. + */ + public function testSetter() + { + $server = new server(); + + $server->config('http')->set('headers', ['X-Test' => 'test']); + + $this->assertEquals( + ['X-Test' => 'test'], + $server->config('http')->get('headers') + ); + } }