ProxyController   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 0
loc 128
ccs 61
cts 61
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
C proxy() 0 88 13
1
<?php
2
/**
3
 * ownCloud - Calendar App
4
 *
5
 * @author Georg Ehrke
6
 * @copyright 2016 Georg Ehrke <[email protected]>
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
10
 * License as published by the Free Software Foundation; either
11
 * version 3 of the License, or any later version.
12
 *
13
 * This library 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
19
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
namespace OCA\Calendar\Controller;
23
24
use GuzzleHttp\Exception\RequestException;
25
use GuzzleHttp\Exception\ClientException;
26
use GuzzleHttp\Exception\ConnectException;
27
28
use OCA\Calendar\Http\StreamResponse;
29
30
use OCP\AppFramework\Http;
31
use OCP\AppFramework\Http\JSONResponse;
32
use OCP\AppFramework\Controller;
33
use OCP\Http\Client\IClientService;
34
use OCP\IL10N;
35
use OCP\ILogger;
36
use OCP\IRequest;
37
38
class ProxyController extends Controller {
39
40
	/**
41
	 * @var IClientService
42
	 */
43
	protected $client;
44
45
	/**
46
	 * @var IL10N
47
	 */
48
	protected $l10n;
49
50
	/**
51
	 * @var ILogger
52
	 */
53
	protected $logger;
54
55
	/**
56
	 * @param string $appName
57
	 * @param IRequest $request an instance of the request
58
	 * @param IClientService $client
59
	 * @param IL10N $l10n
60
	 * @param ILogger $logger
61
	 */
62 10
	public function __construct($appName, IRequest $request,
63
								IClientService $client,
64
								IL10N $l10n, ILogger $logger) {
65 10
		parent::__construct($appName, $request);
66 10
		$this->client = $client;
67 10
		$this->l10n = $l10n;
68 10
		$this->logger = $logger;
69 10
	}
70
71
	/**
72
	 * @NoAdminRequired
73
	 *
74
	 * @param $url
75
	 * @return StreamResponse|JSONResponse
76
	 */
77 10
	public function proxy($url) {
78 10
		$client = $this->client->newClient();
79
		try {
80 10
			$queryUrl = $url;
81 10
			$allow_redirects = false; // try to handle redirects manually at first
82 10
			$redirect_count = 0;
83 10
			$max_redirects = 5;
84 10
			$done = false;
85
86
			// try to find a chain of 301s
87
			do {
88 10
				$clientResponse = $client->get($queryUrl, [
89 10
					'stream' => true,
90 10
					'allow_redirects' => $allow_redirects,
91
				]);
92
93 6
				$statusCode = $clientResponse->getStatusCode();
94 6
				if ($statusCode === 301) { // 400+ goes straight to catch
95 4
					$redirect_count++;
96 4
					$queryUrl = $clientResponse->getHeader('Location');
97 5
				} elseif ($statusCode >= 300) {
98 2
					if ($redirect_count > 0) {
99
						// $redirect_count > 0 => There have been 301s before,
100
						// break and return Location from last 301
101 1
						break;
102
					} else {
103
						// this is the very first request
104
						// it's being redirected, but the redirection is not
105
						// permanently, just let Guzzle take care
106
						$allow_redirects = [
107 1
							'max' => $max_redirects
108
						];
109
					}
110
				} else {
111 4
					$done = true;
112
				}
113 6
			} while(!$done && $redirect_count <= $max_redirects);
114
115 6
			if ($redirect_count > 0 && $redirect_count <= $max_redirects) {
116 3
				$response = new JSONResponse([
117 3
					'proxy_code' => -4,
118 3
					'new_url' => $queryUrl,
119 3
				], Http::STATUS_BAD_REQUEST);
120 3
			} elseif ($done) {
121 2
				$response = new StreamResponse($clientResponse->getBody());
122 2
				$response->setHeaders([
123 2
					'Content-Type' => 'text/calendar',
124
				]);
125
			} else {
126 1
				$response = new JSONResponse([
127 1
					'message' => $this->l10n->t('Too many redirects. Aborting ...'),
128
					'proxy_code' => -3
129 6
				], Http::STATUS_UNPROCESSABLE_ENTITY);
130
			}
131 4
		} catch (ClientException $e) {
0 ignored issues
show
Bug introduced by
The class GuzzleHttp\Exception\ClientException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
132 1
			$error_code = $e->getResponse()->getStatusCode();
133 1
			$message = $e->getMessage();
134
135 1
			$this->logger->debug($message);
136 1
			$response = new JSONResponse([
137 1
				'message' => $this->l10n->t('The remote server did not give us access to the calendar (HTTP {%s} error)', [
138 1
					(string) $error_code
139
				]),
140 1
				'proxy_code' => $error_code
141 1
			], Http::STATUS_UNPROCESSABLE_ENTITY);
142 3
		} catch (ConnectException $e) {
0 ignored issues
show
Bug introduced by
The class GuzzleHttp\Exception\ConnectException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
143 1
			$this->logger->debug($e->getMessage());
144 1
			$response = new JSONResponse([
145 1
				'message' => $this->l10n->t('Error connecting to remote server'),
146
				'proxy_code' => -1
147 1
			], Http::STATUS_UNPROCESSABLE_ENTITY);
148 2
		} catch (RequestException $e) {
0 ignored issues
show
Bug introduced by
The class GuzzleHttp\Exception\RequestException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
149 2
			$this->logger->debug($e->getMessage());
150
151 2
			if (substr($url, 0, 8) === 'https://') {
152 1
				$message = $this->l10n->t('Error requesting resource on remote server. This could possibly be related to a certificate mismatch');
153
			} else {
154 1
				$message = $this->l10n->t('Error requesting resource on remote server');
155
			}
156
157 2
			$response = new JSONResponse([
158 2
				'message' => $message,
159
				'proxy_code' => -2,
160 2
			], Http::STATUS_UNPROCESSABLE_ENTITY);
161
		}
162
163 10
		return $response;
164
	}
165
}
166