-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.php
More file actions
84 lines (70 loc) · 2.19 KB
/
Copy pathserver.php
File metadata and controls
84 lines (70 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\Http\Server;
$serverip = "127.0.0.1";
$serverport = 9501;
$server = new Swoole\HTTP\Server($serverip, $serverport);
// Server settings
$server->set([
// Process ID
"pid_file" => __DIR__ . "/swoole.pid",
// Logging
"log_file" => __DIR__ . "/log/swoole.log",
"log_rotation" => SWOOLE_LOG_ROTATION_DAILY,
"log_date_format" => "%Y-%m-%d %H:%M:%S",
"log_date_with_microseconds" => false,
// HTTP
"http_compression" => true,
"compression_min_length" => 128,
]);
/*
PAGES
---
Add a new page to the array in this format:
"page-on-webserver" => "location/of/page-to-include.php",
Example:
"/help" => __DIR__ . "/inc/help.php",
*/
$pages = array(
"404" => __DIR__ . "/inc/404.php",
"/" => __DIR__ . "/inc/index.php",
"/redirect" => __DIR__ . "/inc/redirect.php",
"/json" => __DIR__ . "/inc/json.php",
"/text" => __DIR__ . "/inc/text.php",
"/get" => __DIR__ . "/inc/get.php",
"/post" => __DIR__ . "/inc/post.php",
"/info" => __DIR__ . "/inc/php-info.php",
);
// Start Server
$server->on("Start", function (Server $server) {
global $serverip, $serverport;
echo "Swoole http server is started at http://".$serverip.":".$serverport."\n";
});
// Display pages on new request
$server->on("Request", function (Request $request, Response $response) {
// Setup
global $serverip, $serverport, $pages;
$uri = $request->server["request_uri"];
$requestip = $request->server["remote_addr"];
// Fix Cloudflare
if (isset($request->header["cf-connecting-ip"])) {
$requestip = $request->header["cf-connecting-ip"];
}
// Variables to PHP style
$_SERVER["REMOTE_ADDR"] = $requestip;
$_SERVER["REQUEST_URI"] = $uri;
$_SERVER["REQUEST_METHOD"] = $request->server["request_method"];
$_GET = $request->get ?? [];
$_FILES = $request->files ?? [];
$_POST = $request->post ?? [];
// Page handler
if (array_key_exists($uri, $pages)) {
// Include page from array
include $pages[$uri];
} else {
// Not in array - display 404 error page
include $pages["404"];
}
});
$server->start();