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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
---@meta
---@class Buffer
---@field bytes fun(self: Buffer): number[]
---@field text fun(self: Buffer): string
---@field json fun(self: Buffer): table Returns the body parsed as JSON -> Lua Table
local http =
--- Represents an HTTP client response.
---@class HTTPClientResponse
---@field status_code fun(self: HTTPClientResponse): number Gets the response HTTP Status code
---@field body fun(self: HTTPClientResponse): Buffer Gets the response HTTP Body which further can be parsed
---@field headers fun(self: HTTPClientResponse): table|nil Returns the entire headers list from the HTTP response
---@field remote_address fun(self: HTTPClientResponse): string|nil Gets the remote address of the HTTP response server
---@diagnostic disable-next-line: duplicate-doc-alias
---@alias http_client_callback fun(response: HTTPClientResponse)
---@class HTTPClientRequestTableType
---@field url string
---@field method string?
---@field body any?
---@field file string?
---@field headers table?
---@field form table?
--- Represents an HTTP client request.
---@class HTTPClientRequest
---@field set_method fun(self: HTTPClientRequest, method: string): HTTPClientRequest
---@field set_header fun(self: HTTPClientRequest, key: string, value: string): HTTPClientRequest
---@field set_headers fun(self: HTTPClientRequest, headers: table): HTTPClientRequest
---@field set_form fun(self: HTTPClientRequest, headers: table): HTTPClientRequest
---@field set_body fun(self: HTTPClientRequest, body: any): HTTPClientRequest
---@field set_file fun(self: HTTPClientRequest, file_path: string): HTTPClientRequest Sets the for-upload file path
---@field execute fun(self: HTTPClientRequest): HTTPClientResponse Executes the request and returns the response
---@field execute_streaming fun(self: HTTPClientRequest, callback: http_client_callback) Executes the request in a streaming manner
---@field execute_websocket fun(self: HTTPClientRequest, callback: wscallback) Executes the request as an async task
---@diagnostic disable-next-line: duplicate-doc-alias
---@alias callback fun(request: HTTPServerRequest, response: HTTPServerResponse): any
---@class HTTPRouteConfiguration
---@field body_limit? number
---@field compression? boolean
---@field headers? table<string, string>
---@class HTTPRoute
---@field path string
---@field method string
---@field func function
---@field static_dir string?
---@field static_file string?
---@field config HTTPRouteConfiguration?
---@class IPAddress
---@field address string
---Converts this address to an `IpAddress_V4` if it is an IPv4-mapped IPv6 address, otherwise returns self as-is.
---@field to_canonical IPAddress
---@field is_ipv4 boolean
---@field is_ipv6 boolean
---@field is_loopback boolean
---@field is_multicast boolean
---@class HTTPMultipartField
---@field name fun(): string
---@field file_name fun(): string|nil
---@field content_type fun(): string|nil
---@field headers fun(): table
---@field text fun(): string
---@field bytes fun(): table Returns the field data as bytes (table of numbers)
---@class HTTPMultipart
---@field fields fun(): table Returns all multipart fields as an array
---@field get_field fun(name: string): HTTPMultipartField Returns a specific field by name
---@field file_name fun(): string|nil Returns the first filename found in the multipart data
---@field save_file fun(multipart: HTTPMultipart, file_path: string | nil): string | nil Saves the multipart into disk
---@class HTTPServerRequest
---@field method fun(self: HTTPServerRequest): string Returns the HTTP method (e.g., "GET", "POST").
---@field uri fun(self: HTTPServerRequest): string
---@field queries fun(self: HTTPServerRequest): table
---@field params fun(self: HTTPServerRequest): table
---@field headers fun(self: HTTPServerRequest): table
---@field form fun(self: HTTPServerRequest): table
---@field body fun(self: HTTPServerRequest): Buffer Returns the body of the request, which can be a table or a string.
---@field ip_address fun(self: HTTPServerRequest): IPAddress
---@field multipart fun(self: HTTPServerRequest): HTTPMultipart
---@field get_cookie fun(self: HTTPServerRequest, name: string): Cookie
---@field new_cookie fun(self: HTTPServerRequest, name: string, value: string): Cookie
---@class HTTPServerResponse
---Sets the HTTP status code of the response
---@field set_status_code fun(self: HTTPServerResponse, new_status_code: number)
---@field set_header fun(self: HTTPServerResponse, key: string, value: string)
---Returns the entire headers list that so far has been set for the response
---@field get_headers fun(self: HTTPServerResponse): table|nil
---@field remove_header fun(self: HTTPServerResponse, key: string)
---@field set_cookie fun(self: HTTPServerResponse, cookie: Cookie)
---@field remove_cookie fun(self: HTTPServerResponse, cookie: Cookie)
---@field redirect_to fun(self: HTTPServerResponse, redirect_uri: string)
---@field redirect_temporary fun(self: HTTPServerResponse, redirect_uri: string)
---@field redirect_permanent fun(self: HTTPServerResponse, redirect_uri: string)
---@class Cookie
---@field set_name fun(self: Cookie, name: string)
---@field set_value fun(self: Cookie, value: string)
---@field set_domain fun(self: Cookie, domain: string)
---@field set_path fun(self: Cookie, path: string)
---@field set_expiration fun(self: Cookie, expiration: number)
---@field set_http_only fun(self: Cookie, http_only: boolean)
---@field set_max_age fun(self: Cookie, max_age: number)
---@field set_permanent fun(self: Cookie)
---@field get_name fun(self: Cookie): string?
---@field get_value fun(self: Cookie): string?
---@field get_domain fun(self: Cookie): string?
---@field get_path fun(self: Cookie): string?
---@field get_expiration fun(self: Cookie): number?
---@field get_http_only fun(self: Cookie): boolean?
---@field get_max_age fun(self: Cookie): number?
---@class CloseFrame
---@field code integer
---@field reason string
---@alias WebSocketMessageType "text" | "bytes" | "close"
---@class WebSocketMessage
---@field type WebSocketMessageType
---@field value string
---@class WebSocket
---Receive another message. Returns `nil` if the stream has closed.
---@field recv fun(socket: WebSocket): WebSocketMessage|nil
---A flexible WebSocket message
---@field send fun(socket: WebSocket, message_type: WebSocketMessageType, message: any)
---A text WebSocket message
---@field send_text fun(socket: WebSocket, message: string)
---A binary WebSocket message
---@field send_bytes fun(socket: WebSocket, bytes: table)
---@field send_close fun(socket: WebSocket, close_frame: CloseFrame?)
---@alias wscallback fun(socket: WebSocket): any
----------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------
---@class HTTPServer
---@field shutdown fun(HTTPServer) Shuts down the server
---@diagnostic disable-next-line: missing-fields
local HTTPServer =
http. =
---@return HTTPServer
local
---@param path string
---@param callback callback
---@param config HTTPRouteConfiguration?
---@param path string
---@param callback callback
---@param config HTTPRouteConfiguration?
---@param path string
---@param callback callback
---@param config HTTPRouteConfiguration?
---@param path string
---@param callback callback
---@param config HTTPRouteConfiguration?
---@param path string
---@param callback callback
---@param config HTTPRouteConfiguration?
---@param path string
---@param callback callback
---@param config HTTPRouteConfiguration?
---@param path string
---@param callback callback
---@param config HTTPRouteConfiguration?
---@param path string
---@param serve_path string
---@param config HTTPRouteConfiguration?
---@param path string
---@param serve_path string
---@param config HTTPRouteConfiguration?
---@param path string
---@param wscallback wscallback
---@param config HTTPRouteConfiguration?
---@param callback callback
---Runs the server
http. =
--- Chains middlewares together in order
---@param chain table A list of middlewares
---@return function middleware Composed middleware
---
--- Functionally
--- ```lua
--- chain {context, html, logger} (handler)
--- ```
--- equals to
--- ```lua
--- context(html(logger(handler)))
--- ```
---
--- and
--- ```lua
--- chain {context, html, logger}
--- ```
--- equals to
--- ```lua
--- function(next_handler)
--- return function(request, response, ctx)
--- context(html(logger(next_handler(request, response, ctx))))
--- end
--- end
--- ```
---Opens a new async HTTP Request. The request is running as a task in parallel
---@param details string | HTTPClientRequestTableType
---@return HTTPClientRequest
---@nodiscard
---@diagnostic disable-next-line: missing-return, lowercase-global
http. =
return http