| @@ -51,57 +51,45 @@ import std/result |
| 51 | 51 | import std/string |
| 52 | 52 | import std/bytes.{type Builder} |
| 53 | 53 | import std/dict.{type Dict} |
| 54 | | −import std/net.{ |
| 55 | | − type SocketAddress |
| 56 | | −, type TcpListener |
| 57 | | −, type TcpStream |
| 58 | | −, SocketAddress |
| 59 | | −} |
| 54 | +import std/net. |
| 55 | + {type SocketAddress, type TcpListener, type TcpStream, SocketAddress} |
| 60 | 56 | import std/ffi.{type Resource} |
| 61 | 57 | import std/html.{type Html} |
| 62 | | −import std/process.{ |
| 63 | | − type proc |
| 64 | | −, type Pid |
| 65 | | −, type Subject |
| 66 | | −, type ExitReason |
| 67 | | −, Crashed |
| 68 | | −, Down |
| 69 | | −} |
| 58 | +import std/process. |
| 59 | + {type proc, type Pid, type Subject, type ExitReason, Crashed, Down} |
| 70 | 60 | import std/random.{type random} |
| 71 | 61 | import std/time.{type clock} |
| 72 | 62 | |
| 73 | | −pub type Request { |
| 74 | | − Request( |
| 75 | | − method: String |
| 76 | | − , path: String |
| 77 | | − , headers: List<(String, String)> |
| 78 | | − , body: Bytes |
| 79 | | − ) |
| 80 | | −} |
| 63 | +pub type Request |
| 64 | + { Request |
| 65 | + ( method: String |
| 66 | + , path: String |
| 67 | + , headers: List<(String, String)> |
| 68 | + , body: Bytes |
| 69 | + ) |
| 70 | + } |
| 81 | 71 | |
| 82 | | −pub type Response { |
| 83 | | − Response(status: Int, headers: List<(String, String)>, body: Body) |
| 84 | | −} |
| 72 | +pub type Response |
| 73 | + { Response(status: Int, headers: List<(String, String)>, body: Body) } |
| 85 | 74 | |
| 86 | 75 | /// What a response's body is (ADR 0053). |
| 87 | | −pub type Body { |
| 88 | | − /// The whole body, in hand. |
| 89 | | − Whole(Bytes) |
| 90 | | − /// Written chunk by chunk: the server sends the head with |
| 91 | | − /// `Transfer-Encoding: chunked`, then calls `write` with a `Writer` |
| 92 | | − /// and sends the final chunk when it returns. Between chunks the |
| 93 | | − /// connection is not timed; each chunk's write is, per chunk, by the |
| 94 | | − /// write timeout. A HEAD request sends the head only (`write` is not |
| 95 | | − /// called). If `write` unwinds, the connection dies without the final |
| 96 | | − /// chunk: the client sees a truncated stream, the only honest end |
| 97 | | − /// once the status line is sent. |
| 98 | | −, Streamed(write: fn(Writer) -> () / <io, proc, clock, random, ffi>) |
| 99 | | −} |
| 76 | +pub type Body |
| 77 | + { /// The whole body, in hand. |
| 78 | + Whole(Bytes) |
| 79 | + /// Written chunk by chunk: the server sends the head with |
| 80 | + /// `Transfer-Encoding: chunked`, then calls `write` with a `Writer` |
| 81 | + /// and sends the final chunk when it returns. Between chunks the |
| 82 | + /// connection is not timed; each chunk's write is, per chunk, by the |
| 83 | + /// write timeout. A HEAD request sends the head only (`write` is not |
| 84 | + /// called). If `write` unwinds, the connection dies without the final |
| 85 | + /// chunk: the client sees a truncated stream, the only honest end |
| 86 | + /// once the status line is sent. |
| 87 | + , Streamed(write: fn(Writer) -> () / <io, proc, clock, random, ffi>) |
| 88 | + } |
| 100 | 89 | |
| 101 | 90 | /// What a streamed response writes with (ADR 0053). |
| 102 | | −pub opaque type Writer { |
| 103 | | − Writer(conn: Resource<TcpStream>, activity: Subject<Activity>) |
| 104 | | −} |
| 91 | +pub opaque type Writer |
| 92 | + { Writer(conn: Resource<TcpStream>, activity: Subject<Activity>) } |
| 105 | 93 | |
| 106 | 94 | /// Write one chunk, flushed (one frame, one socket write). `Error` says |
| 107 | 95 | /// the client is gone: stop writing. |
| @@ -118,8 +106,9 @@ pub fn write_chunk(w: Writer, chunk: Bytes) -> Result<(), String> / <io, proc> { |
| 118 | 106 | } |
| 119 | 107 | |
| 120 | 108 | /// Write one chunk of text, flushed. |
| 121 | | −pub fn write_string(w: Writer, text: String) -> Result<(), String> |
| 122 | | − / <io, proc> { |
| 109 | +pub fn write_string |
| 110 | + (w: Writer, text: String) |
| 111 | + -> Result<(), String> / <io, proc> { |
| 123 | 112 | write_chunk(w, bytes.from_string(text)) |
| 124 | 113 | } |
| 125 | 114 | |
| @@ -164,39 +153,39 @@ fn hex_digit(d: Int) -> String { |
| 164 | 153 | |
| 165 | 154 | /// Change the defaults with a record update: |
| 166 | 155 | /// `Settings(..http.defaults(), idle_timeout: 1000)`. |
| 167 | | −pub type Settings { |
| 168 | | − /// - `connections`: serve this many connections, then return (all of |
| 169 | | − /// them if negative); |
| 170 | | − /// - `idle_timeout`: close a connection that waits this many |
| 171 | | − /// milliseconds for a request to start, or for more of a body (never |
| 172 | | − /// if negative); |
| 173 | | − /// - `write_timeout`: close a connection whose response takes longer |
| 174 | | − /// than this many milliseconds to send (never if negative); |
| 175 | | − /// - `max_body`: answer 413 to a request whose body is larger than this |
| 176 | | − /// many bytes, and close the connection (no limit if negative); |
| 177 | | − /// - `max_open`: keep at most this many connections open at once (no |
| 178 | | − /// limit if negative). At the limit, the next client is held (one |
| 179 | | − /// more open socket) and the oldest connection asked to close, at once |
| 180 | | − /// if it waits for a request, else after its current response; |
| 181 | | − /// - `max_requests`: answer this many requests on one connection, the |
| 182 | | − /// last with `Connection: close` (no limit if negative); |
| 183 | | − /// - `head_timeout`: close a connection whose request head, once begun, |
| 184 | | − /// takes longer than this many milliseconds to arrive whole (never if |
| 185 | | − /// negative); |
| 186 | | − /// - `drain_timeout`: once `serve_until` is told to stop, give the |
| 187 | | − /// requests in flight this many milliseconds to finish, then close |
| 188 | | − /// their connections (no limit if negative). |
| 189 | | − Settings( |
| 190 | | − connections: Int |
| 191 | | − , idle_timeout: Int |
| 192 | | − , write_timeout: Int |
| 193 | | − , max_body: Int |
| 194 | | − , max_open: Int |
| 195 | | − , head_timeout: Int |
| 196 | | − , max_requests: Int |
| 197 | | − , drain_timeout: Int |
| 198 | | − ) |
| 199 | | −} |
| 156 | +pub type Settings |
| 157 | + { /// - `connections`: serve this many connections, then return (all of |
| 158 | + /// them if negative); |
| 159 | + /// - `idle_timeout`: close a connection that waits this many |
| 160 | + /// milliseconds for a request to start, or for more of a body (never |
| 161 | + /// if negative); |
| 162 | + /// - `write_timeout`: close a connection whose response takes longer |
| 163 | + /// than this many milliseconds to send (never if negative); |
| 164 | + /// - `max_body`: answer 413 to a request whose body is larger than this |
| 165 | + /// many bytes, and close the connection (no limit if negative); |
| 166 | + /// - `max_open`: keep at most this many connections open at once (no |
| 167 | + /// limit if negative). At the limit, the next client is held (one |
| 168 | + /// more open socket) and the oldest connection asked to close, at once |
| 169 | + /// if it waits for a request, else after its current response; |
| 170 | + /// - `max_requests`: answer this many requests on one connection, the |
| 171 | + /// last with `Connection: close` (no limit if negative); |
| 172 | + /// - `head_timeout`: close a connection whose request head, once begun, |
| 173 | + /// takes longer than this many milliseconds to arrive whole (never if |
| 174 | + /// negative); |
| 175 | + /// - `drain_timeout`: once `serve_until` is told to stop, give the |
| 176 | + /// requests in flight this many milliseconds to finish, then close |
| 177 | + /// their connections (no limit if negative). |
| 178 | + Settings |
| 179 | + ( connections: Int |
| 180 | + , idle_timeout: Int |
| 181 | + , write_timeout: Int |
| 182 | + , max_body: Int |
| 183 | + , max_open: Int |
| 184 | + , head_timeout: Int |
| 185 | + , max_requests: Int |
| 186 | + , drain_timeout: Int |
| 187 | + ) |
| 188 | + } |
| 200 | 189 | |
| 201 | 190 | /// Every connection, at most 512 at once (under the usual limit of 1024 |
| 202 | 191 | /// open files, so the limit is reached before `accept` fails); closed |
| @@ -205,8 +194,7 @@ pub type Settings { |
| 205 | 194 | /// requests (nginx's `keepalive_requests`); bodies up to 1 MiB; 10 |
| 206 | 195 | /// seconds to drain when stopped. |
| 207 | 196 | pub fn defaults() -> Settings { |
| 208 | | − Settings( |
| 209 | | − connections: -1 |
| 197 | + Settings( connections: -1 |
| 210 | 198 | , idle_timeout: 5000 |
| 211 | 199 | , write_timeout: 30000 |
| 212 | 200 | , max_body: 1048576 |
| @@ -304,11 +292,9 @@ fn forwarded_for(req: Request) -> List<String> { |
| 304 | 292 | |
| 305 | 293 | /// The first of `hops` (right-most first) not in `trusted`, else the |
| 306 | 294 | /// last one seen (`seen`: the peer, before any). |
| 307 | | −fn untrusted_hop( |
| 308 | | − hops: List<String> |
| 309 | | −, trusted: List<String> |
| 310 | | −, seen: String |
| 311 | | −) -> String { |
| 295 | +fn untrusted_hop |
| 296 | + (hops: List<String>, trusted: List<String>, seen: String) |
| 297 | + -> String { |
| 312 | 298 | match hops { |
| 313 | 299 | [] -> seen |
| 314 | 300 | , [hop, ..rest] -> if is_trusted(trusted, hop) { |
| @@ -365,10 +351,11 @@ pub fn response(status: Int, content_type: String, body: Bytes) -> Response { |
| 365 | 351 | /// // each component, as it finishes |
| 366 | 352 | /// let _ = http.write_string(w, "</body></html>") |
| 367 | 353 | /// }) |
| 368 | | −pub fn stream( |
| 369 | | − content_type: String |
| 370 | | −, write: fn(Writer) -> () / <io, proc, clock, random, ffi> |
| 371 | | −) -> Response { |
| 354 | +pub fn stream |
| 355 | + ( content_type: String |
| 356 | + , write: fn(Writer) -> () / <io, proc, clock, random, ffi> |
| 357 | + ) |
| 358 | + -> Response { |
| 372 | 359 | Response(200, [("Content-Type", content_type)], Streamed(write)) |
| 373 | 360 | } |
| 374 | 361 | |
| @@ -383,8 +370,7 @@ pub fn whole_body(resp: Response) -> Option<Bytes> { |
| 383 | 370 | |
| 384 | 371 | /// A 200 response with a rendered page. |
| 385 | 372 | pub fn html(page: Html) -> Response { |
| 386 | | − response( |
| 387 | | − 200 |
| 373 | + response( 200 |
| 388 | 374 | , "text/html; charset=utf-8" |
| 389 | 375 | , bytes.from_string(html.render(page)) |
| 390 | 376 | ) |
| @@ -406,28 +392,30 @@ pub fn with_header(resp: Response, name: String, value: String) -> Response { |
| 406 | 392 | } |
| 407 | 393 | |
| 408 | 394 | /// Listen for HTTP connections. |
| 409 | | −pub fn listen(host: String, port: Int) -> Result<Resource<TcpListener>, String> |
| 410 | | − / <io> { |
| 395 | +pub fn listen |
| 396 | + (host: String, port: Int) |
| 397 | + -> Result<Resource<TcpListener>, String> / <io> { |
| 411 | 398 | net.listen(host, port) |
| 412 | 399 | } |
| 413 | 400 | |
| 414 | 401 | /// Serve forever with the default settings. |
| 415 | | −pub fn serve( |
| 416 | | − server: Resource<TcpListener> |
| 417 | | −, handler: send fn(Request) -> Response / <io, proc, clock, random, ffi> |
| 418 | | −) -> () / <io, proc> { |
| 402 | +pub fn serve |
| 403 | + ( server: Resource<TcpListener> |
| 404 | + , handler: send fn(Request) -> Response / <io, proc, clock, random, ffi> |
| 405 | + ) |
| 406 | + -> () / <io, proc> { |
| 419 | 407 | serve_with(server, defaults(), handler) |
| 420 | 408 | } |
| 421 | 409 | |
| 422 | 410 | /// Serve with `settings`: one actor per connection. When this returns, |
| 423 | 411 | /// the last connections' actors may still be running. |
| 424 | | −pub fn serve_with( |
| 425 | | − server: Resource<TcpListener> |
| 426 | | −, settings: Settings |
| 427 | | −, handler: send fn(Request) -> Response / <io, proc, clock, random, ffi> |
| 428 | | −) -> () / <io, proc> { |
| 429 | | − accept_all( |
| 430 | | − server |
| 412 | +pub fn serve_with |
| 413 | + ( server: Resource<TcpListener> |
| 414 | + , settings: Settings |
| 415 | + , handler: send fn(Request) -> Response / <io, proc, clock, random, ffi> |
| 416 | + ) |
| 417 | + -> () / <io, proc> { |
| 418 | + accept_all( server |
| 431 | 419 | , settings |
| 432 | 420 | , handler |
| 433 | 421 | , process.subject() |
| @@ -447,31 +435,32 @@ pub fn serve_with( |
| 447 | 435 | /// SIGTERM, `process.exits()` for a `main` that traps exits under |
| 448 | 436 | /// `[application]` (ADR 0060), or a `process.subject()` another actor |
| 449 | 437 | /// sends on. |
| 450 | | −pub fn serve_until( |
| 451 | | − server: Resource<TcpListener> |
| 452 | | −, settings: Settings |
| 453 | | −, handler: send fn(Request) -> Response / <io, proc, clock, random, ffi> |
| 454 | | −, stop: Subject<m> |
| 455 | | −) -> () / <io, proc> { |
| 456 | | − let ready = process.subject() |
| 457 | | − let (_, done) = process.spawn_monitor( |
| 458 | | − fn() { |
| 459 | | − let events = process.subject() |
| 460 | | − process.send(ready, events) |
| 461 | | − accept_all( |
| 462 | | − server |
| 463 | | − , settings |
| 464 | | − , handler |
| 465 | | − , events |
| 466 | | − , Open(0, dict.empty(), 0, False) |
| 467 | | − , 10 |
| 468 | | − ) |
| 469 | | − } |
| 438 | +pub fn serve_until |
| 439 | + ( server: Resource<TcpListener> |
| 440 | + , settings: Settings |
| 441 | + , handler: send fn(Request) -> Response / <io, proc, clock, random, ffi> |
| 442 | + , stop: Subject<m> |
| 470 | 443 | ) |
| 444 | + -> () / <io, proc> { |
| 445 | + let ready = process.subject() |
| 446 | + let (_, done) = |
| 447 | + process.spawn_monitor( fn() { |
| 448 | + let events = process.subject() |
| 449 | + process.send(ready, events) |
| 450 | + accept_all( server |
| 451 | + , settings |
| 452 | + , handler |
| 453 | + , events |
| 454 | + , Open(0, dict.empty(), 0, False) |
| 455 | + , 10 |
| 456 | + ) |
| 457 | + } |
| 458 | + ) |
| 471 | 459 | let events = process.receive_forever(ready) |
| 472 | | − let stopped = process.new_selector() |
| 473 | | − |> process.selecting(stop, fn(_) { True }) |
| 474 | | − |> process.selecting(done, fn(_) { False }) |
| 460 | + let stopped = |
| 461 | + process.new_selector() |
| 462 | + |> process.selecting(stop, fn(_) { True }) |
| 463 | + |> process.selecting(done, fn(_) { False }) |
| 475 | 464 | if process.select_forever(stopped) { |
| 476 | 465 | // Wakes the acceptor, parked in `accept`, which then drains. |
| 477 | 466 | process.send(events, Stop) |
| @@ -496,32 +485,34 @@ fn stopped_with(reason: ExitReason) -> () { |
| 496 | 485 | /// watchdogs report on `events` when they open and end. After a failed |
| 497 | 486 | /// `accept` (out of file descriptors, say), wait `pause` milliseconds, |
| 498 | 487 | /// doubling up to a second while it keeps failing (ADR 0012, Revision 2). |
| 499 | | −fn accept_all( |
| 500 | | − server |
| 501 | | −, settings: Settings |
| 502 | | −, handler |
| 503 | | −, events: Subject<Event> |
| 504 | | −, open: Open |
| 505 | | −, pause: Int |
| 506 | | −) -> () / <io, proc> { |
| 488 | +fn accept_all |
| 489 | + ( server |
| 490 | + , settings: Settings |
| 491 | + , handler |
| 492 | + , events: Subject<Event> |
| 493 | + , open: Open |
| 494 | + , pause: Int |
| 495 | + ) |
| 496 | + -> () / <io, proc> { |
| 507 | 497 | if settings.connections != 0 { |
| 508 | 498 | let next = Settings(..settings, connections: settings.connections - 1) |
| 509 | 499 | match net.accept(server) { |
| 510 | 500 | Ok(conn) -> { |
| 511 | 501 | // At `max_open`, this client is held until there is room (or the |
| 512 | 502 | // server stops: it is served, then drained). |
| 513 | | − let open = make_room( |
| 514 | | − events |
| 515 | | − , take_events(events, open, False) |
| 516 | | − , settings.max_open |
| 517 | | − , False |
| 518 | | − ) |
| 503 | + let open = |
| 504 | + make_room( events |
| 505 | + , take_events(events, open, False) |
| 506 | + , settings.max_open |
| 507 | + , False |
| 508 | + ) |
| 519 | 509 | let id = open.next |
| 520 | | − let _ = process.spawn( |
| 521 | | − fn() { handle_connection(conn, id, settings, handler, events) } |
| 522 | | − ) |
| 523 | | − accept_all( |
| 524 | | − server |
| 510 | + let _ = |
| 511 | + process.spawn( fn() { |
| 512 | + handle_connection(conn, id, settings, handler, events) |
| 513 | + } |
| 514 | + ) |
| 515 | + accept_all( server |
| 525 | 516 | , next |
| 526 | 517 | , handler |
| 527 | 518 | , events |
| @@ -536,8 +527,7 @@ fn accept_all( |
| 536 | 527 | } else { |
| 537 | 528 | io.eprintln("http: accept failed: " <> e) |
| 538 | 529 | process.sleep(pause) |
| 539 | | − accept_all( |
| 540 | | − server |
| 530 | + accept_all( server |
| 541 | 531 | , next |
| 542 | 532 | , handler |
| 543 | 533 | , events |
| @@ -556,14 +546,10 @@ fn accept_all( |
| 556 | 546 | /// (idle ones at once, busy ones after their response), and wait until |
| 557 | 547 | /// they have, for `timeout` milliseconds at most; then close the rest. |
| 558 | 548 | fn drain(events: Subject<Event>, open: Open, timeout: Int) -> () / <proc> { |
| 559 | | − let asked = list.map( |
| 560 | | − dict.to_list(open.closable) |
| 561 | | − , fn(entry) { ask_to_close(entry.1) } |
| 562 | | − ) |
| 549 | + let asked = |
| 550 | + list.map(dict.to_list(open.closable), fn(entry) { ask_to_close(entry.1) }) |
| 563 | 551 | let timer = if timeout >= 0 { |
| 564 | | − Some( |
| 565 | | − process.spawn( |
| 566 | | − fn() { |
| 552 | + Some( process.spawn( fn() { |
| 567 | 553 | process.sleep(timeout) |
| 568 | 554 | process.send(events, DrainOver) |
| 569 | 555 | } |
| @@ -591,11 +577,9 @@ fn ask_to_close(subjects: (Subject<()>, Subject<()>)) -> Subject<()> / <proc> { |
| 591 | 577 | /// Wait until `count` connections have ended, asking those that open |
| 592 | 578 | /// meanwhile (accepted just before the stop) to close too; at |
| 593 | 579 | /// `DrainOver`, stop the actors of those asked. |
| 594 | | −fn wait_drained( |
| 595 | | − events: Subject<Event> |
| 596 | | −, count: Int |
| 597 | | −, asked: List<Subject<()>> |
| 598 | | −) -> () / <proc> { |
| 580 | +fn wait_drained |
| 581 | + (events: Subject<Event>, count: Int, asked: List<Subject<()>>) |
| 582 | + -> () / <proc> { |
| 599 | 583 | if count > 0 { |
| 600 | 584 | match process.receive_forever(events) { |
| 601 | 585 | Ended(_) -> wait_drained(events, count - 1, asked) |
| @@ -610,52 +594,46 @@ fn wait_drained( |
| 610 | 594 | } |
| 611 | 595 | |
| 612 | 596 | /// What a connection's watchdog tells the acceptor. |
| 613 | | −type Event { |
| 614 | | − /// Connection `id` is open: a `()` on both subjects (its actor's, its |
| 615 | | − /// watchdog's) asks it to close. |
| 616 | | − Opened(id: Int, actor: Subject<()>, watchdog: Subject<()>) |
| 617 | | − /// Connection `id` ended. |
| 618 | | −, Ended(id: Int) |
| 619 | | − /// `serve_until` was told to stop: drain once `accept` fails. |
| 620 | | −, Stop |
| 621 | | − /// The drain timeout passed. |
| 622 | | −, DrainOver |
| 623 | | −} |
| 597 | +type Event |
| 598 | + { /// Connection `id` is open: a `()` on both subjects (its actor's, its |
| 599 | + /// watchdog's) asks it to close. |
| 600 | + Opened(id: Int, actor: Subject<()>, watchdog: Subject<()>) |
| 601 | + /// Connection `id` ended. |
| 602 | + , Ended(id: Int) |
| 603 | + /// `serve_until` was told to stop: drain once `accept` fails. |
| 604 | + , Stop |
| 605 | + /// The drain timeout passed. |
| 606 | + , DrainOver |
| 607 | + } |
| 624 | 608 | |
| 625 | 609 | /// The acceptor's view: `count` connections open, those it may still ask |
| 626 | 610 | /// to close by id (ids grow, so the least is the oldest), the next id, |
| 627 | 611 | /// and whether `serve_until` was told to stop. |
| 628 | | −type Open { |
| 629 | | − Open( |
| 630 | | − count: Int |
| 631 | | − , closable: Dict<Int, (Subject<()>, Subject<()>)> |
| 632 | | − , next: Int |
| 633 | | − , stopping: Bool |
| 634 | | − ) |
| 635 | | −} |
| 612 | +type Open |
| 613 | + { Open |
| 614 | + ( count: Int |
| 615 | + , closable: Dict<Int, (Subject<()>, Subject<()>)> |
| 616 | + , next: Int |
| 617 | + , stopping: Bool |
| 618 | + ) |
| 619 | + } |
| 636 | 620 | |
| 637 | 621 | /// `open` after the events received so far; with `wait`, after waiting |
| 638 | 622 | /// for one first. |
| 639 | | −fn take_events( |
| 640 | | − events: Subject<Event> |
| 641 | | −, open: Open |
| 642 | | −, wait: Bool |
| 643 | | −) -> Open / <proc> { |
| 623 | +fn take_events |
| 624 | + (events: Subject<Event>, open: Open, wait: Bool) |
| 625 | + -> Open / <proc> { |
| 644 | 626 | match process.receive(events, if wait { -1 } else { 0 }) { |
| 645 | 627 | Some(Opened(id, actor, watchdog)) -> |
| 646 | | − take_events( |
| 647 | | − events |
| 648 | | − , Open( |
| 649 | | − ..open |
| 628 | + take_events( events |
| 629 | + , Open( ..open |
| 650 | 630 | , closable: dict.insert(open.closable, id, (actor, watchdog)) |
| 651 | 631 | ) |
| 652 | 632 | , False |
| 653 | 633 | ) |
| 654 | 634 | , Some(Ended(id)) -> |
| 655 | | − take_events( |
| 656 | | − events |
| 657 | | − , Open( |
| 658 | | − ..open |
| 635 | + take_events( events |
| 636 | + , Open( ..open |
| 659 | 637 | , count: open.count - 1 |
| 660 | 638 | , closable: dict.remove(open.closable, id) |
| 661 | 639 | ) |
| @@ -671,12 +649,9 @@ fn take_events( |
| 671 | 649 | /// oldest connection not asked yet is asked to close (once per held |
| 672 | 650 | /// client, `asked`), and the acceptor waits for one to end: that one, or |
| 673 | 651 | /// any other. |
| 674 | | −fn make_room( |
| 675 | | − events: Subject<Event> |
| 676 | | −, open: Open |
| 677 | | −, max_open: Int |
| 678 | | −, asked: Bool |
| 679 | | −) -> Open / <proc> { |
| 652 | +fn make_room |
| 653 | + (events: Subject<Event>, open: Open, max_open: Int, asked: Bool) |
| 654 | + -> Open / <proc> { |
| 680 | 655 | if max_open < 0 || open.count < max_open || open.stopping { |
| 681 | 656 | open |
| 682 | 657 | } else { |
| @@ -693,44 +668,47 @@ fn make_room( |
| 693 | 668 | } |
| 694 | 669 | |
| 695 | 670 | /// What a connection's watchdog learns. |
| 696 | | −type Activity { |
| 697 | | − /// The connection's actor waits for a request to start: timed by |
| 698 | | − /// `idle_timeout`. Asked to close now, the connection closes at once. |
| 699 | | − Idle |
| 700 | | − /// It waits for more of a body: timed by `idle_timeout` from each read. |
| 701 | | −, Reading |
| 702 | | − /// A request head has begun and is not whole yet: timed by |
| 703 | | − /// `head_timeout` from here, once, since nothing else is sent until the |
| 704 | | − /// head is read. |
| 705 | | −, HeadStarted |
| 706 | | − /// It runs the handler: not timed. |
| 707 | | −, Handling |
| 708 | | − /// It sends a response: timed. |
| 709 | | −, Writing |
| 710 | | − /// A streamed response, between chunks: not timed (a stream may be |
| 711 | | − /// quiet arbitrarily long); each chunk's write is `Writing`. |
| 712 | | −, Streaming |
| 713 | | − /// It stopped (also by crashing). |
| 714 | | −, Stopped |
| 715 | | − /// The acceptor asks the connection to close (at `max_open`, for a |
| 716 | | − /// waiting client): the watchdog closes it if it is `Idle`; otherwise |
| 717 | | − /// its actor answers the current request with `Connection: close`. |
| 718 | | −, Drain |
| 719 | | −} |
| 720 | | − |
| 721 | | −fn handle_connection( |
| 722 | | − conn: Resource<TcpStream> |
| 723 | | −, id: Int |
| 724 | | −, settings: Settings |
| 725 | | −, handler: fn(Request) -> Response / <io, proc, clock, random, ffi> |
| 726 | | −, events: Subject<Event> |
| 727 | | −) -> () / <io, proc, clock, random, ffi> { |
| 671 | +type Activity |
| 672 | + { /// The connection's actor waits for a request to start: timed by |
| 673 | + /// `idle_timeout`. Asked to close now, the connection closes at once. |
| 674 | + Idle |
| 675 | + /// It waits for more of a body: timed by `idle_timeout` from each read. |
| 676 | + , Reading |
| 677 | + /// A request head has begun and is not whole yet: timed by |
| 678 | + /// `head_timeout` from here, once, since nothing else is sent until the |
| 679 | + /// head is read. |
| 680 | + , HeadStarted |
| 681 | + /// It runs the handler: not timed. |
| 682 | + , Handling |
| 683 | + /// It sends a response: timed. |
| 684 | + , Writing |
| 685 | + /// A streamed response, between chunks: not timed (a stream may be |
| 686 | + /// quiet arbitrarily long); each chunk's write is `Writing`. |
| 687 | + , Streaming |
| 688 | + /// It stopped (also by crashing). |
| 689 | + , Stopped |
| 690 | + /// The acceptor asks the connection to close (at `max_open`, for a |
| 691 | + /// waiting client): the watchdog closes it if it is `Idle`; otherwise |
| 692 | + /// its actor answers the current request with `Connection: close`. |
| 693 | + , Drain |
| 694 | + } |
| 695 | + |
| 696 | +fn handle_connection |
| 697 | + ( conn: Resource<TcpStream> |
| 698 | + , id: Int |
| 699 | + , settings: Settings |
| 700 | + , handler: fn(Request) -> Response / <io, proc, clock, random, ffi> |
| 701 | + , events: Subject<Event> |
| 702 | + ) |
| 703 | + -> () / <io, proc, clock, random, ffi> { |
| 728 | 704 | let me = process.self() |
| 729 | 705 | let ready = process.subject() |
| 730 | 706 | let drain = process.subject() |
| 731 | | − let _ = process.spawn( |
| 732 | | − fn() { watchdog(conn, me, id, drain, settings, ready, events) } |
| 733 | | − ) |
| 707 | + let _ = |
| 708 | + process.spawn( fn() { |
| 709 | + watchdog(conn, me, id, drain, settings, ready, events) |
| 710 | + } |
| 711 | + ) |
| 734 | 712 | let activity = process.receive_forever(ready) |
| 735 | 713 | let peer = match net.peer(conn) { |
| 736 | 714 | Ok(address) -> [(":peer", address_text(address))] |
| @@ -748,24 +726,26 @@ fn handle_connection( |
| 748 | 726 | /// reference to the socket. It tells the acceptor that the connection |
| 749 | 727 | /// opened, with how to ask it to close (`drain`, its actor's subject, and |
| 750 | 728 | /// one of its own), and, once, that it ended (for `max_open`). |
| 751 | | −fn watchdog( |
| 752 | | − conn: Resource<TcpStream> |
| 753 | | −, owner: Pid |
| 754 | | −, id: Int |
| 755 | | −, drain: Subject<()> |
| 756 | | −, settings: Settings |
| 757 | | −, ready: Subject<Subject<Activity>> |
| 758 | | −, acceptor: Subject<Event> |
| 759 | | −) -> () / <io, proc> { |
| 729 | +fn watchdog |
| 730 | + ( conn: Resource<TcpStream> |
| 731 | + , owner: Pid |
| 732 | + , id: Int |
| 733 | + , drain: Subject<()> |
| 734 | + , settings: Settings |
| 735 | + , ready: Subject<Subject<Activity>> |
| 736 | + , acceptor: Subject<Event> |
| 737 | + ) |
| 738 | + -> () / <io, proc> { |
| 760 | 739 | let activity = process.subject() |
| 761 | 740 | let asked = process.subject() |
| 762 | 741 | let down = process.monitor(owner) |
| 763 | 742 | process.send(ready, activity) |
| 764 | 743 | process.send(acceptor, Opened(id, drain, asked)) |
| 765 | | − let events = process.new_selector() |
| 766 | | − |> process.selecting(activity, fn(a) { a }) |
| 767 | | − |> process.selecting(down, fn(_) { Stopped }) |
| 768 | | − |> process.selecting(asked, fn(_) { Drain }) |
| 744 | + let events = |
| 745 | + process.new_selector() |
| 746 | + |> process.selecting(activity, fn(a) { a }) |
| 747 | + |> process.selecting(down, fn(_) { Stopped }) |
| 748 | + |> process.selecting(asked, fn(_) { Drain }) |
| 769 | 749 | watch(conn, settings, events, settings.idle_timeout, True) |
| 770 | 750 | process.send(acceptor, Ended(id)) |
| 771 | 751 | } |
| @@ -795,45 +775,41 @@ fn watch(conn, settings: Settings, events, timeout, idle: Bool) { |
| 795 | 775 | } |
| 796 | 776 | |
| 797 | 777 | /// Why no request was read. |
| 798 | | −type Failure { |
| 799 | | − /// The connection closed or failed: nothing to answer. |
| 800 | | − Closed |
| 801 | | − /// A malformed request, answered with `status` before closing. |
| 802 | | −, Bad(status: Int, message: String) |
| 803 | | −} |
| 778 | +type Failure |
| 779 | + { /// The connection closed or failed: nothing to answer. |
| 780 | + Closed |
| 781 | + /// A malformed request, answered with `status` before closing. |
| 782 | + , Bad(status: Int, message: String) |
| 783 | + } |
| 804 | 784 | |
| 805 | 785 | /// A request, whether the client keeps the connection open after it, and |
| 806 | 786 | /// where the next request starts (offset `at` of `buffer`). |
| 807 | | −type Incoming { |
| 808 | | − Incoming(request: Request, keep_alive: Bool, buffer: Bytes, at: Int) |
| 809 | | −} |
| 787 | +type Incoming |
| 788 | + { Incoming(request: Request, keep_alive: Bool, buffer: Bytes, at: Int) } |
| 810 | 789 | |
| 811 | 790 | /// Serve the requests in `buffer` (from offset `at`), then those that |
| 812 | 791 | /// arrive, until either side closes the connection. |
| 813 | 792 | /// A connection's actor: the socket, its watchdog's subject, its own |
| 814 | 793 | /// subject on which the acceptor asks it to close, and how to serve. |
| 815 | | −type Serving { |
| 816 | | − Serving( |
| 817 | | − conn: Resource<TcpStream> |
| 818 | | − // `[(":peer", address)]`, or none if it is not known: put first among |
| 819 | | − // every request's headers (`peer`). |
| 820 | | − , peer: List<(String, String)> |
| 821 | | − , activity: Subject<Activity> |
| 822 | | − , drain: Subject<()> |
| 823 | | − , settings: Settings |
| 824 | | − , handler: fn(Request) -> Response / <io, proc, clock, random, ffi> |
| 825 | | − ) |
| 826 | | −} |
| 794 | +type Serving |
| 795 | + { Serving |
| 796 | + ( conn: Resource<TcpStream> |
| 797 | + // `[(":peer", address)]`, or none if it is not known: put first among |
| 798 | + // every request's headers (`peer`). |
| 799 | + , peer: List<(String, String)> |
| 800 | + , activity: Subject<Activity> |
| 801 | + , drain: Subject<()> |
| 802 | + , settings: Settings |
| 803 | + , handler: fn(Request) -> Response / <io, proc, clock, random, ffi> |
| 804 | + ) |
| 805 | + } |
| 827 | 806 | |
| 828 | 807 | /// Serve requests from `buffer` at `at`, `served` of them so far. The |
| 829 | 808 | /// connection is kept unless the request, the response, the acceptor |
| 830 | 809 | /// (`drain`) or `max_requests` says otherwise. |
| 831 | | −fn serve_requests( |
| 832 | | − serving: Serving |
| 833 | | −, buffer: Bytes |
| 834 | | −, at: Int |
| 835 | | −, served: Int |
| 836 | | −) -> () / <io, proc, clock, random, ffi> { |
| 810 | +fn serve_requests |
| 811 | + (serving: Serving, buffer: Bytes, at: Int, served: Int) |
| 812 | + -> () / <io, proc, clock, random, ffi> { |
| 837 | 813 | let conn = serving.conn |
| 838 | 814 | let activity = serving.activity |
| 839 | 815 | match read_request(conn, activity, serving.settings.max_body, buffer, at) { |
| @@ -847,9 +823,10 @@ fn serve_requests( |
| 847 | 823 | let handler = serving.handler |
| 848 | 824 | let resp = handler(req) |
| 849 | 825 | let served = served + 1 |
| 850 | | − let closes = find_header(resp.headers, "connection") |
| 851 | | − |> option.unwrap_or("") |
| 852 | | − |> has_token("close") |
| 826 | + let closes = |
| 827 | + find_header(resp.headers, "connection") |
| 828 | + |> option.unwrap_or("") |
| 829 | + |> has_token("close") |
| 853 | 830 | let max = serving.settings.max_requests |
| 854 | 831 | let drained = option.is_some(process.receive(serving.drain, 0)) |
| 855 | 832 | let keep = keep_alive && !closes && !drained && (max < 0 || served < max) |
| @@ -874,11 +851,11 @@ fn serve_requests( |
| 874 | 851 | } |
| 875 | 852 | , Error(Closed) -> () |
| 876 | 853 | , Error(Bad(status, message)) -> { |
| 877 | | − let _ = send_response( |
| 878 | | − conn |
| 879 | | − , activity |
| 880 | | − , serialize(text(status, message), False, True) |
| 881 | | − ) |
| 854 | + let _ = |
| 855 | + send_response( conn |
| 856 | + , activity |
| 857 | + , serialize(text(status, message), False, True) |
| 858 | + ) |
| 882 | 859 | () |
| 883 | 860 | } |
| 884 | 861 | } |
| @@ -887,16 +864,16 @@ fn serve_requests( |
| 887 | 864 | /// A streamed response (ADR 0053): the head (chunked, no length), the |
| 888 | 865 | /// stream fn, the final chunk. Between chunks the connection is untimed; |
| 889 | 866 | /// each write is timed per chunk by the watchdog. |
| 890 | | −fn serve_streamed( |
| 891 | | − conn: Resource<TcpStream> |
| 892 | | −, activity: Subject<Activity> |
| 893 | | −, resp: Response |
| 894 | | −, keep: Bool |
| 895 | | −, write: fn(Writer) -> () / <io, proc, clock, random, ffi> |
| 896 | | −) -> Result<(), String> / <io, proc, clock, random, ffi> { |
| 897 | | − use () <- result.try( |
| 898 | | − send_response(conn, activity, serialize_stream_head(resp, keep)) |
| 867 | +fn serve_streamed |
| 868 | + ( conn: Resource<TcpStream> |
| 869 | + , activity: Subject<Activity> |
| 870 | + , resp: Response |
| 871 | + , keep: Bool |
| 872 | + , write: fn(Writer) -> () / <io, proc, clock, random, ffi> |
| 899 | 873 | ) |
| 874 | + -> Result<(), String> / <io, proc, clock, random, ffi> { |
| 875 | + use () <- |
| 876 | + result.try(send_response(conn, activity, serialize_stream_head(resp, keep))) |
| 900 | 877 | process.send(activity, Streaming) |
| 901 | 878 | write(Writer(conn, activity)) |
| 902 | 879 | process.send(activity, Writing) |
| @@ -904,11 +881,9 @@ fn serve_streamed( |
| 904 | 881 | } |
| 905 | 882 | |
| 906 | 883 | /// Write a response (timed by the watchdog). |
| 907 | | −fn send_response( |
| 908 | | − conn: Resource<TcpStream> |
| 909 | | −, activity: Subject<Activity> |
| 910 | | −, data: Bytes |
| 911 | | −) -> Result<(), String> / <io, proc> { |
| 884 | +fn send_response |
| 885 | + (conn: Resource<TcpStream>, activity: Subject<Activity>, data: Bytes) |
| 886 | + -> Result<(), String> / <io, proc> { |
| 912 | 887 | process.send(activity, Writing) |
| 913 | 888 | net.write(conn, data) |
| 914 | 889 | } |
| @@ -918,20 +893,15 @@ fn head_limit() -> Int { |
| 918 | 893 | 65536 |
| 919 | 894 | } |
| 920 | 895 | |
| 921 | | −fn read_request( |
| 922 | | − conn |
| 923 | | −, activity |
| 924 | | −, max_body: Int |
| 925 | | −, buffer: Bytes |
| 926 | | −, at: Int |
| 927 | | −) -> Result<Incoming, Failure> / <io, proc> { |
| 896 | +fn read_request |
| 897 | + (conn, activity, max_body: Int, buffer: Bytes, at: Int) |
| 898 | + -> Result<Incoming, Failure> / <io, proc> { |
| 928 | 899 | match parse_head(buffer, at) { |
| 929 | 900 | Partial -> if bytes.length(buffer) > at { |
| 930 | 901 | // A head has begun: it must arrive whole within `head_timeout`. |
| 931 | 902 | process.send(activity, HeadStarted) |
| 932 | 903 | let begun = bytes.slice(buffer, at, bytes.length(buffer)) |
| 933 | | − read_head( |
| 934 | | − conn |
| 904 | + read_head( conn |
| 935 | 905 | , activity |
| 936 | 906 | , max_body |
| 937 | 907 | , bytes.append(bytes.builder(), begun) |
| @@ -952,30 +922,23 @@ fn read_request( |
| 952 | 922 | } |
| 953 | 923 | |
| 954 | 924 | /// The request whose head, from offset `at` of `buffer`, is `head`. |
| 955 | | −fn request_of( |
| 956 | | − conn |
| 957 | | −, activity |
| 958 | | −, max_body: Int |
| 959 | | −, head: Head |
| 960 | | −, buffer: Bytes |
| 961 | | −, at: Int |
| 962 | | −) -> Result<Incoming, Failure> / <io, proc> { |
| 925 | +fn request_of |
| 926 | + (conn, activity, max_body: Int, head: Head, buffer: Bytes, at: Int) |
| 927 | + -> Result<Incoming, Failure> / <io, proc> { |
| 963 | 928 | match head { |
| 964 | | − Complete( |
| 965 | | − method |
| 966 | | − , path |
| 967 | | − , version |
| 968 | | − , headers |
| 969 | | − , end |
| 970 | | − , length |
| 971 | | − ) -> if end - at > head_limit() { |
| 929 | + Complete |
| 930 | + ( method |
| 931 | + , path |
| 932 | + , version |
| 933 | + , headers |
| 934 | + , end |
| 935 | + , length |
| 936 | + ) -> if end - at > head_limit() { |
| 972 | 937 | Error(Bad(431, "request head too large")) |
| 973 | 938 | } else { |
| 974 | 939 | match read_body(conn, activity, max_body, headers, length, buffer, end) { |
| 975 | 940 | Ok((body, buffer, at)) -> |
| 976 | | − Ok( |
| 977 | | − Incoming( |
| 978 | | − Request(method, path, headers, body) |
| 941 | + Ok( Incoming( Request(method, path, headers, body) |
| 979 | 942 | , keeps_alive(version, headers) |
| 980 | 943 | , buffer |
| 981 | 944 | , at |
| @@ -996,14 +959,9 @@ fn request_of( |
| 996 | 959 | /// `head_timeout`. Each read is scanned once for the head's end, with |
| 997 | 960 | /// the 3 bytes before it; then the head is built and parsed once (ADR |
| 998 | 961 | /// 0012, Revision 2). |
| 999 | | −fn read_head( |
| 1000 | | − conn |
| 1001 | | −, activity |
| 1002 | | −, max_body: Int |
| 1003 | | −, received: Builder |
| 1004 | | −, size: Int |
| 1005 | | −, tail: Bytes |
| 1006 | | −) -> Result<Incoming, Failure> / <io, proc> { |
| 962 | +fn read_head |
| 963 | + (conn, activity, max_body: Int, received: Builder, size: Int, tail: Bytes) |
| 964 | + -> Result<Incoming, Failure> / <io, proc> { |
| 1007 | 965 | if size > head_limit() { |
| 1008 | 966 | Error(Bad(431, "request head too large")) |
| 1009 | 967 | } else { |
| @@ -1015,8 +973,7 @@ fn read_head( |
| 1015 | 973 | let buffer = bytes.build(received) |
| 1016 | 974 | request_of(conn, activity, max_body, parse_head(buffer, 0), buffer, 0) |
| 1017 | 975 | } else { |
| 1018 | | − read_head( |
| 1019 | | − conn |
| 976 | + read_head( conn |
| 1020 | 977 | , activity |
| 1021 | 978 | , max_body |
| 1022 | 979 | , received |
| @@ -1049,8 +1006,7 @@ fn keeps_alive(version: Int, headers: List<(String, String)>) -> Bool { |
| 1049 | 1006 | |
| 1050 | 1007 | /// Whether the comma-separated `value` lists `token` (lowercase). |
| 1051 | 1008 | fn has_token(value: String, token: String) -> Bool { |
| 1052 | | − list.any( |
| 1053 | | − string.split(string.to_lower(value), ",") |
| 1009 | + list.any( string.split(string.to_lower(value), ",") |
| 1054 | 1010 | , fn(t) { string.trim(t) == token } |
| 1055 | 1011 | ) |
| 1056 | 1012 | } |
| @@ -1058,19 +1014,13 @@ fn has_token(value: String, token: String) -> Bool { |
| 1058 | 1014 | /// The body that starts at offset `at` of `buffer`, then where the next |
| 1059 | 1015 | /// request starts. A body over `max_body` bytes (if not negative) is not |
| 1060 | 1016 | /// read: it gets a 413, and the connection is closed. |
| 1061 | | −fn read_body( |
| 1062 | | − conn |
| 1063 | | −, activity |
| 1064 | | −, max_body: Int |
| 1065 | | −, headers |
| 1066 | | −, length: Int |
| 1067 | | −, buffer: Bytes |
| 1068 | | −, at: Int |
| 1069 | | −) -> Result<(Bytes, Bytes, Int), Failure> / <io, proc> { |
| 1017 | +fn read_body |
| 1018 | + (conn, activity, max_body: Int, headers, length: Int, buffer: Bytes, at: Int) |
| 1019 | + -> Result<(Bytes, Bytes, Int), Failure> / <io, proc> { |
| 1070 | 1020 | match find_header(headers, "transfer-encoding") { |
| 1071 | | − Some( |
| 1072 | | − coding |
| 1073 | | − ) -> if length >= 0 { |
| 1021 | + Some |
| 1022 | + ( coding |
| 1023 | + ) -> if length >= 0 { |
| 1074 | 1024 | // Two framings: reading the wrong one would desynchronize the |
| 1075 | 1025 | // connection (request smuggling). |
| 1076 | 1026 | Error(Bad(400, "bad request: both Content-Length and Transfer-Encoding")) |
| @@ -1114,15 +1064,16 @@ fn continue_if_expected(conn, headers, buffer: Bytes, at: Int) -> () / <io> { |
| 1114 | 1064 | |
| 1115 | 1065 | /// The data of the chunks from offset `at` of `buffer` on, appended to |
| 1116 | 1066 | /// `body` (`size` bytes so far); then where the next request starts. |
| 1117 | | −fn read_chunks( |
| 1118 | | − conn |
| 1119 | | −, activity |
| 1120 | | −, max_body: Int |
| 1121 | | −, buffer: Bytes |
| 1122 | | −, at: Int |
| 1123 | | −, body: Builder |
| 1124 | | −, size: Int |
| 1125 | | −) -> Result<(Bytes, Bytes, Int), Failure> / <io, proc> { |
| 1067 | +fn read_chunks |
| 1068 | + ( conn |
| 1069 | + , activity |
| 1070 | + , max_body: Int |
| 1071 | + , buffer: Bytes |
| 1072 | + , at: Int |
| 1073 | + , body: Builder |
| 1074 | + , size: Int |
| 1075 | + ) |
| 1076 | + -> Result<(Bytes, Bytes, Int), Failure> / <io, proc> { |
| 1126 | 1077 | match parse_chunk(buffer, at) { |
| 1127 | 1078 | ChunkData(start, chunk) -> if too_large(size + chunk, max_body) { |
| 1128 | 1079 | Error(too_large_error()) |
| @@ -1130,8 +1081,7 @@ fn read_chunks( |
| 1130 | 1081 | match take(conn, activity, buffer, start, chunk + 2) { |
| 1131 | 1082 | Ok((data, buffer, at)) -> if bytes.slice(data, chunk, chunk + 2) |
| 1132 | 1083 | == bytes.from_string("\r\n") { |
| 1133 | | − read_chunks( |
| 1134 | | − conn |
| 1084 | + read_chunks( conn |
| 1135 | 1085 | , activity |
| 1136 | 1086 | , max_body |
| 1137 | 1087 | , buffer |
| @@ -1162,18 +1112,15 @@ fn read_chunks( |
| 1162 | 1112 | /// `length` bytes from offset `at` of `buffer`, reading more as needed; |
| 1163 | 1113 | /// then where what follows them starts. Reads go into a builder, so a |
| 1164 | 1114 | /// large body is copied a bounded number of times. |
| 1165 | | −fn take(conn, activity, buffer: Bytes, at: Int, length: Int) -> Result< |
| 1166 | | − (Bytes, Bytes, Int) |
| 1167 | | −, Failure |
| 1168 | | −> |
| 1169 | | − / <io, proc> { |
| 1115 | +fn take |
| 1116 | + (conn, activity, buffer: Bytes, at: Int, length: Int) |
| 1117 | + -> Result<(Bytes, Bytes, Int), Failure> / <io, proc> { |
| 1170 | 1118 | let available = bytes.length(buffer) - at |
| 1171 | 1119 | if available >= length { |
| 1172 | 1120 | Ok((bytes.slice(buffer, at, at + length), buffer, at + length)) |
| 1173 | 1121 | } else { |
| 1174 | 1122 | let rest = bytes.slice(buffer, at, bytes.length(buffer)) |
| 1175 | | − take_more( |
| 1176 | | − conn |
| 1123 | + take_more( conn |
| 1177 | 1124 | , activity |
| 1178 | 1125 | , bytes.append(bytes.builder(), rest) |
| 1179 | 1126 | , available |
| @@ -1182,21 +1129,16 @@ fn take(conn, activity, buffer: Bytes, at: Int, length: Int) -> Result< |
| 1182 | 1129 | } |
| 1183 | 1130 | } |
| 1184 | 1131 | |
| 1185 | | −fn take_more( |
| 1186 | | − conn |
| 1187 | | −, activity |
| 1188 | | −, received: Builder |
| 1189 | | −, size: Int |
| 1190 | | −, length: Int |
| 1191 | | −) -> Result<(Bytes, Bytes, Int), Failure> / <io, proc> { |
| 1132 | +fn take_more |
| 1133 | + (conn, activity, received: Builder, size: Int, length: Int) |
| 1134 | + -> Result<(Bytes, Bytes, Int), Failure> / <io, proc> { |
| 1192 | 1135 | if size >= length { |
| 1193 | 1136 | let all = bytes.build(received) |
| 1194 | 1137 | Ok((bytes.slice(all, 0, length), all, length)) |
| 1195 | 1138 | } else { |
| 1196 | 1139 | match read_some(conn, activity) { |
| 1197 | 1140 | Ok(more) -> |
| 1198 | | − take_more( |
| 1199 | | − conn |
| 1141 | + take_more( conn |
| 1200 | 1142 | , activity |
| 1201 | 1143 | , bytes.append(received, more) |
| 1202 | 1144 | , size + bytes.length(more) |
| @@ -1208,11 +1150,9 @@ fn take_more( |
| 1208 | 1150 | } |
| 1209 | 1151 | |
| 1210 | 1152 | /// The unread part of `buffer` (from offset `at`), then what arrives next. |
| 1211 | | −fn receive_more(conn, activity, buffer: Bytes, at: Int) -> Result< |
| 1212 | | − Bytes |
| 1213 | | −, Failure |
| 1214 | | −> |
| 1215 | | − / <io, proc> { |
| 1153 | +fn receive_more |
| 1154 | + (conn, activity, buffer: Bytes, at: Int) |
| 1155 | + -> Result<Bytes, Failure> / <io, proc> { |
| 1216 | 1156 | match read_some(conn, activity) { |
| 1217 | 1157 | Ok(more) -> |
| 1218 | 1158 | Ok(bytes.concat(bytes.slice(buffer, at, bytes.length(buffer)), more)) |
| @@ -1221,11 +1161,9 @@ fn receive_more(conn, activity, buffer: Bytes, at: Int) -> Result< |
| 1221 | 1161 | } |
| 1222 | 1162 | |
| 1223 | 1163 | /// The next bytes from the connection, timed by `idle_timeout` from now. |
| 1224 | | −fn read_some(conn: Resource<TcpStream>, activity: Subject<Activity>) -> Result< |
| 1225 | | − Bytes |
| 1226 | | −, Failure |
| 1227 | | −> |
| 1228 | | − / <io, proc> { |
| 1164 | +fn read_some |
| 1165 | + (conn: Resource<TcpStream>, activity: Subject<Activity>) |
| 1166 | + -> Result<Bytes, Failure> / <io, proc> { |
| 1229 | 1167 | process.send(activity, Reading) |
| 1230 | 1168 | read_raw(conn) |
| 1231 | 1169 | } |
| @@ -1238,19 +1176,19 @@ fn read_raw(conn: Resource<TcpStream>) -> Result<Bytes, Failure> / <io> { |
| 1238 | 1176 | } |
| 1239 | 1177 | } |
| 1240 | 1178 | |
| 1241 | | −type Head { |
| 1242 | | − Partial |
| 1243 | | −, Invalid(reason: String) |
| 1244 | | − /// The head ends at offset `end`; `content_length` is -1 when absent. |
| 1245 | | −, Complete( |
| 1246 | | − method: String |
| 1247 | | − , path: String |
| 1248 | | − , version: Int |
| 1249 | | − , headers: List<(String, String)> |
| 1250 | | − , end: Int |
| 1251 | | − , content_length: Int |
| 1252 | | − ) |
| 1253 | | −} |
| 1179 | +type Head |
| 1180 | + { Partial |
| 1181 | + , Invalid(reason: String) |
| 1182 | + /// The head ends at offset `end`; `content_length` is -1 when absent. |
| 1183 | + , Complete |
| 1184 | + ( method: String |
| 1185 | + , path: String |
| 1186 | + , version: Int |
| 1187 | + , headers: List<(String, String)> |
| 1188 | + , end: Int |
| 1189 | + , content_length: Int |
| 1190 | + ) |
| 1191 | + } |
| 1254 | 1192 | |
| 1255 | 1193 | @extern(c, "tl_http_parse_request") |
| 1256 | 1194 | fn parse_head(buffer: Bytes, at: Int) -> Head |
| @@ -1260,14 +1198,14 @@ fn parse_head(buffer: Bytes, at: Int) -> Head |
| 1260 | 1198 | @extern(c, "tl_http_parse_head_end") |
| 1261 | 1199 | fn parse_head_end(buffer: Bytes, from: Int) -> Int |
| 1262 | 1200 | |
| 1263 | | −type Chunk { |
| 1264 | | − ChunkPartial |
| 1265 | | −, ChunkInvalid(reason: String) |
| 1266 | | − /// `size` bytes of data from offset `start`, then CRLF. |
| 1267 | | −, ChunkData(start: Int, size: Int) |
| 1268 | | − /// The last chunk and its trailers end at offset `end`. |
| 1269 | | −, ChunkEnd(end: Int) |
| 1270 | | −} |
| 1201 | +type Chunk |
| 1202 | + { ChunkPartial |
| 1203 | + , ChunkInvalid(reason: String) |
| 1204 | + /// `size` bytes of data from offset `start`, then CRLF. |
| 1205 | + , ChunkData(start: Int, size: Int) |
| 1206 | + /// The last chunk and its trailers end at offset `end`. |
| 1207 | + , ChunkEnd(end: Int) |
| 1208 | + } |
| 1271 | 1209 | |
| 1272 | 1210 | @extern(c, "tl_http_parse_chunk") |
| 1273 | 1211 | fn parse_chunk(buffer: Bytes, at: Int) -> Chunk |
| @@ -1276,21 +1214,19 @@ fn parse_chunk(buffer: Bytes, at: Int) -> Chunk |
| 1276 | 1214 | /// in one buffer so they leave in one write. The whole-body case. |
| 1277 | 1215 | fn serialize(resp: Response, keep_alive: Bool, with_body: Bool) -> Bytes { |
| 1278 | 1216 | let body = option.unwrap_or(whole_body(resp), bytes.empty()) |
| 1279 | | − let head = head_bytes( |
| 1280 | | − resp.status |
| 1281 | | − , resp.headers |
| 1282 | | − , keep_alive |
| 1283 | | − , "Content-Length: " <> show(bytes.length(body)) |
| 1284 | | − ) |
| 1217 | + let head = |
| 1218 | + head_bytes( resp.status |
| 1219 | + , resp.headers |
| 1220 | + , keep_alive |
| 1221 | + , "Content-Length: " <> show(bytes.length(body)) |
| 1222 | + ) |
| 1285 | 1223 | let out = bytes.append_string(bytes.builder(), head) |
| 1286 | 1224 | bytes.build(if with_body { bytes.append(out, body) } else { out }) |
| 1287 | 1225 | } |
| 1288 | 1226 | |
| 1289 | 1227 | /// A streamed response's head: `Transfer-Encoding: chunked`, no length. |
| 1290 | 1228 | fn serialize_stream_head(resp: Response, keep_alive: Bool) -> Bytes { |
| 1291 | | − bytes.from_string( |
| 1292 | | − head_bytes( |
| 1293 | | − resp.status |
| 1229 | + bytes.from_string( head_bytes( resp.status |
| 1294 | 1230 | , resp.headers |
| 1295 | 1231 | , keep_alive |
| 1296 | 1232 | , "Transfer-Encoding: chunked" |
| @@ -1300,16 +1236,15 @@ fn serialize_stream_head(resp: Response, keep_alive: Bool) -> Bytes { |
| 1300 | 1236 | |
| 1301 | 1237 | /// The status line, the caller's headers (framing filtered), the framing |
| 1302 | 1238 | /// header, and the keep-alive line. |
| 1303 | | −fn head_bytes( |
| 1304 | | − status: Int |
| 1305 | | −, headers: List<(String, String)> |
| 1306 | | −, keep_alive: Bool |
| 1307 | | −, framing: String |
| 1308 | | −) -> String { |
| 1239 | +fn head_bytes |
| 1240 | + ( status: Int |
| 1241 | + , headers: List<(String, String)> |
| 1242 | + , keep_alive: Bool |
| 1243 | + , framing: String |
| 1244 | + ) |
| 1245 | + -> String { |
| 1309 | 1246 | let own = list.filter(headers, fn(h) { !is_framing(string.to_lower(h.0)) }) |
| 1310 | | − string.concat( |
| 1311 | | − [ |
| 1312 | | − "HTTP/1.1 " |
| 1247 | + string.concat( [ "HTTP/1.1 " |
| 1313 | 1248 | , show(status) |
| 1314 | 1249 | , " " |
| 1315 | 1250 | , reason(status) |
| @@ -1433,8 +1368,7 @@ pub fn query(req: Request) -> String { |
| 1433 | 1368 | /// percent-decoded. Decoding happens after splitting, so an encoded slash |
| 1434 | 1369 | /// (`%2F`) stays inside its segment. |
| 1435 | 1370 | pub fn segments(req: Request) -> List<String> { |
| 1436 | | − list.map( |
| 1437 | | − list.filter(string.split(path(req), "/"), fn(s) { s != "" }) |
| 1371 | + list.map( list.filter(string.split(path(req), "/"), fn(s) { s != "" }) |
| 1438 | 1372 | , url_decode |
| 1439 | 1373 | ) |
| 1440 | 1374 | } |