tobogganSe connecter

louise

The Louise language: compiler, runtime, std, tooling

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

1700 lignes · 50224 octetsBrut · Historique
1/// A small HTTP/1.1 server (native programs only).
2///
3/// pub fn main() -> () / <io, proc, net> {
4/// match http.listen("0.0.0.0", 8080) {
5/// Ok(server) -> http.serve(server, fn(req) {
6/// http.html(<h1>{"Hello from " <> req.path}</h1>)
7/// }),
8/// Error(e) -> io.eprintln(e),
9/// }
10/// }
11///
12/// Each connection is handled by an actor of its own, which serves its
13/// requests one after another (keep-alive; ADR 0012):
14/// - an HTTP/1.1 connection stays open unless the request or the response
15/// has `Connection: close`; an HTTP/1.0 one only if the request has
16/// `Connection: keep-alive`;
17/// - a connection that waits longer than the idle timeout for a request
18/// to start, or for more of a body, is closed; so is one whose request
19/// head, once begun, takes longer than the head timeout to arrive whole
20/// (a client that trickles it), and one whose response takes longer
21/// than the write timeout to send (a client that stops reading); the
22/// handler is not timed;
23/// - at most `max_open` connections are open at once. At the limit, the
24/// next client is accepted and held while the oldest connection is
25/// asked to close: at once if it waits for a request, else after its
26/// current response (`Connection: close`). So kept-alive clients cannot
27/// starve waiting ones (issue #304), and the next client gets in within
28/// about one response time. A streamed response is not cut: its
29/// connection closes when the stream ends;
30/// - a connection serves at most `max_requests` requests: the last one is
31/// answered with `Connection: close`, and the client reconnects;
32/// - request bodies are read whole, sized by `Content-Length` or chunked
33/// (`Transfer-Encoding: chunked`), up to a size limit (413 beyond it),
34/// which can be larger under some path prefixes (`large_bodies`); a
35/// malformed request gets an error response, and the connection is
36/// closed;
37/// - the server sets `Content-Length` and `Connection` on responses, except
38/// that final 204 and 304 responses have no content length or body;
39/// - a response can be **streamed** (ADR 0053): the head goes out with
40/// `Transfer-Encoding: chunked`, the handler's stream fn writes chunks
41/// (each flushed, each write timed per chunk; the stream between chunks
42/// is not timed), and keep-alive continues after the final chunk.
43/// Server-Sent Events are a streamed response that stays open. A response
44/// with final status 204 or 304 sends no framing, chunks or final chunk, and
45/// does not call the stream fn.
46///
47/// Request heads and chunk sizes are parsed by httparse (picohttpparser's
48/// design) in the runtime.
49
50import std/bytes.{type Builder}
51import std/dict.{type Dict}
52import std/ffi.{type Resource}
53import std/file.{type fs}
54import std/html.{type Html}
55import std/int
56import std/io
57import std/list
58import std/net.
59{type SocketAddress, type TcpListener, type TcpStream, type net, SocketAddress}
60import std/option
61import std/process.
62{type ExitReason, type Pid, type Subject, type proc, Crashed, Down}
63import std/random.{type random}
64import std/result
65import std/string
66import std/time.{type clock}
67
68pub type Request
69{ Request(
70 method: String
71 , path: String
72 , headers: List<(String, String)>
73 , body: Bytes
74 )
75}
76
77pub type Response
78{ Response(status: Int, headers: List<(String, String)>, body: Body) }
79
80/// What a response's body is (ADR 0053).
81pub type Body
82{ /// The whole body, in hand.
83 Whole(Bytes)
84 /// Written chunk by chunk: the server sends the head with
85 /// `Transfer-Encoding: chunked`, then calls `write` with a `Writer`
86 /// and sends the final chunk when it returns. Between chunks the
87 /// connection is not timed; each chunk's write is, per chunk, by the
88 /// write timeout. A 204 or 304 response skips `write` and sends neither
89 /// framing nor a final chunk. A HEAD request for any other status
90 /// sends the head only: `write` runs with a writer whose every write fails,
91 /// so it stops, and whatever cleanup it does on a failed write runs (#363).
92 /// If `write` unwinds, the connection dies without the final
93 /// chunk: the client sees a truncated stream, the only honest end
94 /// once the status line is sent.
95, Streamed(write: fn(Writer) -> () / <io, proc, clock, random, fs, net, ffi>)
96}
97
98/// What a streamed response writes with (ADR 0053): the connection,
99/// memory when the body is read in hand (`collect_body`), or nothing, for
100/// a HEAD request, whose writes all fail.
101pub opaque type Writer
102{ Socket(conn: Resource<TcpStream>, activity: Subject<Activity>)
103, Memory(chunks: Subject<Bytes>)
104, Headless
105}
106
107/// Write one chunk, flushed (one frame, one socket write). `Error` says
108/// the client is gone: stop writing. An empty chunk writes nothing: its
109/// frame would be the chunked encoding's last one, ending the body.
110pub fn write_chunk :: w: Writer chunk: Bytes -> Result<(), String> / <io, proc>
111{
112 match w
113 { _ if bytes.length chunk == 0 -> Ok(())
114 , Socket(conn, activity) ->
115 {
116 process.send activity Writing
117 match net.write conn (frame chunk)
118 { Ok(()) ->
119 {
120 process.send activity Streaming
121 Ok(())
122 }
123 , Error(e) -> Error(e)
124 }
125 }
126 , Memory(chunks) ->
127 {
128 process.send chunks chunk
129 Ok(())
130 }
131 , Headless -> Error("a HEAD request has no body")
132 }
133}
134
135/// Write one chunk of text, flushed.
136pub fn write_string :: w: Writer text: String -> Result<(), String> / <io, proc>
137{
138 write_chunk w (bytes.from_string text)
139}
140
141/// A chunk's frame: its length in hex, CRLF, the data, CRLF.
142fn frame :: chunk: Bytes -> Bytes
143{
144 let head = to_hex (bytes.length chunk) <> "\r\n"
145 bytes.concat_all [bytes.from_string head, chunk, bytes.from_string "\r\n"]
146}
147
148fn to_hex :: n: Int -> String
149{
150 if n <= 0
151 {
152 "0"
153 }
154 else
155 {
156 hex_up n ""
157 }
158}
159
160fn hex_up :: n: Int acc: String -> String
161{
162 if n == 0
163 {
164 acc
165 }
166 else
167 {
168 hex_up (int.shift_right n 4) (hex_digit (int.and n 15) <> acc)
169 }
170}
171
172fn hex_digit :: d: Int -> String
173{
174 match d
175 { 0 -> "0"
176 , 1 -> "1"
177 , 2 -> "2"
178 , 3 -> "3"
179 , 4 -> "4"
180 , 5 -> "5"
181 , 6 -> "6"
182 , 7 -> "7"
183 , 8 -> "8"
184 , 9 -> "9"
185 , 10 -> "a"
186 , 11 -> "b"
187 , 12 -> "c"
188 , 13 -> "d"
189 , 14 -> "e"
190 , _ -> "f"
191 }
192}
193
194/// Change the defaults with a record update:
195/// `Settings(..http.defaults(), idle_timeout: 1000)`.
196pub type Settings
197{ /// - `connections`: serve this many connections, then return (all of
198 /// them if negative);
199 /// - `idle_timeout`: close a connection that waits this many
200 /// milliseconds for a request to start, or for more of a body (never
201 /// if negative);
202 /// - `write_timeout`: close a connection whose response takes longer
203 /// than this many milliseconds to send (never if negative);
204 /// - `max_body`: answer 413 to a request whose body is larger than this
205 /// many bytes, and close the connection (no limit if negative);
206 /// - `large_bodies`: other body limits for some paths, as `(prefix,
207 /// limit)`: the first prefix that matches the request's path on whole
208 /// segments (`/uploads` matches `/uploads` and `/uploads/a`, not
209 /// `/uploadsx`) sets its limit instead of `max_body` (see
210 /// `body_limit`). The 413 still comes before any byte of the body is
211 /// read;
212 /// - `max_open`: keep at most this many connections open at once (no
213 /// limit if negative). At the limit, the next client is held (one
214 /// more open socket) and the oldest connection asked to close, at once
215 /// if it waits for a request, else after its current response;
216 /// - `max_requests`: answer this many requests on one connection, the
217 /// last with `Connection: close` (no limit if negative);
218 /// - `head_timeout`: close a connection whose request head, once begun,
219 /// takes longer than this many milliseconds to arrive whole (never if
220 /// negative);
221 /// - `drain_timeout`: once `serve_until` is told to stop, give the
222 /// requests in flight this many milliseconds to finish, then close
223 /// their connections (no limit if negative).
224 Settings(
225 connections: Int
226 , idle_timeout: Int
227 , write_timeout: Int
228 , max_body: Int
229 , large_bodies: List<(String, Int)>
230 , max_open: Int
231 , head_timeout: Int
232 , max_requests: Int
233 , drain_timeout: Int
234 )
235}
236
237/// Every connection, at most 512 at once (under the usual limit of 1024
238/// open files, so the limit is reached before `accept` fails); closed
239/// after 5 idle seconds, when a request head takes over 10 seconds to
240/// arrive, when a response takes over 30 seconds to send, or after 1,000
241/// requests (nginx's `keepalive_requests`); bodies up to 1 MiB on every
242/// path; 10 seconds to drain when stopped.
243pub fn defaults :: -> Settings
244{
245 Settings( connections: -1
246 , idle_timeout: 5000
247 , write_timeout: 30000
248 , max_body: 1048576
249 , large_bodies: []
250 , max_open: 512
251 , head_timeout: 10000
252 , max_requests: 1000
253 , drain_timeout: 10000
254 )
255}
256
257/// The client's address (issue #308): the other end of the connection the
258/// request came on, as the server accepted it. Behind a proxy, that is
259/// the proxy: see `client_address`. `None` for a request built by hand
260/// (in tests).
261///
262/// The server puts it among the request's headers as the pseudo-header
263/// `:peer` (`"127.0.0.1:52144"`, `"[::1]:52144"`), which no client can
264/// send: a header name cannot contain `:`. It is found anywhere in the
265/// list (issue #313); a server-built request always has it first.
266/// `wire_headers` leaves it out.
267pub fn peer :: req: Request -> Option<SocketAddress>
268{
269 option.then (find_header req.headers ":peer") parse_address
270}
271
272/// `req` as if it arrived from `address` (for tests): the `:peer`
273/// pseudo-header first, where the server puts it.
274pub fn with_peer :: req: Request address: SocketAddress -> Request
275{
276 Request(..req, headers: [(":peer", address_text address), ..req.headers])
277}
278
279/// `host:port`, or `[host]:port` for an IPv6 host.
280fn parse_address :: value: String -> Option<SocketAddress>
281{
282 let ( host
283 , port
284 ) = if string.starts_with value "["
285 {
286 match string.split (string.slice value 1 (string.length value)) "]:"
287 { [host, port] -> (host, port), _ -> ("", "") }
288 }
289 else
290 {
291 match string.split value ":" { [host, port] -> (host, port), _ -> ("", "") }
292 }
293 match string.to_int port
294 { Ok(port) -> if host == "" { None } else { Some(SocketAddress(host, port)) }
295 , Error(_) -> None
296 }
297}
298
299/// How the server writes a peer in `:peer`.
300fn address_text :: address: SocketAddress -> String
301{
302 let host = if string.contains address.host ":"
303 {
304 "[" <> address.host <> "]"
305 }
306 else
307 {
308 address.host
309 }
310 host <> ":" <> show address.port
311}
312
313/// The headers the client sent, without the server's pseudo-headers
314/// (`:peer`): what to forward or log.
315pub fn wire_headers :: req: Request -> List<(String, String)>
316{
317 use h <- list.filter req.headers
318 !string.starts_with h.0 ":"
319}
320
321/// The client's IP address, through the proxies in `trusted` (exact
322/// addresses, as text). When the peer is not trusted, it is the client:
323/// its `X-Forwarded-For`, which anyone can send, is ignored. When it is,
324/// the client is the right-most `X-Forwarded-For` entry that is not
325/// trusted (each proxy appends the address it got the request from), or
326/// the left-most one if all are; with no entries, the peer itself.
327/// `None` without a peer (a request built by hand).
328///
329/// // Behind one nginx on 10.0.0.2:
330/// http.client_address(req, ["10.0.0.2"])
331pub fn client_address :: req: Request trusted: List<String> -> Option<String>
332{
333 match peer req
334 { None -> None
335 , Some(p) -> if !is_trusted trusted p.host
336 {
337 Some(p.host)
338 }
339 else
340 {
341 Some(untrusted_hop (list.reverse (forwarded_for req)) trusted p.host)
342 }
343 }
344}
345
346/// Every `X-Forwarded-For` entry, in order, across repeated headers.
347fn forwarded_for :: req: Request -> List<String>
348{
349 use h <- list.flat_map req.headers
350 if string.eq_ignore_ascii_case h.0 "x-forwarded-for"
351 {
352 string.split h.1 ","
353 |> list.map string.trim
354 |> list.filter fn entry { entry != "" }
355 }
356 else
357 {
358 []
359 }
360}
361
362/// The first of `hops` (right-most first) not in `trusted`, else the
363/// last one seen (`seen`: the peer, before any).
364fn untrusted_hop ::
365 hops: List<String>
366 trusted: List<String>
367 seen: String
368 -> String
369{
370 match hops
371 { [] -> seen
372 , [ hop
373 , ..rest
374 ] -> if is_trusted trusted hop
375 {
376 untrusted_hop rest trusted hop
377 }
378 else
379 {
380 hop
381 }
382 }
383}
384
385fn is_trusted :: trusted: List<String> address: String -> Bool
386{
387 list.any trusted fn t { t == address }
388}
389
390/// The value of header `name` (case-insensitive), if present.
391pub fn header :: req: Request name: String -> Option<String>
392{
393 find_header req.headers name
394}
395
396fn find_header :: headers name
397{
398 match headers
399 { [] -> None
400 , [(k, v), ..rest] -> if string.eq_ignore_ascii_case k name
401 {
402 Some(v)
403 }
404 else
405 {
406 find_header rest name
407 }
408 }
409}
410
411/// The request body as text, if it is valid UTF-8.
412pub fn body_text :: req: Request -> Result<String, String>
413{
414 bytes.to_string req.body
415}
416
417/// The fields of a form-encoded request body (see `form`).
418pub fn form_body :: req: Request -> List<(String, String)>
419{
420 form (bytes.to_string_lossy req.body)
421}
422
423/// A response with `body` of type `content_type`.
424pub fn response :: status: Int content_type: String body: Bytes -> Response
425{
426 Response(status, [("Content-Type", content_type)], Whole(body))
427}
428
429/// A 200 response written chunk by chunk (ADR 0053): the head goes out
430/// first (`Transfer-Encoding: chunked`), then `write` runs with a
431/// `Writer`; each `write_chunk`/`write_string` flushes. Between chunks
432/// the connection is not timed; each write is, per chunk. Keep-alive
433/// continues after the final chunk.
434///
435/// http.stream("text/html; charset=utf-8", fn(w) {
436/// let _ = http.write_string(w, html.render(shell_top))
437/// // each component, as it finishes
438/// let _ = http.write_string(w, "</body></html>")
439/// })
440pub fn stream ::
441 content_type: String
442 write: fn(Writer) -> () / <io, proc, clock, random, fs, net, ffi>
443 -> Response
444{
445 Response(200, [("Content-Type", content_type)], Streamed(write))
446}
447
448/// The body a response holds, if it is a whole one (a streamed response's
449/// body is written, not held).
450pub fn whole_body :: resp: Response -> Option<Bytes>
451{
452 match resp.body { Whole(body) -> Some(body), Streamed(_) -> None }
453}
454
455/// A response's body in hand: a streamed one is written into memory,
456/// chunk by chunk, as the server would send it. For tests and tools: the
457/// whole body is held at once.
458pub fn collect_body ::
459 resp: Response
460 -> Bytes / <io, proc, clock, random, fs, net, ffi>
461{
462 match resp.body
463 { Whole(body) -> body
464 , Streamed(write) ->
465 {
466 let chunks = process.subject ()
467 write Memory(chunks)
468 bytes.concat_all (written chunks [])
469 }
470 }
471}
472
473/// The chunks a memory writer was given, in order.
474fn written :: chunks: Subject<Bytes> held: List<Bytes> -> List<Bytes> / <proc>
475{
476 match process.receive chunks 0
477 { Some(chunk) -> written chunks [chunk, ..held], None -> list.reverse held }
478}
479
480/// A 200 response with a rendered page.
481pub fn html :: page: Html -> Response
482{
483 response 200 "text/html; charset=utf-8" (bytes.from_string (html.render page))
484}
485
486/// A plain-text response.
487pub fn text :: status: Int body: String -> Response
488{
489 response status "text/plain; charset=utf-8" (bytes.from_string body)
490}
491
492pub fn not_found :: -> Response
493{
494 text 404 "not found"
495}
496
497/// `resp` with the header `name: value` added, after the ones it has
498/// (headers can repeat: each `Set-Cookie` is its own header).
499pub fn with_header :: resp: Response name: String value: String -> Response
500{
501 Response(..resp, headers: list.append resp.headers [(name, value)])
502}
503
504/// Listen for HTTP connections. Opening the listener needs `net`; the returned
505/// listener then carries its own authority for the serving functions below.
506pub fn listen ::
507 host: String
508 port: Int
509 -> Result<Resource<TcpListener>, String> / <net>
510{
511 net.listen host port
512}
513
514/// Serve forever with the default settings. This accepts on the listener value
515/// already handed to it and retains `/ <io, proc>`: it does not demand unrelated
516/// resolver/opening authority.
517pub fn serve ::
518 server: Resource<TcpListener>
519 handler: send fn(Request)
520 -> Response / <io, proc, clock, random, fs, net, ffi>
521 -> () / <io, proc>
522{
523 serve_with server (defaults ()) handler
524}
525
526/// Serve with `settings`: one actor per connection. When this returns,
527/// the last connections' actors may still be running.
528pub fn serve_with ::
529 server: Resource<TcpListener>
530 settings: Settings
531 handler: send fn(Request)
532 -> Response / <io, proc, clock, random, fs, net, ffi>
533 -> () / <io, proc>
534{
535 accept_all
536 server
537 settings
538 handler
539 (process.subject ())
540 Open(0, dict.empty (), 0, False)
541 10
542}
543
544/// Serve with `settings` until a message arrives on `stop`, then stop
545/// gracefully (ADR 0060): new clients are refused at once; idle
546/// connections are closed, and busy ones after their current response
547/// (with `Connection: close`); after `settings.drain_timeout`, the
548/// connections still open are closed. Returns once every connection is.
549///
550/// `stop` is one of the calling actor's subjects, and any message on it
551/// stops the server: `os.signals([Term])` for a program that stops on
552/// SIGTERM, `process.exits()` for a `main` that traps exits under
553/// `[application]` (ADR 0060), or a `process.subject()` another actor
554/// sends on.
555pub fn serve_until ::
556 server: Resource<TcpListener>
557 settings: Settings
558 handler: send fn(Request)
559 -> Response / <io, proc, clock, random, fs, net, ffi>
560 stop: Subject<m>
561 -> () / <io, proc>
562{
563 let ready = process.subject ()
564 let (_, done) =
565 process.spawn_monitor
566 fn
567 {
568 let events = process.subject ()
569 process.send ready events
570 accept_all
571 server
572 settings
573 handler
574 events
575 Open(0, dict.empty (), 0, False)
576 10
577 }
578 let events = process.receive_forever ready
579 let stopped =
580 process.new_selector ()
581 |> process.selecting stop fn _ { True }
582 |> process.selecting done fn _ { False }
583 if process.select_forever stopped
584 {
585 // Wakes the acceptor, parked in `accept`, which then drains.
586 process.send events Stop
587 net.close_listener server
588 let Down(_, reason) = process.receive_forever done
589 stopped_with reason
590 }
591 else
592 {
593 // `settings.connections` were served.
594 ()
595 }
596}
597
598/// The acceptor's end: a crash is `serve_until`'s.
599fn stopped_with :: reason: ExitReason -> ()
600{
601 match reason
602 { Crashed(message) -> panic ("http: the acceptor crashed: " <> message)
603 , _ -> ()
604 }
605}
606
607/// Accept connections while `settings.connections` lasts. Their
608/// watchdogs report on `events` when they open and end. After a failed
609/// `accept` (out of file descriptors, say), wait `pause` milliseconds,
610/// doubling up to a second while it keeps failing (ADR 0012, Revision 2).
611fn accept_all ::
612 server
613 settings: Settings
614 handler
615 events: Subject<Event>
616 open: Open
617 pause: Int
618 -> () / <io, proc>
619{
620 if settings.connections != 0
621 {
622 let next = Settings(..settings, connections: settings.connections - 1)
623 match net.accept server
624 { Ok(conn) ->
625 {
626 // At `max_open`, this client is held until there is room (or the
627 // server stops: it is served, then drained).
628 let open =
629 make_room
630 events
631 (take_events events open False)
632 settings.max_open
633 False
634 let id = open.next
635 let _ =
636 process.spawn fn { handle_connection conn id settings handler events }
637 accept_all
638 server
639 next
640 handler
641 events
642 Open(..open, count: open.count + 1, next: id + 1)
643 10
644 }
645 , Error(e) ->
646 {
647 let open = take_events events open False
648 if open.stopping
649 {
650 drain events open settings.drain_timeout
651 }
652 else
653 {
654 io.eprintln ("http: accept failed: " <> e)
655 process.sleep pause
656 accept_all
657 server
658 next
659 handler
660 events
661 open
662 if pause >= 500 { 1000 } else { pause * 2 }
663 }
664 }
665 }
666 }
667 else
668 {
669 ()
670 }
671}
672
673/// Stop serving (`serve_until`): ask every open connection to close
674/// (idle ones at once, busy ones after their response), and wait until
675/// they have, for `timeout` milliseconds at most; then close the rest.
676fn drain :: events: Subject<Event> open: Open timeout: Int -> () / <proc>
677{
678 let asked =
679 list.map (dict.to_list open.closable) fn entry { ask_to_close entry.1 }
680 let timer = if timeout >= 0
681 {
682 Some( process.spawn
683 fn
684 {
685 process.sleep timeout
686 process.send events DrainOver
687 }
688 )
689 }
690 else
691 {
692 None
693 }
694 wait_drained events open.count asked
695 match timer { Some(pid) -> process.kill pid, None -> () }
696}
697
698/// Ask a connection to close: its actor and its watchdog (see `Opened`).
699/// Returns its actor's subject.
700fn ask_to_close :: subjects: (Subject<()>, Subject<()>) -> Subject<()> / <proc>
701{
702 let (actor, watchdog) = subjects
703 process.send actor ()
704 process.send watchdog ()
705 actor
706}
707
708/// Wait until `count` connections have ended, asking those that open
709/// meanwhile (accepted just before the stop) to close too; at
710/// `DrainOver`, stop the actors of those asked.
711fn wait_drained ::
712 events: Subject<Event>
713 count: Int
714 asked: List<Subject<()>>
715 -> () / <proc>
716{
717 if count > 0
718 {
719 match process.receive_forever events
720 { Ended(_) -> wait_drained events (count - 1) asked
721 , Opened(_, actor, watchdog) ->
722 wait_drained events count [ask_to_close (actor, watchdog), ..asked]
723 , DrainOver ->
724 list.each asked fn actor { process.kill (process.owner actor) }
725 , Stop -> wait_drained events count asked
726 }
727 }
728 else
729 {
730 ()
731 }
732}
733
734/// What a connection's watchdog tells the acceptor.
735type Event
736{ /// Connection `id` is open: a `()` on both subjects (its actor's, its
737 /// watchdog's) asks it to close.
738 Opened(id: Int, actor: Subject<()>, watchdog: Subject<()>)
739 /// Connection `id` ended.
740, Ended(id: Int)
741 /// `serve_until` was told to stop: drain once `accept` fails.
742, Stop
743 /// The drain timeout passed.
744, DrainOver
745}
746
747/// The acceptor's view: `count` connections open, those it may still ask
748/// to close by id (ids grow, so the least is the oldest), the next id,
749/// and whether `serve_until` was told to stop.
750type Open
751{ Open(
752 count: Int
753 , closable: Dict<Int, (Subject<()>, Subject<()>)>
754 , next: Int
755 , stopping: Bool
756 )
757}
758
759/// `open` after the events received so far; with `wait`, after waiting
760/// for one first.
761fn take_events :: events: Subject<Event> open: Open wait: Bool -> Open / <proc>
762{
763 match process.receive events if wait { -1 } else { 0 }
764 { Some(Opened(id, actor, watchdog)) ->
765 take_events
766 events
767 Open(..open, closable: dict.insert open.closable id (actor, watchdog))
768 False
769 , Some(Ended(id)) ->
770 take_events
771 events
772 Open( ..open
773 , count: open.count - 1
774 , closable: dict.remove open.closable id
775 )
776 False
777 , Some(Stop) -> take_events events Open(..open, stopping: True) False
778 , Some(DrainOver) -> take_events events open False
779 , None -> open
780 }
781}
782
783/// `open` once a connection more fits under `max_open`. While full, the
784/// oldest connection not asked yet is asked to close (once per held
785/// client, `asked`), and the acceptor waits for one to end: that one, or
786/// any other.
787fn make_room ::
788 events: Subject<Event>
789 open: Open
790 max_open: Int
791 asked: Bool
792 -> Open / <proc>
793{
794 if max_open < 0 || open.count < max_open || open.stopping
795 {
796 open
797 }
798 else
799 {
800 let (open, asked) = match (asked, dict.first open.closable)
801 { (False, Some((id, (actor, watchdog)))) ->
802 {
803 process.send actor ()
804 process.send watchdog ()
805 (Open(..open, closable: dict.remove open.closable id), True)
806 }
807 , _ -> (open, asked)
808 }
809 make_room events (take_events events open True) max_open asked
810 }
811}
812
813/// What a connection's watchdog learns.
814type Activity
815{ /// The connection's actor waits for a request to start: timed by
816 /// `idle_timeout`. Asked to close now, the connection closes at once.
817 Idle
818 /// It waits for more of a body: timed by `idle_timeout` from each read.
819, Reading
820 /// A request head has begun and is not whole yet: timed by
821 /// `head_timeout` from here, once, since nothing else is sent until the
822 /// head is read.
823, HeadStarted
824 /// It runs the handler: not timed.
825, Handling
826 /// It sends a response: timed.
827, Writing
828 /// A streamed response, between chunks: not timed (a stream may be
829 /// quiet arbitrarily long); each chunk's write is `Writing`.
830, Streaming
831 /// It stopped (also by crashing).
832, Stopped
833 /// The acceptor asks the connection to close (at `max_open`, for a
834 /// waiting client): the watchdog closes it if it is `Idle`; otherwise
835 /// its actor answers the current request with `Connection: close`.
836, Drain
837}
838
839fn handle_connection ::
840 conn: Resource<TcpStream>
841 id: Int
842 settings: Settings
843 handler: fn(Request) -> Response / <io, proc, clock, random, fs, net, ffi>
844 events: Subject<Event>
845 -> () / <io, proc, clock, random, fs, net, ffi>
846{
847 let me = process.self ()
848 let ready = process.subject ()
849 let drain = process.subject ()
850 let _ = process.spawn fn { watchdog conn me id drain settings ready events }
851 let activity = process.receive_forever ready
852 let peer = match net.peer conn
853 { Ok(address) -> [(":peer", address_text address)], Error(_) -> [] }
854 let serving = Serving(conn, peer, activity, drain, settings, handler)
855 serve_requests serving (bytes.empty ()) 0 0
856 net.close conn
857}
858
859/// Close the connection when it waits too long for a request, takes too
860/// long to send its head, or takes too long to send a response: its actor
861/// then reads the end of the stream, or fails to write, and stops. The
862/// watchdog monitors that actor, and stops with it, releasing its
863/// reference to the socket. It tells the acceptor that the connection
864/// opened, with how to ask it to close (`drain`, its actor's subject, and
865/// one of its own), and, once, that it ended (for `max_open`).
866fn watchdog ::
867 conn: Resource<TcpStream>
868 owner: Pid
869 id: Int
870 drain: Subject<()>
871 settings: Settings
872 ready: Subject<Subject<Activity>>
873 acceptor: Subject<Event>
874 -> () / <io, proc>
875{
876 let activity = process.subject ()
877 let asked = process.subject ()
878 let down = process.monitor owner
879 process.send ready activity
880 process.send acceptor Opened(id, drain, asked)
881 let events =
882 process.new_selector ()
883 |> process.selecting activity fn a { a }
884 |> process.selecting down fn _ { Stopped }
885 |> process.selecting asked fn _ { Drain }
886 watch conn settings events settings.idle_timeout True
887 process.send acceptor Ended(id)
888}
889
890/// `idle`: the actor waits for a request to start. (A `Drain` otherwise
891/// restarts the current timeout; it comes at most once.) Closing an idle
892/// connection races with a request just sent, as the idle timeout does:
893/// a client retries it on a new connection (RFC 9112, section 9.3.1).
894fn watch :: conn settings: Settings events timeout idle: Bool
895{
896 match process.select events timeout
897 { None -> net.close conn
898 , Some(Idle) -> watch conn settings events settings.idle_timeout True
899 , Some(Reading) -> watch conn settings events settings.idle_timeout False
900 , Some(HeadStarted) -> watch conn settings events settings.head_timeout False
901 , Some(Handling) -> watch conn settings events (-1) False
902 , Some(Writing) -> watch conn settings events settings.write_timeout False
903 , Some(Streaming) -> watch conn settings events (-1) False
904 , Some
905 ( Drain
906 ) -> if idle
907 {
908 net.close conn
909 }
910 else
911 {
912 watch conn settings events timeout False
913 }
914 , Some(Stopped) -> ()
915 }
916}
917
918/// Why no request was read.
919type Failure
920{ /// The connection closed or failed: nothing to answer.
921 Closed
922 /// A malformed request, answered with `status` before closing.
923, Bad(status: Int, message: String)
924}
925
926/// A request, whether the client keeps the connection open after it, and
927/// where the next request starts (offset `at` of `buffer`).
928type Incoming
929{ Incoming(request: Request, keep_alive: Bool, buffer: Bytes, at: Int) }
930
931/// Serve the requests in `buffer` (from offset `at`), then those that
932/// arrive, until either side closes the connection.
933/// A connection's actor: the socket, its watchdog's subject, its own
934/// subject on which the acceptor asks it to close, and how to serve.
935type Serving
936{ Serving(
937 conn: Resource<TcpStream>
938 // `[(":peer", address)]`, or none if it is not known: put first among
939 // every request's headers (`peer`).
940 , peer: List<(String, String)>
941 , activity: Subject<Activity>
942 , drain: Subject<()>
943 , settings: Settings
944 , handler: fn(Request) -> Response / <io, proc, clock, random, fs, net, ffi>
945 )
946}
947
948/// Serve requests from `buffer` at `at`, `served` of them so far. The
949/// connection is kept unless the request, the response, the acceptor
950/// (`drain`) or `max_requests` says otherwise.
951fn serve_requests ::
952 serving: Serving
953 buffer: Bytes
954 at: Int
955 served: Int
956 -> () / <io, proc, clock, random, fs, net, ffi>
957{
958 let conn = serving.conn
959 let activity = serving.activity
960 match read_request conn activity serving.settings buffer at
961 { Ok(Incoming(req, keep_alive, buffer, at)) ->
962 {
963 process.send activity Handling
964 let req = Request(..req, headers: list.append serving.peer req.headers)
965 // HEAD is GET without the body (RFC 9110 §9.3.2): the handler sees
966 // a GET, and the body is not sent (a stream fn is not called).
967 let with_body = req.method != "HEAD"
968 let req = if with_body { req } else { Request(..req, method: "GET") }
969 let handler = serving.handler
970 let resp = handler req
971 let served = served + 1
972 let closes =
973 find_header resp.headers "connection"
974 |> option.unwrap_or ""
975 |> has_token "close"
976 let max = serving.settings.max_requests
977 let drained = option.is_some (process.receive serving.drain 0)
978 let keep = keep_alive && !closes && !drained && (max < 0 || served < max)
979 let sent = if final_status_without_body resp.status
980 {
981 send_response
982 conn
983 activity
984 (bytes.from_string (head_bytes resp.status resp.headers keep ""))
985 }
986 else
987 {
988 match resp.body
989 { Whole(_) ->
990 send_response conn activity (serialize resp keep with_body)
991 , Streamed
992 ( write
993 ) -> if with_body
994 {
995 serve_streamed conn activity resp keep write
996 }
997 else
998 {
999 // HEAD: the head only. The stream fn still runs, and its first
1000 // write fails, so what it started is stopped (#363).
1001 let head =
1002 send_response conn activity (serialize_stream_head resp keep)
1003 write Headless
1004 head
1005 }
1006 }
1007 }
1008 match sent
1009 { Ok(_) -> if keep { serve_requests serving buffer at served } else { () }
1010 , Error(_) -> ()
1011 }
1012 }
1013 , Error(Closed) -> ()
1014 , Error(Bad(status, message)) ->
1015 {
1016 let _ =
1017 send_response conn activity (serialize (text status message) False True)
1018 ()
1019 }
1020 }
1021}
1022
1023/// A streamed response (ADR 0053): the head (chunked, no length), the
1024/// stream fn, the final chunk. Between chunks the connection is untimed;
1025/// each write is timed per chunk by the watchdog.
1026fn serve_streamed ::
1027 conn: Resource<TcpStream>
1028 activity: Subject<Activity>
1029 resp: Response
1030 keep: Bool
1031 write: fn(Writer) -> () / <io, proc, clock, random, fs, net, ffi>
1032 -> Result<(), String> / <io, proc, clock, random, fs, net, ffi>
1033{
1034 use () <-
1035 result.try (send_response conn activity (serialize_stream_head resp keep))
1036 process.send activity Streaming
1037 write Socket(conn, activity)
1038 process.send activity Writing
1039 net.write conn (bytes.from_string "0\r\n\r\n")
1040}
1041
1042/// Write a response (timed by the watchdog).
1043fn send_response ::
1044 conn: Resource<TcpStream>
1045 activity: Subject<Activity>
1046 data: Bytes
1047 -> Result<(), String> / <io, proc>
1048{
1049 process.send activity Writing
1050 net.write conn data
1051}
1052
1053/// The largest request head, in bytes (431 beyond it).
1054fn head_limit :: -> Int
1055{
1056 65536
1057}
1058
1059fn read_request ::
1060 conn
1061 activity
1062 settings: Settings
1063 buffer: Bytes
1064 at: Int
1065 -> Result<Incoming, Failure> / <io, proc>
1066{
1067 match parse_head buffer at
1068 { Partial -> if bytes.length buffer > at
1069 {
1070 // A head has begun: it must arrive whole within `head_timeout`.
1071 process.send activity HeadStarted
1072 let begun = bytes.slice buffer at (bytes.length buffer)
1073 read_head
1074 conn
1075 activity
1076 settings
1077 (bytes.append (bytes.builder ()) begun)
1078 (bytes.length begun)
1079 (last_bytes begun)
1080 }
1081 else
1082 {
1083 // Waiting for a request to start: `idle_timeout`, and the acceptor
1084 // may close the connection now (`Idle`).
1085 process.send activity Idle
1086 match read_raw conn
1087 { Ok(more) -> read_request conn activity settings more 0
1088 , Error(e) -> Error(e)
1089 }
1090 }
1091 , head -> request_of conn activity settings head buffer at
1092 }
1093}
1094
1095/// The request whose head, from offset `at` of `buffer`, is `head`.
1096fn request_of ::
1097 conn
1098 activity
1099 settings: Settings
1100 head: Head
1101 buffer: Bytes
1102 at: Int
1103 -> Result<Incoming, Failure> / <io, proc>
1104{
1105 match head
1106 { Complete
1107 ( method
1108 , path
1109 , version
1110 , headers
1111 , end
1112 , length
1113 ) -> if end - at > head_limit ()
1114 {
1115 Error(Bad(431, "request head too large"))
1116 }
1117 else
1118 {
1119 let max_body = body_limit settings path
1120 match read_body conn activity max_body headers length buffer end
1121 { Ok((body, buffer, at)) ->
1122 Ok( Incoming( Request(method, path, headers, body)
1123 , keeps_alive version headers
1124 , buffer
1125 , at
1126 )
1127 )
1128 , Error(e) -> Error(e)
1129 }
1130 }
1131 , Invalid(reason) -> Error(Bad(400, "bad request: " <> reason))
1132 // Its end was found, yet it is incomplete: blank lines before the
1133 // request line, which would otherwise restart `head_timeout`.
1134 , Partial -> Error(Bad(400, "bad request: an empty head"))
1135 }
1136}
1137
1138/// Read the rest of a head begun in `received` (`size` bytes, ending in
1139/// `tail`), not reported to the watchdog, which times it by
1140/// `head_timeout`. Each read is scanned once for the head's end, with
1141/// the 3 bytes before it; then the head is built and parsed once (ADR
1142/// 0012, Revision 2).
1143fn read_head ::
1144 conn
1145 activity
1146 settings: Settings
1147 received: Builder
1148 size: Int
1149 tail: Bytes
1150 -> Result<Incoming, Failure> / <io, proc>
1151{
1152 if size > head_limit ()
1153 {
1154 Error(Bad(431, "request head too large"))
1155 }
1156 else
1157 {
1158 match read_raw conn
1159 { Ok(more) ->
1160 {
1161 let window = bytes.concat tail more
1162 let received = bytes.append received more
1163 if parse_head_end window 0 >= 0
1164 {
1165 let buffer = bytes.build received
1166 request_of conn activity settings (parse_head buffer 0) buffer 0
1167 }
1168 else
1169 {
1170 read_head
1171 conn
1172 activity
1173 settings
1174 received
1175 (size + bytes.length more)
1176 (last_bytes window)
1177 }
1178 }
1179 , Error(e) -> Error(e)
1180 }
1181 }
1182}
1183
1184/// The last 3 bytes of `b` (all of them if fewer): an end of head may
1185/// straddle two reads.
1186fn last_bytes :: b: Bytes -> Bytes
1187{
1188 let n = bytes.length b
1189 bytes.slice b if n > 3 { n - 3 } else { 0 } n
1190}
1191
1192/// Whether the client wants the connection kept open after its request.
1193fn keeps_alive :: version: Int headers: List<(String, String)> -> Bool
1194{
1195 let connection = option.unwrap_or (find_header headers "connection") ""
1196 if version == 0
1197 {
1198 has_token connection "keep-alive"
1199 }
1200 else
1201 {
1202 !has_token connection "close"
1203 }
1204}
1205
1206/// Whether the comma-separated `value` lists `token` (lowercase).
1207fn has_token :: value: String token: String -> Bool
1208{
1209 list.any
1210 (string.split (string.to_lower value) ",")
1211 fn t { string.trim t == token }
1212}
1213
1214/// The body that starts at offset `at` of `buffer`, then where the next
1215/// request starts. A body over `max_body` bytes (if not negative) is not
1216/// read: it gets a 413, and the connection is closed.
1217fn read_body ::
1218 conn
1219 activity
1220 max_body: Int
1221 headers
1222 length: Int
1223 buffer: Bytes
1224 at: Int
1225 -> Result<(Bytes, Bytes, Int), Failure> / <io, proc>
1226{
1227 match find_header headers "transfer-encoding"
1228 { Some
1229 ( coding
1230 ) -> if length >= 0
1231 {
1232 // Two framings: reading the wrong one would desynchronize the
1233 // connection (request smuggling).
1234 Error(Bad(400, "bad request: both Content-Length and Transfer-Encoding"))
1235 }
1236 else if string.to_lower (string.trim coding) == "chunked"
1237 {
1238 continue_if_expected conn headers buffer at
1239 read_chunks conn activity max_body buffer at (bytes.builder ()) 0
1240 }
1241 else
1242 {
1243 Error(Bad(501, "unsupported Transfer-Encoding: " <> coding))
1244 }
1245 , None -> if too_large length max_body
1246 {
1247 // Before `100 Continue`: a waiting client does not send the body.
1248 Error(too_large_error ())
1249 }
1250 else if length > 0
1251 {
1252 continue_if_expected conn headers buffer at
1253 take conn activity buffer at length
1254 }
1255 else
1256 {
1257 Ok((bytes.empty (), buffer, at))
1258 }
1259 }
1260}
1261
1262/// The body limit for a request to `target` (a path, with or without its
1263/// query): the limit of the first of `settings.large_bodies` whose
1264/// prefix matches the path on whole segments, else `settings.max_body`.
1265/// The path is matched as the client wrote it, not decoded: a request
1266/// that spells a prefix another way (`/upl%6Fads`) gets `max_body`.
1267pub fn body_limit :: settings: Settings target: String -> Int
1268{
1269 let path = (target_split target).0
1270 match list.find settings.large_bodies fn entry { under path entry.0 }
1271 { Some((_, limit)) -> limit, None -> settings.max_body }
1272}
1273
1274/// Whether `path` is `prefix` or below it, on whole segments.
1275fn under :: path: String prefix: String -> Bool
1276{
1277 string.starts_with path prefix
1278 && ( string.ends_with prefix "/"
1279 || string.byte_length path == string.byte_length prefix
1280 || string.starts_with
1281 (string.slice path (string.length prefix) (string.length path))
1282 "/"
1283 )
1284}
1285
1286fn too_large :: size: Int max_body: Int -> Bool
1287{
1288 max_body >= 0 && size > max_body
1289}
1290
1291fn too_large_error :: -> Failure
1292{
1293 Bad(413, "request body too large")
1294}
1295
1296/// Tell a client that waits for it (`Expect: 100-continue`) to send the
1297/// body.
1298fn continue_if_expected :: conn headers buffer: Bytes at: Int -> () / <io>
1299{
1300 let expect = option.map (find_header headers "expect") string.to_lower
1301 if expect == Some("100-continue") && bytes.length buffer == at
1302 {
1303 let _ = net.write_string conn "HTTP/1.1 100 Continue\r\n\r\n"
1304 ()
1305 }
1306 else
1307 {
1308 ()
1309 }
1310}
1311
1312/// The data of the chunks from offset `at` of `buffer` on, appended to
1313/// `body` (`size` bytes so far); then where the next request starts.
1314fn read_chunks ::
1315 conn
1316 activity
1317 max_body: Int
1318 buffer: Bytes
1319 at: Int
1320 body: Builder
1321 size: Int
1322 -> Result<(Bytes, Bytes, Int), Failure> / <io, proc>
1323{
1324 match parse_chunk buffer at
1325 { ChunkData(start, chunk) -> if too_large (size + chunk) max_body
1326 {
1327 Error(too_large_error ())
1328 }
1329 else
1330 {
1331 match take conn activity buffer start (chunk + 2)
1332 { Ok((data, buffer, at)) -> if bytes.slice data chunk (chunk + 2)
1333 == bytes.from_string "\r\n"
1334 {
1335 read_chunks
1336 conn
1337 activity
1338 max_body
1339 buffer
1340 at
1341 (bytes.append body (bytes.slice data 0 chunk))
1342 (size + chunk)
1343 }
1344 else
1345 {
1346 Error(Bad(400, "bad request: no CRLF after a chunk"))
1347 }
1348 , Error(e) -> Error(e)
1349 }
1350 }
1351 , ChunkEnd(end) -> Ok((bytes.build body, buffer, end))
1352 , ChunkInvalid(reason) -> Error(Bad(400, "bad request: " <> reason))
1353 , ChunkPartial -> if bytes.length buffer - at > 65536
1354 {
1355 Error(Bad(400, "bad request: chunk header too large"))
1356 }
1357 else
1358 {
1359 match receive_more conn activity buffer at
1360 { Ok(buffer) -> read_chunks conn activity max_body buffer 0 body size
1361 , Error(e) -> Error(e)
1362 }
1363 }
1364 }
1365}
1366
1367/// `length` bytes from offset `at` of `buffer`, reading more as needed;
1368/// then where what follows them starts. Reads go into a builder, so a
1369/// large body is copied a bounded number of times.
1370fn take ::
1371 conn
1372 activity
1373 buffer: Bytes
1374 at: Int
1375 length: Int
1376 -> Result<(Bytes, Bytes, Int), Failure> / <io, proc>
1377{
1378 let available = bytes.length buffer - at
1379 if available >= length
1380 {
1381 Ok((bytes.slice buffer at (at + length), buffer, at + length))
1382 }
1383 else
1384 {
1385 let rest = bytes.slice buffer at (bytes.length buffer)
1386 take_more
1387 conn
1388 activity
1389 (bytes.append (bytes.builder ()) rest)
1390 available
1391 length
1392 }
1393}
1394
1395fn take_more ::
1396 conn
1397 activity
1398 received: Builder
1399 size: Int
1400 length: Int
1401 -> Result<(Bytes, Bytes, Int), Failure> / <io, proc>
1402{
1403 if size >= length
1404 {
1405 let all = bytes.build received
1406 Ok((bytes.slice all 0 length, all, length))
1407 }
1408 else
1409 {
1410 match read_some conn activity
1411 { Ok(more) ->
1412 take_more
1413 conn
1414 activity
1415 (bytes.append received more)
1416 (size + bytes.length more)
1417 length
1418 , Error(e) -> Error(e)
1419 }
1420 }
1421}
1422
1423/// The unread part of `buffer` (from offset `at`), then what arrives next.
1424fn receive_more ::
1425 conn
1426 activity
1427 buffer: Bytes
1428 at: Int
1429 -> Result<Bytes, Failure> / <io, proc>
1430{
1431 match read_some conn activity
1432 { Ok(more) ->
1433 Ok(bytes.concat (bytes.slice buffer at (bytes.length buffer)) more)
1434 , Error(e) -> Error(e)
1435 }
1436}
1437
1438/// The next bytes from the connection, timed by `idle_timeout` from now.
1439fn read_some ::
1440 conn: Resource<TcpStream>
1441 activity: Subject<Activity>
1442 -> Result<Bytes, Failure> / <io, proc>
1443{
1444 process.send activity Reading
1445 read_raw conn
1446}
1447
1448/// The next bytes from the connection, under whatever timeout runs.
1449fn read_raw :: conn: Resource<TcpStream> -> Result<Bytes, Failure> / <io>
1450{
1451 match net.read conn 65536
1452 { Ok(more) -> if bytes.length more == 0 { Error(Closed) } else { Ok(more) }
1453 , Error(_) -> Error(Closed)
1454 }
1455}
1456
1457type Head
1458{ Partial
1459, Invalid(reason: String)
1460 /// The head ends at offset `end`; `content_length` is -1 when absent.
1461, Complete(
1462 method: String
1463 , path: String
1464 , version: Int
1465 , headers: List<(String, String)>
1466 , end: Int
1467 , content_length: Int
1468 )
1469}
1470
1471@extern c "tl_http_parse_request"
1472fn parse_head :: buffer: Bytes at: Int -> Head
1473
1474/// Where the head in `buffer` ends (just past its empty line), if it ends
1475/// at or after `from`; else -1.
1476@extern c "tl_http_parse_head_end"
1477fn parse_head_end :: buffer: Bytes from: Int -> Int
1478
1479type Chunk
1480{ ChunkPartial
1481, ChunkInvalid(reason: String)
1482 /// `size` bytes of data from offset `start`, then CRLF.
1483, ChunkData(start: Int, size: Int)
1484 /// The last chunk and its trailers end at offset `end`.
1485, ChunkEnd(end: Int)
1486}
1487
1488@extern c "tl_http_parse_chunk"
1489fn parse_chunk :: buffer: Bytes at: Int -> Chunk
1490
1491/// Whether `status` is a final status this server sends without framing or a
1492/// message body: 204 No Content or 304 Not Modified.
1493fn final_status_without_body :: status: Int -> Bool
1494{
1495 status == 204 || status == 304
1496}
1497
1498/// The head and (unless `with_body` is false, for HEAD requests) the body,
1499/// in one buffer so they leave in one write. The whole-body case.
1500fn serialize :: resp: Response keep_alive: Bool with_body: Bool -> Bytes
1501{
1502 let body = option.unwrap_or (whole_body resp) (bytes.empty ())
1503 let head =
1504 head_bytes
1505 resp.status
1506 resp.headers
1507 keep_alive
1508 ("Content-Length: " <> show (bytes.length body))
1509 let out = bytes.append_string (bytes.builder ()) head
1510 bytes.build if with_body { bytes.append out body } else { out }
1511}
1512
1513/// A streamed response's head: `Transfer-Encoding: chunked`, no length.
1514fn serialize_stream_head :: resp: Response keep_alive: Bool -> Bytes
1515{
1516 bytes.from_string
1517 ( head_bytes
1518 resp.status
1519 resp.headers
1520 keep_alive
1521 "Transfer-Encoding: chunked"
1522 )
1523}
1524
1525/// The status line, the caller's headers (framing filtered), the framing
1526/// header, and the keep-alive line.
1527fn head_bytes ::
1528 status: Int
1529 headers: List<(String, String)>
1530 keep_alive: Bool
1531 framing: String
1532 -> String
1533{
1534 let own = list.filter headers fn h { !is_framing (string.to_lower h.0) }
1535 string.concat
1536 [ "HTTP/1.1 "
1537 , show status
1538 , " "
1539 , reason status
1540 , "\r\n"
1541 , string.concat (list.map own fn h { h.0 <> ": " <> h.1 <> "\r\n" })
1542 , if framing == "" { "" } else { framing <> "\r\n" }
1543 , if keep_alive
1544 {
1545 "Connection: keep-alive\r\n\r\n"
1546 }
1547 else
1548 {
1549 "Connection: close\r\n\r\n"
1550 }
1551 ]
1552}
1553
1554/// Headers the server sets itself.
1555fn is_framing :: name: String -> Bool
1556{
1557 match name
1558 { "content-length" | "connection" | "transfer-encoding" -> True, _ -> False }
1559}
1560
1561/// The status line's reason phrase (RFC 9110 §15 and the registered
1562/// extensions); `"Status"` for one not in the set.
1563pub fn reason :: status: Int -> String
1564{
1565 match status
1566 { 100 -> "Continue"
1567 , 101 -> "Switching Protocols"
1568 , 200 -> "OK"
1569 , 201 -> "Created"
1570 , 202 -> "Accepted"
1571 , 203 -> "Non-Authoritative Information"
1572 , 204 -> "No Content"
1573 , 205 -> "Reset Content"
1574 , 206 -> "Partial Content"
1575 , 226 -> "IM Used"
1576 , 300 -> "Multiple Choices"
1577 , 301 -> "Moved Permanently"
1578 , 302 -> "Found"
1579 , 303 -> "See Other"
1580 , 304 -> "Not Modified"
1581 , 305 -> "Use Proxy"
1582 , 307 -> "Temporary Redirect"
1583 , 308 -> "Permanent Redirect"
1584 , 400 -> "Bad Request"
1585 , 401 -> "Unauthorized"
1586 , 402 -> "Payment Required"
1587 , 403 -> "Forbidden"
1588 , 404 -> "Not Found"
1589 , 405 -> "Method Not Allowed"
1590 , 406 -> "Not Acceptable"
1591 , 407 -> "Proxy Authentication Required"
1592 , 408 -> "Request Timeout"
1593 , 409 -> "Conflict"
1594 , 410 -> "Gone"
1595 , 411 -> "Length Required"
1596 , 412 -> "Precondition Failed"
1597 , 413 -> "Content Too Large"
1598 , 414 -> "URI Too Long"
1599 , 415 -> "Unsupported Media Type"
1600 , 416 -> "Range Not Satisfiable"
1601 , 417 -> "Expectation Failed"
1602 , 418 -> "I'm a Teapot"
1603 , 421 -> "Misdirected Request"
1604 , 422 -> "Unprocessable Content"
1605 , 423 -> "Locked"
1606 , 424 -> "Failed Dependency"
1607 , 425 -> "Too Early"
1608 , 426 -> "Upgrade Required"
1609 , 428 -> "Precondition Required"
1610 , 429 -> "Too Many Requests"
1611 , 431 -> "Request Header Fields Too Large"
1612 , 451 -> "Unavailable For Legal Reasons"
1613 , 500 -> "Internal Server Error"
1614 , 501 -> "Not Implemented"
1615 , 502 -> "Bad Gateway"
1616 , 503 -> "Service Unavailable"
1617 , 504 -> "Gateway Timeout"
1618 , 505 -> "HTTP Version Not Supported"
1619 , 507 -> "Insufficient Storage"
1620 , 508 -> "Loop Detected"
1621 , 510 -> "Not Extended"
1622 , 511 -> "Network Authentication Required"
1623 , _ -> "Status"
1624 }
1625}
1626
1627/// The fields of an `application/x-www-form-urlencoded` body (or query
1628/// string): `a=1&b=two+words` gives `[("a", "1"), ("b", "two words")]`.
1629/// Pairs split on `&`, each on its first `=`, both sides percent-decoded
1630/// with `+` a space — in the query (`query_params`) as in a form body,
1631/// as browsers send both; empty pairs are dropped, a bare `k` is
1632/// `("k", "")`.
1633@extern c "tl_http_parse_query"
1634pub fn form :: body: String -> List<(String, String)>
1635
1636/// The request target split at its first `?`: `(path, query)`, the query
1637/// without the `?` ("" when there is none). Later `?`s stay in the query:
1638/// `target_split("/a?b?c")` is `("/a", "b?c")`.
1639pub fn target_split :: target: String -> (String, String)
1640{
1641 match string.split target "?"
1642 { [path] -> (path, "")
1643 , [path, ..rest] -> (path, string.join rest "?")
1644 , [] -> ("", "")
1645 }
1646}
1647
1648/// The path of the request's target (before any `?`).
1649pub fn path :: req: Request -> String
1650{
1651 (target_split req.path).0
1652}
1653
1654/// The query of the request's target (after the first `?`), "" when it
1655/// has none.
1656pub fn query :: req: Request -> String
1657{
1658 (target_split req.path).1
1659}
1660
1661/// The path's segments: split on `/` with empty segments dropped, each
1662/// percent-decoded. Decoding happens after splitting, so an encoded slash
1663/// (`%2F`) stays inside its segment.
1664pub fn segments :: req: Request -> List<String>
1665{
1666 list.map
1667 (list.filter (string.split (path req) "/") fn s { s != "" })
1668 url_decode
1669}
1670
1671/// The query's parameters: split on `&`, each `k=v` on its first `=`,
1672/// both sides percent-decoded; a bare `k` is `("k", "")`.
1673pub fn query_params :: req: Request -> List<(String, String)>
1674{
1675 form (query req)
1676}
1677
1678/// The value of field `name` in a form, if present.
1679pub fn field :: fields: List<(String, String)> name: String -> Option<String>
1680{
1681 match fields
1682 { [] -> None
1683 , [(k, v), ..rest] -> if k == name { Some(v) } else { field rest name }
1684 }
1685}
1686
1687/// Decode `%XX` escapes and `+` (a space); invalid escapes stay as they
1688/// are. A string without `%` or `+` is returned as is.
1689@extern c "tl_url_decode" owned
1690pub fn url_decode :: s: String -> String
1691
1692/// Percent-encode `s` for a URL path or query (RFC 3986): every byte
1693/// outside the unreserved set (`A-Z a-z 0-9 - _ . ~`) becomes `%XX`
1694/// (uppercase hex). A space is `%20`, not `+`: `url_decode` maps both
1695/// back to a space, and a `+` in a path segment decodes as a space only
1696/// in a query or form, so `%20` is the safe spelling everywhere.
1697/// `url_decode(url_encode(s))` is `s`. A string without a byte to encode
1698/// is returned as is.
1699@extern c "tl_url_encode" owned
1700pub fn url_encode :: s: String -> String