-
Notifications
You must be signed in to change notification settings - Fork 0
/
aquarius.php
257 lines (222 loc) · 6.25 KB
/
aquarius.php
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
namespace aquarius;
/*
* aquarius
* An application framework for Gemini capsules.
*
* Andy Green [email protected]
*/
class Request
{
/** @var string */
protected $path;
/** @var string */
protected $query;
/** @var string */
protected $remote_user;
public function __construct()
{
$this->path = '/'.trim($_SERVER['PATH_INFO'] ?? '', '/');
$this->query = rawurldecode($_SERVER['QUERY_STRING'] ?? '');
$this->remote_user = $_SERVER['REMOTE_USER'] ?? '';
$cert_fingerprint = $_SERVER['TLS_CLIENT_HASH'] ?? '';
if ('' !== $cert_fingerprint) {
// Generate a valid session ID from certificate hash. We never need
// to decode the base64, so it's fine to strip any =s off the end.
$session_id = rtrim(base64_encode($cert_fingerprint), '=');
session_id($session_id);
session_start([
'use_cookies' => 0,
]);
}
}
/**
* Get the current PATH_INFO, normalised with leading slash and without
* trailing slash.
*/
public function getPath(): string
{
return $this->path;
}
/**
* Get the current QUERY_STRING, URL-decoded (using rawurldecode()).
*/
public function getQuery(): string
{
return $this->query;
}
/**
* Get the current REMOTE_USER (probably a client certificate Common Name).
*/
public function getRemoteUser(): string
{
return $this->remote_user;
}
}
class Response
{
const STATUS_INPUT = 10;
const STATUS_SENSITIVE_INPUT = 11;
const STATUS_SUCCESS = 20;
const STATUS_REDIRECT_TEMPORARY = 30;
const STATUS_REDIRECT_PERMANENT = 31;
const STATUS_TEMPORARY_FAILURE = 40;
const STATUS_SERVER_UNAVAILABLE = 41;
const STATUS_CGI_ERROR = 42;
const STATUS_PROXY_ERROR = 43;
const STATUS_SLOW_DOWN = 44;
const STATUS_PERMANENT_FAILURE = 50;
const STATUS_NOT_FOUND = 51;
const STATUS_GONE = 52;
const STATUS_PROXY_REQUEST_REFUSED = 53;
const STATUS_BAD_REQUEST = 59;
const STATUS_CLIENT_CERTIFICATE_REQUIRED = 60;
const STATUS_CERTIFICATE_NOT_AUTHORISED = 61;
const STATUS_CERTIFICATE_NOT_VALID = 62;
/** @var int */
protected $status;
/** @var string */
protected $meta;
/** @var string */
protected $body;
public function __construct(
int $status = self::STATUS_SUCCESS,
string $meta = 'text/gemini',
string $body = ''
) {
$this->status = $status;
$this->meta = $meta;
$this->body = $body;
}
/**
* Default ($status, $meta) is (Response::STATUS_SUCCESS, 'text/gemini').
*/
public function setHeader(int $status, string $meta): void
{
$this->status = $status;
$this->meta = $meta;
}
public function getStatus(): int
{
return $this->status;
}
public function getMeta(): string
{
return $this->meta;
}
public function setBody(string $body): void
{
$this->body = $body;
}
public function appendBody(string $body): void
{
$this->body .= $body;
}
public function getBody(): string
{
return $this->body;
}
}
class Handler
{
/** @var string */
protected $path_regex;
/** @var array<mixed> */
protected $path_parameters = [];
/** @var array<callable> */
protected $stack = [];
public function __construct(string $path_regex, callable $callable)
{
$path_regex = '/'.trim($path_regex, '/');
$this->path_regex = '/^'.str_replace('/', '\/', $path_regex).'$/';
$this->stack[] = $callable;
}
public function __invoke(Request $request): ?Response
{
if (1 !== preg_match($this->path_regex, $request->getPath(), $matches)) {
return null;
}
$this->path_parameters = array_slice($matches, 1);
return call_user_func_array(
[$this, 'next'],
[$request, new Response()]
);
}
/**
* Call the next function in this handler's stack.
*/
public function next(Request $request, Response $response): Response
{
$callable = array_pop($this->stack);
if (null === $callable) {
return $response;
}
$callable = \Closure::fromCallable($callable)->bindTo($this);
return $callable($request, $response);
}
/**
* Add a function to this handler's stack. The last one added will be the
* first called.
*/
public function butFirst(callable $callable): self
{
$this->stack[] = $callable;
return $this;
}
/**
* Get the path parameters captured from this handler's regex pattern.
*
* @return array<mixed>
*/
public function getPathParameters(): array
{
return $this->path_parameters;
}
}
class App
{
/** @var array<Handler> */
protected $handlers = [];
public function addHandler(string $path_regex, callable $callable): Handler
{
$handler = new Handler($path_regex, $callable);
$this->handlers[] = $handler;
return $handler;
}
public function run(): void
{
$request = new Request();
// Intercept any output from handler callables (e.g. from 'echo'), else
// we'd mess up the response headers.
ob_start();
$response = null;
try {
foreach ($this->handlers as $handler) {
$response = $handler($request);
if (null !== $response) {
break;
}
}
} catch (\Exception $e) {
error_log($e->getMessage());
$response = new Response(
Response::STATUS_TEMPORARY_FAILURE,
'Server error'
);
}
if (null === $response) {
$response = new Response(
Response::STATUS_NOT_FOUND,
'Not found'
);
}
$handler_output = ob_get_clean();
if (is_string($handler_output)) {
$response->appendBody($handler_output);
}
ob_start();
echo "{$response->getStatus()} {$response->getMeta()}\r\n";
echo $response->getBody();
ob_end_flush();
}
}