11<?php
22/**
3- * MIT License
4- *
5- * Copyright (c) 2019, WebFiori HTTP.
3+ * This file is licensed under MIT License.
64 *
7- * Permission is hereby granted, free of charge, to any person obtaining a copy
8- * of this software and associated documentation files (the "Software"), to deal
9- * in the Software without restriction, including without limitation the rights
10- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11- * copies of the Software, and to permit persons to whom the Software is
12- * furnished to do so, subject to the following conditions:
5+ * Copyright (c) 2019 Ibrahim BinAlshikh
136 *
14- * The above copyright notice and this permission notice shall be included in all
15- * copies or substantial portions of the Software.
16- *
17- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23- * SOFTWARE.
7+ * For more information on the license, please visit:
8+ * https://github.com/WebFiori/http/blob/master/LICENSE
249 */
2510namespace webfiori \http ;
2611
@@ -182,6 +167,7 @@ public static function getHeader(string $name) : array {
182167
183168 return self ::getHeadersPool ()->getHeader ($ name );
184169 }
170+
185171 /**
186172 * Returns HTTP request headers.
187173 *
@@ -199,28 +185,55 @@ public static function getHeader(string $name) : array {
199185 public static function getHeaders () : array {
200186 if (defined ('__PHPUNIT_PHAR__ ' )) {
201187 //Always Refresh headers if in testing environment.
202- self ::get ()->headersPool = new HeadersPool ();
203-
204- if (function_exists ('apache_request_headers ' )) {
205- $ headers = apache_request_headers ();
188+ self ::extractHeaders ();
189+ }
190+
191+ if (self ::get ()->headersPool === null ) {
192+ self ::extractHeaders ();
193+ }
206194
207- foreach ($ headers as $ k => $ v ) {
208- self ::get ()->headersPool ->addHeader ($ k , filter_var ($ v , FILTER_SANITIZE_FULL_SPECIAL_CHARS ));
209- }
210- }
195+ return self ::getHeadersPool ()->getHeaders ();
196+ }
197+ /**
198+ * Returns an associative array of request headers.
199+ *
200+ * @return array The indices of the array will be headers names and the
201+ * values are sub-arrays. Each array contains the values of the header.
202+ */
203+ public static function getHeadersAssoc () : array {
204+ $ retVal = [];
205+ $ headers = self ::getHeaders ();
206+
207+ foreach ($ headers as $ headerObj ) {
208+
209+ if (!isset ($ retVal [$ headerObj ->getName ()])) {
210+ $ retVal [$ headerObj ->getName ()] = [];
211+ }
212+ $ retVal [$ headerObj ->getName ()][] = $ headerObj ->getValue ();
213+ }
214+
215+ return $ retVal ;
216+ }
217+ private static function extractHeaders () {
218+ self ::get ()->headersPool = new HeadersPool ();
211219
212- if (isset ( $ _SERVER )) {
213- $ headersArr = self :: _getRequestHeadersFromServer ();
220+ if (function_exists ( ' apache_request_headers ' )) {
221+ $ headers = apache_request_headers ();
214222
215- foreach ($ headersArr as $ header ) {
216- self ::get ()->headersPool ->addHeader ($ header ->getName (), $ header ->getValue ());
217- }
223+ foreach ($ headers as $ k => $ v ) {
224+ self ::get ()->headersPool ->addHeader ($ k , filter_var ($ v , FILTER_SANITIZE_FULL_SPECIAL_CHARS ));
218225 }
219- }
226+ }
220227
228+ if (isset ($ _SERVER )) {
229+ $ headersArr = self ::_getRequestHeadersFromServer ();
221230
222- return self ::getHeadersPool ()->getHeaders ();
231+ foreach ($ headersArr as $ header ) {
232+ self ::get ()->headersPool ->addHeader ($ header ->getName (), $ header ->getValue ());
233+ }
234+ }
223235 }
236+
224237 /**
225238 * Returns the pool which is used to hold request headers.
226239 *
@@ -276,11 +289,9 @@ public static function getParam(string $paramName) {
276289 $ val = null ;
277290
278291 if ($ requMethod == 'POST ' || $ requMethod == 'PUT ' ) {
279- $ val = self ::filter (INPUT_POST , $ paramName );
280- } else {
281- if ($ requMethod == 'DELETE ' || $ requMethod == 'GET ' ) {
282- $ val = self ::filter (INPUT_GET , $ paramName );
283- }
292+ $ val = self ::filter (INPUT_POST , $ trimmed );
293+ } else if ($ requMethod == 'DELETE ' || $ requMethod == 'GET ' ) {
294+ $ val = self ::filter (INPUT_GET , $ trimmed );
284295 }
285296
286297 if ($ val === false ) {
@@ -292,7 +303,8 @@ public static function getParam(string $paramName) {
292303 /**
293304 * Returns the URI of the requested resource.
294305 *
295- * @return string The URI of the requested resource.
306+ * @return string The URI of the requested resource
307+ * (e.g. http://example.com/get-random?range=[1,100]).
296308 *
297309 * @since 1.0
298310 */
@@ -339,14 +351,10 @@ private static function _getRequestHeadersFromServer() {
339351 for ($ x = 0 ; $ x < $ count ; $ x ++) {
340352 if ($ x + 1 == $ count && $ split [$ x ] != 'HTTP ' ) {
341353 $ headerName = $ headerName .$ split [$ x ];
342- } else {
343- if ($ x == 1 && $ split [$ x ] != 'HTTP ' ) {
354+ } else if ($ x == 1 && $ split [$ x ] != 'HTTP ' ) {
344355 $ headerName = $ split [$ x ].'- ' ;
345- } else {
346- if ($ split [$ x ] != 'HTTP ' ) {
347- $ headerName = $ headerName .$ split [$ x ].'- ' ;
348- }
349- }
356+ } else if ($ split [$ x ] != 'HTTP ' ) {
357+ $ headerName = $ headerName .$ split [$ x ].'- ' ;
350358 }
351359 }
352360 $ retVal [] = new HttpHeader ($ headerName , filter_var ($ v , FILTER_SANITIZE_FULL_SPECIAL_CHARS ));
@@ -361,10 +369,8 @@ private static function filter($inputSource, $varName) {
361369 if ($ val === null ) {
362370 if ($ inputSource == INPUT_POST && isset ($ _POST [$ varName ])) {
363371 $ val = filter_var (urldecode ($ _POST [$ varName ]));
364- } else {
365- if ($ inputSource == INPUT_GET && isset ($ _GET [$ varName ])) {
366- $ val = filter_var (urldecode ($ _GET [$ varName ]));
367- }
372+ } else if ($ inputSource == INPUT_GET && isset ($ _GET [$ varName ])) {
373+ $ val = filter_var (urldecode ($ _GET [$ varName ]));
368374 }
369375 }
370376
0 commit comments