summaryrefslogtreecommitdiffstats
path: root/lib/private/Http
diff options
context:
space:
mode:
authorDaniel Kesselberg <mail@danielkesselberg.de>2019-02-24 14:48:05 +0100
committerDaniel Kesselberg <mail@danielkesselberg.de>2019-04-16 21:13:29 +0200
commit2708d264071fc33d81b53a8f33de2074e4210a76 (patch)
tree79eec4f1150fd28eb8b8993285a03d936d20a633 /lib/private/Http
parent5df12cbf47ecdd25a637a06ce4b65c799de8fa72 (diff)
downloadnextcloud-server-2708d264071fc33d81b53a8f33de2074e4210a76.tar.gz
nextcloud-server-2708d264071fc33d81b53a8f33de2074e4210a76.zip
Set User-Agent as header without middleware
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
Diffstat (limited to 'lib/private/Http')
-rw-r--r--lib/private/Http/Client/Client.php89
-rw-r--r--lib/private/Http/Client/ClientService.php3
2 files changed, 35 insertions, 57 deletions
diff --git a/lib/private/Http/Client/Client.php b/lib/private/Http/Client/Client.php
index 03b09bf54b7..baa358f2fea 100644
--- a/lib/private/Http/Client/Client.php
+++ b/lib/private/Http/Client/Client.php
@@ -25,13 +25,11 @@ declare(strict_types=1);
namespace OC\Http\Client;
use GuzzleHttp\Client as GuzzleClient;
-use GuzzleHttp\HandlerStack;
-use GuzzleHttp\Middleware;
+use GuzzleHttp\RequestOptions;
use OCP\Http\Client\IClient;
use OCP\Http\Client\IResponse;
use OCP\ICertificateManager;
use OCP\IConfig;
-use Psr\Http\Message\RequestInterface;
/**
* Class Client
@@ -45,9 +43,6 @@ class Client implements IClient {
private $config;
/** @var ICertificateManager */
private $certificateManager;
- private $configured = false;
- /** @var HandlerStack */
- private $stack;
/**
* @param IConfig $config
@@ -57,66 +52,57 @@ class Client implements IClient {
public function __construct(
IConfig $config,
ICertificateManager $certificateManager,
- GuzzleClient $client,
- HandlerStack $stack
+ GuzzleClient $client
) {
$this->config = $config;
$this->client = $client;
- $this->stack = $stack;
$this->certificateManager = $certificateManager;
}
- /**
- * Sets the default options to the client
- */
- private function setDefaultOptions() {
- if ($this->configured) {
- return;
- }
- $this->configured = true;
+ private function buildRequestOptions(array $options): array {
+ $defaults = [
+ RequestOptions::PROXY => $this->getProxyUri(),
+ RequestOptions::VERIFY => $this->getCertBundle(),
+ ];
- $this->stack->push(Middleware::mapRequest(function (RequestInterface $request) {
- return $request
- ->withHeader('User-Agent', 'Nextcloud Server Crawler');
- }));
- }
+ $options = array_merge($defaults, $options);
- private function getRequestOptions() {
- $options = [
- 'verify' => $this->getCertBundle(),
- ];
- $proxyUri = $this->getProxyUri();
- if ($proxyUri !== '') {
- $options['proxy'] = $proxyUri;
+ if (!isset($options[RequestOptions::HEADERS]['User-Agent'])) {
+ $options[RequestOptions::HEADERS]['User-Agent'] = 'Nextcloud Server Crawler';
}
+
return $options;
}
- private function getCertBundle() {
+ private function getCertBundle(): string {
if ($this->certificateManager->listCertificates() !== []) {
return $this->certificateManager->getAbsoluteBundlePath();
- } else {
- // If the instance is not yet setup we need to use the static path as
- // $this->certificateManager->getAbsoluteBundlePath() tries to instantiiate
- // a view
- if ($this->config->getSystemValue('installed', false)) {
- return $this->certificateManager->getAbsoluteBundlePath(null);
- } else {
- return \OC::$SERVERROOT . '/resources/config/ca-bundle.crt';
- }
}
+
+ // If the instance is not yet setup we need to use the static path as
+ // $this->certificateManager->getAbsoluteBundlePath() tries to instantiiate
+ // a view
+ if ($this->config->getSystemValue('installed', false)) {
+ return $this->certificateManager->getAbsoluteBundlePath(null);
+ }
+
+ return \OC::$SERVERROOT . '/resources/config/ca-bundle.crt';
}
/**
* Get the proxy URI
*
- * @return string
+ * @return string|null
*/
- private function getProxyUri(): string {
+ private function getProxyUri(): ?string {
$proxyHost = $this->config->getSystemValue('proxy', null);
$proxyUserPwd = $this->config->getSystemValue('proxyuserpwd', null);
- $proxyUri = '';
+ if ($proxyHost === null && $proxyUserPwd === null) {
+ return null;
+ }
+
+ $proxyUri = '';
if ($proxyUserPwd !== null) {
$proxyUri .= $proxyUserPwd . '@';
}
@@ -157,8 +143,7 @@ class Client implements IClient {
* @throws \Exception If the request could not get completed
*/
public function get(string $uri, array $options = []): IResponse {
- $this->setDefaultOptions();
- $response = $this->client->request('get', $uri, array_merge($this->getRequestOptions(), $options));
+ $response = $this->client->request('get', $uri, $this->buildRequestOptions($options));
$isStream = isset($options['stream']) && $options['stream'];
return new Response($response, $isStream);
}
@@ -188,8 +173,7 @@ class Client implements IClient {
* @throws \Exception If the request could not get completed
*/
public function head(string $uri, array $options = []): IResponse {
- $this->setDefaultOptions();
- $response = $this->client->request('head', $uri, array_merge($this->getRequestOptions(), $options));
+ $response = $this->client->request('head', $uri, $this->buildRequestOptions($options));
return new Response($response);
}
@@ -223,12 +207,11 @@ class Client implements IClient {
* @throws \Exception If the request could not get completed
*/
public function post(string $uri, array $options = []): IResponse {
- $this->setDefaultOptions();
if (isset($options['body']) && is_array($options['body'])) {
$options['form_params'] = $options['body'];
unset($options['body']);
}
- $response = $this->client->request('post', $uri, array_merge($this->getRequestOptions(), $options));
+ $response = $this->client->request('post', $uri, $this->buildRequestOptions($options));
return new Response($response);
}
@@ -262,8 +245,7 @@ class Client implements IClient {
* @throws \Exception If the request could not get completed
*/
public function put(string $uri, array $options = []): IResponse {
- $this->setDefaultOptions();
- $response = $this->client->request('put', $uri, array_merge($this->getRequestOptions(), $options));
+ $response = $this->client->request('put', $uri, $this->buildRequestOptions($options));
return new Response($response);
}
@@ -297,12 +279,10 @@ class Client implements IClient {
* @throws \Exception If the request could not get completed
*/
public function delete(string $uri, array $options = []): IResponse {
- $this->setDefaultOptions();
- $response = $this->client->request('delete', $uri, array_merge($this->getRequestOptions(), $options));
+ $response = $this->client->request('delete', $uri, $this->buildRequestOptions($options));
return new Response($response);
}
-
/**
* Sends a options request
*
@@ -333,8 +313,7 @@ class Client implements IClient {
* @throws \Exception If the request could not get completed
*/
public function options(string $uri, array $options = []): IResponse {
- $this->setDefaultOptions();
- $response = $this->client->request('options', $uri, array_merge($this->getRequestOptions(), $options));
+ $response = $this->client->request('options', $uri, $this->buildRequestOptions($options));
return new Response($response);
}
}
diff --git a/lib/private/Http/Client/ClientService.php b/lib/private/Http/Client/ClientService.php
index fa8544f07a5..1df54010a2d 100644
--- a/lib/private/Http/Client/ClientService.php
+++ b/lib/private/Http/Client/ClientService.php
@@ -24,7 +24,6 @@ declare(strict_types=1);
namespace OC\Http\Client;
use GuzzleHttp\Client as GuzzleClient;
-use GuzzleHttp\HandlerStack;
use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
use OCP\ICertificateManager;
@@ -55,6 +54,6 @@ class ClientService implements IClientService {
* @return Client
*/
public function newClient(): IClient {
- return new Client($this->config, $this->certificateManager, new GuzzleClient(), HandlerStack::create());
+ return new Client($this->config, $this->certificateManager, new GuzzleClient());
}
}