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

Request   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 71.1%

Importance

Changes 5
Bugs 0 Features 2
Metric Value
wmc 19
c 5
b 0
f 2
lcom 1
cbo 0
dl 0
loc 95
ccs 32
cts 45
cp 0.711
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getRequestUri() 0 3 1
B getServerProtocol() 0 19 6
A getHost() 0 18 4
A postParameter() 0 3 2
A header() 0 4 1
A server() 0 3 2
A getPartBeforeComma() 0 9 2
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 18
	public function __construct($vars = []){
29 18
		$this->vars = $vars;
30 18
	}
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
			$proto = strtolower($this->getPartBeforeComma($forwardedProto));
44
			// Verify that the protocol is always HTTP or HTTPS
45
			// default to http if an invalid value is provided
46
			return $proto === 'https' ? 'https' : 'http';
47
		}
48
49
		$isHttps = $this->server('HTTPS');
50
		if ($isHttps !== null
51
			&& $isHttps !== 'off'
52
			&& $isHttps !== ''
53
		) {
54
			return 'https';
55
		}
56
57
		return 'http';
58
	}
59
60 5
	public function getHost(){
61 5
		$host = 'localhost';
62 5
		$forwardedHost = $this->server('HTTP_X_FORWARDED_HOST');
63 5
		if (!is_null($forwardedHost)) {
64 3
			$host = $this->getPartBeforeComma($forwardedHost);
65 3
		} else {
66 2
			$httpHost = $this->server('HTTP_HOST');
67 2
			if (is_null($httpHost)) {
68 1
				$serverName = $this->server('SERVER_NAME');
69 1
				if (!is_null($serverName)){
70 1
					$host = $serverName;
71 1
				}
72 1
			} else {
73 1
				$host = $httpHost;
74
			}
75
		}
76 5
		return $host;
77
	}
78
79
	/**
80
	 * @param string $name
81
	 * @return mixed
82
	 */
83 4
	public function postParameter($name){
84 4
		return isset($this->vars['post'][$name]) ? $this->vars['post'][$name] : null;
85
	}
86
87
	/**
88
	 * @param string $name
89
	 * @return mixed
90
	 */
91 5
	public function header($name) {
92 5
		$name = strtoupper($name);
93 5
		return $this->server('HTTP_'.$name);
94
	}
95
96
	/**
97
	 * @param string $name
98
	 * @return mixed
99
	 */
100 14
	public function server($name){
101 14
		return isset($this->vars['headers'][$name]) ? $this->vars['headers'][$name] : null;
102
	}
103
104
	/**
105
	 * Return first part before comma or the string itself if there is no comma
106
	 * @param string $str
107
	 * @return string
108
	 */
109 3
	private function getPartBeforeComma($str){
110 3
		if (strpos($str, ',') !== false) {
111 2
			$parts = explode(',', $str);
112 2
			$result = $parts[0];
113 2
		} else {
114 1
			$result = $str;
115
		}
116 3
		return trim($result);
117
	}
118
119
}
120
121