-
Notifications
You must be signed in to change notification settings - Fork 11
/
mist.gleam
517 lines (472 loc) · 15.8 KB
/
mist.gleam
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
import gleam/bit_builder.{BitBuilder}
import gleam/bit_string
import gleam/erlang/process.{ProcessDown, Selector}
import gleam/function
import gleam/http/request.{Request}
import gleam/http/response.{Response}
import gleam/int
import gleam/io
import gleam/iterator.{Iterator}
import gleam/option.{None, Option, Some}
import gleam/otp/actor
import gleam/result
import glisten
import glisten/acceptor
import glisten/socket
import glisten/socket/transport
import mist/internal/buffer.{Buffer}
import mist/internal/file
import mist/internal/handler.{
Bytes as InternalBytes, Chunked as InternalChunked, File as InternalFile,
ResponseData as InternalResponseData, Websocket as InternalWebsocket,
}
import mist/internal/http.{Connection as InternalConnection}
import mist/internal/websocket.{
BinaryFrame, Data, Internal, SocketClosed, TextFrame, User, ValidMessage,
WebsocketConnection,
}
/// Re-exported type that represents the default `Request` body type. See
/// `mist.read_body` to convert this type into a `BitString`. The `Connection`
/// also holds some additional information about the request. Currently, the
/// only useful field is `client_ip` which is a `Result` with a tuple of
/// integers representing the IPv4 address.
pub type Connection =
InternalConnection
/// The response body type. This allows `mist` to handle these different cases
/// for you. `Bytes` is the regular data return. `Websocket` will upgrade the
/// socket to websockets, but should not be used directly. See the
/// `mist.upgrade` function for usage. `Chunked` will use
/// `Transfer-Encoding: chunked` to send an iterator in chunks. `File` will use
/// Erlang's `sendfile` to more efficiently return a file to the client.
pub type ResponseData {
Websocket(Selector(ProcessDown))
Bytes(BitBuilder)
Chunked(Iterator(BitBuilder))
/// See `mist.send_file` to use this response type.
File(descriptor: file.FileDescriptor, offset: Int, length: Int)
}
/// Potential errors when opening a file to send. This list is
/// currently not exhaustive with POSIX errors.
pub type FileError {
IsDir
NoAccess
NoEntry
UnknownFileError
}
fn convert_file_errors(err: file.FileError) -> FileError {
case err {
file.IsDir -> IsDir
file.NoAccess -> NoAccess
file.NoEntry -> NoEntry
file.UnknownFileError -> UnknownFileError
}
}
/// To respond with a file using Erlang's `sendfile`, use this function
/// with the specified offset and limit (optional). It will attempt to open the
/// file for reading, get its file size, and then send the file. If the read
/// errors, this will return the relevant `FileError`. Generally, this will be
/// more memory efficient than manually doing this process with `mist.Bytes`.
pub fn send_file(
path: String,
offset offset: Int,
limit limit: Option(Int),
) -> Result(ResponseData, FileError) {
path
|> bit_string.from_string
|> file.stat
|> result.map_error(convert_file_errors)
|> result.map(fn(stat) {
File(
descriptor: stat.descriptor,
offset: offset,
length: option.unwrap(limit, stat.file_size),
)
})
}
/// The possible errors from reading the request body. If the size is larger
/// than the provided value, `ExcessBody` is returned. If there is an error
/// reading the body from the socket or the body is malformed (i.e a chunked
/// request with invalid sizes), `MalformedBody` is returned.
pub type ReadError {
ExcessBody
MalformedBody
}
/// The request body is not pulled from the socket until requested. The
/// `content-length` header is used to determine whether the socket is read
/// from or not. The read may also fail, and a `ReadError` is raised.
pub fn read_body(
req: Request(Connection),
max_body_limit max_body_limit: Int,
) -> Result(Request(BitString), ReadError) {
req
|> request.get_header("content-length")
|> result.then(int.parse)
|> result.unwrap(0)
|> fn(content_length) {
case content_length {
value if value <= max_body_limit -> {
http.read_body(req)
|> result.replace_error(MalformedBody)
}
_ -> {
Error(ExcessBody)
}
}
}
}
/// The values returning from streaming the request body. The `Chunk`
/// variant gives back some data and the next token. `Done` signifies
/// that we have completed reading the body.
pub type Chunk {
Chunk(data: BitString, consume: fn(Int) -> Result(Chunk, ReadError))
Done
}
fn do_stream(
req: Request(Connection),
buffer: Buffer,
) -> fn(Int) -> Result(Chunk, ReadError) {
fn(size) {
let socket = req.body.socket
let transport = req.body.transport
let byte_size = bit_string.byte_size(buffer.data)
case buffer.remaining, byte_size {
0, 0 -> Ok(Done)
0, _buffer_size -> {
let #(data, rest) = buffer.slice(buffer, size)
Ok(Chunk(data, do_stream(req, buffer.new(rest))))
}
_, buffer_size if buffer_size >= size -> {
let #(data, rest) = buffer.slice(buffer, size)
let new_buffer = Buffer(..buffer, data: rest)
Ok(Chunk(data, do_stream(req, new_buffer)))
}
_, _buffer_size -> {
http.read_data(socket, transport, buffer.empty(), http.InvalidBody)
|> result.replace_error(MalformedBody)
|> result.map(fn(data) {
let fetched_data = bit_string.byte_size(data)
let new_buffer =
Buffer(
data: bit_string.append(buffer.data, data),
remaining: int.max(0, buffer.remaining - fetched_data),
)
let #(new_data, rest) = buffer.slice(new_buffer, size)
Chunk(new_data, do_stream(req, Buffer(..new_buffer, data: rest)))
})
}
}
}
}
type ChunkState {
ChunkState(data_buffer: Buffer, chunk_buffer: Buffer, done: Bool)
}
fn do_stream_chunked(
req: Request(Connection),
state: ChunkState,
) -> fn(Int) -> Result(Chunk, ReadError) {
let socket = req.body.socket
let transport = req.body.transport
fn(size) {
case fetch_chunks_until(socket, transport, state, size) {
Ok(#(data, ChunkState(done: True, ..))) -> {
Ok(Chunk(data, fn(_size) { Ok(Done) }))
}
Ok(#(data, state)) -> {
Ok(Chunk(data, do_stream_chunked(req, state)))
}
Error(_) -> Error(MalformedBody)
}
}
}
fn fetch_chunks_until(
socket: socket.Socket,
transport: transport.Transport,
state: ChunkState,
byte_size: Int,
) -> Result(#(BitString, ChunkState), ReadError) {
let data_size = bit_string.byte_size(state.data_buffer.data)
case state.done, data_size {
_, size if size >= byte_size -> {
let #(value, rest) = buffer.slice(state.data_buffer, byte_size)
Ok(#(value, ChunkState(..state, data_buffer: buffer.new(rest))))
}
True, _ -> {
Ok(#(state.data_buffer.data, ChunkState(..state, done: True)))
}
False, _ -> {
case http.parse_chunk(state.chunk_buffer.data) {
http.Complete -> {
let updated_state =
ChunkState(..state, chunk_buffer: buffer.empty(), done: True)
fetch_chunks_until(socket, transport, updated_state, byte_size)
}
http.Chunk(<<>>, next_buffer) -> {
http.read_data(socket, transport, next_buffer, http.InvalidBody)
|> result.replace_error(MalformedBody)
|> result.then(fn(new_data) {
let updated_state =
ChunkState(..state, chunk_buffer: buffer.new(new_data))
fetch_chunks_until(socket, transport, updated_state, byte_size)
})
}
http.Chunk(data, next_buffer) -> {
let updated_state =
ChunkState(
..state,
data_buffer: buffer.append(state.data_buffer, data),
chunk_buffer: next_buffer,
)
fetch_chunks_until(socket, transport, updated_state, byte_size)
}
}
}
}
}
/// Rather than explicitly reading either the whole body (optionally up to
/// `N` bytes), this function allows you to consume a stream of the request
/// body. Any errors reading the body will propagate out, or `Chunk`s will be
/// emitted. This provides a `consume` method to attempt to grab the next
/// `size` chunk from the socket.
pub fn stream(
req: Request(Connection),
) -> Result(fn(Int) -> Result(Chunk, ReadError), ReadError) {
let continue =
req
|> http.handle_continue
|> result.replace_error(MalformedBody)
use _nil <- result.map(continue)
let is_chunked = case request.get_header(req, "transfer-encoding") {
Ok("chunked") -> True
_ -> False
}
let assert http.Initial(data) = req.body.body
case is_chunked {
True -> {
let state = ChunkState(buffer.new(<<>>), buffer.new(data), False)
do_stream_chunked(req, state)
}
False -> {
let content_length =
req
|> request.get_header("content-length")
|> result.then(int.parse)
|> result.unwrap(0)
let initial_size = bit_string.byte_size(data)
let buffer =
Buffer(data: data, remaining: int.max(0, content_length - initial_size))
do_stream(req, buffer)
}
}
}
pub opaque type Builder(request_body, response_body) {
Builder(
port: Int,
handler: fn(Request(request_body)) -> Response(response_body),
after_start: fn(Int) -> Nil,
)
}
/// Create a new `mist` handler with a given function. The default port is
/// 4000.
pub fn new(handler: fn(Request(in)) -> Response(out)) -> Builder(in, out) {
Builder(
port: 4000,
handler: handler,
after_start: fn(port) {
let message = "Listening on localhost:" <> int.to_string(port)
io.println(message)
},
)
}
/// Assign a different listening port to the service.
pub fn port(builder: Builder(in, out), port: Int) -> Builder(in, out) {
Builder(..builder, port: port)
}
/// This function allows for implicitly reading the body of requests up
/// to a given size. If the size is too large, or the read fails, the provided
/// `failure_response` will be sent back as the response.
pub fn read_request_body(
builder: Builder(BitString, out),
bytes_limit bytes_limit: Int,
failure_response failure_response: Response(out),
) -> Builder(Connection, out) {
let handler = fn(request) {
case read_body(request, bytes_limit) {
Ok(request) -> builder.handler(request)
Error(_) -> failure_response
}
}
Builder(builder.port, handler, builder.after_start)
}
/// Override the default function to be called after the service starts. The
/// default is to log a message with the listening port.
pub fn after_start(
builder: Builder(in, out),
after_start: fn(Int) -> Nil,
) -> Builder(in, out) {
Builder(..builder, after_start: after_start)
}
fn convert_body_types(
resp: Response(ResponseData),
) -> Response(InternalResponseData) {
let new_body = case resp.body {
Websocket(selector) -> InternalWebsocket(selector)
Bytes(data) -> InternalBytes(data)
File(descriptor, offset, length) -> InternalFile(descriptor, offset, length)
Chunked(iter) -> InternalChunked(iter)
}
response.set_body(resp, new_body)
}
/// Start a `mist` service over HTTP with the provided builder.
pub fn start_http(
builder: Builder(Connection, ResponseData),
) -> Result(Nil, glisten.StartError) {
builder.handler
|> function.compose(convert_body_types)
|> handler.with_func
|> acceptor.new_pool_with_data(handler.new_state())
|> glisten.serve(builder.port, _)
|> result.map(fn(nil) {
builder.after_start(builder.port)
// TODO: This should not be `Nil` but instead a subject that can receive
// messages, such as shutdown
nil
})
}
/// Start a `mist` service over HTTPS with the provided builder. This method
/// requires both a certificate file and a key file. The library will attempt
/// to read these files off of the disk.
pub fn start_https(
builder: Builder(Connection, ResponseData),
certfile certfile: String,
keyfile keyfile: String,
) -> Result(Nil, glisten.StartError) {
builder.handler
|> function.compose(convert_body_types)
|> handler.with_func
|> acceptor.new_pool_with_data(handler.new_state())
|> glisten.serve_ssl(builder.port, certfile, keyfile, _)
|> result.map(fn(nil) {
builder.after_start(builder.port)
// TODO: This should not be `Nil` but instead a subject that can receive
// messages, such as shutdown
nil
})
}
/// These are the types of messages that a websocket handler may receive.
pub type WebsocketMessage(custom) {
Text(BitString)
Binary(BitString)
Closed
Shutdown
Custom(custom)
}
fn internal_to_public_ws_message(
msg: ValidMessage(custom),
) -> WebsocketMessage(custom) {
case msg {
Internal(Data(TextFrame(_length, data))) -> Text(data)
Internal(Data(BinaryFrame(_length, data))) -> Binary(data)
SocketClosed -> Closed
User(msg) -> Custom(msg)
}
}
pub opaque type WebsocketBuilder(state, message) {
WebsocketBuilder(
request: Request(Connection),
state: state,
handler: fn(state, WebsocketConnection, ValidMessage(message)) ->
actor.Next(message, state),
selector: Option(process.Selector(message)),
)
}
/// Initializes a builder for upgrading a connection to Websockets. The
/// default handler will shut down the process on receipt of a message.
/// The default state is empty, and no external messages can be received.
pub fn websocket(request: Request(Connection)) -> WebsocketBuilder(Nil, any) {
WebsocketBuilder(
request: request,
state: Nil,
handler: fn(_, _, _) { actor.Stop(process.Normal) },
selector: None,
)
}
/// Provide an external selector for user-specified messages that the websocket
/// process may receive. These will be provided in the `Custom` type.
pub fn selecting(
builder: WebsocketBuilder(state, message),
selector: process.Selector(message),
) -> WebsocketBuilder(state, message) {
WebsocketBuilder(..builder, selector: Some(selector))
}
/// Adds some initial state to the websocket handler.
pub fn with_state(
builder: WebsocketBuilder(state, message),
state: state,
) -> WebsocketBuilder(state, message) {
WebsocketBuilder(..builder, state: state)
}
/// Provides a function to call for each `WebsocketMessage` received by the
/// process.
pub fn on_message(
builder: WebsocketBuilder(state, message),
handler: fn(state, WebsocketConnection, WebsocketMessage(message)) ->
actor.Next(message, state),
) -> WebsocketBuilder(state, message) {
let handler = fn(state, connection, message) {
message
|> internal_to_public_ws_message
|> handler(state, connection, _)
}
WebsocketBuilder(builder.request, builder.state, handler, builder.selector)
}
/// Sends a binary frame across the websocket.
pub fn send_binary_frame(
connection: WebsocketConnection,
frame: BitString,
) -> Result(Nil, socket.SocketReason) {
frame
|> websocket.to_binary_frame
|> connection.transport.send(connection.socket, _)
}
/// Sends a text frame across the websocket.
pub fn send_text_frame(
connection: WebsocketConnection,
frame: BitString,
) -> Result(Nil, socket.SocketReason) {
frame
|> websocket.to_text_frame
|> connection.transport.send(connection.socket, _)
}
/// Upgrade a request to handle websockets. If the request is
/// malformed, or the websocket process fails to initialize, an empty
/// 400 response will be sent to the client.
pub fn upgrade(
builder: WebsocketBuilder(state, message),
) -> Response(ResponseData) {
let socket = builder.request.body.socket
let transport = builder.request.body.transport
builder.request
|> http.upgrade(socket, transport, _)
|> result.then(fn(_nil) {
websocket.initialize_connection(
builder.state,
builder.selector,
builder.handler,
socket,
transport,
)
})
|> result.map(fn(subj) {
let ws_process = process.subject_owner(subj)
let monitor = process.monitor_process(ws_process)
let selector =
process.new_selector()
|> process.selecting_process_down(monitor, function.identity)
response.new(200)
|> response.set_body(Websocket(selector))
})
|> result.lazy_unwrap(fn() {
response.new(400)
|> response.set_body(Bytes(bit_builder.new()))
})
}