tobogganSe connecter

packages

The Louise packages, each released on its own

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

964 lignes · 25221 octetsBrut · Historique
1/// An HTTP/1.1 client for fetching what a URL points to (ADR 0079): one
2/// request (a GET, or any method with a body: `request`, ADR 0079
3/// Revision 1), over TCP or TLS, guarded against server-side request
4/// forgery.
5///
6/// use tls_config <- result.try (tls.config (tls.defaults ())) // once, and share it
7/// let opts = Options(..fetch.options tls_config, max_body: 2097152)
8/// use page <- result.try (url.parse "https://example.com/")
9/// match fetch.get page [("User-Agent", "toki")] opts
10/// { Ok(Response(200, headers, body, _)) -> ...
11/// , Ok(Response(status, ..)) -> ... // 3xx too: redirects are not followed
12/// , Error(e) -> ...
13/// }
14///
15/// What `get` does:
16/// - only `http` and `https`; a header name that is not a token, or a
17/// value with a CR, LF or NUL, is refused;
18/// - resolves the host and checks **every** address it names against the
19/// policy (`PublicOnly` refuses any private one, see `fetch/address`),
20/// then connects to those addresses in order, never resolving again;
21/// - over `https`, verifies the certificate against the host name (also
22/// sent as SNI) with the caller's TLS configuration;
23/// - sends `Host`, `Connection: close` and `Accept-Encoding: identity`
24/// (there is no decompression), then the caller's headers (theirs for
25/// `Host`, `Connection`, `Accept-Encoding`, `Content-Length`,
26/// `Transfer-Encoding`, `TE` and `Upgrade` are dropped);
27/// - skips `1xx` responses; reads the body by `Content-Length`, chunked,
28/// or until the server closes (a TLS close without `close_notify` ends
29/// such a body too); keeps at most `max_body` bytes of it and says when
30/// it cut it (`truncated`);
31/// - never follows redirects: a 3xx is returned as it is, and the caller
32/// resolves `Location` with `url.resolve` and calls `get` again, which
33/// checks the new host;
34/// - runs the whole exchange in an actor of its own, and kills it at the
35/// deadline (`TimedOut`), wherever it waits: resolving, connecting, the
36/// handshake or a read.
37///
38/// `request_streaming` does the same, but returns once the head has
39/// arrived, with the body to read as it comes (`next`, `cancel`; ADR 0079
40/// Revision 2); `fetch/sse` parses server-sent events from it.
41///
42/// **Needs engineering review** (ADR 0079): this is an SSRF surface.
43
44import address
45import response.
46{type Dechunker, type Framing, type Head, Chunked, Length, NoBody, UntilClose}
47import std/bytes
48import std/ffi.{type Resource}
49import std/list
50import std/net.{type TcpStream}
51import std/process.{type Down, type Pid, type Subject, type proc, Down}
52import std/result
53import std/string
54import std/time.{type clock}
55import std/tls.{type TlsConfig, type TlsError, type TlsStream, Truncated}
56import url.{type Url, Url}
57
58/// Which addresses a fetch may connect to.
59@derive Eq Show Debug
60pub type Policy
61{ /// Only public ones: every address of the host must be public.
62 PublicOnly
63 /// Any: for tests against `127.0.0.1`, and for a deployment that knows
64 /// its network.
65, AnyAddress
66}
67
68/// How a fetch runs: the TLS configuration for `https`, the address
69/// policy, the most body bytes kept, the largest response head, and the
70/// deadline (a `time.monotonic` instant in milliseconds; see
71/// [`deadline_in`]).
72pub type Options
73{ Options(
74 tls: Resource<TlsConfig>
75 , policy: Policy
76 , max_body: Int
77 , max_head: Int
78 , deadline: Int
79 )
80}
81
82/// `PublicOnly`, bodies kept to 1 MiB, heads up to 64 KiB, and a deadline
83/// 10 seconds from now.
84pub fn options :: config: Resource<TlsConfig> -> Options / <clock>
85{
86 Options(config, PublicOnly, 1048576, 65536, deadline_in 10000)
87}
88
89/// The deadline `ms` milliseconds from now. One deadline can cover
90/// several fetches (a redirect chain).
91pub fn deadline_in :: ms: Int -> Int / <clock>
92{
93 now_ms () + ms
94}
95
96/// A response: its status, its headers in order, its body (at most
97/// `max_body` bytes), and whether the body was cut there.
98@derive Eq Debug
99pub type Response
100{ Response(
101 status: Int
102 , headers: List<(String, String)>
103 , body: Bytes
104 , truncated: Bool
105 )
106}
107
108impl Show<Response>
109{
110 fn show :: resp
111 {
112 debug resp
113 }
114}
115
116/// The value of the response's header `name` (any case), if present.
117pub fn header :: resp: Response name: String -> Option<String>
118{
119 response.header resp.headers name
120}
121
122/// Why a fetch failed.
123@derive Eq Show Debug
124pub type Error
125{ /// Not an `http` or `https` URL with a valid host.
126 BadUrl(String)
127 /// A request header that cannot be sent; its name.
128, BadHeader(String)
129 /// The host names an address the policy refuses.
130, Refused(host: String, ip: String)
131 /// The host names no address.
132, NoAddress(String)
133 /// No address of the host accepted a connection, or the connection
134 /// failed while sending or reading.
135, Connect(String)
136 /// TLS failed (the certificate, the handshake).
137, Tls(TlsError)
138 /// The response is not HTTP/1.1 as this client reads it.
139, Malformed(String)
140 /// The deadline passed.
141, TimedOut
142}
143
144/// GET `target` with `headers` (see the module's documentation).
145pub fn get ::
146 target: Url
147 headers: List<(String, String)>
148 opts: Options
149 -> Result<Response, Error> / <proc, clock>
150{
151 request "GET" target headers (bytes.empty ()) opts
152}
153
154/// `method` (`POST`, `PUT`, …) to `target` with `headers` and `body`,
155/// under the same guards as `get`. A body is sent with its
156/// `Content-Length` (one of the caller's is dropped, as the other framing
157/// headers are); the caller names its type (`Content-Type`).
158///
159/// fetch.request "POST" api [("Content-Type", "application/json")] (bytes.from_string json) opts
160pub fn request ::
161 method: String
162 target: Url
163 headers: List<(String, String)>
164 body: Bytes
165 opts: Options
166 -> Result<Response, Error> / <proc, clock>
167{
168 use checked <- result.try (valid_url target)
169 use () <- result.try (valid_headers headers)
170 use () <- result.try (valid_method method)
171 let left = opts.deadline - now_ms ()
172 if left <= 0
173 {
174 Error(TimedOut)
175 }
176 else
177 {
178 let reply = process.subject ()
179 let (pid, down) =
180 process.spawn_monitor
181 fn { process.send reply (exchange method checked headers body opts) }
182 let selector =
183 process.new_selector ()
184 |> process.selecting reply Got
185 |> process.selecting down Stopped
186 match process.select selector left
187 { Some(Got(answer)) ->
188 {
189 process.demonitor_flush pid down
190 answer
191 }
192 , Some(Stopped(Down(_, reason))) ->
193 Error(Malformed("the fetch stopped: {show reason}"))
194 , None ->
195 {
196 process.kill pid
197 process.demonitor_flush pid down
198 // A reply sent just before the kill would stay in the mailbox.
199 let _ = process.receive reply 0
200 Error(TimedOut)
201 }
202 }
203 }
204}
205
206type Outcome { Got(Result<Response, Error>), Stopped(Down) }
207
208fn now_ms :: -> Int / <clock>
209{
210 time.in_milliseconds (time.monotonic ())
211}
212
213// ---- streamed responses ---------------------------------------------------------
214
215/// A response whose body is read as it arrives (ADR 0079, Revision 2):
216/// its status, its headers, and the body to read with [`next`].
217pub type Streamed
218{ Streamed(status: Int, headers: List<(String, String)>, body: BodyStream) }
219
220/// A body still arriving. Only the actor that made the request can read
221/// it; one that stops reading early calls [`cancel`].
222pub opaque type BodyStream
223{ BodyStream(
224 pid: Pid
225 , down: Subject<Down>
226 , events: Subject<StreamEvent>
227 , deadline: Int
228 )
229}
230
231/// A piece of a streamed body: some bytes, or the end (and whether the
232/// body was cut at `max_body`).
233@derive Eq Debug
234pub type Piece { Data(Bytes), End(truncated: Bool) }
235
236impl Show<Piece>
237{
238 fn show :: piece
239 {
240 debug piece
241 }
242}
243
244/// What the exchange's actor sends a streaming caller, in order: the head,
245/// body bytes, then the end or a failure.
246type StreamEvent
247{ Opened(Int, List<(String, String)>)
248, Received(Bytes)
249, Finished(Bool)
250, Failed(Error)
251}
252
253type Arrival { Arrived(StreamEvent), Ended(Down) }
254
255/// [`request`], but returning once the response head has arrived, with
256/// the body to read as it comes ([`next`]): for server-sent events and
257/// other long answers. Everything `request` checks and does applies, and
258/// `opts.deadline` covers the whole exchange, the body included: give a
259/// long answer a long deadline. At most `max_body` bytes are delivered,
260/// so a reader that falls behind holds at most that much.
261///
262/// match fetch.request_streaming "POST" api headers body opts
263/// { Ok(Streamed(200, _, stream)) -> read stream // fetch.next until End
264/// , Ok(Streamed(status, _, stream)) -> fetch.cancel stream
265/// , Error(e) -> ...
266/// }
267pub fn request_streaming ::
268 method: String
269 target: Url
270 headers: List<(String, String)>
271 body: Bytes
272 opts: Options
273 -> Result<Streamed, Error> / <proc, clock>
274{
275 use checked <- result.try (valid_url target)
276 use () <- result.try (valid_headers headers)
277 use () <- result.try (valid_method method)
278 if opts.deadline - now_ms () <= 0
279 {
280 Error(TimedOut)
281 }
282 else
283 {
284 let events = process.subject ()
285 let (pid, down) =
286 process.spawn_monitor
287 fn { stream_exchange method checked headers body opts events }
288 let stream = BodyStream(pid, down, events, opts.deadline)
289 match wait stream
290 { Ok(Opened(status, head)) -> Ok(Streamed(status, head, stream))
291 , Ok(Failed(e)) ->
292 {
293 process.demonitor_flush pid down
294 Error(e)
295 }
296 , Ok(_) ->
297 {
298 cancel stream
299 Error(Malformed("the body came before the head"))
300 }
301 , Error(e) -> Error(e)
302 }
303 }
304}
305
306/// The next piece of `stream`: bytes as they arrived (a chunked body
307/// already decoded), then `End`. After `End` or an `Error` the stream is
308/// over: its connection is closed. `TimedOut` at the request's deadline.
309pub fn next :: stream: BodyStream -> Result<Piece, Error> / <proc, clock>
310{
311 match wait stream
312 { Ok(Received(data)) -> Ok(Data(data))
313 , Ok(Finished(truncated)) ->
314 {
315 process.demonitor_flush stream.pid stream.down
316 Ok(End(truncated))
317 }
318 , Ok(Failed(e)) ->
319 {
320 process.demonitor_flush stream.pid stream.down
321 Error(e)
322 }
323 , Ok(Opened(_, _)) ->
324 {
325 cancel stream
326 Error(Malformed("a second response head"))
327 }
328 , Error(e) -> Error(e)
329 }
330}
331
332/// Stops reading `stream`: closes its connection (the server sees the
333/// client leave) and drops what arrived unread. Harmless after the end.
334pub fn cancel :: stream: BodyStream -> () / <proc>
335{
336 process.kill stream.pid
337 process.demonitor_flush stream.pid stream.down
338 drain stream.events
339}
340
341/// The next event, until the deadline. On a timeout or a crash, the
342/// exchange is stopped and its leftovers dropped.
343fn wait :: stream: BodyStream -> Result<StreamEvent, Error> / <proc, clock>
344{
345 let left = stream.deadline - now_ms ()
346 let selector =
347 process.new_selector ()
348 |> process.selecting stream.events Arrived
349 |> process.selecting stream.down Ended
350 match process.select selector (if left < 0 { 0 } else { left })
351 { Some(Arrived(event)) -> Ok(event)
352 , Some(Ended(Down(_, reason))) ->
353 {
354 drain stream.events
355 Error(Malformed("the fetch stopped: {show reason}"))
356 }
357 , None ->
358 {
359 cancel stream
360 Error(TimedOut)
361 }
362 }
363}
364
365fn drain :: events: Subject<StreamEvent> -> () / <proc>
366{
367 match process.receive events 0 { Some(_) -> drain events, None -> () }
368}
369
370// ---- checks --------------------------------------------------------------------
371
372/// The URL parsed again from its text (a `Url` can be built by hand),
373/// and only `http` or `https`.
374fn valid_url :: target: Url -> Result<Url, Error>
375{
376 use parsed <-
377 result.try (url.parse (url.to_string target) |> result.map_error BadUrl)
378 if parsed.scheme == "http" || parsed.scheme == "https"
379 {
380 Ok(parsed)
381 }
382 else
383 {
384 Error(BadUrl("only http and https can be fetched"))
385 }
386}
387
388/// A method is a token (RFC 9110), as a header name is.
389fn valid_method :: method: String -> Result<(), Error>
390{
391 if is_token method
392 {
393 Ok(())
394 }
395 else
396 {
397 Error(BadHeader(method))
398 }
399}
400
401fn valid_headers :: headers: List<(String, String)> -> Result<(), Error>
402{
403 match list.find headers fn h { !is_token h.0 || has_line_break h.1 }
404 { Some((name, _)) -> Error(BadHeader(name)), None -> Ok(()) }
405}
406
407/// RFC 9110's `token`: digits, letters and ``!#$%&'*+-.^_`|~``.
408fn is_token :: name: String -> Bool
409{
410 let symbols = bytes.to_list (bytes.from_string "!#$%&'*+-.^_`|~")
411 name != ""
412 && list.all
413 (bytes.to_list (bytes.from_string name))
414 fn b
415 {
416 b >= 48 && b <= 57 || b >= 65 && b <= 90 || b >= 97 && b <= 122
417 || list.any symbols fn s { s == b }
418 }
419}
420
421fn has_line_break :: value: String -> Bool
422{
423 list.any
424 (bytes.to_list (bytes.from_string value))
425 fn b { b == 13 || b == 10 || b == 0 }
426}
427
428// ---- the exchange (in its own actor) -------------------------------------------
429
430/// A connection, plain or over TLS.
431type Conn { Plain(Resource<TcpStream>), Secure(Resource<TlsStream>) }
432
433fn exchange ::
434 method: String
435 target: Url
436 headers: List<(String, String)>
437 body: Bytes
438 opts: Options
439 -> Result<Response, Error> / <io>
440{
441 use conn <- result.try (connected target opts)
442 let answer = round_trip conn method target headers body opts
443 close conn
444 answer
445}
446
447/// A connection to `target`'s host, under the policy: resolved once,
448/// every address checked, then the first that accepts, secured for
449/// `https`.
450fn connected :: target: Url opts: Options -> Result<Conn, Error> / <io>
451{
452 let host = host_name target.host
453 let port = match url.port_or_default target { Some(p) -> p, None -> 80 }
454 use ips <-
455 result.try (net.resolve host |> result.map_error fn _ { NoAddress(host) })
456 use ips <- result.try (allowed opts.policy host ips)
457 use tcp <- result.try (connect_any ips port "")
458 secured target.scheme tcp host opts.tls
459}
460
461/// The host without an IPv6 address's brackets: what is resolved, and
462/// the TLS server name.
463fn host_name :: host: String -> String
464{
465 if string.starts_with host "["
466 {
467 string.slice host 1 (string.length host - 1)
468 }
469 else
470 {
471 host
472 }
473}
474
475/// Every address, if the policy allows them all.
476fn allowed ::
477 policy: Policy
478 host: String
479 ips: List<String>
480 -> Result<List<String>, Error>
481{
482 match (policy, ips)
483 { (_, []) -> Error(NoAddress(host))
484 , (AnyAddress, _) -> Ok(ips)
485 , (PublicOnly, _) -> match list.find ips fn ip { !address.is_public ip }
486 { Some(ip) -> Error(Refused(host, ip)), None -> Ok(ips) }
487 }
488}
489
490/// A connection to the first of `ips` that accepts.
491fn connect_any ::
492 ips: List<String>
493 port: Int
494 last: String
495 -> Result<Resource<TcpStream>, Error> / <io>
496{
497 match ips
498 { [] -> Error(Connect(last))
499 , [ip, ..rest] -> match net.connect ip port
500 { Ok(conn) -> Ok(conn), Error(e) -> connect_any rest port e }
501 }
502}
503
504fn secured ::
505 scheme: String
506 tcp: Resource<TcpStream>
507 host: String
508 config: Resource<TlsConfig>
509 -> Result<Conn, Error> / <io>
510{
511 if scheme == "https"
512 {
513 match tls.connect tcp host config
514 { Ok(stream) -> Ok(Secure(stream))
515 , Error(e) ->
516 {
517 net.close tcp
518 Error(Tls(e))
519 }
520 }
521 }
522 else
523 {
524 Ok(Plain(tcp))
525 }
526}
527
528fn round_trip ::
529 conn: Conn
530 method: String
531 target: Url
532 headers: List<(String, String)>
533 body: Bytes
534 opts: Options
535 -> Result<Response, Error> / <io>
536{
537 use (head, framing, rest) <-
538 result.try (send_and_read_head conn method target headers body opts)
539 use (body, truncated) <-
540 result.try (read_body conn framing rest opts.max_body)
541 Ok(Response(head.status, head.headers, body, truncated))
542}
543
544/// Sends the request, then reads the final head: it, the body's framing,
545/// and the body bytes read with it.
546fn send_and_read_head ::
547 conn: Conn
548 method: String
549 target: Url
550 headers: List<(String, String)>
551 body: Bytes
552 opts: Options
553 -> Result<(Head, Framing, Bytes), Error> / <io>
554{
555 use () <-
556 result.try
557 (write conn (bytes.concat (request_head method target headers body) body))
558 use (head, rest) <-
559 result.try (final_head conn opts.max_head (bytes.empty ()))
560 use framing <-
561 result.try (response.framing head |> result.map_error Malformed)
562 Ok((head, framing, rest))
563}
564
565fn request_head ::
566 method: String
567 target: Url
568 headers: List<(String, String)>
569 body: Bytes
570 -> Bytes
571{
572 let default = url.port_or_default Url(..target, port: None)
573 let port = match target.port
574 { Some(p) if Some(p) != default -> ":" <> show p, _ -> "" }
575 let own = list.filter headers fn h { !is_framing h.0 }
576 // A body says its length; so does a POST or PUT without one, which
577 // some servers otherwise wait on.
578 let length = if bytes.length body > 0 || method == "POST" || method == "PUT"
579 {
580 ["Content-Length: " <> show (bytes.length body)]
581 }
582 else
583 {
584 []
585 }
586 let lines =
587 [ method <> " " <> url.request_target target <> " HTTP/1.1"
588 , "Host: " <> target.host <> port
589 , "Connection: close"
590 , "Accept-Encoding: identity"
591 , ..list.append length (list.map own fn h { h.0 <> ": " <> h.1 })
592 ]
593 bytes.from_string (string.join lines "\r\n" <> "\r\n\r\n")
594}
595
596fn is_framing :: name: String -> Bool
597{
598 list.any
599 [ "host"
600 , "connection"
601 , "accept-encoding"
602 , "content-length"
603 , "transfer-encoding"
604 , "te"
605 , "upgrade"
606 ]
607 fn n { string.eq_ignore_ascii_case n name }
608}
609
610// ---- reading -------------------------------------------------------------------
611
612/// The first head that is not `1xx`, and the bytes read after it.
613fn final_head ::
614 conn: Conn
615 max_head: Int
616 buffer: Bytes
617 -> Result<(Head, Bytes), Error> / <io>
618{
619 match response.head buffer max_head
620 { Error(e) -> Error(Malformed(e))
621 , Ok(Some(head)) ->
622 {
623 let rest = bytes.slice buffer head.size (bytes.length buffer)
624 if head.status < 200
625 {
626 final_head conn max_head rest
627 }
628 else
629 {
630 Ok((head, rest))
631 }
632 }
633 , Ok(None) -> more_head conn max_head buffer
634 }
635}
636
637/// Reads until the head's blank line has arrived (only the new bytes
638/// and the 3 before them are searched), or until there is too much.
639fn more_head ::
640 conn: Conn
641 max_head: Int
642 buffer: Bytes
643 -> Result<(Head, Bytes), Error> / <io>
644{
645 use more <- result.try (read conn)
646 if bytes.length more == 0
647 {
648 Error(Malformed("the connection closed before the response head"))
649 }
650 else
651 {
652 let n = bytes.length buffer
653 let from = if n > 3 { n - 3 } else { 0 }
654 let buffer = bytes.concat buffer more
655 if has_blank_line buffer from || bytes.length buffer > max_head
656 {
657 final_head conn max_head buffer
658 }
659 else
660 {
661 more_head conn max_head buffer
662 }
663 }
664}
665
666fn has_blank_line :: data: Bytes at: Int -> Bool
667{
668 if at + 4 > bytes.length data
669 {
670 False
671 }
672 else if bytes.slice data at (at + 4) == bytes.from_string "\r\n\r\n"
673 {
674 True
675 }
676 else
677 {
678 has_blank_line data (at + 1)
679 }
680}
681
682fn read_body ::
683 conn: Conn
684 framing: Framing
685 rest: Bytes
686 max: Int
687 -> Result<(Bytes, Bool), Error> / <io>
688{
689 match framing
690 { NoBody -> Ok((bytes.empty (), False))
691 , Length(n) -> if n > max
692 {
693 exactly conn rest max False |> result.map fn b { (b, True) }
694 }
695 else
696 {
697 exactly conn rest n False |> result.map fn b { (b, False) }
698 }
699 , UntilClose ->
700 until_close
701 conn
702 (bytes.append (bytes.builder ()) rest)
703 (bytes.length rest)
704 max
705 , Chunked -> match response.feed (response.dechunker max) rest
706 { Ok(d) -> chunks conn d, Error(e) -> Error(Malformed(e)) }
707 }
708}
709
710/// The first `n` bytes, starting with `have`; `closed` once a read found
711/// the end.
712fn exactly ::
713 conn: Conn
714 have: Bytes
715 n: Int
716 closed: Bool
717 -> Result<Bytes, Error> / <io>
718{
719 if bytes.length have >= n
720 {
721 Ok(bytes.slice have 0 n)
722 }
723 else if closed
724 {
725 Error(Malformed("the connection closed before the end of the body"))
726 }
727 else
728 {
729 use more <- result.try (read conn)
730 exactly conn (bytes.concat have more) n (bytes.length more == 0)
731 }
732}
733
734fn until_close ::
735 conn: Conn
736 body: bytes.Builder
737 size: Int
738 max: Int
739 -> Result<(Bytes, Bool), Error> / <io>
740{
741 if size > max
742 {
743 Ok((bytes.slice (bytes.build body) 0 max, True))
744 }
745 else
746 {
747 use more <- result.try (read conn)
748 if bytes.length more == 0
749 {
750 Ok((bytes.build body, False))
751 }
752 else
753 {
754 until_close conn (bytes.append body more) (size + bytes.length more) max
755 }
756 }
757}
758
759fn chunks :: conn: Conn d: Dechunker -> Result<(Bytes, Bool), Error> / <io>
760{
761 if response.is_done d
762 {
763 Ok(response.body d)
764 }
765 else
766 {
767 use more <- result.try (read conn)
768 if bytes.length more == 0
769 {
770 Error(Malformed("the connection closed inside a chunked body"))
771 }
772 else
773 {
774 match response.feed d more
775 { Ok(d) -> chunks conn d, Error(e) -> Error(Malformed(e)) }
776 }
777 }
778}
779
780// ---- the streamed exchange (in its own actor) ----------------------------------
781
782fn stream_exchange ::
783 method: String
784 target: Url
785 headers: List<(String, String)>
786 body: Bytes
787 opts: Options
788 to: Subject<StreamEvent>
789 -> () / <io, proc>
790{
791 let outcome = match connected target opts
792 { Ok(conn) ->
793 {
794 let streamed = stream_round_trip conn method target headers body opts to
795 close conn
796 streamed
797 }
798 , Error(e) -> Error(e)
799 }
800 match outcome
801 { Ok(truncated) -> process.send to (Finished(truncated))
802 , Error(e) -> process.send to (Failed(e))
803 }
804}
805
806/// Sends the head once it is read, then the body as it arrives; whether
807/// the body was cut at `max_body`.
808fn stream_round_trip ::
809 conn: Conn
810 method: String
811 target: Url
812 headers: List<(String, String)>
813 body: Bytes
814 opts: Options
815 to: Subject<StreamEvent>
816 -> Result<Bool, Error> / <io, proc>
817{
818 use (head, framing, rest) <-
819 result.try (send_and_read_head conn method target headers body opts)
820 process.send to (Opened(head.status, head.headers))
821 let max = opts.max_body
822 match framing
823 { NoBody -> Ok(False)
824 , Length
825 ( n
826 ) -> if n > max
827 {
828 stream_exactly conn rest max True to
829 }
830 else
831 {
832 stream_exactly conn rest n False to
833 }
834 , UntilClose -> stream_until_close conn rest 0 max to
835 , Chunked -> match response.feed (response.dechunker max) rest
836 { Ok(d) -> stream_chunks conn d to, Error(e) -> Error(Malformed(e)) }
837 }
838}
839
840fn emit :: to: Subject<StreamEvent> data: Bytes -> () / <proc>
841{
842 if bytes.length data > 0
843 {
844 process.send to (Received(data))
845 }
846}
847
848/// Delivers the next `left` bytes, starting with `have`; `cut` is the
849/// answer once they are all delivered.
850fn stream_exactly ::
851 conn: Conn
852 have: Bytes
853 left: Int
854 cut: Bool
855 to: Subject<StreamEvent>
856 -> Result<Bool, Error> / <io, proc>
857{
858 let piece = bytes.slice have 0 left
859 emit to piece
860 let left = left - bytes.length piece
861 if left == 0
862 {
863 Ok(cut)
864 }
865 else
866 {
867 use more <- result.try (read conn)
868 if bytes.length more == 0
869 {
870 Error(Malformed("the connection closed before the end of the body"))
871 }
872 else
873 {
874 stream_exactly conn more left cut to
875 }
876 }
877}
878
879/// Delivers what arrives until the server closes, `size` bytes so far.
880fn stream_until_close ::
881 conn: Conn
882 have: Bytes
883 size: Int
884 max: Int
885 to: Subject<StreamEvent>
886 -> Result<Bool, Error> / <io, proc>
887{
888 let room = max - size
889 if bytes.length have > room
890 {
891 emit to (bytes.slice have 0 room)
892 Ok(True)
893 }
894 else
895 {
896 emit to have
897 use more <- result.try (read conn)
898 if bytes.length more == 0
899 {
900 Ok(False)
901 }
902 else
903 {
904 stream_until_close conn more (size + bytes.length have) max to
905 }
906 }
907}
908
909fn stream_chunks ::
910 conn: Conn
911 d: Dechunker
912 to: Subject<StreamEvent>
913 -> Result<Bool, Error> / <io, proc>
914{
915 let (data, d) = response.take d
916 emit to data
917 if response.is_done d
918 {
919 Ok((response.body d).1)
920 }
921 else
922 {
923 use more <- result.try (read conn)
924 if bytes.length more == 0
925 {
926 Error(Malformed("the connection closed inside a chunked body"))
927 }
928 else
929 {
930 match response.feed d more
931 { Ok(d) -> stream_chunks conn d to, Error(e) -> Error(Malformed(e)) }
932 }
933 }
934}
935
936// ---- the connection ------------------------------------------------------------
937
938/// Up to 64 KiB; empty at the end. A TLS stream that ends without
939/// `close_notify` (`Truncated`) is an end too: the framing says whether
940/// the body was whole.
941fn read :: conn: Conn -> Result<Bytes, Error> / <io>
942{
943 match conn
944 { Plain(tcp) -> net.read tcp 65536 |> result.map_error Connect
945 , Secure(stream) -> match tls.read stream 65536
946 { Ok(b) -> Ok(b)
947 , Error(Truncated) -> Ok(bytes.empty ())
948 , Error(e) -> Error(Tls(e))
949 }
950 }
951}
952
953fn write :: conn: Conn data: Bytes -> Result<(), Error> / <io>
954{
955 match conn
956 { Plain(tcp) -> net.write tcp data |> result.map_error Connect
957 , Secure(stream) -> tls.write stream data |> result.map_error Tls
958 }
959}
960
961fn close :: conn: Conn -> () / <io>
962{
963 match conn { Plain(tcp) -> net.close tcp, Secure(stream) -> tls.close stream }
964}