| @@ -61,8 +61,12 @@ import std/string |
| 61 | 61 | import std/time.{type clock} |
| 62 | 62 | |
| 63 | 63 | pub type Request |
| 64 | | −{ Request |
| 65 | | − (method: String, path: String, headers: List<(String, String)>, body: Bytes) |
| 64 | +{ Request( |
| 65 | + method: String |
| 66 | + , path: String |
| 67 | + , headers: List<(String, String)> |
| 68 | + , body: Bytes |
| 69 | + ) |
| 66 | 70 | } |
| 67 | 71 | |
| 68 | 72 | pub type Response |
| @@ -89,14 +93,13 @@ pub opaque type Writer |
| 89 | 93 | |
| 90 | 94 | /// Write one chunk, flushed (one frame, one socket write). `Error` says |
| 91 | 95 | /// the client is gone: stop writing. |
| 92 | | −pub fn write_chunk :: |
| 93 | | − w: Writer |
| 94 | | − chunk: Bytes |
| 95 | | − -> Result<(), String> / <io, proc> { |
| 96 | +pub fn write_chunk :: w: Writer chunk: Bytes -> Result<(), String> / <io, proc> |
| 97 | +{ |
| 96 | 98 | let Writer(conn, activity) = w |
| 97 | 99 | process.send activity Writing |
| 98 | 100 | match net.write conn (frame chunk) |
| 99 | | − { Ok(()) -> { |
| 101 | + { Ok(()) -> |
| 102 | + { |
| 100 | 103 | process.send activity Streaming |
| 101 | 104 | Ok(()) |
| 102 | 105 | } |
| @@ -105,30 +108,44 @@ pub fn write_chunk :: |
| 105 | 108 | } |
| 106 | 109 | |
| 107 | 110 | /// Write one chunk of text, flushed. |
| 108 | | −pub fn write_string :: |
| 109 | | − w: Writer |
| 110 | | − text: String |
| 111 | | − -> Result<(), String> / <io, proc> { |
| 111 | +pub fn write_string :: w: Writer text: String -> Result<(), String> / <io, proc> |
| 112 | +{ |
| 112 | 113 | write_chunk w (bytes.from_string text) |
| 113 | 114 | } |
| 114 | 115 | |
| 115 | 116 | /// A chunk's frame: its length in hex, CRLF, the data, CRLF. |
| 116 | | −fn frame :: chunk: Bytes -> Bytes { |
| 117 | +fn frame :: chunk: Bytes -> Bytes |
| 118 | +{ |
| 117 | 119 | let head = to_hex (bytes.length chunk) <> "\r\n" |
| 118 | 120 | bytes.concat_all [bytes.from_string head, chunk, bytes.from_string "\r\n"] |
| 119 | 121 | } |
| 120 | 122 | |
| 121 | | −fn to_hex :: n: Int -> String { |
| 122 | | − if n <= 0 { "0" } else { hex_up n "" } |
| 123 | +fn to_hex :: n: Int -> String |
| 124 | +{ |
| 125 | + if n <= 0 |
| 126 | + { |
| 127 | + "0" |
| 128 | + } |
| 129 | + else |
| 130 | + { |
| 131 | + hex_up n "" |
| 132 | + } |
| 123 | 133 | } |
| 124 | 134 | |
| 125 | | −fn hex_up :: n: Int acc: String -> String { |
| 126 | | − if n == 0 { acc } else { |
| 135 | +fn hex_up :: n: Int acc: String -> String |
| 136 | +{ |
| 137 | + if n == 0 |
| 138 | + { |
| 139 | + acc |
| 140 | + } |
| 141 | + else |
| 142 | + { |
| 127 | 143 | hex_up (int.shift_right n 4) (hex_digit (int.and n 15) <> acc) |
| 128 | 144 | } |
| 129 | 145 | } |
| 130 | 146 | |
| 131 | | −fn hex_digit :: d: Int -> String { |
| 147 | +fn hex_digit :: d: Int -> String |
| 148 | +{ |
| 132 | 149 | match d |
| 133 | 150 | { 0 -> "0" |
| 134 | 151 | , 1 -> "1" |
| @@ -173,16 +190,16 @@ pub type Settings |
| 173 | 190 | /// - `drain_timeout`: once `serve_until` is told to stop, give the |
| 174 | 191 | /// requests in flight this many milliseconds to finish, then close |
| 175 | 192 | /// their connections (no limit if negative). |
| 176 | | − Settings |
| 177 | | − ( connections: Int |
| 178 | | − , idle_timeout: Int |
| 179 | | − , write_timeout: Int |
| 180 | | − , max_body: Int |
| 181 | | − , max_open: Int |
| 182 | | − , head_timeout: Int |
| 183 | | − , max_requests: Int |
| 184 | | − , drain_timeout: Int |
| 185 | | − ) |
| 193 | + Settings( |
| 194 | + connections: Int |
| 195 | + , idle_timeout: Int |
| 196 | + , write_timeout: Int |
| 197 | + , max_body: Int |
| 198 | + , max_open: Int |
| 199 | + , head_timeout: Int |
| 200 | + , max_requests: Int |
| 201 | + , drain_timeout: Int |
| 202 | + ) |
| 186 | 203 | } |
| 187 | 204 | |
| 188 | 205 | /// Every connection, at most 512 at once (under the usual limit of 1024 |
| @@ -191,7 +208,8 @@ pub type Settings |
| 191 | 208 | /// arrive, when a response takes over 30 seconds to send, or after 1,000 |
| 192 | 209 | /// requests (nginx's `keepalive_requests`); bodies up to 1 MiB; 10 |
| 193 | 210 | /// seconds to drain when stopped. |
| 194 | | −pub fn defaults :: -> Settings { |
| 211 | +pub fn defaults :: -> Settings |
| 212 | +{ |
| 195 | 213 | Settings( connections: -1 |
| 196 | 214 | , idle_timeout: 5000 |
| 197 | 215 | , write_timeout: 30000 |
| @@ -213,22 +231,30 @@ pub fn defaults :: -> Settings { |
| 213 | 231 | /// send: a header name cannot contain `:`. It is found anywhere in the |
| 214 | 232 | /// list (issue #313); a server-built request always has it first. |
| 215 | 233 | /// `wire_headers` leaves it out. |
| 216 | | −pub fn peer :: req: Request -> Option<SocketAddress> { |
| 234 | +pub fn peer :: req: Request -> Option<SocketAddress> |
| 235 | +{ |
| 217 | 236 | option.then (find_header req.headers ":peer") parse_address |
| 218 | 237 | } |
| 219 | 238 | |
| 220 | 239 | /// `req` as if it arrived from `address` (for tests): the `:peer` |
| 221 | 240 | /// pseudo-header first, where the server puts it. |
| 222 | | −pub fn with_peer :: req: Request address: SocketAddress -> Request { |
| 241 | +pub fn with_peer :: req: Request address: SocketAddress -> Request |
| 242 | +{ |
| 223 | 243 | Request(..req, headers: [(":peer", address_text address), ..req.headers]) |
| 224 | 244 | } |
| 225 | 245 | |
| 226 | 246 | /// `host:port`, or `[host]:port` for an IPv6 host. |
| 227 | | −fn parse_address :: value: String -> Option<SocketAddress> { |
| 228 | | − let (host, port) = if string.starts_with value "[" { |
| 247 | +fn parse_address :: value: String -> Option<SocketAddress> |
| 248 | +{ |
| 249 | + let ( host |
| 250 | + , port |
| 251 | + ) = if string.starts_with value "[" |
| 252 | + { |
| 229 | 253 | match string.split (string.slice value 1 (string.length value)) "]:" |
| 230 | 254 | { [host, port] -> (host, port), _ -> ("", "") } |
| 231 | | − } else { |
| 255 | + } |
| 256 | + else |
| 257 | + { |
| 232 | 258 | match string.split value ":" { [host, port] -> (host, port), _ -> ("", "") } |
| 233 | 259 | } |
| 234 | 260 | match string.to_int port |
| @@ -238,16 +264,23 @@ fn parse_address :: value: String -> Option<SocketAddress> { |
| 238 | 264 | } |
| 239 | 265 | |
| 240 | 266 | /// How the server writes a peer in `:peer`. |
| 241 | | −fn address_text :: address: SocketAddress -> String { |
| 242 | | − let host = if string.contains address.host ":" { |
| 267 | +fn address_text :: address: SocketAddress -> String |
| 268 | +{ |
| 269 | + let host = if string.contains address.host ":" |
| 270 | + { |
| 243 | 271 | "[" <> address.host <> "]" |
| 244 | | − } else { address.host } |
| 272 | + } |
| 273 | + else |
| 274 | + { |
| 275 | + address.host |
| 276 | + } |
| 245 | 277 | host <> ":" <> show address.port |
| 246 | 278 | } |
| 247 | 279 | |
| 248 | 280 | /// The headers the client sent, without the server's pseudo-headers |
| 249 | 281 | /// (`:peer`): what to forward or log. |
| 250 | | −pub fn wire_headers :: req: Request -> List<(String, String)> { |
| 282 | +pub fn wire_headers :: req: Request -> List<(String, String)> |
| 283 | +{ |
| 251 | 284 | use h <- list.filter req.headers |
| 252 | 285 | !string.starts_with h.0 ":" |
| 253 | 286 | } |
| @@ -262,23 +295,35 @@ pub fn wire_headers :: req: Request -> List<(String, String)> { |
| 262 | 295 | /// |
| 263 | 296 | /// // Behind one nginx on 10.0.0.2: |
| 264 | 297 | /// http.client_address(req, ["10.0.0.2"]) |
| 265 | | −pub fn client_address :: req: Request trusted: List<String> -> Option<String> { |
| 298 | +pub fn client_address :: req: Request trusted: List<String> -> Option<String> |
| 299 | +{ |
| 266 | 300 | match peer req |
| 267 | 301 | { None -> None |
| 268 | | − , Some(p) -> if !is_trusted trusted p.host { Some(p.host) } else { |
| 302 | + , Some(p) -> if !is_trusted trusted p.host |
| 303 | + { |
| 304 | + Some(p.host) |
| 305 | + } |
| 306 | + else |
| 307 | + { |
| 269 | 308 | Some(untrusted_hop (list.reverse (forwarded_for req)) trusted p.host) |
| 270 | 309 | } |
| 271 | 310 | } |
| 272 | 311 | } |
| 273 | 312 | |
| 274 | 313 | /// Every `X-Forwarded-For` entry, in order, across repeated headers. |
| 275 | | −fn forwarded_for :: req: Request -> List<String> { |
| 314 | +fn forwarded_for :: req: Request -> List<String> |
| 315 | +{ |
| 276 | 316 | use h <- list.flat_map req.headers |
| 277 | | − if string.eq_ignore_ascii_case h.0 "x-forwarded-for" { |
| 317 | + if string.eq_ignore_ascii_case h.0 "x-forwarded-for" |
| 318 | + { |
| 278 | 319 | string.split h.1 "," |
| 279 | 320 | |> list.map string.trim |
| 280 | 321 | |> list.filter fn entry { entry != "" } |
| 281 | | − } else { [] } |
| 322 | + } |
| 323 | + else |
| 324 | + { |
| 325 | + [] |
| 326 | + } |
| 282 | 327 | } |
| 283 | 328 | |
| 284 | 329 | /// The first of `hops` (right-most first) not in `trusted`, else the |
| @@ -287,45 +332,64 @@ fn untrusted_hop :: |
| 287 | 332 | hops: List<String> |
| 288 | 333 | trusted: List<String> |
| 289 | 334 | seen: String |
| 290 | | − -> String { |
| 335 | + -> String |
| 336 | +{ |
| 291 | 337 | match hops |
| 292 | 338 | { [] -> seen |
| 293 | | − , [hop, ..rest] -> if is_trusted trusted hop { |
| 339 | + , [ hop |
| 340 | + , ..rest |
| 341 | + ] -> if is_trusted trusted hop |
| 342 | + { |
| 294 | 343 | untrusted_hop rest trusted hop |
| 295 | | − } else { hop } |
| 344 | + } |
| 345 | + else |
| 346 | + { |
| 347 | + hop |
| 348 | + } |
| 296 | 349 | } |
| 297 | 350 | } |
| 298 | 351 | |
| 299 | | −fn is_trusted :: trusted: List<String> address: String -> Bool { |
| 352 | +fn is_trusted :: trusted: List<String> address: String -> Bool |
| 353 | +{ |
| 300 | 354 | list.any trusted fn t { t == address } |
| 301 | 355 | } |
| 302 | 356 | |
| 303 | 357 | /// The value of header `name` (case-insensitive), if present. |
| 304 | | −pub fn header :: req: Request name: String -> Option<String> { |
| 358 | +pub fn header :: req: Request name: String -> Option<String> |
| 359 | +{ |
| 305 | 360 | find_header req.headers name |
| 306 | 361 | } |
| 307 | 362 | |
| 308 | | −fn find_header :: headers name { |
| 363 | +fn find_header :: headers name |
| 364 | +{ |
| 309 | 365 | match headers |
| 310 | 366 | { [] -> None |
| 311 | | − , [(k, v), ..rest] -> if string.eq_ignore_ascii_case k name { Some(v) } else { |
| 367 | + , [(k, v), ..rest] -> if string.eq_ignore_ascii_case k name |
| 368 | + { |
| 369 | + Some(v) |
| 370 | + } |
| 371 | + else |
| 372 | + { |
| 312 | 373 | find_header rest name |
| 313 | 374 | } |
| 314 | 375 | } |
| 315 | 376 | } |
| 316 | 377 | |
| 317 | 378 | /// The request body as text, if it is valid UTF-8. |
| 318 | | −pub fn body_text :: req: Request -> Result<String, String> { |
| 379 | +pub fn body_text :: req: Request -> Result<String, String> |
| 380 | +{ |
| 319 | 381 | bytes.to_string req.body |
| 320 | 382 | } |
| 321 | 383 | |
| 322 | 384 | /// The fields of a form-encoded request body (see `form`). |
| 323 | | −pub fn form_body :: req: Request -> List<(String, String)> { |
| 385 | +pub fn form_body :: req: Request -> List<(String, String)> |
| 386 | +{ |
| 324 | 387 | form (bytes.to_string_lossy req.body) |
| 325 | 388 | } |
| 326 | 389 | |
| 327 | 390 | /// A response with `body` of type `content_type`. |
| 328 | | −pub fn response :: status: Int content_type: String body: Bytes -> Response { |
| 391 | +pub fn response :: status: Int content_type: String body: Bytes -> Response |
| 392 | +{ |
| 329 | 393 | Response(status, [("Content-Type", content_type)], Whole(body)) |
| 330 | 394 | } |
| 331 | 395 | |
| @@ -343,33 +407,39 @@ pub fn response :: status: Int content_type: String body: Bytes -> Response { |
| 343 | 407 | pub fn stream :: |
| 344 | 408 | content_type: String |
| 345 | 409 | write: fn(Writer) -> () / <io, proc, clock, random, ffi> |
| 346 | | − -> Response { |
| 410 | + -> Response |
| 411 | +{ |
| 347 | 412 | Response(200, [("Content-Type", content_type)], Streamed(write)) |
| 348 | 413 | } |
| 349 | 414 | |
| 350 | 415 | /// The body a response holds, if it is a whole one (a streamed response's |
| 351 | 416 | /// body is written, not held). |
| 352 | | −pub fn whole_body :: resp: Response -> Option<Bytes> { |
| 417 | +pub fn whole_body :: resp: Response -> Option<Bytes> |
| 418 | +{ |
| 353 | 419 | match resp.body { Whole(body) -> Some(body), Streamed(_) -> None } |
| 354 | 420 | } |
| 355 | 421 | |
| 356 | 422 | /// A 200 response with a rendered page. |
| 357 | | −pub fn html :: page: Html -> Response { |
| 423 | +pub fn html :: page: Html -> Response |
| 424 | +{ |
| 358 | 425 | response 200 "text/html; charset=utf-8" (bytes.from_string (html.render page)) |
| 359 | 426 | } |
| 360 | 427 | |
| 361 | 428 | /// A plain-text response. |
| 362 | | −pub fn text :: status: Int body: String -> Response { |
| 429 | +pub fn text :: status: Int body: String -> Response |
| 430 | +{ |
| 363 | 431 | response status "text/plain; charset=utf-8" (bytes.from_string body) |
| 364 | 432 | } |
| 365 | 433 | |
| 366 | | −pub fn not_found :: -> Response { |
| 434 | +pub fn not_found :: -> Response |
| 435 | +{ |
| 367 | 436 | text 404 "not found" |
| 368 | 437 | } |
| 369 | 438 | |
| 370 | 439 | /// `resp` with the header `name: value` added, after the ones it has |
| 371 | 440 | /// (headers can repeat: each `Set-Cookie` is its own header). |
| 372 | | −pub fn with_header :: resp: Response name: String value: String -> Response { |
| 441 | +pub fn with_header :: resp: Response name: String value: String -> Response |
| 442 | +{ |
| 373 | 443 | Response(..resp, headers: list.append resp.headers [(name, value)]) |
| 374 | 444 | } |
| 375 | 445 | |
| @@ -377,7 +447,8 @@ pub fn with_header :: resp: Response name: String value: String -> Response { |
| 377 | 447 | pub fn listen :: |
| 378 | 448 | host: String |
| 379 | 449 | port: Int |
| 380 | | − -> Result<Resource<TcpListener>, String> / <io> { |
| 450 | + -> Result<Resource<TcpListener>, String> / <io> |
| 451 | +{ |
| 381 | 452 | net.listen host port |
| 382 | 453 | } |
| 383 | 454 | |
| @@ -385,7 +456,8 @@ pub fn listen :: |
| 385 | 456 | pub fn serve :: |
| 386 | 457 | server: Resource<TcpListener> |
| 387 | 458 | handler: send fn(Request) -> Response / <io, proc, clock, random, ffi> |
| 388 | | − -> () / <io, proc> { |
| 459 | + -> () / <io, proc> |
| 460 | +{ |
| 389 | 461 | serve_with server (defaults ()) handler |
| 390 | 462 | } |
| 391 | 463 | |
| @@ -395,7 +467,8 @@ pub fn serve_with :: |
| 395 | 467 | server: Resource<TcpListener> |
| 396 | 468 | settings: Settings |
| 397 | 469 | handler: send fn(Request) -> Response / <io, proc, clock, random, ffi> |
| 398 | | − -> () / <io, proc> { |
| 470 | + -> () / <io, proc> |
| 471 | +{ |
| 399 | 472 | accept_all |
| 400 | 473 | server |
| 401 | 474 | settings |
| @@ -421,11 +494,13 @@ pub fn serve_until :: |
| 421 | 494 | settings: Settings |
| 422 | 495 | handler: send fn(Request) -> Response / <io, proc, clock, random, ffi> |
| 423 | 496 | stop: Subject<m> |
| 424 | | − -> () / <io, proc> { |
| 497 | + -> () / <io, proc> |
| 498 | +{ |
| 425 | 499 | let ready = process.subject () |
| 426 | 500 | let (_, done) = |
| 427 | 501 | process.spawn_monitor |
| 428 | | − fn { |
| 502 | + fn |
| 503 | + { |
| 429 | 504 | let events = process.subject () |
| 430 | 505 | process.send ready events |
| 431 | 506 | accept_all |
| @@ -441,20 +516,24 @@ pub fn serve_until :: |
| 441 | 516 | process.new_selector () |
| 442 | 517 | |> process.selecting stop fn _ { True } |
| 443 | 518 | |> process.selecting done fn _ { False } |
| 444 | | − if process.select_forever stopped { |
| 519 | + if process.select_forever stopped |
| 520 | + { |
| 445 | 521 | // Wakes the acceptor, parked in `accept`, which then drains. |
| 446 | 522 | process.send events Stop |
| 447 | 523 | net.close_listener server |
| 448 | 524 | let Down(_, reason) = process.receive_forever done |
| 449 | 525 | stopped_with reason |
| 450 | | − } else { |
| 526 | + } |
| 527 | + else |
| 528 | + { |
| 451 | 529 | // `settings.connections` were served. |
| 452 | 530 | () |
| 453 | 531 | } |
| 454 | 532 | } |
| 455 | 533 | |
| 456 | 534 | /// The acceptor's end: a crash is `serve_until`'s. |
| 457 | | −fn stopped_with :: reason: ExitReason -> () { |
| 535 | +fn stopped_with :: reason: ExitReason -> () |
| 536 | +{ |
| 458 | 537 | match reason |
| 459 | 538 | { Crashed(message) -> panic ("http: the acceptor crashed: " <> message) |
| 460 | 539 | , _ -> () |
| @@ -472,11 +551,14 @@ fn accept_all :: |
| 472 | 551 | events: Subject<Event> |
| 473 | 552 | open: Open |
| 474 | 553 | pause: Int |
| 475 | | − -> () / <io, proc> { |
| 476 | | − if settings.connections != 0 { |
| 554 | + -> () / <io, proc> |
| 555 | +{ |
| 556 | + if settings.connections != 0 |
| 557 | + { |
| 477 | 558 | let next = Settings(..settings, connections: settings.connections - 1) |
| 478 | 559 | match net.accept server |
| 479 | | − { Ok(conn) -> { |
| 560 | + { Ok(conn) -> |
| 561 | + { |
| 480 | 562 | // At `max_open`, this client is held until there is room (or the |
| 481 | 563 | // server stops: it is served, then drained). |
| 482 | 564 | let open = |
| @@ -496,9 +578,15 @@ fn accept_all :: |
| 496 | 578 | Open(..open, count: open.count + 1, next: id + 1) |
| 497 | 579 | 10 |
| 498 | 580 | } |
| 499 | | − , Error(e) -> { |
| 581 | + , Error(e) -> |
| 582 | + { |
| 500 | 583 | let open = take_events events open False |
| 501 | | − if open.stopping { drain events open settings.drain_timeout } else { |
| 584 | + if open.stopping |
| 585 | + { |
| 586 | + drain events open settings.drain_timeout |
| 587 | + } |
| 588 | + else |
| 589 | + { |
| 502 | 590 | io.eprintln ("http: accept failed: " <> e) |
| 503 | 591 | process.sleep pause |
| 504 | 592 | accept_all |
| @@ -511,32 +599,42 @@ fn accept_all :: |
| 511 | 599 | } |
| 512 | 600 | } |
| 513 | 601 | } |
| 514 | | − } else { () } |
| 602 | + } |
| 603 | + else |
| 604 | + { |
| 605 | + () |
| 606 | + } |
| 515 | 607 | } |
| 516 | 608 | |
| 517 | 609 | /// Stop serving (`serve_until`): ask every open connection to close |
| 518 | 610 | /// (idle ones at once, busy ones after their response), and wait until |
| 519 | 611 | /// they have, for `timeout` milliseconds at most; then close the rest. |
| 520 | | −fn drain :: events: Subject<Event> open: Open timeout: Int -> () / <proc> { |
| 612 | +fn drain :: events: Subject<Event> open: Open timeout: Int -> () / <proc> |
| 613 | +{ |
| 521 | 614 | let asked = |
| 522 | 615 | list.map (dict.to_list open.closable) fn entry { ask_to_close entry.1 } |
| 523 | | − let timer = if timeout >= 0 { |
| 616 | + let timer = if timeout >= 0 |
| 617 | + { |
| 524 | 618 | Some( process.spawn |
| 525 | | − fn { |
| 619 | + fn |
| 620 | + { |
| 526 | 621 | process.sleep timeout |
| 527 | 622 | process.send events DrainOver |
| 528 | 623 | } |
| 529 | 624 | ) |
| 530 | | − } else { None } |
| 625 | + } |
| 626 | + else |
| 627 | + { |
| 628 | + None |
| 629 | + } |
| 531 | 630 | wait_drained events open.count asked |
| 532 | 631 | match timer { Some(pid) -> process.kill pid, None -> () } |
| 533 | 632 | } |
| 534 | 633 | |
| 535 | 634 | /// Ask a connection to close: its actor and its watchdog (see `Opened`). |
| 536 | 635 | /// Returns its actor's subject. |
| 537 | | −fn ask_to_close :: |
| 538 | | − subjects: (Subject<()>, Subject<()>) |
| 539 | | − -> Subject<()> / <proc> { |
| 636 | +fn ask_to_close :: subjects: (Subject<()>, Subject<()>) -> Subject<()> / <proc> |
| 637 | +{ |
| 540 | 638 | let (actor, watchdog) = subjects |
| 541 | 639 | process.send actor () |
| 542 | 640 | process.send watchdog () |
| @@ -550,8 +648,10 @@ fn wait_drained :: |
| 550 | 648 | events: Subject<Event> |
| 551 | 649 | count: Int |
| 552 | 650 | asked: List<Subject<()>> |
| 553 | | − -> () / <proc> { |
| 554 | | − if count > 0 { |
| 651 | + -> () / <proc> |
| 652 | +{ |
| 653 | + if count > 0 |
| 654 | + { |
| 555 | 655 | match process.receive_forever events |
| 556 | 656 | { Ended(_) -> wait_drained events (count - 1) asked |
| 557 | 657 | , Opened(_, actor, watchdog) -> |
| @@ -559,7 +659,11 @@ fn wait_drained :: |
| 559 | 659 | , DrainOver -> list.each asked fn actor { process.kill actor.owner } |
| 560 | 660 | , Stop -> wait_drained events count asked |
| 561 | 661 | } |
| 562 | | − } else { () } |
| 662 | + } |
| 663 | + else |
| 664 | + { |
| 665 | + () |
| 666 | + } |
| 563 | 667 | } |
| 564 | 668 | |
| 565 | 669 | /// What a connection's watchdog tells the acceptor. |
| @@ -579,21 +683,18 @@ type Event |
| 579 | 683 | /// to close by id (ids grow, so the least is the oldest), the next id, |
| 580 | 684 | /// and whether `serve_until` was told to stop. |
| 581 | 685 | type Open |
| 582 | | −{ Open |
| 583 | | − ( count: Int |
| 584 | | − , closable: Dict<Int, (Subject<()>, Subject<()>)> |
| 585 | | − , next: Int |
| 586 | | − , stopping: Bool |
| 587 | | − ) |
| 686 | +{ Open( |
| 687 | + count: Int |
| 688 | + , closable: Dict<Int, (Subject<()>, Subject<()>)> |
| 689 | + , next: Int |
| 690 | + , stopping: Bool |
| 691 | + ) |
| 588 | 692 | } |
| 589 | 693 | |
| 590 | 694 | /// `open` after the events received so far; with `wait`, after waiting |
| 591 | 695 | /// for one first. |
| 592 | | −fn take_events :: |
| 593 | | − events: Subject<Event> |
| 594 | | − open: Open |
| 595 | | − wait: Bool |
| 596 | | − -> Open / <proc> { |
| 696 | +fn take_events :: events: Subject<Event> open: Open wait: Bool -> Open / <proc> |
| 697 | +{ |
| 597 | 698 | match process.receive events if wait { -1 } else { 0 } |
| 598 | 699 | { Some(Opened(id, actor, watchdog)) -> |
| 599 | 700 | take_events |
| @@ -623,10 +724,17 @@ fn make_room :: |
| 623 | 724 | open: Open |
| 624 | 725 | max_open: Int |
| 625 | 726 | asked: Bool |
| 626 | | − -> Open / <proc> { |
| 627 | | − if max_open < 0 || open.count < max_open || open.stopping { open } else { |
| 727 | + -> Open / <proc> |
| 728 | +{ |
| 729 | + if max_open < 0 || open.count < max_open || open.stopping |
| 730 | + { |
| 731 | + open |
| 732 | + } |
| 733 | + else |
| 734 | + { |
| 628 | 735 | let (open, asked) = match (asked, dict.first open.closable) |
| 629 | | − { (False, Some((id, (actor, watchdog)))) -> { |
| 736 | + { (False, Some((id, (actor, watchdog)))) -> |
| 737 | + { |
| 630 | 738 | process.send actor () |
| 631 | 739 | process.send watchdog () |
| 632 | 740 | (Open(..open, closable: dict.remove open.closable id), True) |
| @@ -669,7 +777,8 @@ fn handle_connection :: |
| 669 | 777 | settings: Settings |
| 670 | 778 | handler: fn(Request) -> Response / <io, proc, clock, random, ffi> |
| 671 | 779 | events: Subject<Event> |
| 672 | | − -> () / <io, proc, clock, random, ffi> { |
| 780 | + -> () / <io, proc, clock, random, ffi> |
| 781 | +{ |
| 673 | 782 | let me = process.self () |
| 674 | 783 | let ready = process.subject () |
| 675 | 784 | let drain = process.subject () |
| @@ -697,7 +806,8 @@ fn watchdog :: |
| 697 | 806 | settings: Settings |
| 698 | 807 | ready: Subject<Subject<Activity>> |
| 699 | 808 | acceptor: Subject<Event> |
| 700 | | − -> () / <io, proc> { |
| 809 | + -> () / <io, proc> |
| 810 | +{ |
| 701 | 811 | let activity = process.subject () |
| 702 | 812 | let asked = process.subject () |
| 703 | 813 | let down = process.monitor owner |
| @@ -716,7 +826,8 @@ fn watchdog :: |
| 716 | 826 | /// restarts the current timeout; it comes at most once.) Closing an idle |
| 717 | 827 | /// connection races with a request just sent, as the idle timeout does: |
| 718 | 828 | /// a client retries it on a new connection (RFC 9112, section 9.3.1). |
| 719 | | −fn watch :: conn settings: Settings events timeout idle: Bool { |
| 829 | +fn watch :: conn settings: Settings events timeout idle: Bool |
| 830 | +{ |
| 720 | 831 | match process.select events timeout |
| 721 | 832 | { None -> net.close conn |
| 722 | 833 | , Some(Idle) -> watch conn settings events settings.idle_timeout True |
| @@ -725,7 +836,14 @@ fn watch :: conn settings: Settings events timeout idle: Bool { |
| 725 | 836 | , Some(Handling) -> watch conn settings events (-1) False |
| 726 | 837 | , Some(Writing) -> watch conn settings events settings.write_timeout False |
| 727 | 838 | , Some(Streaming) -> watch conn settings events (-1) False |
| 728 | | − , Some(Drain) -> if idle { net.close conn } else { |
| 839 | + , Some |
| 840 | + ( Drain |
| 841 | + ) -> if idle |
| 842 | + { |
| 843 | + net.close conn |
| 844 | + } |
| 845 | + else |
| 846 | + { |
| 729 | 847 | watch conn settings events timeout False |
| 730 | 848 | } |
| 731 | 849 | , Some(Stopped) -> () |
| @@ -750,16 +868,16 @@ type Incoming |
| 750 | 868 | /// A connection's actor: the socket, its watchdog's subject, its own |
| 751 | 869 | /// subject on which the acceptor asks it to close, and how to serve. |
| 752 | 870 | type Serving |
| 753 | | −{ Serving |
| 754 | | − ( conn: Resource<TcpStream> |
| 755 | | − // `[(":peer", address)]`, or none if it is not known: put first among |
| 756 | | − // every request's headers (`peer`). |
| 757 | | − , peer: List<(String, String)> |
| 758 | | − , activity: Subject<Activity> |
| 759 | | − , drain: Subject<()> |
| 760 | | − , settings: Settings |
| 761 | | − , handler: fn(Request) -> Response / <io, proc, clock, random, ffi> |
| 762 | | − ) |
| 871 | +{ Serving( |
| 872 | + conn: Resource<TcpStream> |
| 873 | + // `[(":peer", address)]`, or none if it is not known: put first among |
| 874 | + // every request's headers (`peer`). |
| 875 | + , peer: List<(String, String)> |
| 876 | + , activity: Subject<Activity> |
| 877 | + , drain: Subject<()> |
| 878 | + , settings: Settings |
| 879 | + , handler: fn(Request) -> Response / <io, proc, clock, random, ffi> |
| 880 | + ) |
| 763 | 881 | } |
| 764 | 882 | |
| 765 | 883 | /// Serve requests from `buffer` at `at`, `served` of them so far. The |
| @@ -770,11 +888,13 @@ fn serve_requests :: |
| 770 | 888 | buffer: Bytes |
| 771 | 889 | at: Int |
| 772 | 890 | served: Int |
| 773 | | − -> () / <io, proc, clock, random, ffi> { |
| 891 | + -> () / <io, proc, clock, random, ffi> |
| 892 | +{ |
| 774 | 893 | let conn = serving.conn |
| 775 | 894 | let activity = serving.activity |
| 776 | 895 | match read_request conn activity serving.settings.max_body buffer at |
| 777 | | − { Ok(Incoming(req, keep_alive, buffer, at)) -> { |
| 896 | + { Ok(Incoming(req, keep_alive, buffer, at)) -> |
| 897 | + { |
| 778 | 898 | process.send activity Handling |
| 779 | 899 | let req = Request(..req, headers: list.append serving.peer req.headers) |
| 780 | 900 | // HEAD is GET without the body (RFC 9110 §9.3.2): the handler sees |
| @@ -793,9 +913,14 @@ fn serve_requests :: |
| 793 | 913 | let keep = keep_alive && !closes && !drained && (max < 0 || served < max) |
| 794 | 914 | let sent = match resp.body |
| 795 | 915 | { Whole(_) -> send_response conn activity (serialize resp keep with_body) |
| 796 | | − , Streamed(write) -> if with_body { |
| 916 | + , Streamed |
| 917 | + ( write |
| 918 | + ) -> if with_body |
| 919 | + { |
| 797 | 920 | serve_streamed conn activity resp keep write |
| 798 | | − } else { |
| 921 | + } |
| 922 | + else |
| 923 | + { |
| 799 | 924 | // HEAD: the head only; the stream fn is not called. |
| 800 | 925 | send_response conn activity (serialize_stream_head resp keep) |
| 801 | 926 | } |
| @@ -806,7 +931,8 @@ fn serve_requests :: |
| 806 | 931 | } |
| 807 | 932 | } |
| 808 | 933 | , Error(Closed) -> () |
| 809 | | − , Error(Bad(status, message)) -> { |
| 934 | + , Error(Bad(status, message)) -> |
| 935 | + { |
| 810 | 936 | let _ = |
| 811 | 937 | send_response conn activity (serialize (text status message) False True) |
| 812 | 938 | () |
| @@ -823,7 +949,8 @@ fn serve_streamed :: |
| 823 | 949 | resp: Response |
| 824 | 950 | keep: Bool |
| 825 | 951 | write: fn(Writer) -> () / <io, proc, clock, random, ffi> |
| 826 | | − -> Result<(), String> / <io, proc, clock, random, ffi> { |
| 952 | + -> Result<(), String> / <io, proc, clock, random, ffi> |
| 953 | +{ |
| 827 | 954 | use () <- |
| 828 | 955 | result.try (send_response conn activity (serialize_stream_head resp keep)) |
| 829 | 956 | process.send activity Streaming |
| @@ -837,13 +964,15 @@ fn send_response :: |
| 837 | 964 | conn: Resource<TcpStream> |
| 838 | 965 | activity: Subject<Activity> |
| 839 | 966 | data: Bytes |
| 840 | | − -> Result<(), String> / <io, proc> { |
| 967 | + -> Result<(), String> / <io, proc> |
| 968 | +{ |
| 841 | 969 | process.send activity Writing |
| 842 | 970 | net.write conn data |
| 843 | 971 | } |
| 844 | 972 | |
| 845 | 973 | /// The largest request head, in bytes (431 beyond it). |
| 846 | | −fn head_limit :: -> Int { |
| 974 | +fn head_limit :: -> Int |
| 975 | +{ |
| 847 | 976 | 65536 |
| 848 | 977 | } |
| 849 | 978 | |
| @@ -853,9 +982,11 @@ fn read_request :: |
| 853 | 982 | max_body: Int |
| 854 | 983 | buffer: Bytes |
| 855 | 984 | at: Int |
| 856 | | − -> Result<Incoming, Failure> / <io, proc> { |
| 985 | + -> Result<Incoming, Failure> / <io, proc> |
| 986 | +{ |
| 857 | 987 | match parse_head buffer at |
| 858 | | − { Partial -> if bytes.length buffer > at { |
| 988 | + { Partial -> if bytes.length buffer > at |
| 989 | + { |
| 859 | 990 | // A head has begun: it must arrive whole within `head_timeout`. |
| 860 | 991 | process.send activity HeadStarted |
| 861 | 992 | let begun = bytes.slice buffer at (bytes.length buffer) |
| @@ -866,7 +997,9 @@ fn read_request :: |
| 866 | 997 | (bytes.append (bytes.builder ()) begun) |
| 867 | 998 | (bytes.length begun) |
| 868 | 999 | (last_bytes begun) |
| 869 | | − } else { |
| 1000 | + } |
| 1001 | + else |
| 1002 | + { |
| 870 | 1003 | // Waiting for a request to start: `idle_timeout`, and the acceptor |
| 871 | 1004 | // may close the connection now (`Idle`). |
| 872 | 1005 | process.send activity Idle |
| @@ -887,7 +1020,8 @@ fn request_of :: |
| 887 | 1020 | head: Head |
| 888 | 1021 | buffer: Bytes |
| 889 | 1022 | at: Int |
| 890 | | − -> Result<Incoming, Failure> / <io, proc> { |
| 1023 | + -> Result<Incoming, Failure> / <io, proc> |
| 1024 | +{ |
| 891 | 1025 | match head |
| 892 | 1026 | { Complete |
| 893 | 1027 | ( method |
| @@ -896,9 +1030,12 @@ fn request_of :: |
| 896 | 1030 | , headers |
| 897 | 1031 | , end |
| 898 | 1032 | , length |
| 899 | | − ) -> if end - at > head_limit () { |
| 1033 | + ) -> if end - at > head_limit () |
| 1034 | + { |
| 900 | 1035 | Error(Bad(431, "request head too large")) |
| 901 | | − } else { |
| 1036 | + } |
| 1037 | + else |
| 1038 | + { |
| 902 | 1039 | match read_body conn activity max_body headers length buffer end |
| 903 | 1040 | { Ok((body, buffer, at)) -> |
| 904 | 1041 | Ok( Incoming( Request(method, path, headers, body) |
| @@ -929,16 +1066,26 @@ fn read_head :: |
| 929 | 1066 | received: Builder |
| 930 | 1067 | size: Int |
| 931 | 1068 | tail: Bytes |
| 932 | | − -> Result<Incoming, Failure> / <io, proc> { |
| 933 | | − if size > head_limit () { Error(Bad(431, "request head too large")) } else { |
| 1069 | + -> Result<Incoming, Failure> / <io, proc> |
| 1070 | +{ |
| 1071 | + if size > head_limit () |
| 1072 | + { |
| 1073 | + Error(Bad(431, "request head too large")) |
| 1074 | + } |
| 1075 | + else |
| 1076 | + { |
| 934 | 1077 | match read_raw conn |
| 935 | | − { Ok(more) -> { |
| 1078 | + { Ok(more) -> |
| 1079 | + { |
| 936 | 1080 | let window = bytes.concat tail more |
| 937 | 1081 | let received = bytes.append received more |
| 938 | | − if parse_head_end window 0 >= 0 { |
| 1082 | + if parse_head_end window 0 >= 0 |
| 1083 | + { |
| 939 | 1084 | let buffer = bytes.build received |
| 940 | 1085 | request_of conn activity max_body (parse_head buffer 0) buffer 0 |
| 941 | | − } else { |
| 1086 | + } |
| 1087 | + else |
| 1088 | + { |
| 942 | 1089 | read_head |
| 943 | 1090 | conn |
| 944 | 1091 | activity |
| @@ -955,21 +1102,29 @@ fn read_head :: |
| 955 | 1102 | |
| 956 | 1103 | /// The last 3 bytes of `b` (all of them if fewer): an end of head may |
| 957 | 1104 | /// straddle two reads. |
| 958 | | −fn last_bytes :: b: Bytes -> Bytes { |
| 1105 | +fn last_bytes :: b: Bytes -> Bytes |
| 1106 | +{ |
| 959 | 1107 | let n = bytes.length b |
| 960 | 1108 | bytes.slice b if n > 3 { n - 3 } else { 0 } n |
| 961 | 1109 | } |
| 962 | 1110 | |
| 963 | 1111 | /// Whether the client wants the connection kept open after its request. |
| 964 | | −fn keeps_alive :: version: Int headers: List<(String, String)> -> Bool { |
| 1112 | +fn keeps_alive :: version: Int headers: List<(String, String)> -> Bool |
| 1113 | +{ |
| 965 | 1114 | let connection = option.unwrap_or (find_header headers "connection") "" |
| 966 | | − if version == 0 { has_token connection "keep-alive" } else { |
| 1115 | + if version == 0 |
| 1116 | + { |
| 1117 | + has_token connection "keep-alive" |
| 1118 | + } |
| 1119 | + else |
| 1120 | + { |
| 967 | 1121 | !has_token connection "close" |
| 968 | 1122 | } |
| 969 | 1123 | } |
| 970 | 1124 | |
| 971 | 1125 | /// Whether the comma-separated `value` lists `token` (lowercase). |
| 972 | | −fn has_token :: value: String token: String -> Bool { |
| 1126 | +fn has_token :: value: String token: String -> Bool |
| 1127 | +{ |
| 973 | 1128 | list.any |
| 974 | 1129 | (string.split (string.to_lower value) ",") |
| 975 | 1130 | fn t { string.trim t == token } |
| @@ -986,42 +1141,67 @@ fn read_body :: |
| 986 | 1141 | length: Int |
| 987 | 1142 | buffer: Bytes |
| 988 | 1143 | at: Int |
| 989 | | − -> Result<(Bytes, Bytes, Int), Failure> / <io, proc> { |
| 1144 | + -> Result<(Bytes, Bytes, Int), Failure> / <io, proc> |
| 1145 | +{ |
| 990 | 1146 | match find_header headers "transfer-encoding" |
| 991 | | − { Some(coding) -> if length >= 0 { |
| 1147 | + { Some |
| 1148 | + ( coding |
| 1149 | + ) -> if length >= 0 |
| 1150 | + { |
| 992 | 1151 | // Two framings: reading the wrong one would desynchronize the |
| 993 | 1152 | // connection (request smuggling). |
| 994 | 1153 | Error(Bad(400, "bad request: both Content-Length and Transfer-Encoding")) |
| 995 | | − } else if string.to_lower (string.trim coding) == "chunked" { |
| 1154 | + } |
| 1155 | + else if string.to_lower (string.trim coding) == "chunked" |
| 1156 | + { |
| 996 | 1157 | continue_if_expected conn headers buffer at |
| 997 | 1158 | read_chunks conn activity max_body buffer at (bytes.builder ()) 0 |
| 998 | | − } else { Error(Bad(501, "unsupported Transfer-Encoding: " <> coding)) } |
| 999 | | − , None -> if too_large length max_body { |
| 1159 | + } |
| 1160 | + else |
| 1161 | + { |
| 1162 | + Error(Bad(501, "unsupported Transfer-Encoding: " <> coding)) |
| 1163 | + } |
| 1164 | + , None -> if too_large length max_body |
| 1165 | + { |
| 1000 | 1166 | // Before `100 Continue`: a waiting client does not send the body. |
| 1001 | 1167 | Error(too_large_error ()) |
| 1002 | | − } else if length > 0 { |
| 1168 | + } |
| 1169 | + else if length > 0 |
| 1170 | + { |
| 1003 | 1171 | continue_if_expected conn headers buffer at |
| 1004 | 1172 | take conn activity buffer at length |
| 1005 | | − } else { Ok((bytes.empty (), buffer, at)) } |
| 1173 | + } |
| 1174 | + else |
| 1175 | + { |
| 1176 | + Ok((bytes.empty (), buffer, at)) |
| 1177 | + } |
| 1006 | 1178 | } |
| 1007 | 1179 | } |
| 1008 | 1180 | |
| 1009 | | −fn too_large :: size: Int max_body: Int -> Bool { |
| 1181 | +fn too_large :: size: Int max_body: Int -> Bool |
| 1182 | +{ |
| 1010 | 1183 | max_body >= 0 && size > max_body |
| 1011 | 1184 | } |
| 1012 | 1185 | |
| 1013 | | −fn too_large_error :: -> Failure { |
| 1186 | +fn too_large_error :: -> Failure |
| 1187 | +{ |
| 1014 | 1188 | Bad(413, "request body too large") |
| 1015 | 1189 | } |
| 1016 | 1190 | |
| 1017 | 1191 | /// Tell a client that waits for it (`Expect: 100-continue`) to send the |
| 1018 | 1192 | /// body. |
| 1019 | | −fn continue_if_expected :: conn headers buffer: Bytes at: Int -> () / <io> { |
| 1193 | +fn continue_if_expected :: conn headers buffer: Bytes at: Int -> () / <io> |
| 1194 | +{ |
| 1020 | 1195 | let expect = option.map (find_header headers "expect") string.to_lower |
| 1021 | | − if expect == Some("100-continue") && bytes.length buffer == at { |
| 1196 | + if expect == Some("100-continue") && bytes.length buffer == at |
| 1197 | + { |
| 1022 | 1198 | let _ = net.write_string conn "HTTP/1.1 100 Continue\r\n\r\n" |
| 1023 | 1199 | () |
| 1024 | | − } else { () } |
| 1200 | + } |
| 1201 | + else |
| 1202 | + { |
| 1203 | + () |
| 1204 | + } |
| 1025 | 1205 | } |
| 1026 | 1206 | |
| 1027 | 1207 | /// The data of the chunks from offset `at` of `buffer` on, appended to |
| @@ -1034,14 +1214,19 @@ fn read_chunks :: |
| 1034 | 1214 | at: Int |
| 1035 | 1215 | body: Builder |
| 1036 | 1216 | size: Int |
| 1037 | | − -> Result<(Bytes, Bytes, Int), Failure> / <io, proc> { |
| 1217 | + -> Result<(Bytes, Bytes, Int), Failure> / <io, proc> |
| 1218 | +{ |
| 1038 | 1219 | match parse_chunk buffer at |
| 1039 | | − { ChunkData(start, chunk) -> if too_large (size + chunk) max_body { |
| 1220 | + { ChunkData(start, chunk) -> if too_large (size + chunk) max_body |
| 1221 | + { |
| 1040 | 1222 | Error(too_large_error ()) |
| 1041 | | − } else { |
| 1223 | + } |
| 1224 | + else |
| 1225 | + { |
| 1042 | 1226 | match take conn activity buffer start (chunk + 2) |
| 1043 | 1227 | { Ok((data, buffer, at)) -> if bytes.slice data chunk (chunk + 2) |
| 1044 | | − == bytes.from_string "\r\n" { |
| 1228 | + == bytes.from_string "\r\n" |
| 1229 | + { |
| 1045 | 1230 | read_chunks |
| 1046 | 1231 | conn |
| 1047 | 1232 | activity |
| @@ -1050,15 +1235,22 @@ fn read_chunks :: |
| 1050 | 1235 | at |
| 1051 | 1236 | (bytes.append body (bytes.slice data 0 chunk)) |
| 1052 | 1237 | (size + chunk) |
| 1053 | | − } else { Error(Bad(400, "bad request: no CRLF after a chunk")) } |
| 1238 | + } |
| 1239 | + else |
| 1240 | + { |
| 1241 | + Error(Bad(400, "bad request: no CRLF after a chunk")) |
| 1242 | + } |
| 1054 | 1243 | , Error(e) -> Error(e) |
| 1055 | 1244 | } |
| 1056 | 1245 | } |
| 1057 | 1246 | , ChunkEnd(end) -> Ok((bytes.build body, buffer, end)) |
| 1058 | 1247 | , ChunkInvalid(reason) -> Error(Bad(400, "bad request: " <> reason)) |
| 1059 | | − , ChunkPartial -> if bytes.length buffer - at > 65536 { |
| 1248 | + , ChunkPartial -> if bytes.length buffer - at > 65536 |
| 1249 | + { |
| 1060 | 1250 | Error(Bad(400, "bad request: chunk header too large")) |
| 1061 | | − } else { |
| 1251 | + } |
| 1252 | + else |
| 1253 | + { |
| 1062 | 1254 | match receive_more conn activity buffer at |
| 1063 | 1255 | { Ok(buffer) -> read_chunks conn activity max_body buffer 0 body size |
| 1064 | 1256 | , Error(e) -> Error(e) |
| @@ -1076,11 +1268,15 @@ fn take :: |
| 1076 | 1268 | buffer: Bytes |
| 1077 | 1269 | at: Int |
| 1078 | 1270 | length: Int |
| 1079 | | − -> Result<(Bytes, Bytes, Int), Failure> / <io, proc> { |
| 1271 | + -> Result<(Bytes, Bytes, Int), Failure> / <io, proc> |
| 1272 | +{ |
| 1080 | 1273 | let available = bytes.length buffer - at |
| 1081 | | − if available >= length { |
| 1274 | + if available >= length |
| 1275 | + { |
| 1082 | 1276 | Ok((bytes.slice buffer at (at + length), buffer, at + length)) |
| 1083 | | − } else { |
| 1277 | + } |
| 1278 | + else |
| 1279 | + { |
| 1084 | 1280 | let rest = bytes.slice buffer at (bytes.length buffer) |
| 1085 | 1281 | take_more |
| 1086 | 1282 | conn |
| @@ -1097,11 +1293,15 @@ fn take_more :: |
| 1097 | 1293 | received: Builder |
| 1098 | 1294 | size: Int |
| 1099 | 1295 | length: Int |
| 1100 | | − -> Result<(Bytes, Bytes, Int), Failure> / <io, proc> { |
| 1101 | | − if size >= length { |
| 1296 | + -> Result<(Bytes, Bytes, Int), Failure> / <io, proc> |
| 1297 | +{ |
| 1298 | + if size >= length |
| 1299 | + { |
| 1102 | 1300 | let all = bytes.build received |
| 1103 | 1301 | Ok((bytes.slice all 0 length, all, length)) |
| 1104 | | − } else { |
| 1302 | + } |
| 1303 | + else |
| 1304 | + { |
| 1105 | 1305 | match read_some conn activity |
| 1106 | 1306 | { Ok(more) -> |
| 1107 | 1307 | take_more |
| @@ -1121,7 +1321,8 @@ fn receive_more :: |
| 1121 | 1321 | activity |
| 1122 | 1322 | buffer: Bytes |
| 1123 | 1323 | at: Int |
| 1124 | | − -> Result<Bytes, Failure> / <io, proc> { |
| 1324 | + -> Result<Bytes, Failure> / <io, proc> |
| 1325 | +{ |
| 1125 | 1326 | match read_some conn activity |
| 1126 | 1327 | { Ok(more) -> |
| 1127 | 1328 | Ok(bytes.concat (bytes.slice buffer at (bytes.length buffer)) more) |
| @@ -1133,13 +1334,15 @@ fn receive_more :: |
| 1133 | 1334 | fn read_some :: |
| 1134 | 1335 | conn: Resource<TcpStream> |
| 1135 | 1336 | activity: Subject<Activity> |
| 1136 | | − -> Result<Bytes, Failure> / <io, proc> { |
| 1337 | + -> Result<Bytes, Failure> / <io, proc> |
| 1338 | +{ |
| 1137 | 1339 | process.send activity Reading |
| 1138 | 1340 | read_raw conn |
| 1139 | 1341 | } |
| 1140 | 1342 | |
| 1141 | 1343 | /// The next bytes from the connection, under whatever timeout runs. |
| 1142 | | −fn read_raw :: conn: Resource<TcpStream> -> Result<Bytes, Failure> / <io> { |
| 1344 | +fn read_raw :: conn: Resource<TcpStream> -> Result<Bytes, Failure> / <io> |
| 1345 | +{ |
| 1143 | 1346 | match net.read conn 65536 |
| 1144 | 1347 | { Ok(more) -> if bytes.length more == 0 { Error(Closed) } else { Ok(more) } |
| 1145 | 1348 | , Error(_) -> Error(Closed) |
| @@ -1150,14 +1353,14 @@ type Head |
| 1150 | 1353 | { Partial |
| 1151 | 1354 | , Invalid(reason: String) |
| 1152 | 1355 | /// The head ends at offset `end`; `content_length` is -1 when absent. |
| 1153 | | −, Complete |
| 1154 | | − ( method: String |
| 1155 | | − , path: String |
| 1156 | | − , version: Int |
| 1157 | | − , headers: List<(String, String)> |
| 1158 | | − , end: Int |
| 1159 | | − , content_length: Int |
| 1160 | | − ) |
| 1356 | +, Complete( |
| 1357 | + method: String |
| 1358 | + , path: String |
| 1359 | + , version: Int |
| 1360 | + , headers: List<(String, String)> |
| 1361 | + , end: Int |
| 1362 | + , content_length: Int |
| 1363 | + ) |
| 1161 | 1364 | } |
| 1162 | 1365 | |
| 1163 | 1366 | @extern c "tl_http_parse_request" |
| @@ -1182,7 +1385,8 @@ fn parse_chunk :: buffer: Bytes at: Int -> Chunk |
| 1182 | 1385 | |
| 1183 | 1386 | /// The head and (unless `with_body` is false, for HEAD requests) the body, |
| 1184 | 1387 | /// in one buffer so they leave in one write. The whole-body case. |
| 1185 | | −fn serialize :: resp: Response keep_alive: Bool with_body: Bool -> Bytes { |
| 1388 | +fn serialize :: resp: Response keep_alive: Bool with_body: Bool -> Bytes |
| 1389 | +{ |
| 1186 | 1390 | let body = option.unwrap_or (whole_body resp) (bytes.empty ()) |
| 1187 | 1391 | let head = |
| 1188 | 1392 | head_bytes |
| @@ -1195,7 +1399,8 @@ fn serialize :: resp: Response keep_alive: Bool with_body: Bool -> Bytes { |
| 1195 | 1399 | } |
| 1196 | 1400 | |
| 1197 | 1401 | /// A streamed response's head: `Transfer-Encoding: chunked`, no length. |
| 1198 | | −fn serialize_stream_head :: resp: Response keep_alive: Bool -> Bytes { |
| 1402 | +fn serialize_stream_head :: resp: Response keep_alive: Bool -> Bytes |
| 1403 | +{ |
| 1199 | 1404 | bytes.from_string |
| 1200 | 1405 | ( head_bytes |
| 1201 | 1406 | resp.status |
| @@ -1212,7 +1417,8 @@ fn head_bytes :: |
| 1212 | 1417 | headers: List<(String, String)> |
| 1213 | 1418 | keep_alive: Bool |
| 1214 | 1419 | framing: String |
| 1215 | | − -> String { |
| 1420 | + -> String |
| 1421 | +{ |
| 1216 | 1422 | let own = list.filter headers fn h { !is_framing (string.to_lower h.0) } |
| 1217 | 1423 | string.concat |
| 1218 | 1424 | [ "HTTP/1.1 " |
| @@ -1222,21 +1428,28 @@ fn head_bytes :: |
| 1222 | 1428 | , "\r\n" |
| 1223 | 1429 | , string.concat (list.map own fn h { h.0 <> ": " <> h.1 <> "\r\n" }) |
| 1224 | 1430 | , framing |
| 1225 | | − , if keep_alive { "\r\nConnection: keep-alive\r\n\r\n" } else { |
| 1431 | + , if keep_alive |
| 1432 | + { |
| 1433 | + "\r\nConnection: keep-alive\r\n\r\n" |
| 1434 | + } |
| 1435 | + else |
| 1436 | + { |
| 1226 | 1437 | "\r\nConnection: close\r\n\r\n" |
| 1227 | 1438 | } |
| 1228 | 1439 | ] |
| 1229 | 1440 | } |
| 1230 | 1441 | |
| 1231 | 1442 | /// Headers the server sets itself. |
| 1232 | | −fn is_framing :: name: String -> Bool { |
| 1443 | +fn is_framing :: name: String -> Bool |
| 1444 | +{ |
| 1233 | 1445 | match name |
| 1234 | 1446 | { "content-length" | "connection" | "transfer-encoding" -> True, _ -> False } |
| 1235 | 1447 | } |
| 1236 | 1448 | |
| 1237 | 1449 | /// The status line's reason phrase (RFC 9110 §15 and the registered |
| 1238 | 1450 | /// extensions); `"Status"` for one not in the set. |
| 1239 | | −pub fn reason :: status: Int -> String { |
| 1451 | +pub fn reason :: status: Int -> String |
| 1452 | +{ |
| 1240 | 1453 | match status |
| 1241 | 1454 | { 100 -> "Continue" |
| 1242 | 1455 | , 101 -> "Switching Protocols" |
| @@ -1311,7 +1524,8 @@ pub fn form :: body: String -> List<(String, String)> |
| 1311 | 1524 | /// The request target split at its first `?`: `(path, query)`, the query |
| 1312 | 1525 | /// without the `?` ("" when there is none). Later `?`s stay in the query: |
| 1313 | 1526 | /// `target_split("/a?b?c")` is `("/a", "b?c")`. |
| 1314 | | −pub fn target_split :: target: String -> (String, String) { |
| 1527 | +pub fn target_split :: target: String -> (String, String) |
| 1528 | +{ |
| 1315 | 1529 | match string.split target "?" |
| 1316 | 1530 | { [path] -> (path, "") |
| 1317 | 1531 | , [path, ..rest] -> (path, string.join rest "?") |
| @@ -1320,20 +1534,23 @@ pub fn target_split :: target: String -> (String, String) { |
| 1320 | 1534 | } |
| 1321 | 1535 | |
| 1322 | 1536 | /// The path of the request's target (before any `?`). |
| 1323 | | −pub fn path :: req: Request -> String { |
| 1537 | +pub fn path :: req: Request -> String |
| 1538 | +{ |
| 1324 | 1539 | (target_split req.path).0 |
| 1325 | 1540 | } |
| 1326 | 1541 | |
| 1327 | 1542 | /// The query of the request's target (after the first `?`), "" when it |
| 1328 | 1543 | /// has none. |
| 1329 | | −pub fn query :: req: Request -> String { |
| 1544 | +pub fn query :: req: Request -> String |
| 1545 | +{ |
| 1330 | 1546 | (target_split req.path).1 |
| 1331 | 1547 | } |
| 1332 | 1548 | |
| 1333 | 1549 | /// The path's segments: split on `/` with empty segments dropped, each |
| 1334 | 1550 | /// percent-decoded. Decoding happens after splitting, so an encoded slash |
| 1335 | 1551 | /// (`%2F`) stays inside its segment. |
| 1336 | | −pub fn segments :: req: Request -> List<String> { |
| 1552 | +pub fn segments :: req: Request -> List<String> |
| 1553 | +{ |
| 1337 | 1554 | list.map |
| 1338 | 1555 | (list.filter (string.split (path req) "/") fn s { s != "" }) |
| 1339 | 1556 | url_decode |
| @@ -1341,12 +1558,14 @@ pub fn segments :: req: Request -> List<String> { |
| 1341 | 1558 | |
| 1342 | 1559 | /// The query's parameters: split on `&`, each `k=v` on its first `=`, |
| 1343 | 1560 | /// both sides percent-decoded; a bare `k` is `("k", "")`. |
| 1344 | | −pub fn query_params :: req: Request -> List<(String, String)> { |
| 1561 | +pub fn query_params :: req: Request -> List<(String, String)> |
| 1562 | +{ |
| 1345 | 1563 | form (query req) |
| 1346 | 1564 | } |
| 1347 | 1565 | |
| 1348 | 1566 | /// The value of field `name` in a form, if present. |
| 1349 | | −pub fn field :: fields: List<(String, String)> name: String -> Option<String> { |
| 1567 | +pub fn field :: fields: List<(String, String)> name: String -> Option<String> |
| 1568 | +{ |
| 1350 | 1569 | match fields |
| 1351 | 1570 | { [] -> None |
| 1352 | 1571 | , [(k, v), ..rest] -> if k == name { Some(v) } else { field rest name } |