diff options
author | Robin Appelman <robin@icewind.nl> | 2018-02-08 13:39:27 +0100 |
---|---|---|
committer | Robin Appelman <robin@icewind.nl> | 2018-02-09 17:13:21 +0100 |
commit | 9b25ff9fcb7299419c42b457da23ea3bae186157 (patch) | |
tree | 9cfaa02ad646bfaf71fc769b3bd8a235650e72cd /lib/private/Http | |
parent | 18215eb01d0812651f4e3ef4dd772002df388a3a (diff) | |
download | nextcloud-server-9b25ff9fcb7299419c42b457da23ea3bae186157.tar.gz nextcloud-server-9b25ff9fcb7299419c42b457da23ea3bae186157.zip |
adjust httpclient to guzzle6
Signed-off-by: Robin Appelman <robin@icewind.nl>
Diffstat (limited to 'lib/private/Http')
-rw-r--r-- | lib/private/Http/Client/Client.php | 59 | ||||
-rw-r--r-- | lib/private/Http/Client/ClientService.php | 3 | ||||
-rw-r--r-- | lib/private/Http/Client/Response.php | 10 |
3 files changed, 47 insertions, 25 deletions
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); } } diff --git a/lib/private/Http/Client/ClientService.php b/lib/private/Http/Client/ClientService.php index 1df54010a2d..fa8544f07a5 100644 --- a/lib/private/Http/Client/ClientService.php +++ b/lib/private/Http/Client/ClientService.php @@ -24,6 +24,7 @@ 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; @@ -54,6 +55,6 @@ class ClientService implements IClientService { * @return Client */ public function newClient(): IClient { - return new Client($this->config, $this->certificateManager, new GuzzleClient()); + return new Client($this->config, $this->certificateManager, new GuzzleClient(), HandlerStack::create()); } } diff --git a/lib/private/Http/Client/Response.php b/lib/private/Http/Client/Response.php index 0ce6cc98e0d..73c14c2926d 100644 --- a/lib/private/Http/Client/Response.php +++ b/lib/private/Http/Client/Response.php @@ -25,7 +25,7 @@ declare(strict_types=1); namespace OC\Http\Client; use OCP\Http\Client\IResponse; -use GuzzleHttp\Message\ResponseInterface as GuzzleResponse; +use Psr\Http\Message\ResponseInterface; /** * Class Response @@ -33,7 +33,7 @@ use GuzzleHttp\Message\ResponseInterface as GuzzleResponse; * @package OC\Http */ class Response implements IResponse { - /** @var GuzzleResponse */ + /** @var ResponseInterface */ private $response; /** @@ -42,10 +42,10 @@ class Response implements IResponse { private $stream; /** - * @param GuzzleResponse $response + * @param ResponseInterface $response * @param bool $stream */ - public function __construct(GuzzleResponse $response, $stream = false) { + public function __construct(ResponseInterface $response, $stream = false) { $this->response = $response; $this->stream = $stream; } @@ -71,7 +71,7 @@ class Response implements IResponse { * @return string */ public function getHeader(string $key): string { - return $this->response->getHeader($key); + return $this->response->getHeader($key)[0]; } /** |