Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
123 lines (108 loc) · 4.06 KB

File metadata and controls

123 lines (108 loc) · 4.06 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
* TrueAsync Server demo.
*
* php -d extension=./modules/true_async_server.so demo-server.php
*
* Then in another terminal:
* curl -v http://127.0.0.1:8080/
* curl -v http://127.0.0.1:8080/hello?name=Edmond
* curl -v -X POST -d 'payload' http://127.0.0.1:8080/echo
* curl -v http://127.0.0.1:8080/json
* curl -v http://127.0.0.1:8080/stop # graceful shutdown
*
* Press Ctrl+C to exit.
*/
use TrueAsync\HttpServer;
use TrueAsync\HttpServerConfig;
use TrueAsync\LogSeverity;
$host = '0.0.0.0';
$port = 8080;
$config = (new HttpServerConfig())
->addListener($host, $port)
->setReadTimeout(30)
->setWriteTimeout(30)
// Built-in logger: writes events directly from the C core to the
// given php_stream. Severity is fixed at start() — change DEBUG
// to INFO / WARN / ERROR for less verbose output, or LogSeverity::OFF
// to disable.
->setLogSeverity(LogSeverity::DEBUG)
->setLogStream(STDOUT);
$server = new HttpServer($config);
$server->addHttpHandler(function ($request, $response) use ($server) {
$method = $request->getMethod();
$uri = $request->getUri();
$path = parse_url($uri, PHP_URL_PATH) ?? '/';
fprintf(STDERR, "[%s] %s %s\n", date('H:i:s'), $method, $uri);
switch ($path) {
case '/':
$response
->setStatusCode(200)
->setHeader('Content-Type', 'text/html; charset=utf-8')
->setBody(<<<HTML
<!doctype html>
<html><head><title>TrueAsync Server</title></head>
<body>
<h1>It works!</h1>
<p>Served by <code>true_async_server</code> directly from PHP.</p>
<ul>
<li><a href="/hello?name=World">/hello?name=World</a></li>
<li><a href="/json">/json</a></li>
<li><code>POST /echo</code> &mdash; echoes the request body</li>
<li><a href="/stop">/stop</a> &mdash; shuts the server down</li>
</ul>
</body></html>
HTML)
->end();
return;
case '/hello':
parse_str(parse_url($uri, PHP_URL_QUERY) ?? '', $q);
$name = $q['name'] ?? 'stranger';
$response
->setStatusCode(200)
->setHeader('Content-Type', 'text/plain; charset=utf-8')
->setBody("Hello, {$name}!\n")
->end();
return;
case '/json':
$response
->setStatusCode(200)
->setHeader('Content-Type', 'application/json')
->setBody(json_encode([
'server' => 'true_async_server',
'pid' => getmypid(),
'time' => date(DATE_ATOM),
'method' => $method,
'uri' => $uri,
], JSON_PRETTY_PRINT) . "\n")
->end();
return;
case '/echo':
$body = method_exists($request, 'getBody') ? (string)$request->getBody() : '';
$response
->setStatusCode(200)
->setHeader('Content-Type', 'text/plain')
->setBody("You sent {$method} with " . strlen($body) . " bytes:\n{$body}\n")
->end();
return;
case '/stop':
$response
->setStatusCode(200)
->setHeader('Content-Type', 'text/plain')
->setBody("bye\n")
->end();
$server->stop();
return;
default:
$response
->setStatusCode(404)
->setHeader('Content-Type', 'text/plain')
->setBody("404 Not Found: {$path}\n")
->end();
}
});
echo "TrueAsync Server listening on http://{$host}:{$port}\n";
echo "Try: curl -v http://127.0.0.1:{$port}/\n";
echo "Stop: curl http://127.0.0.1:{$port}/stop (or Ctrl+C)\n\n";
$server->start();
echo "Server stopped.\n";
Morty Proxy This is a proxified and sanitized view of the page, visit original site.