Skip to content

Commit 4d120d2

Browse files
authored
Merge pull request #1914 from NatLibFi/set-user-agent-version
Include version number and website URL in HTTP User-Agent header
2 parents a8978e9 + d733ac0 commit 4d120d2

File tree

6 files changed

+26
-4
lines changed

6 files changed

+26
-4
lines changed

src/model/Model.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,17 @@ public function getVersion(): string
103103
return $ver;
104104
}
105105

106+
/**
107+
* Return a User-Agent header for this Skosmos installation, including the
108+
* name of the software (Skosmos), the version number and URL.
109+
* @return string userAgent
110+
*/
111+
public function getUserAgent(): string
112+
{
113+
$version = $this->getVersion();
114+
return "Skosmos/$version (https://skosmos.org/)";
115+
}
116+
106117
/**
107118
* Return all the vocabularies available.
108119
* @param boolean $categories whether you want everything included in a subarray of

src/model/resolver/LOCResource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function resolve(int $timeout): ?EasyRdf\Resource
1212

1313
try {
1414
$opts = array('http' => array('method' => 'HEAD',
15-
'user_agent' => 'Skosmos',
15+
'user_agent' => $this->model->getUserAgent(),
1616
'timeout' => $timeout));
1717
$context = stream_context_create($opts);
1818
$fd = fopen($this->uri, 'rb', false, $context);

src/model/resolver/LinkedDataResource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public function resolve(int $timeout): ?EasyRdf\Resource
77
try {
88
// change the timeout setting for external requests
99
$httpclient = EasyRdf\Http::getDefaultHttpClient();
10-
$httpclient->setConfig(array('timeout' => $timeout, 'useragent' => 'Skosmos'));
10+
$httpclient->setConfig(array('timeout' => $timeout, 'useragent' => $this->model->getUserAgent()));
1111
EasyRdf\Http::setDefaultHttpClient($httpclient);
1212

1313
$graph = EasyRdf\Graph::newAndLoad(EasyRdf\Utils::removeFragmentFromUri($this->uri));

src/model/resolver/WDQSResource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public function resolve(int $timeout): ?EasyRdf\Resource
99
try {
1010
// change the timeout setting for external requests
1111
$httpclient = EasyRdf\Http::getDefaultHttpClient();
12-
$httpclient->setConfig(array('timeout' => $timeout, 'useragent' => 'Skosmos'));
12+
$httpclient->setConfig(array('timeout' => $timeout, 'useragent' => $this->model->getUserAgent()));
1313
$httpclient->setHeaders('Accept', 'text/turtle');
1414
EasyRdf\Http::setDefaultHttpClient($httpclient);
1515

src/model/sparql/GenericSparql.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ protected function initializeHttpClient()
155155
// configure the HTTP client used by EasyRdf\Sparql\Client
156156
$httpclient = EasyRdf\Http::getDefaultHttpClient();
157157
$httpclient->setConfig(array('timeout' => $this->model->getConfig()->getSparqlTimeout(),
158-
'useragent' => 'Skosmos'));
158+
'useragent' => $this->model->getUserAgent()));
159159

160160
// if special cache control (typically no-cache) was requested by the
161161
// client, set the same type of cache control headers also in subsequent

tests/ModelTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ public function testGetVersion()
4848
);
4949
}
5050

51+
/**
52+
* @covers Model::getUserAgent
53+
*/
54+
public function testGetUserAgent()
55+
{
56+
$userAgent = $this->model->getUserAgent();
57+
$version = $this->model->getVersion();
58+
$expected = "Skosmos/$version (https://skosmos.org/)";
59+
$this->assertEquals($expected, $userAgent);
60+
}
61+
5162
/**
5263
* @covers Model::getVocabularyList
5364
*/

0 commit comments

Comments
 (0)