From 9b25ff9fcb7299419c42b457da23ea3bae186157 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 8 Feb 2018 13:39:27 +0100 Subject: adjust httpclient to guzzle6 Signed-off-by: Robin Appelman --- lib/private/Http/Client/Client.php | 59 ++++++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 19 deletions(-) (limited to 'lib/private/Http/Client/Client.php') diff --git a/lib/private/Http/Client/Client.php b/lib/private/Http/Client/Client.php index 4e6843d7b9f..e853099e67f 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,7 @@ class Client implements IClient { */ public function post(string $uri, array $options = []): IResponse { $this->setDefaultOptions(); - $response = $this->client->post($uri, $options); + $response = $this->client->request('post', $uri, array_merge($options, $this->getRequestOptions())); return new Response($response); } @@ -238,7 +259,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 +294,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 +330,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); } } -- cgit v1.2.3 From fe23bb59160ac992ce125f1cc40cc7c2627921a3 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Fri, 9 Feb 2018 17:11:59 +0100 Subject: adjust post body for new guzzle Signed-off-by: Robin Appelman --- lib/private/Http/Client/Client.php | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'lib/private/Http/Client/Client.php') diff --git a/lib/private/Http/Client/Client.php b/lib/private/Http/Client/Client.php index e853099e67f..0387fcabfaf 100644 --- a/lib/private/Http/Client/Client.php +++ b/lib/private/Http/Client/Client.php @@ -224,6 +224,10 @@ class Client implements IClient { */ 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($options, $this->getRequestOptions())); return new Response($response); } -- cgit v1.2.3