summaryrefslogtreecommitdiffstats
path: root/lib/private/Http/Client/Client.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/Http/Client/Client.php')
-rw-r--r--lib/private/Http/Client/Client.php63
1 files changed, 44 insertions, 19 deletions
diff --git a/lib/private/Http/Client/Client.php b/lib/private/Http/Client/Client.php
index 4e6843d7b9f..0387fcabfaf 100644
--- a/lib/private/Http/Client/Client.php
+++ b/lib/private/Http/Client/Client.php
@@ -25,10 +25,13 @@ declare(strict_types=1);
namespace OC\Http\Client;
use GuzzleHttp\Client as GuzzleClient;
+use GuzzleHttp\HandlerStack;
+use GuzzleHttp\Middleware;
use OCP\Http\Client\IClient;
use OCP\Http\Client\IResponse;
use OCP\ICertificateManager;
use OCP\IConfig;
+use Psr\Http\Message\RequestInterface;
/**
* Class Client
@@ -43,17 +46,23 @@ class Client implements IClient {
/** @var ICertificateManager */
private $certificateManager;
private $configured = false;
+ /** @var HandlerStack */
+ private $stack;
/**
* @param IConfig $config
* @param ICertificateManager $certificateManager
* @param GuzzleClient $client
*/
- public function __construct(IConfig $config,
- ICertificateManager $certificateManager,
- GuzzleClient $client) {
+ public function __construct(
+ IConfig $config,
+ ICertificateManager $certificateManager,
+ GuzzleClient $client,
+ HandlerStack $stack
+ ) {
$this->config = $config;
$this->client = $client;
+ $this->stack = $stack;
$this->certificateManager = $certificateManager;
}
@@ -65,25 +74,37 @@ class Client implements IClient {
return;
}
$this->configured = true;
- // Either use user bundle or the system bundle if nothing is specified
+
+ $this->stack->push(Middleware::mapRequest(function (RequestInterface $request) {
+ return $request
+ ->withHeader('User-Agent', 'Nextcloud Server Crawler');
+ }));
+ }
+
+ private function getRequestOptions() {
+ $options = [
+ 'verify' => $this->getCertBundle(),
+ ];
+ $proxyUri = $this->getProxyUri();
+ if ($proxyUri !== '') {
+ $options['proxy'] = $proxyUri;
+ }
+ return $options;
+ }
+
+ private function getCertBundle() {
if ($this->certificateManager->listCertificates() !== []) {
- $this->client->setDefaultOption('verify', $this->certificateManager->getAbsoluteBundlePath());
+ 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)) {
- $this->client->setDefaultOption('verify', $this->certificateManager->getAbsoluteBundlePath(null));
+ return $this->certificateManager->getAbsoluteBundlePath(null);
} else {
- $this->client->setDefaultOption('verify', \OC::$SERVERROOT . '/resources/config/ca-bundle.crt');
+ return \OC::$SERVERROOT . '/resources/config/ca-bundle.crt';
}
}
-
- $this->client->setDefaultOption('headers/User-Agent', 'Nextcloud Server Crawler');
- $proxyUri = $this->getProxyUri();
- if ($proxyUri !== '') {
- $this->client->setDefaultOption('proxy', $proxyUri);
- }
}
/**
@@ -137,7 +158,7 @@ class Client implements IClient {
*/
public function get(string $uri, array $options = []): IResponse {
$this->setDefaultOptions();
- $response = $this->client->get($uri, $options);
+ $response = $this->client->request('get', $uri, array_merge($options, $this->getRequestOptions()));
$isStream = isset($options['stream']) && $options['stream'];
return new Response($response, $isStream);
}
@@ -168,7 +189,7 @@ class Client implements IClient {
*/
public function head(string $uri, array $options = []): IResponse {
$this->setDefaultOptions();
- $response = $this->client->head($uri, $options);
+ $response = $this->client->request('head', $uri, array_merge($options, $this->getRequestOptions()));
return new Response($response);
}
@@ -203,7 +224,11 @@ class Client implements IClient {
*/
public function post(string $uri, array $options = []): IResponse {
$this->setDefaultOptions();
- $response = $this->client->post($uri, $options);
+ if (isset($options['body']) && is_array($options['body'])) {
+ $options['form_params'] = $options['body'];
+ unset($options['body']);
+ }
+ $response = $this->client->request('post', $uri, array_merge($options, $this->getRequestOptions()));
return new Response($response);
}
@@ -238,7 +263,7 @@ class Client implements IClient {
*/
public function put(string $uri, array $options = []): IResponse {
$this->setDefaultOptions();
- $response = $this->client->put($uri, $options);
+ $response = $this->client->request('put', $uri, array_merge($options, $this->getRequestOptions()));
return new Response($response);
}
@@ -273,7 +298,7 @@ class Client implements IClient {
*/
public function delete(string $uri, array $options = []): IResponse {
$this->setDefaultOptions();
- $response = $this->client->delete($uri, $options);
+ $response = $this->client->request('delete', $uri, array_merge($options, $this->getRequestOptions()));
return new Response($response);
}
@@ -309,7 +334,7 @@ class Client implements IClient {
*/
public function options(string $uri, array $options = []): IResponse {
$this->setDefaultOptions();
- $response = $this->client->options($uri, $options);
+ $response = $this->client->request('options', $uri, array_merge($options, $this->getRequestOptions()));
return new Response($response);
}
}