Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/ActivityPhp/Server/Actor/ActorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
28 changes: 24 additions & 4 deletions src/ActivityPhp/Server/Configuration/AbstractConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

/**
* Abstract methods for configurations classes
*/
*/
abstract class AbstractConfiguration
{
/**
Expand All @@ -25,7 +25,7 @@ public function __construct(array $params = [])
{
$this->setArray($params);
}

/**
* Get a config value
*
Expand All @@ -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
*
Expand All @@ -56,13 +76,13 @@ 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;
}
}
}


}
5 changes: 5 additions & 0 deletions src/ActivityPhp/Server/Configuration/HttpConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
5 changes: 1 addition & 4 deletions src/ActivityPhp/Server/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -82,7 +79,7 @@ public static function validateAcceptHeader($accept, $strict = false)
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')
Expand Down
7 changes: 6 additions & 1 deletion src/ActivityPhp/Server/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,20 @@ class Request
*
* @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];

if ($agent) {
$headers['User-Agent'] = $agent;
}

if (!empty($extra_headers)) {
$headers = array_merge($headers, $extra_headers);
}

$this->client = new Client([
'timeout' => $timeout,
'headers' => $headers
Expand Down
3 changes: 2 additions & 1 deletion src/ActivityPhp/Server/Http/WebFingerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);

Expand Down
15 changes: 15 additions & 0 deletions tests/ActivityPhp/Server/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
);
}
}
Loading