Passed
Push — master ( 599585...a7cf01 )
by Béla
03:50
created

IncomingRequest::getQueryString()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 3
nc 2
nop 0
crap 3
1
<?php
2
3
namespace POData\OperationContext\Web;
4
5
use POData\Common\ODataConstants;
6
use POData\HttpProcessUtility;
7
use POData\OperationContext\HTTPRequestMethod;
8
use POData\OperationContext\IHTTPRequest;
9
10
/**
11
 * Class IncomingRequest
12
 * Class represents HTTP methods,headers and stream associated with a HTTP request
13
 * Note: This class will not throw any error
14
 * @package POData\OperationContext\Web
15
 */
16
class IncomingRequest implements IHTTPRequest
17
{
18
    /**
19
     * The request headers
20
     *
21
     * @var array
22
     */
23
    private $_headers;
24
25
    /**
26
     * The incoming url in raw format
27
     *
28
     * @var string
29
     */
30
    private $_rawUrl = null;
31
32
33
    /**
34
     * The request method (GET, POST, PUT, DELETE or MERGE)
35
     *
36
     * @var string HttpVerb
37
     */
38
    private $_method;
39
40
    /**
41
     * The query options as key value.
42
     *
43
     * @var array(string, string);
44
     */
45
    private $_queryOptions;
46
47
    /**
48
     * A collection that represents mapping between query
49
     * option and its count.
50
     *
51
     * @var array(string, int)
52
     */
53
    private $_queryOptionsCount;
54
55
    /**
56
     * Initialize a new instance of IncomingWebRequestContext
57
     */
58 86
    public function __construct()
59
    {
60 86
        $this->_method = $_SERVER['REQUEST_METHOD'];
61 86
        $this->_queryOptions = null;
62 86
        $this->_queryOptionsCount = null;
63 86
        $this->_headers = null;
64 86
        $this->getHeaders();
65
		//Not sure about this
66 86
		$this->getQueryParameters();
67
    }
68
69
    /**
70
     * Get the request headers
71
     * By-default we will get the following headers:
72
     * HTTP_HOST
73
     * HTTP_USER_AGENT
74
     * HTTP_ACCEPT
75
     * HTTP_ACCEPT_LANGUAGE
76
     * HTTP_ACCEPT_ENCODING
77
     * HTTP_ACCEPT_CHARSET
78
     * HTTP_KEEP_ALIVE
79
     * HTTP_CONNECTION,
80
     * HTTP_CACHE_CONTROL
81
     * HTTP_USER_AGENT
82
     * HTTP_IF_MATCH
83
     * HTTP_IF_NONE_MATCH
84
     * HTTP_IF_MODIFIED
85
     * HTTP_IF_MATCH
86
     * HTTP_IF_NONE_MATCH
87
     * HTTP_IF_UNMODIFIED_SINCE
88
     * //TODO: these aren't really headers...
89
     * REQUEST_URI
90
     * REQUEST_METHOD
91
     * REQUEST_TIME
92
     * SERVER_NAME
93
     * SERVER_PORT
94
     * SERVER_PORT_SECURE
95
     * SERVER_PROTOCOL
96
     * SERVER_SOFTWARE
97
     * CONTENT_TYPE
98
     * CONTENT_LENGTH
99
     * We may get user defined customized headers also like
100
     * HTTP_DATASERVICEVERSION, HTTP_MAXDATASERVICEVERSION
101
     *
102
     * @return string[]
103
     */
104 86
    private function getHeaders()
105
    {
106 86
        if (is_null($this->_headers)) {
0 ignored issues
show
introduced by
The condition is_null($this->_headers) is always false.
Loading history...
107 86
            $this->_headers = array();
108
109 86
            foreach ($_SERVER as $key => $value) {
110 86
                if ((strpos($key, 'HTTP_') === 0)
111 86
                    || (strpos($key, 'REQUEST_') === 0)
112 86
                    || (strpos($key, 'SERVER_') === 0)
113 86
                    || (strpos($key, 'CONTENT_') === 0)
114
                ) {
115 86
                    $trimmedValue = trim($value);
116 86
                    $this->_headers[$key] = isset($trimmedValue) ? $trimmedValue : null;
117
                }
118
            }
119
120
        }
121
122 86
        return $this->_headers;
123
    }
124
125
    /**
126
     * get the raw incoming url
127
     *
128
     * @return string RequestURI called by User with the value of QueryString
129
     */
130 84
    public function getRawUrl()
131
    {
132 84
        if (is_null($this->_rawUrl)) {
0 ignored issues
show
introduced by
The condition is_null($this->_rawUrl) is always false.
Loading history...
133 84
            if (!preg_match('/^HTTTPS/', $_SERVER[ODataConstants::HTTPREQUEST_PROTOCOL])) {
134 84
                $this->_rawUrl = ODataConstants::HTTPREQUEST_PROTOCOL_HTTP;
135
            } else {
136
                $this->_rawUrl = ODataConstants::HTTPREQUEST_PROTOCOL_HTTPS;
137
            }
138
139 84
            $this->_rawUrl .= "://" . $_SERVER[HttpProcessUtility::headerToServerKey(ODataConstants::HTTPREQUEST_HEADER_HOST)];
140 84
            $this->_rawUrl .= $_SERVER[ODataConstants::HTTPREQUEST_URI];
141
			//It looks like it is not working, for example if there is an $inlinecount then it will double the parameter
142
			//if (!empty($_SERVER[ODataConstants::HTTPREQUEST_QUERY_STRING])) {
143
			//	$this->_rawUrl .= "?" . $_SERVER[ODataConstants::HTTPREQUEST_QUERY_STRING];
144
			//}
145
        }
146
147 84
        return $this->_rawUrl;
148
    }
149
150
    /**
151
     * get the specific request headers
152
     *
153
     * @param string $key The header name
154
     *
155
     * @return string|null value of the header, NULL if header is absent.
156
     */
157 84
    public function getRequestHeader($key)
158
    {
159 84
        if (!$this->_headers) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->_headers of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
160
            $this->getHeaders();
161
        }
162
        //PHP normalizes header keys
163 84
        $trimmedKey = HttpProcessUtility::headerToServerKey(trim($key));
164
165 84
        if (array_key_exists($trimmedKey, $this->_headers)) {
166 84
            return $this->_headers[$trimmedKey];
167
        }
168
169
        return null;
170
    }
171
172
    /**
173
     * Get the QUERY_STRING
174
     * Note: This method will return empty string if no query string present.
175
     *
176
     * @return string $_header[HttpRequestHeaderQueryString]
177
     */
178 86
    private function getQueryString()
179
    {
180 86
        if (array_key_exists(ODataConstants::HTTPREQUEST_QUERY_STRING, $_SERVER) && !empty($_SERVER[ODataConstants::HTTPREQUEST_QUERY_STRING])) {
181 75
            return mb_convert_encoding(trim($_SERVER[ODataConstants::HTTPREQUEST_QUERY_STRING]), 'UTF-8');
0 ignored issues
show
Bug Best Practice introduced by
The expression return mb_convert_encodi...UERY_STRING]), 'UTF-8') also could return the type array which is incompatible with the documented return type string.
Loading history...
182
        } else {
183 11
            return "";
184
        }
185
    }
186
187
    /**
188
     * Split the QueryString and assigns them as array element in KEY=VALUE
189
     *
190
     * @return string[]
191
     */
192 86
    public function getQueryParameters()
193
    {
194 86
        if (is_null($this->_queryOptions)) {
0 ignored issues
show
introduced by
The condition is_null($this->_queryOptions) is always false.
Loading history...
195 86
            $queryString = $this->getQueryString();
196 86
            $this->_queryOptions = array();
197
			//example $filter=CustomerID gt \'ALFKI\'
198 86
            foreach (explode('&', $queryString) as $queryOptionAsString) {
199 86
                $queryOptionAsString = trim($queryOptionAsString);
200 86
                if (!empty($queryOptionAsString)) {
201 75
                    $result = explode('=', $queryOptionAsString, 2);
202 75
                    $isNamedOptions = count($result) == 2;
203 75
                    if ($isNamedOptions) {
204 75
                        $this->_queryOptions[rawurldecode($result[0])] = trim(rawurldecode($result[1]));
205
                    } else {
206
                        $this->_queryOptions[]
207
                            = array(null => trim(rawurldecode($result[0])));
208
                    }
209
                }
210
            }
211
        }
212
213 86
        return $this->_queryOptions;
214
    }
215
216
217
218
    /**
219
     * Get the HTTP method
220
     * Value will be set from the value of the HTTP method of the
221
     * incoming Web request.
222
     *
223
     * @return string $_header[HttpRequestHeaderMethod]
224
     */
225 84
    public function getMethod()
226
    {
227 84
        return $this->_method;
228
    }
229
230
231
}
232