Skip to content

Commit beb4ce9

Browse files
authored
Merge pull request #29 from WebFiori/dev
Added Support for Getting Headers as Associative Array
2 parents 1338837 + 4eb9ff4 commit beb4ce9

14 files changed

Lines changed: 158 additions & 208 deletions

tests/webfiori/tests/http/RequestTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,19 @@ public function testGetRequestedURL00() {
104104
$_SERVER['PATH_INFO'] = '/my/app';
105105
$this->assertEquals('http://127.0.0.1/my/app', Request::getRequestedURI());
106106
}
107-
107+
/**
108+
* @test
109+
*/
110+
public function testGetHeaders00() {
111+
$_SERVER['HTTP_CONTENT_TYPE'] = "application/json";
112+
$_SERVER['HTTP_X_HOST'] = "Custom H";
113+
$this->assertEquals([
114+
'content-type' => [
115+
'application/json'
116+
],
117+
'x-host' => [
118+
'Custom H'
119+
]
120+
], Request::getHeadersAssoc());
121+
}
108122
}

webfiori/http/APIFilter.php

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,11 @@
11
<?php
2-
3-
/*
4-
* The MIT License
5-
*
6-
* Copyright 2019 Ibrahim BinAlshikh, WebFiori HTTP.
7-
*
8-
* Permission is hereby granted, free of charge, to any person obtaining a copy
9-
* of this software and associated documentation files (the "Software"), to deal
10-
* in the Software without restriction, including without limitation the rights
11-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12-
* copies of the Software, and to permit persons to whom the Software is
13-
* furnished to do so, subject to the following conditions:
14-
*
15-
* The above copyright notice and this permission notice shall be included in
16-
* all copies or substantial portions of the Software.
17-
*
18-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24-
* THE SOFTWARE.
2+
/**
3+
* This file is licensed under MIT License.
4+
*
5+
* Copyright (c) 2019 Ibrahim BinAlshikh
6+
*
7+
* For more information on the license, please visit:
8+
* https://github.com/WebFiori/http/blob/master/LICENSE
259
*/
2610
namespace webfiori\http;
2711

webfiori/http/AbstractWebService.php

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,11 @@
11
<?php
2-
3-
/*
4-
* The MIT License
5-
*
6-
* Copyright 2019 Ibrahim BinAlshikh, WebFiori HTTP.
7-
*
8-
* Permission is hereby granted, free of charge, to any person obtaining a copy
9-
* of this software and associated documentation files (the "Software"), to deal
10-
* in the Software without restriction, including without limitation the rights
11-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12-
* copies of the Software, and to permit persons to whom the Software is
13-
* furnished to do so, subject to the following conditions:
14-
*
15-
* The above copyright notice and this permission notice shall be included in
16-
* all copies or substantial portions of the Software.
17-
*
18-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24-
* THE SOFTWARE.
2+
/**
3+
* This file is licensed under MIT License.
4+
*
5+
* Copyright (c) 2019 Ibrahim BinAlshikh
6+
*
7+
* For more information on the license, please visit:
8+
* https://github.com/WebFiori/http/blob/master/LICENSE
259
*/
2610
namespace webfiori\http;
2711

webfiori/http/HeadersPool.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
<?php
2+
/**
3+
* This file is licensed under MIT License.
4+
*
5+
* Copyright (c) 2022 Ibrahim BinAlshikh
6+
*
7+
* For more information on the license, please visit:
8+
* https://github.com/WebFiori/http/blob/master/LICENSE
9+
*/
210
namespace webfiori\http;
311

412
/**

webfiori/http/HttpCookie.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
<?php
2+
/**
3+
* This file is licensed under MIT License.
4+
*
5+
* Copyright (c) 2022 Ibrahim BinAlshikh
6+
*
7+
* For more information on the license, please visit:
8+
* https://github.com/WebFiori/http/blob/master/LICENSE
9+
*/
210
namespace webfiori\http;
311

412
/**

webfiori/http/HttpHeader.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
<?php
2+
/**
3+
* This file is licensed under MIT License.
4+
*
5+
* Copyright (c) 2022 Ibrahim BinAlshikh
6+
*
7+
* For more information on the license, please visit:
8+
* https://github.com/WebFiori/http/blob/master/LICENSE
9+
*/
210
namespace webfiori\http;
311

412
/**

webfiori/http/ManagerInfoService.php

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,11 @@
11
<?php
2-
3-
/*
4-
* The MIT License
5-
*
6-
* Copyright 2020 Ibrahim BinAlshikh, WebFiori HTTP.
7-
*
8-
* Permission is hereby granted, free of charge, to any person obtaining a copy
9-
* of this software and associated documentation files (the "Software"), to deal
10-
* in the Software without restriction, including without limitation the rights
11-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12-
* copies of the Software, and to permit persons to whom the Software is
13-
* furnished to do so, subject to the following conditions:
14-
*
15-
* The above copyright notice and this permission notice shall be included in
16-
* all copies or substantial portions of the Software.
17-
*
18-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24-
* THE SOFTWARE.
2+
/**
3+
* This file is licensed under MIT License.
4+
*
5+
* Copyright (c) 2020 Ibrahim BinAlshikh
6+
*
7+
* For more information on the license, please visit:
8+
* https://github.com/WebFiori/http/blob/master/LICENSE
259
*/
2610
namespace webfiori\http;
2711

webfiori/http/ParamTypes.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
<?php
2+
/**
3+
* This file is licensed under MIT License.
4+
*
5+
* Copyright (c) 2019 Ibrahim BinAlshikh
6+
*
7+
* For more information on the license, please visit:
8+
* https://github.com/WebFiori/http/blob/master/LICENSE
9+
*/
210
namespace webfiori\http;
311

412
/**

webfiori/http/Request.php

Lines changed: 57 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,11 @@
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
*/
2510
namespace 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

webfiori/http/RequestParameter.php

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,11 @@
11
<?php
2-
3-
/*
4-
* The MIT License
5-
*
6-
* Copyright 2019 Ibrahim BinAlshikh, WebFiori HTTP.
7-
*
8-
* Permission is hereby granted, free of charge, to any person obtaining a copy
9-
* of this software and associated documentation files (the "Software"), to deal
10-
* in the Software without restriction, including without limitation the rights
11-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12-
* copies of the Software, and to permit persons to whom the Software is
13-
* furnished to do so, subject to the following conditions:
14-
*
15-
* The above copyright notice and this permission notice shall be included in
16-
* all copies or substantial portions of the Software.
17-
*
18-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24-
* THE SOFTWARE.
2+
/**
3+
* This file is licensed under MIT License.
4+
*
5+
* Copyright (c) 2019 Ibrahim BinAlshikh
6+
*
7+
* For more information on the license, please visit:
8+
* https://github.com/WebFiori/http/blob/master/LICENSE
259
*/
2610
namespace webfiori\http;
2711

0 commit comments

Comments
 (0)