Skip to content

Commit

Permalink
Merge branch 'master' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane committed Aug 26, 2024
2 parents f4ab5a3 + f3e59fc commit aab60f6
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 14 deletions.
34 changes: 22 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# WebFiori HTTP
A simple library for creating RESTful web APIs in aadition to providing the developer with utilities for handling HTTP request and response.
A simple library for creating RESTful web APIs in adition to providing utilities for handling HTTP request and response.
It includes inputs feltering and data validation in addion to creating user-defined inputs filters.

<p align="center">
<a href="https://github.com/WebFiori/http/actions">
<img src="https://github.com/WebFiori/cli/actions/workflows/php83.yml/badge.svg?branch=master">
<img src="https://github.com/WebFiori/http/actions/workflows/php83.yml/badge.svg?branch=master">
</a>
<a href="https://codecov.io/gh/WebFiori/http">
<img src="https://codecov.io/gh/WebFiori/http/branch/master/graph/badge.svg" />
Expand All @@ -23,21 +23,31 @@ It includes inputs feltering and data validation in addion to creating user-defi
## Supported PHP Versions
| Build Status |
|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:|
| <a target="_blank" href="https://github.com/WebFiori/http/actions/workflows/php70.yml"><img src="https://github.com/WebFiori/cli/actions/workflows/php70.yml/badge.svg?branch=master"></a> |
| <a target="_blank" href="https://github.com/WebFiori/http/actions/workflows/php71.yml"><img src="https://github.com/WebFiori/cli/actions/workflows/php71.yml/badge.svg?branch=master"></a> |
| <a target="_blank" href="https://github.com/WebFiori/http/actions/workflows/php72.yml"><img src="https://github.com/WebFiori/cli/actions/workflows/php72.yml/badge.svg?branch=master"></a> |
| <a target="_blank" href="https://github.com/WebFiori/http/actions/workflows/php73.yml"><img src="https://github.com/WebFiori/cli/actions/workflows/php73.yml/badge.svg?branch=master"></a> |
| <a target="_blank" href="https://github.com/WebFiori/http/actions/workflows/php74.yml"><img src="https://github.com/WebFiori/cli/actions/workflows/php74.yml/badge.svg?branch=master"></a> |
| <a target="_blank" href="https://github.com/WebFiori/http/actions/workflows/php80.yml"><img src="https://github.com/WebFiori/cli/actions/workflows/php80.yml/badge.svg?branch=master"></a> |
| <a target="_blank" href="https://github.com/WebFiori/http/actions/workflows/php81.yml"><img src="https://github.com/WebFiori/cli/actions/workflows/php81.yml/badge.svg?branch=master"></a> |
| <a target="_blank" href="https://github.com/WebFiori/http/actions/workflows/php82.yml"><img src="https://github.com/WebFiori/cli/actions/workflows/php82.yml/badge.svg?branch=master"></a> |
| <a target="_blank" href="https://github.com/WebFiori/http/actions/workflows/php83.yml"><img src="https://github.com/WebFiori/cli/actions/workflows/php83.yml/badge.svg?branch=master"></a> |
| <a target="_blank" href="https://github.com/WebFiori/http/actions/workflows/php70.yml"><img src="https://github.com/WebFiori/http/actions/workflows/php70.yml/badge.svg?branch=master"></a> |
| <a target="_blank" href="https://github.com/WebFiori/http/actions/workflows/php71.yml"><img src="https://github.com/WebFiori/http/actions/workflows/php71.yml/badge.svg?branch=master"></a> |
| <a target="_blank" href="https://github.com/WebFiori/http/actions/workflows/php72.yml"><img src="https://github.com/WebFiori/http/actions/workflows/php72.yml/badge.svg?branch=master"></a> |
| <a target="_blank" href="https://github.com/WebFiori/http/actions/workflows/php73.yml"><img src="https://github.com/WebFiori/http/actions/workflows/php73.yml/badge.svg?branch=master"></a> |
| <a target="_blank" href="https://github.com/WebFiori/http/actions/workflows/php74.yml"><img src="https://github.com/WebFiori/http/actions/workflows/php74.yml/badge.svg?branch=master"></a> |
| <a target="_blank" href="https://github.com/WebFiori/http/actions/workflows/php80.yml"><img src="https://github.com/WebFiori/http/actions/workflows/php80.yml/badge.svg?branch=master"></a> |
| <a target="_blank" href="https://github.com/WebFiori/http/actions/workflows/php81.yml"><img src="https://github.com/WebFiori/http/actions/workflows/php81.yml/badge.svg?branch=master"></a> |
| <a target="_blank" href="https://github.com/WebFiori/http/actions/workflows/php82.yml"><img src="https://github.com/WebFiori/http/actions/workflows/php82.yml/badge.svg?branch=master"></a> |
| <a target="_blank" href="https://github.com/WebFiori/http/actions/workflows/php83.yml"><img src="https://github.com/WebFiori/http/actions/workflows/php83.yml/badge.svg?branch=master"></a> |

## API Docs
This library is a part of <a>WebFiori Framework</a>. To access API docs of the library, you can visid the following link: https://webfiori.com/docs/webfiori/http .

## Terminology

Following terminology is used by the library:
| Term | Definition|
|:------------:|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:|
| Web Service | A single end pont that implements a REST service. It is represented as an instance of the class `AbstractWebService`. |
| Services Manager | An entity which is used to manage a set of web services. Represented by the class `WebServicesManager`. |
| Request Parameter | A way to pass values from a client such as a web browser to the server. Represented by the class `RequestParameter`. |

## The Idea
The idea of the library is as follows, when a client performs a request to a web service, he is usually intersted in performing specific action. Related actions are kept in one place as a set of web services (e.g. CRUD operations on a reasorce). The client can pass arguments (or parameters) to the end point (the services set) in request body or as a query string.

The idea of the library is as follows, when a client performs a request to a web service, he is usually intersted in performing specific action. Related actions are kept in one place as a set of web services (e.g. CRUD operations on a reasorce). The client can pass arguments (or parameters) to the end point in request body as `POST` or `PUT` request method or as a query string when using `GET` or `DELETE`.

An end point is represented by the class [`AbstractWebService`](https://webfiori.com/docs/webfiori/http/AbstractWebService) and a set of web service (or end ponts) are grouped using the class [`WebServicesManager`](https://webfiori.com/docs/webfiori/http/WebServicesManager). Also, body parameters represented by the class [`RequestParameter`](https://webfiori.com/docs/webfiori/http/RequestParameter).

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"license": "MIT",
"require": {
"php": ">=7.0",
"webfiori/jsonx":"3.2.x",
"webfiori/jsonx":"3.3.x",
"ext-json": "*",
"ext-mbstring": "*"
},
Expand Down
26 changes: 26 additions & 0 deletions tests/webfiori/tests/http/APIFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,32 @@ public function testFilterGet23() {
$this->assertEquals(1,count($filtered));
$this->assertEquals([false, "Hello", null, "World"],$filtered['array']);
}
/**
* @test
*/
public function testFilterGet31() {
$this->apiFilter = new APIFilter();
$param00 = new RequestParameter('array', 'array');
$this->apiFilter->addRequestParameter($param00);
$_GET['array'] = [false, "Hello", null, "World"];
$this->apiFilter->filterGET();
$filtered = $this->apiFilter->getInputs();
$this->assertEquals(1,count($filtered));
$this->assertEquals([false, "Hello", null, "World"],$filtered['array']);
}
/**
* @test
*/
public function testFilterGet32() {
$this->apiFilter = new APIFilter();
$param00 = new RequestParameter('array', 'array');
$this->apiFilter->addRequestParameter($param00);
$_GET['array'] = [false, ["Hello"], null, "World"];
$this->apiFilter->filterGET();
$filtered = $this->apiFilter->getInputs();
$this->assertEquals(1,count($filtered));
$this->assertEquals([false, ["Hello"], null, "World"],$filtered['array']);
}
/**
* @test
*/
Expand Down
20 changes: 19 additions & 1 deletion webfiori/http/APIFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,11 @@ public static function filter(APIFilter $apiFilter, array $arr): array {
$defaultVal = $def[$paramIdx]->getDefault();

if (isset($arr[$name])) {
$toBeFiltered = urldecode($arr[$name]);
if (gettype($arr[$name]) != 'array') {
$toBeFiltered = urldecode($arr[$name]);
} else {
$toBeFiltered = self::decodeArray($arr[$name]);
}
$retVal[$noFIdx][$name] = $toBeFiltered;

if (isset($def[$optIdx]['filter-func'])) {
Expand All @@ -194,6 +198,17 @@ public static function filter(APIFilter $apiFilter, array $arr): array {

return $retVal;
}
private static function decodeArray(array $array) {
$retVal = [];
foreach ($array as $arrEl) {
if (gettype($arrEl) == 'array') {
$retVal[] = self::decodeArray($arrEl);
} else {
$retVal[] = urldecode($arrEl.'');
}
}
return $retVal;
}
/**
* Validate and sanitize GET parameters.
*
Expand Down Expand Up @@ -341,6 +356,9 @@ public function setInputStream($pathOrResource) : bool {
return false;
}
private static function applyBasicFilterOnly($def,$toBeFiltered) {
if (gettype($toBeFiltered) == 'array') {
return $toBeFiltered;
}
$toBeFiltered = strip_tags($toBeFiltered);

$paramObj = $def['parameter'];
Expand Down

0 comments on commit aab60f6

Please sign in to comment.