Skip to content

Commit

Permalink
Add a default value for user agent
Browse files Browse the repository at this point in the history
  • Loading branch information
landrok committed Oct 29, 2020
1 parent c43e768 commit f44ba12
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 13 deletions.
17 changes: 17 additions & 0 deletions docs/server/server-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,23 @@ $server = new Server([
]);
```

**agent**

When talking to other federated servers, an instance
uses the default user agent *ActivityPhp/x.y.z (+https://{host})*.

It can be customized with agent parameter.

```php
use ActivityPhp\Server;

$server = new Server([
'http' => [
'agent' => 'MyCustomUserAgent'
],
]);
```

________________________________________________________________________


Expand Down
2 changes: 1 addition & 1 deletion src/ActivityPhp/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public function actor(string $handle)
/**
* Get server instance with a static call
*/
public static function singleton(array $settings = []): self
public static function server(array $settings = []): self
{
if (is_null(self::$singleton)) {
self::$singleton = new self($settings);
Expand Down
12 changes: 6 additions & 6 deletions src/ActivityPhp/Server/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ public function dispatchParameters(array $params): void
{
// Create default configuration for each component
foreach ([
'cache',
'logger',
'instance',
'http',
'dialects',
'ontologies'
'cache',
'logger',
'instance',
'http',
'dialects',
'ontologies'
] as $config
) {

Expand Down
45 changes: 39 additions & 6 deletions src/ActivityPhp/Server/Configuration/HttpConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,13 @@
namespace ActivityPhp\Server\Configuration;

use ActivityPhp\Server;
use ActivityPhp\Version;

/**
* Server HTTP configuration stack
*/
class HttpConfiguration extends AbstractConfiguration
{
/**
* @var string default HTTP scheme
*/
protected $scheme = 'https';

/**
* @var float|int HTTP timeout for request
*/
Expand All @@ -31,7 +27,7 @@ class HttpConfiguration extends AbstractConfiguration
/**
* @var string The User Agent.
*/
protected $agent = '';
protected $agent;

/**
* Dispatch configuration parameters
Expand All @@ -41,5 +37,42 @@ class HttpConfiguration extends AbstractConfiguration
public function __construct(array $params = [])
{
parent::__construct($params);

// Configure a default value for user agent
if (is_null($this->agent)) {
$this->agent = $this->getUserAgent();
}
}

/**
* Prepare a default value for user agent
*/
private function getUserAgent(): string
{
$scheme = Server::server()->config('instance.scheme');
$host = Server::server()->config('instance.host');
$port = Server::server()->config('instance.port');

if ($port == 443 && $scheme == 'https') {
$port = null;
}

if ($port == 80 && $scheme == 'http') {
$port = null;
}

$url = sprintf(
'%s://%s%s',
$scheme,
$host,
is_null($port) ? '' : ":{$port}"
);

return sprintf(
'%s/%s (+%s)',
Version::getRootNamespace(),
Version::getVersion(),
$url
);
}
}
63 changes: 63 additions & 0 deletions tests/ActivityPhp/Server/ServerHttpTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace ActivityPhpTest\Server;

use ActivityPhp\Server;
use PHPUnit\Framework\TestCase;

class ServerHttpTest extends TestCase
{
public function testDefaultUserAgent()
{
$server = new Server([
'logger' => [
'driver' => '\Psr\Log\NullLogger'
],
'cache' => [
'enabled' => false,
],
'http' => [],
'instance' => [
'host' => 'custom.federated',
],
]);

//
// PHPUnit >= 9
if (method_exists($this, 'assertMatchesRegularExpression')) {
$this->assertMatchesRegularExpression(
'/ActivityPhp\/\d.\d.\d \(\+https:\/\/custom.federated\)/',
Server::server()->config('http.agent')
);
// PHPUnit < 9
} else {
$this->assertRegExp(
'/ActivityPhp\/\d.\d.\d \(\+https:\/\/custom.federated\)/',
Server::server()->config('http.agent')
);
}
}

public function testUserAgentCustomization()
{
$server = new Server([
'logger' => [
'driver' => '\Psr\Log\NullLogger'
],
'cache' => [
'enabled' => false,
],
'http' => [
'agent' => "MyUserAgent"
],
'instance' => [
'host' => 'custom.federated',
],
]);

$this->assertEquals(
Server::server()->config('http.agent'),
"MyUserAgent"
);
}
}

0 comments on commit f44ba12

Please sign in to comment.