Skip to content

Commit e55e425

Browse files
authored
Merge pull request #14363 from nextcloud/debt/cleanup-httpservice
Set User-Agent as header without middleware
2 parents 16b8017 + 6c156d8 commit e55e425

File tree

4 files changed

+106
-107
lines changed

4 files changed

+106
-107
lines changed

lib/private/Http/Client/Client.php

Lines changed: 37 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,11 @@
2525
namespace OC\Http\Client;
2626

2727
use GuzzleHttp\Client as GuzzleClient;
28-
use GuzzleHttp\HandlerStack;
29-
use GuzzleHttp\Middleware;
28+
use GuzzleHttp\RequestOptions;
3029
use OCP\Http\Client\IClient;
3130
use OCP\Http\Client\IResponse;
3231
use OCP\ICertificateManager;
3332
use OCP\IConfig;
34-
use Psr\Http\Message\RequestInterface;
3533

3634
/**
3735
* Class Client
@@ -45,9 +43,6 @@ class Client implements IClient {
4543
private $config;
4644
/** @var ICertificateManager */
4745
private $certificateManager;
48-
private $configured = false;
49-
/** @var HandlerStack */
50-
private $stack;
5146

5247
/**
5348
* @param IConfig $config
@@ -57,74 +52,62 @@ class Client implements IClient {
5752
public function __construct(
5853
IConfig $config,
5954
ICertificateManager $certificateManager,
60-
GuzzleClient $client,
61-
HandlerStack $stack
55+
GuzzleClient $client
6256
) {
6357
$this->config = $config;
6458
$this->client = $client;
65-
$this->stack = $stack;
6659
$this->certificateManager = $certificateManager;
6760
}
6861

69-
/**
70-
* Sets the default options to the client
71-
*/
72-
private function setDefaultOptions() {
73-
if ($this->configured) {
74-
return;
75-
}
76-
$this->configured = true;
62+
private function buildRequestOptions(array $options): array {
63+
$defaults = [
64+
RequestOptions::PROXY => $this->getProxyUri(),
65+
RequestOptions::VERIFY => $this->getCertBundle(),
66+
];
7767

78-
$this->stack->push(Middleware::mapRequest(function (RequestInterface $request) {
79-
return $request
80-
->withHeader('User-Agent', 'Nextcloud Server Crawler');
81-
}));
82-
}
68+
$options = array_merge($defaults, $options);
8369

84-
private function getRequestOptions() {
85-
$options = [
86-
'verify' => $this->getCertBundle(),
87-
];
88-
$proxyUri = $this->getProxyUri();
89-
if ($proxyUri !== '') {
90-
$options['proxy'] = $proxyUri;
70+
if (!isset($options[RequestOptions::HEADERS]['User-Agent'])) {
71+
$options[RequestOptions::HEADERS]['User-Agent'] = 'Nextcloud Server Crawler';
9172
}
73+
9274
return $options;
9375
}
9476

95-
private function getCertBundle() {
77+
private function getCertBundle(): string {
9678
if ($this->certificateManager->listCertificates() !== []) {
9779
return $this->certificateManager->getAbsoluteBundlePath();
98-
} else {
99-
// If the instance is not yet setup we need to use the static path as
100-
// $this->certificateManager->getAbsoluteBundlePath() tries to instantiiate
101-
// a view
102-
if ($this->config->getSystemValue('installed', false)) {
103-
return $this->certificateManager->getAbsoluteBundlePath(null);
104-
} else {
105-
return \OC::$SERVERROOT . '/resources/config/ca-bundle.crt';
106-
}
10780
}
81+
82+
// If the instance is not yet setup we need to use the static path as
83+
// $this->certificateManager->getAbsoluteBundlePath() tries to instantiiate
84+
// a view
85+
if ($this->config->getSystemValue('installed', false)) {
86+
return $this->certificateManager->getAbsoluteBundlePath(null);
87+
}
88+
89+
return \OC::$SERVERROOT . '/resources/config/ca-bundle.crt';
10890
}
10991

11092
/**
11193
* Get the proxy URI
11294
*
113-
* @return string
95+
* @return string|null
11496
*/
115-
private function getProxyUri(): string {
97+
private function getProxyUri(): ?string {
11698
$proxyHost = $this->config->getSystemValue('proxy', null);
117-
$proxyUserPwd = $this->config->getSystemValue('proxyuserpwd', null);
118-
$proxyUri = '';
11999

120-
if ($proxyUserPwd !== null) {
121-
$proxyUri .= $proxyUserPwd . '@';
100+
if ($proxyHost === null) {
101+
return null;
122102
}
123-
if ($proxyHost !== null) {
124-
$proxyUri .= $proxyHost;
103+
104+
$proxyUserPwd = $this->config->getSystemValue('proxyuserpwd', null);
105+
106+
if ($proxyUserPwd === null) {
107+
return $proxyHost;
125108
}
126109

127-
return $proxyUri;
110+
return $proxyUserPwd . '@' . $proxyHost;
128111
}
129112

130113
/**
@@ -157,8 +140,7 @@ private function getProxyUri(): string {
157140
* @throws \Exception If the request could not get completed
158141
*/
159142
public function get(string $uri, array $options = []): IResponse {
160-
$this->setDefaultOptions();
161-
$response = $this->client->request('get', $uri, array_merge($this->getRequestOptions(), $options));
143+
$response = $this->client->request('get', $uri, $this->buildRequestOptions($options));
162144
$isStream = isset($options['stream']) && $options['stream'];
163145
return new Response($response, $isStream);
164146
}
@@ -188,8 +170,7 @@ public function get(string $uri, array $options = []): IResponse {
188170
* @throws \Exception If the request could not get completed
189171
*/
190172
public function head(string $uri, array $options = []): IResponse {
191-
$this->setDefaultOptions();
192-
$response = $this->client->request('head', $uri, array_merge($this->getRequestOptions(), $options));
173+
$response = $this->client->request('head', $uri, $this->buildRequestOptions($options));
193174
return new Response($response);
194175
}
195176

@@ -223,12 +204,11 @@ public function head(string $uri, array $options = []): IResponse {
223204
* @throws \Exception If the request could not get completed
224205
*/
225206
public function post(string $uri, array $options = []): IResponse {
226-
$this->setDefaultOptions();
227207
if (isset($options['body']) && is_array($options['body'])) {
228208
$options['form_params'] = $options['body'];
229209
unset($options['body']);
230210
}
231-
$response = $this->client->request('post', $uri, array_merge($this->getRequestOptions(), $options));
211+
$response = $this->client->request('post', $uri, $this->buildRequestOptions($options));
232212
return new Response($response);
233213
}
234214

@@ -262,8 +242,7 @@ public function post(string $uri, array $options = []): IResponse {
262242
* @throws \Exception If the request could not get completed
263243
*/
264244
public function put(string $uri, array $options = []): IResponse {
265-
$this->setDefaultOptions();
266-
$response = $this->client->request('put', $uri, array_merge($this->getRequestOptions(), $options));
245+
$response = $this->client->request('put', $uri, $this->buildRequestOptions($options));
267246
return new Response($response);
268247
}
269248

@@ -297,12 +276,10 @@ public function put(string $uri, array $options = []): IResponse {
297276
* @throws \Exception If the request could not get completed
298277
*/
299278
public function delete(string $uri, array $options = []): IResponse {
300-
$this->setDefaultOptions();
301-
$response = $this->client->request('delete', $uri, array_merge($this->getRequestOptions(), $options));
279+
$response = $this->client->request('delete', $uri, $this->buildRequestOptions($options));
302280
return new Response($response);
303281
}
304282

305-
306283
/**
307284
* Sends a options request
308285
*
@@ -333,8 +310,7 @@ public function delete(string $uri, array $options = []): IResponse {
333310
* @throws \Exception If the request could not get completed
334311
*/
335312
public function options(string $uri, array $options = []): IResponse {
336-
$this->setDefaultOptions();
337-
$response = $this->client->request('options', $uri, array_merge($this->getRequestOptions(), $options));
313+
$response = $this->client->request('options', $uri, $this->buildRequestOptions($options));
338314
return new Response($response);
339315
}
340316
}

lib/private/Http/Client/ClientService.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
namespace OC\Http\Client;
2525

2626
use GuzzleHttp\Client as GuzzleClient;
27-
use GuzzleHttp\HandlerStack;
2827
use OCP\Http\Client\IClient;
2928
use OCP\Http\Client\IClientService;
3029
use OCP\ICertificateManager;
@@ -55,6 +54,6 @@ public function __construct(IConfig $config,
5554
* @return Client
5655
*/
5756
public function newClient(): IClient {
58-
return new Client($this->config, $this->certificateManager, new GuzzleClient(), HandlerStack::create());
57+
return new Client($this->config, $this->certificateManager, new GuzzleClient());
5958
}
6059
}

tests/lib/Http/Client/ClientServiceTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
namespace Test\Http\Client;
1010

1111
use GuzzleHttp\Client as GuzzleClient;
12-
use GuzzleHttp\HandlerStack;
1312
use OC\Http\Client\Client;
1413
use OC\Http\Client\ClientService;
1514
use OCP\ICertificateManager;
@@ -19,12 +18,16 @@
1918
* Class ClientServiceTest
2019
*/
2120
class ClientServiceTest extends \Test\TestCase {
22-
public function testNewClient() {
21+
public function testNewClient(): void {
22+
/** @var IConfig $config */
2323
$config = $this->createMock(IConfig::class);
24+
/** @var ICertificateManager $certificateManager */
2425
$certificateManager = $this->createMock(ICertificateManager::class);
2526

26-
$expected = new Client($config, $certificateManager, new GuzzleClient(), HandlerStack::create());
2727
$clientService = new ClientService($config, $certificateManager);
28-
$this->assertEquals($expected, $clientService->newClient());
28+
$this->assertEquals(
29+
new Client($config, $certificateManager, new GuzzleClient()),
30+
$clientService->newClient()
31+
);
2932
}
3033
}

0 commit comments

Comments
 (0)