Skip to content

Commit 7770f75

Browse files
committed
PCNTL support, type safety, error catching
1 parent a109054 commit 7770f75

File tree

11 files changed

+413
-113
lines changed

11 files changed

+413
-113
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
## [v2.0.0]
6+
7+
### Added
8+
- Graceful shutdown support on non-Windows systems with `pcntl` extension.
9+
10+
### Changed
11+
- Refactored `Server` class to improve error handling.
12+
- Updated `README.md` to include information about new features and usage of bash scripts.
13+
14+
### Fixed
15+
- Fixed issues with handling client requests and responses in `Server` class.

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Verifier-Server is a web server designed to handle BYOND user verification. It s
1111
- Handles GET and POST requests for user verification.
1212
- Supports both filesystem and SQL (SQLite/MySQL) storage.
1313
- Configurable via `.env` file.
14+
- Verbose mode for detailed request and response logging.
15+
- Graceful shutdown support on non-Windows systems with `pcntl` extension.
1416

1517
## Installation
1618

@@ -44,7 +46,7 @@ Verifier-Server is a web server designed to handle BYOND user verification. It s
4446
#DB_OPTIONS=
4547
4648
# MySQL configuration
47-
DB_DSN=mysql:host=127.0.0.1;port=3307;dbname=verify_list
49+
DB_DSN=mysql:host=127.0.0.1;port=3306;dbname=verify_list
4850
DB_PORT=3306
4951
DB_USERNAME=your_username
5052
DB_PASSWORD=your_password

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
"require-dev": {
1515
"phpunit/phpunit": "^9.5"
1616
},
17+
"suggest": {
18+
"ext-pcntl": "Enables process control support for better server management on non-Windows systems"
19+
},
1720
"autoload": {
1821
"psr-4": {
1922
"VerifierServer\\": "src/"

example.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ STORAGE_TYPE=filesystem
1010
#DB_OPTIONS=
1111

1212
# MySQL configuration
13-
DB_DSN=mysql:host=127.0.0.1;port=3307;dbname=verify_list
13+
DB_DSN=mysql:host=127.0.0.1;port=3306;dbname=verify_list
1414
DB_PORT=3306
1515
DB_USERNAME=your_username
1616
DB_PASSWORD=your_password

run.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44
use VerifierServer\PersistentState;
55
use VerifierServer\Server;
66

7-
$verifyFile = PersistentState::loadVerifyFile();
87
$envConfig = PersistentState::loadEnvConfig();
9-
$hostAddr = $envConfig['HOST_ADDR'] . ':' . $envConfig['HOST_PORT'];
10-
$civToken = $envConfig['TOKEN'];
11-
$storageType = $envConfig['STORAGE_TYPE'] ?? 'sql';
12-
$state = new PersistentState($verifyFile, $civToken, $storageType);
138

14-
$server = new Server($state, $hostAddr);
9+
$server = new Server(
10+
new PersistentState(
11+
$envConfig['TOKEN'],
12+
PersistentState::loadVerifyFile(),
13+
$envConfig['STORAGE_TYPE'] ?? 'filesystem'
14+
),
15+
$envConfig['HOST_ADDR'] . ':' . $envConfig['HOST_PORT']
16+
);
17+
18+
$server->init();
19+
$server->setVerbose(true);
1520
$server->start();

src/Endpoints/VerifiedEndpoint.php

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44
use VerifierServer\PersistentState;
55

66
class VerifiedEndpoint {
7-
private PersistentState $state;
7+
public function __construct(private PersistentState $state)
8+
{}
89

9-
public function __construct(PersistentState $state) {
10-
$this->state = $state;
11-
}
12-
13-
public function handleRequest($method, $request, &$response) {
10+
/**
11+
* Handles the incoming HTTP request and generates the appropriate response.
12+
*
13+
* @param string $method The HTTP method of the request (e.g., 'GET', 'POST').
14+
* @param string $request The request payload, typically used for 'POST' requests.
15+
* @param string &$response The variable to store the generated response.
16+
*/
17+
public function handleRequest(string $method, string $request, string &$response): void
18+
{
1419
switch ($method) {
1520
case 'GET':
1621
$this->handleGet($response);
@@ -24,12 +29,36 @@ public function handleRequest($method, $request, &$response) {
2429
}
2530
}
2631

27-
private function handleGet(&$response) {
32+
/**
33+
* Handles the GET request and prepares the response.
34+
*
35+
* @param string &$response The response string to be sent back to the client.
36+
*
37+
* This method sets the HTTP status code to 200 OK and the Content-Type to application/json.
38+
* It then appends the JSON-encoded verification list to the response.
39+
*/
40+
private function handleGet(string &$response): void
41+
{
2842
$response = "HTTP/1.1 200 OK" . PHP_EOL . "Content-Type: application/json" . PHP_EOL . PHP_EOL;
2943
$response .= json_encode($this->state->getVerifyList());
3044
}
3145

32-
private function handlePost($request, &$response) {
46+
/**
47+
* Handles POST requests by parsing the request data and performing actions based on the method type.
48+
*
49+
* @param string $request The raw HTTP request string.
50+
* @param string &$response The response string to be modified based on the request handling.
51+
*
52+
* The function performs the following steps:
53+
* 1. Extracts the raw data from the request.
54+
* 2. Parses the raw data into an associative array.
55+
* 3. Retrieves the method type, ckey, discord, and token from the parsed data.
56+
* 4. Checks if the provided token matches the expected token. If not, sets the response to 401 Unauthorized.
57+
* 5. Retrieves the verification list from the state.
58+
* 6. Based on the method type, either deletes an entry from the list or handles the default case.
59+
*/
60+
private function handlePost(string $request, string &$response): void
61+
{
3362
$rawData = explode(PHP_EOL . PHP_EOL, $request, 2)[1];
3463
parse_str($rawData, $formData);
3564

@@ -44,19 +73,26 @@ private function handlePost($request, &$response) {
4473
}
4574

4675
$list = $this->state->getVerifyList();
47-
$existingIndex = array_search($discord, array_column($list, 'discord'));
4876

4977
switch ($methodType) {
5078
case 'delete':
51-
$this->handleDelete($existingIndex, $list, $response);
79+
$this->handleDelete(array_search($discord, array_column($list, 'discord')), $list, $response);
5280
break;
5381
default:
54-
$this->handleDefault($existingIndex, $list, $ckey, $discord, $response);
82+
$this->handleDefault($list, $ckey, $discord, $response);
5583
break;
5684
}
5785
}
5886

59-
private function handleDelete($existingIndex, &$list, &$response) {
87+
/**
88+
* Handles the deletion of an item from the list.
89+
*
90+
* @param int|string|false $existingIndex The index of the item to delete, or false if the item does not exist.
91+
* @param array &$list The list from which the item will be deleted.
92+
* @param string &$response The HTTP response message to be returned.
93+
*/
94+
private function handleDelete(int|string|false $existingIndex, array &$list, string &$response): void
95+
{
6096
if ($existingIndex !== false) {
6197
array_splice($list, $existingIndex, 1);
6298
PersistentState::writeJson("verify.json", $list);
@@ -67,7 +103,21 @@ private function handleDelete($existingIndex, &$list, &$response) {
67103
}
68104
}
69105

70-
public function handleDefault($existingIndex, &$list, $ckey, $discord, &$response) {
106+
/**
107+
* Handles the default verification process.
108+
*
109+
* This method checks if the provided `ckey` or `discord` already exists in the list.
110+
* If either exists, it sets the response to a 403 Forbidden status.
111+
* If neither exists, it adds the new entry to the list, writes the updated list to a JSON file,
112+
* updates the state, and sets the response to a 200 OK status.
113+
*
114+
* @param array $list The list of existing entries.
115+
* @param string $ckey The ckey to be verified.
116+
* @param string $discord The discord identifier to be verified.
117+
* @param string &$response The response message to be set based on the verification result.
118+
*/
119+
public function handleDefault(array &$list, string $ckey, string $discord, string &$response): void
120+
{
71121
$existingCkeyIndex = array_search($ckey, array_column($list, 'ss13'));
72122
$existingDiscordIndex = array_search($discord, array_column($list, 'discord'));
73123

0 commit comments

Comments
 (0)