Completed
Push — master ( c77917...12b0e9 )
by Jörn Friedrich
57:44
created

Client::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * @author Lukas Reschke <[email protected]>
4
 * @author Robin Appelman <[email protected]>
5
 *
6
 * @copyright Copyright (c) 2015, ownCloud, Inc.
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace OC\Http\Client;
24
25
use GuzzleHttp\Client as GuzzleClient;
26
use OCP\Http\Client\IClient;
27
use OCP\ICertificateManager;
28
use OCP\IConfig;
29
30
/**
31
 * Class Client
32
 *
33
 * @package OC\Http
34
 */
35
class Client implements IClient {
36
	/** @var GuzzleClient */
37
	private $client;
38
	/** @var IConfig */
39
	private $config;
40
	/** @var ICertificateManager */
41
	private $certificateManager;
42
43
	/**
44
	 * @param IConfig $config
45
	 * @param ICertificateManager $certificateManager
46
	 * @param GuzzleClient $client
47
	 */
48 9
	public function __construct(IConfig $config,
49
								ICertificateManager $certificateManager,
50
								GuzzleClient $client) {
51 9
		$this->config = $config;
52 9
		$this->client = $client;
53 9
		$this->certificateManager = $certificateManager;
54 9
		$this->setDefaultOptions();
55 9
	}
56
57
	/**
58
	 * Sets the default options to the client
59
	 */
60 9
	private function setDefaultOptions() {
61
		// Either use default bundle or the user bundle if nothing is specified
62 9
		if($this->certificateManager->listCertificates() !== []) {
63 9
			$dataDir = $this->config->getSystemValue('datadirectory');
64 9
			$this->client->setDefaultOption('verify', $dataDir.'/'.$this->certificateManager->getCertificateBundle());
65 9
		} else {
66
			$this->client->setDefaultOption('verify', \OC::$SERVERROOT . '/resources/config/ca-bundle.crt');
67
		}
68
69 9
		$this->client->setDefaultOption('headers/User-Agent', 'ownCloud Server Crawler');
70 9
		if($this->getProxyUri() !== '') {
71
			$this->client->setDefaultOption('proxy', $this->getProxyUri());
72
		}
73 9
	}
74
75
	/**
76
	 * Get the proxy URI
77
	 * @return string
78
	 */
79 9
	private function getProxyUri() {
80 9
		$proxyHost = $this->config->getSystemValue('proxy', null);
81 9
		$proxyUserPwd = $this->config->getSystemValue('proxyuserpwd', null);
82 9
		$proxyUri = '';
83
84 9
		if(!is_null($proxyUserPwd)) {
85 1
			$proxyUri .= $proxyUserPwd.'@';
86 1
		}
87 9
		if(!is_null($proxyHost)) {
88 2
			$proxyUri .= $proxyHost;
89 2
		}
90
91 9
		return $proxyUri;
92
	}
93
94
	/**
95
	 * Sends a GET request
96
	 * @param string $uri
97
	 * @param array $options Array such as
98
	 *              'query' => [
99
	 *                  'field' => 'abc',
100
	 *                  'other_field' => '123',
101
	 *                  'file_name' => fopen('/path/to/file', 'r'),
102
	 *              ],
103
	 *              'headers' => [
104
	 *                  'foo' => 'bar',
105
	 *              ],
106
	 *              'cookies' => ['
107
	 *                  'foo' => 'bar',
108
	 *              ],
109
	 *              'allow_redirects' => [
110
	 *                   'max'       => 10,  // allow at most 10 redirects.
111
	 *                   'strict'    => true,     // use "strict" RFC compliant redirects.
112
	 *                   'referer'   => true,     // add a Referer header
113
	 *                   'protocols' => ['https'] // only allow https URLs
114
	 *              ],
115
	 *              'save_to' => '/path/to/file', // save to a file or a stream
116
	 *              'verify' => true, // bool or string to CA file
117
	 *              'debug' => true,
118
	 *              'timeout' => 5,
119
	 * @return Response
120
	 * @throws \Exception If the request could not get completed
121
	 */
122 1
	public function get($uri, array $options = []) {
123 1
		$response = $this->client->get($uri, $options);
124 1
		$isStream = isset($options['stream']) && $options['stream'];
125 1
		return new Response($response, $isStream);
126
	}
127
128
	/**
129
	 * Sends a HEAD request
130
	 * @param string $uri
131
	 * @param array $options Array such as
132
	 *              'headers' => [
133
	 *                  'foo' => 'bar',
134
	 *              ],
135
	 *              'cookies' => ['
136
	 *                  'foo' => 'bar',
137
	 *              ],
138
	 *              'allow_redirects' => [
139
	 *                   'max'       => 10,  // allow at most 10 redirects.
140
	 *                   'strict'    => true,     // use "strict" RFC compliant redirects.
141
	 *                   'referer'   => true,     // add a Referer header
142
	 *                   'protocols' => ['https'] // only allow https URLs
143
	 *              ],
144
	 *              'save_to' => '/path/to/file', // save to a file or a stream
145
	 *              'verify' => true, // bool or string to CA file
146
	 *              'debug' => true,
147
	 *              'timeout' => 5,
148
	 * @return Response
149
	 * @throws \Exception If the request could not get completed
150
	 */
151
	public function head($uri, $options = []) {
152
		$response = $this->client->head($uri, $options);
153
		return new Response($response);
154
	}
155
156
	/**
157
	 * Sends a POST request
158
	 * @param string $uri
159
	 * @param array $options Array such as
160
	 *              'body' => [
161
	 *                  'field' => 'abc',
162
	 *                  'other_field' => '123',
163
	 *                  'file_name' => fopen('/path/to/file', 'r'),
164
	 *              ],
165
	 *              'headers' => [
166
	 *                  'foo' => 'bar',
167
	 *              ],
168
	 *              'cookies' => ['
169
	 *                  'foo' => 'bar',
170
	 *              ],
171
	 *              'allow_redirects' => [
172
	 *                   'max'       => 10,  // allow at most 10 redirects.
173
	 *                   'strict'    => true,     // use "strict" RFC compliant redirects.
174
	 *                   'referer'   => true,     // add a Referer header
175
	 *                   'protocols' => ['https'] // only allow https URLs
176
	 *              ],
177
	 *              'save_to' => '/path/to/file', // save to a file or a stream
178
	 *              'verify' => true, // bool or string to CA file
179
	 *              'debug' => true,
180
	 *              'timeout' => 5,
181
	 * @return Response
182
	 * @throws \Exception If the request could not get completed
183
	 */
184 1
	public function post($uri, array $options = []) {
185 1
		$response = $this->client->post($uri, $options);
186 1
		return new Response($response);
187
	}
188
189
	/**
190
	 * Sends a PUT request
191
	 * @param string $uri
192
	 * @param array $options Array such as
193
	 *              'body' => [
194
	 *                  'field' => 'abc',
195
	 *                  'other_field' => '123',
196
	 *                  'file_name' => fopen('/path/to/file', 'r'),
197
	 *              ],
198
	 *              'headers' => [
199
	 *                  'foo' => 'bar',
200
	 *              ],
201
	 *              'cookies' => ['
202
	 *                  'foo' => 'bar',
203
	 *              ],
204
	 *              'allow_redirects' => [
205
	 *                   'max'       => 10,  // allow at most 10 redirects.
206
	 *                   'strict'    => true,     // use "strict" RFC compliant redirects.
207
	 *                   'referer'   => true,     // add a Referer header
208
	 *                   'protocols' => ['https'] // only allow https URLs
209
	 *              ],
210
	 *              'save_to' => '/path/to/file', // save to a file or a stream
211
	 *              'verify' => true, // bool or string to CA file
212
	 *              'debug' => true,
213
	 *              'timeout' => 5,
214
	 * @return Response
215
	 * @throws \Exception If the request could not get completed
216
	 */
217 1
	public function put($uri, array $options = []) {
218 1
		$response = $this->client->put($uri, $options);
219 1
		return new Response($response);
220
	}
221
222
	/**
223
	 * Sends a DELETE request
224
	 * @param string $uri
225
	 * @param array $options Array such as
226
	 *              'body' => [
227
	 *                  'field' => 'abc',
228
	 *                  'other_field' => '123',
229
	 *                  'file_name' => fopen('/path/to/file', 'r'),
230
	 *              ],
231
	 *              'headers' => [
232
	 *                  'foo' => 'bar',
233
	 *              ],
234
	 *              'cookies' => ['
235
	 *                  'foo' => 'bar',
236
	 *              ],
237
	 *              'allow_redirects' => [
238
	 *                   'max'       => 10,  // allow at most 10 redirects.
239
	 *                   'strict'    => true,     // use "strict" RFC compliant redirects.
240
	 *                   'referer'   => true,     // add a Referer header
241
	 *                   'protocols' => ['https'] // only allow https URLs
242
	 *              ],
243
	 *              'save_to' => '/path/to/file', // save to a file or a stream
244
	 *              'verify' => true, // bool or string to CA file
245
	 *              'debug' => true,
246
	 *              'timeout' => 5,
247
	 * @return Response
248
	 * @throws \Exception If the request could not get completed
249
	 */
250 1
	public function delete($uri, array $options = []) {
251 1
		$response = $this->client->delete($uri, $options);
252 1
		return new Response($response);
253
	}
254
255
256
	/**
257
	 * Sends a options request
258
	 * @param string $uri
259
	 * @param array $options Array such as
260
	 *              'body' => [
261
	 *                  'field' => 'abc',
262
	 *                  'other_field' => '123',
263
	 *                  'file_name' => fopen('/path/to/file', 'r'),
264
	 *              ],
265
	 *              'headers' => [
266
	 *                  'foo' => 'bar',
267
	 *              ],
268
	 *              'cookies' => ['
269
	 *                  'foo' => 'bar',
270
	 *              ],
271
	 *              'allow_redirects' => [
272
	 *                   'max'       => 10,  // allow at most 10 redirects.
273
	 *                   'strict'    => true,     // use "strict" RFC compliant redirects.
274
	 *                   'referer'   => true,     // add a Referer header
275
	 *                   'protocols' => ['https'] // only allow https URLs
276
	 *              ],
277
	 *              'save_to' => '/path/to/file', // save to a file or a stream
278
	 *              'verify' => true, // bool or string to CA file
279
	 *              'debug' => true,
280
	 *              'timeout' => 5,
281
	 * @return Response
282
	 * @throws \Exception If the request could not get completed
283
	 */
284 1
	public function options($uri, array $options = []) {
285 1
		$response = $this->client->options($uri, $options);
286 1
		return new Response($response);
287
	}
288
}
289