Completed
Pull Request — master (#355)
by Victor
03:31
created

Request::getHost()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 18

Duplication

Lines 6
Ratio 26.09 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 6
loc 23
ccs 0
cts 19
cp 0
rs 8.5906
cc 5
eloc 18
nc 5
nop 0
crap 30
1
<?php
2
3
/**
4
 * @author Victor Dubiniuk <[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 Owncloud\Updater\Http;
24
25
class Request {
26
	protected $vars;
27
28 4
	public function __construct($vars = []){
29 4
		$this->vars = $vars;
30 4
	}
31
32
	/**
33
	 * Returns the request uri
34
	 * @return string
35
	 */
36
	public function getRequestUri() {
37
		return $this->server('REQUEST_URI');
38
	}
39
40
	public function getServerProtocol() {
41
		$forwardedProto = $this->server('HTTP_X_FORWARDED_PROTO');
42
		if (!is_null($forwardedProto)) {
43 View Code Duplication
			if (strpos($forwardedProto, ',') !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
				$parts = explode(',', $forwardedProto);
45
				$proto = strtolower(trim($parts[0]));
46
			} else {
47
				$proto = strtolower($forwardedProto);
48
			}
49
50
			// Verify that the protocol is always HTTP or HTTPS
51
			// default to http if an invalid value is provided
52
			return $proto === 'https' ? 'https' : 'http';
53
		}
54
55
		$isHttps = $this->server('HTTPS');
56
		if ($isHttps !== null
57
			&& $isHttps !== 'off'
58
			&& $isHttps !== ''
59
		) {
60
			return 'https';
61
		}
62
63
		return 'http';
64
	}
65
66
	public function getHost(){
67
		$host = 'localhost';
68
		$forwardedHost = $this->server('HTTP_X_FORWARDED_HOST');
69
		if (!is_null($forwardedHost)) {
70 View Code Duplication
			if (strpos($forwardedHost, ',') !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
				$parts = explode(',', $forwardedHost);
72
				$host = trim(current($parts));
73
			} else {
74
				$host = $forwardedHost;
75
			}
76
		} else {
77
			$httpHost = $this->server('HTTP_HOST');
78
			if (is_null($httpHost)) {
79
				$serverName = $this->server('SERVER_NAME');
80
				if (!is_null($serverName)){
81
					$host = $serverName;
82
				}
83
			} else {
84
				$host = $httpHost;
85
			}
86
		}
87
		return $host;
88
	}
89
90
	/**
91
	 * @param string $name
92
	 * @return mixed
93
	 */
94 4
	public function postParameter($name){
95 4
		return isset($this->vars['post'][$name]) ? $this->vars['post'][$name] : null;
96
	}
97
98
	/**
99
	 * @param string $name
100
	 * @return mixed
101
	 */
102
	public function header($name) {
103
		$name = strtoupper($name);
104
		return isset($this->vars['headers']['HTTP_'.$name]) ? $this->vars['headers']['HTTP_'.$name] : null;
105
	}
106
107
	/**
108
	 * @param string $name
109
	 * @return mixed
110
	 */
111
	public function server($name){
112
		return isset($this->vars['headers'][$name]) ? $this->vars['headers'][$name] : null;
113
	}
114
115
}
116
117