rong_rt 0.3.1

Async runtime, HTTP client, and platform services for RongJS
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
use bytes::Bytes;
use http::Request as HttpRequest;
use http::header;
use http_body_util::{BodyExt, Full};
use std::io::Error;
use tokio::io::AsyncWriteExt;
use tokio::sync::oneshot;

use crate::client::{
    HttpBody, RequestTimeouts, send_request_with_shared_abort, shared_abort_signal,
};
use tokio::time::{Duration, Instant};

const DEFAULT_DOWNLOAD_SMALL_THRESHOLD: usize = 64 * 1024;
const MAX_DOWNLOAD_REDIRECTS: usize = 10;

pub struct DownloadOptions {
    url: String,
    dest: std::path::PathBuf,
    sink: Option<Box<dyn BodySink>>,
    request_timeout: Option<Duration>,
    connect_timeout: Option<Duration>,
}

impl DownloadOptions {
    /// Build download options for a target URL and destination file path.
    pub fn new(url: impl Into<String>, dest: impl Into<std::path::PathBuf>) -> Self {
        Self {
            url: url.into(),
            dest: dest.into(),
            sink: None,
            request_timeout: None,
            connect_timeout: None,
        }
    }

    /// Mirror each downloaded chunk into an additional sink.
    pub fn with_sink(mut self, sink: Box<dyn BodySink>) -> Self {
        self.sink = Some(sink);
        self
    }

    /// Override the request timeout for this download.
    pub fn with_request_timeout(mut self, timeout: Duration) -> Self {
        self.request_timeout = Some(timeout);
        self
    }

    /// Override the socket-connect timeout for this download.
    pub fn with_connect_timeout(mut self, timeout: Duration) -> Self {
        self.connect_timeout = Some(timeout);
        self
    }

    fn timeouts(&self) -> RequestTimeouts {
        RequestTimeouts {
            request_timeout: self.request_timeout,
            connect_timeout: self.connect_timeout,
        }
    }
}

pub trait BodySink: Send {
    fn write(&mut self, chunk: &[u8]) -> Result<(), String>;
    fn close(&mut self, result: &Result<(), String>);
}

/// Download a resource directly on the current task.
pub async fn download(
    options: DownloadOptions,
    abort_rx: Option<oneshot::Receiver<()>>,
) -> Result<(), String> {
    let timeouts = options.timeouts();
    download_resource(
        &options.url,
        &options.dest,
        abort_rx,
        options.sink,
        timeouts,
    )
    .await
}

/// Spawn a background download and receive completion through a oneshot channel.
pub fn spawn_download(
    options: DownloadOptions,
    abort_rx: Option<oneshot::Receiver<()>>,
) -> Result<oneshot::Receiver<Result<(), String>>, String> {
    let timeouts = options.timeouts();
    request_download_inner(options.url, options.dest, abort_rx, options.sink, timeouts)
}

pub fn request_download(
    url: impl Into<String>,
    dest: impl Into<std::path::PathBuf>,
    abort_rx: Option<oneshot::Receiver<()>>,
    sink: Option<Box<dyn BodySink>>,
) -> Result<oneshot::Receiver<Result<(), String>>, String> {
    request_download_inner(url, dest, abort_rx, sink, RequestTimeouts::default())
}

fn request_download_inner(
    url: impl Into<String>,
    dest: impl Into<std::path::PathBuf>,
    abort_rx: Option<oneshot::Receiver<()>>,
    sink: Option<Box<dyn BodySink>>,
    timeouts: RequestTimeouts,
) -> Result<oneshot::Receiver<Result<(), String>>, String> {
    let (completion_tx, completion_rx) = oneshot::channel();

    let url = url.into();
    let dest = dest.into();
    let network_access_guard = crate::http::current_network_access_guard();
    crate::RongExecutor::global().spawn(async move {
        let res = crate::http::scope_network_access_guard_opt(
            network_access_guard,
            download_resource(&url, &dest, abort_rx, sink, timeouts),
        )
        .await;
        let _ = completion_tx.send(res);
    });

    Ok(completion_rx)
}

async fn download_resource(
    url: &str,
    dest: &std::path::PathBuf,
    abort_rx: Option<oneshot::Receiver<()>>,
    sink: Option<Box<dyn BodySink>>,
    timeouts: RequestTimeouts,
) -> Result<(), String> {
    let mut sink_opt = sink;

    if let Some(parent) = dest.parent()
        && let Err(e) = tokio::fs::create_dir_all(parent).await
    {
        return finalize_sink(sink_opt, Err(format!("create dir: {}", e)));
    }

    let temp_path = dest.with_extension("part");
    let mut file = match tokio::fs::File::create(&temp_path).await {
        Ok(f) => f,
        Err(e) => return finalize_sink(sink_opt, Err(format!("open: {}", e))),
    };

    let small_threshold = DEFAULT_DOWNLOAD_SMALL_THRESHOLD;
    let mut current_url = url.to_string();
    let mut redirect_count = 0usize;
    let abort_signal = shared_abort_signal(abort_rx);
    let request_deadline = timeouts
        .request_timeout
        .map(|timeout| Instant::now() + timeout);
    let resp = loop {
        let request = match build_download_request(&current_url) {
            Ok(request) => request,
            Err(e) => return finalize_sink(sink_opt, Err(e)),
        };
        if let Err(err) = crate::http::check_current_network_access(&request) {
            return finalize_sink(sink_opt, Err(err.to_string()));
        }
        let remaining_timeouts = match remaining_request_timeouts(timeouts, request_deadline) {
            Ok(timeouts) => timeouts,
            Err(e) => return finalize_sink(sink_opt, Err(e)),
        };
        let resp = match send_request_with_shared_abort(
            request,
            small_threshold,
            abort_signal.clone(),
            crate::client::DEFAULT_STREAM_COALESCE_TARGET,
            remaining_timeouts,
        )
        .await
        {
            Ok(r) => r,
            Err(e) => return finalize_sink(sink_opt, Err(e)),
        };

        if !is_download_redirect(resp.status) {
            break resp;
        }
        if redirect_count >= MAX_DOWNLOAD_REDIRECTS {
            return finalize_sink(sink_opt, Err("too many redirects".to_string()));
        }
        current_url = match redirect_target(&current_url, &resp.headers) {
            Ok(url) => url,
            Err(e) => return finalize_sink(sink_opt, Err(e)),
        };
        redirect_count += 1;
    };
    if !resp.status.is_success() {
        return finalize_sink(sink_opt, Err(format!("http status: {}", resp.status)));
    }

    let mut sink_active = true;
    let forward = |data: &[u8], sink_opt: &mut Option<Box<dyn BodySink>>, active: &mut bool| {
        if *active
            && let Some(sink) = sink_opt.as_mut()
            && let Err(err) = sink.write(data)
        {
            let sink_err = Err(err.clone());
            sink.close(&sink_err);
            *sink_opt = None;
            *active = false;
        }
    };

    match resp.body {
        HttpBody::Empty => {}
        HttpBody::Small(buf) => {
            forward(buf.as_ref(), &mut sink_opt, &mut sink_active);
            if let Err(e) = file.write_all(buf.as_ref()).await {
                return finalize_sink(sink_opt, Err(format!("write chunk: {}", e)));
            }
        }
        HttpBody::Stream(mut rx_body) => {
            while let Some(chunk_res) = rx_body.recv().await {
                let bytes = match chunk_res {
                    Ok(b) => b,
                    Err(e) => return finalize_sink(sink_opt, Err(e)),
                };

                forward(bytes.as_ref(), &mut sink_opt, &mut sink_active);

                if let Err(e) = file.write_all(bytes.as_ref()).await {
                    return finalize_sink(sink_opt, Err(format!("write chunk: {}", e)));
                }
            }
        }
    }

    if let Err(e) = file.flush().await {
        return finalize_sink(sink_opt, Err(format!("flush: {}", e)));
    }
    drop(file);

    if let Err(e) = tokio::fs::rename(&temp_path, dest).await {
        return finalize_sink(sink_opt, Err(format!("rename: {}", e)));
    }

    finalize_sink(sink_opt, Ok(()))
}

fn remaining_request_timeouts(
    timeouts: RequestTimeouts,
    deadline: Option<Instant>,
) -> Result<RequestTimeouts, String> {
    let request_timeout = match deadline {
        Some(deadline) => {
            let now = Instant::now();
            if now >= deadline {
                return Err("request timeout".to_string());
            }
            Some(deadline - now)
        }
        None => timeouts.request_timeout,
    };

    Ok(RequestTimeouts {
        request_timeout,
        connect_timeout: timeouts.connect_timeout,
    })
}

fn finalize_sink(
    sink_opt: Option<Box<dyn BodySink>>,
    res: Result<(), String>,
) -> Result<(), String> {
    if let Some(mut sink) = sink_opt {
        sink.close(&res);
    }
    res
}

fn build_download_request(
    url: &str,
) -> Result<HttpRequest<http_body_util::combinators::BoxBody<Bytes, Error>>, String> {
    let mut builder = HttpRequest::builder()
        .method("GET")
        .uri(url)
        .header(header::ACCEPT, "*/*");
    if let Some(headers) = builder.headers_mut() {
        let ua = crate::get_user_agent();
        let value = header::HeaderValue::from_str(&ua)
            .map_err(|e| format!("invalid user agent header: {}", e))?;
        headers.insert(header::USER_AGENT, value);
    }
    let empty = Full::new(Bytes::new())
        .map_err(|_| Error::other("body error"))
        .boxed();
    builder
        .body(empty)
        .map_err(|e| format!("build request: {}", e))
}

fn is_download_redirect(status: http::StatusCode) -> bool {
    matches!(status.as_u16(), 301 | 302 | 303 | 307 | 308)
}

fn redirect_target(current_url: &str, headers: &http::HeaderMap) -> Result<String, String> {
    let location = headers
        .get(header::LOCATION)
        .ok_or_else(|| "redirect response missing Location header".to_string())?
        .to_str()
        .map_err(|e| format!("invalid redirect Location header: {}", e))?
        .trim();
    if location.is_empty() {
        return Err("redirect Location header is empty".to_string());
    }

    let base = url::Url::parse(current_url)
        .map_err(|e| format!("invalid current URL for redirect: {}", e))?;
    let next = base
        .join(location)
        .map_err(|e| format!("invalid redirect URL: {}", e))?;
    match next.scheme() {
        "http" | "https" => Ok(next.to_string()),
        scheme => Err(format!("unsupported redirect URL scheme: {}", scheme)),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;

    struct DenyExampleGuard;

    impl crate::http::NetworkAccessGuard for DenyExampleGuard {
        fn check_access(&self, uri: &crate::http::Uri) -> Result<(), crate::http::HttpError> {
            if uri.host() == Some("denied.example.com") {
                return Err(crate::http::HttpError::access_denied(
                    "network access denied",
                ));
            }
            Ok(())
        }
    }

    // Pool starts lazily on first spawn/handle; nothing to do here.

    async fn spawn_file_server(content: &'static [u8]) -> std::net::SocketAddr {
        use axum::Router;
        use axum::routing::get;

        let app = Router::new().route("/file", get(move || async move { content }));
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        tokio::spawn(async move {
            axum::serve(listener, app).await.unwrap();
        });
        addr
    }

    async fn spawn_slow_server(delay_ms: u64) -> std::net::SocketAddr {
        use axum::Router;
        use axum::routing::get;

        let app = Router::new().route(
            "/slow",
            get(move || async move {
                tokio::time::sleep(Duration::from_millis(delay_ms)).await;
                "data"
            }),
        );
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        tokio::spawn(async move {
            axum::serve(listener, app).await.unwrap();
        });
        addr
    }

    #[test]
    fn spawn_download_with_options_succeeds() {
        let _guard = crate::client::test_guard();
        let handle = crate::RongExecutor::global().handle();
        handle.block_on(async {
            let content = b"hello download";
            let addr = spawn_file_server(content).await;
            let url = format!("http://{}/file", addr);

            let dest = std::env::temp_dir().join(format!(
                "rong_dl_test_{}.bin",
                std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap()
                    .subsec_nanos()
            ));

            let rx = spawn_download(
                DownloadOptions::new(&url, &dest)
                    .with_request_timeout(Duration::from_secs(5))
                    .with_connect_timeout(Duration::from_secs(1)),
                None,
            )
            .expect("should queue download");
            rx.await
                .expect("channel dropped")
                .expect("download should succeed");

            let written = tokio::fs::read(&dest).await.expect("file should exist");
            assert_eq!(written, content);
            let _ = tokio::fs::remove_file(&dest).await;
        });
    }

    #[test]
    fn download_convenience_succeeds() {
        let _guard = crate::client::test_guard();
        let handle = crate::RongExecutor::global().handle();
        handle.block_on(async {
            let content = b"hello direct download";
            let addr = spawn_file_server(content).await;
            let url = format!("http://{}/file", addr);
            let dest = std::env::temp_dir().join(format!(
                "rong_dl_direct_test_{}.bin",
                std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap()
                    .subsec_nanos()
            ));

            download(DownloadOptions::new(&url, &dest), None)
                .await
                .expect("download should succeed");

            let written = tokio::fs::read(&dest).await.expect("file should exist");
            assert_eq!(written, content);
            let _ = tokio::fs::remove_file(&dest).await;
        });
    }

    #[test]
    fn scoped_network_access_guard_blocks_spawn_download() {
        let handle = crate::RongExecutor::global().handle();
        handle.block_on(async {
            let dest = std::env::temp_dir().join(format!(
                "rong_dl_denied_test_{}.bin",
                std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap()
                    .subsec_nanos()
            ));

            let err = crate::http::scope_network_access_guard(Arc::new(DenyExampleGuard), async {
                let rx = spawn_download(
                    DownloadOptions::new("http://denied.example.com/file", &dest),
                    None,
                )
                .expect("should queue download");
                rx.await
                    .expect("channel dropped")
                    .expect_err("download should be denied")
            })
            .await;

            assert_eq!(err, "network access denied");
            let _ = tokio::fs::remove_file(&dest).await;
            let _ = tokio::fs::remove_file(dest.with_extension("part")).await;
        });
    }

    #[test]
    fn redirect_target_resolves_relative_location() {
        let mut headers = http::HeaderMap::new();
        headers.insert(
            header::LOCATION,
            header::HeaderValue::from_static("/next.jpg"),
        );
        let target = redirect_target("https://example.com/a/start.jpg", &headers).unwrap();
        assert_eq!(target, "https://example.com/next.jpg");
    }

    #[test]
    fn redirect_target_rejects_non_http_scheme() {
        let mut headers = http::HeaderMap::new();
        headers.insert(
            header::LOCATION,
            header::HeaderValue::from_static("file:///tmp/a.jpg"),
        );
        let err = redirect_target("https://example.com/a/start.jpg", &headers).unwrap_err();
        assert!(err.contains("unsupported redirect URL scheme"));
    }

    #[test]
    fn download_follows_redirect() {
        let _guard = crate::client::test_guard();
        let handle = crate::RongExecutor::global().handle();
        handle.block_on(async {
            use axum::Router;
            use axum::response::IntoResponse;
            use axum::routing::get;

            let content = b"redirected download";
            let app = Router::new()
                .route("/file", get(move || async move { content }))
                .route(
                    "/redirect",
                    get(|| async {
                        (
                            http::StatusCode::FOUND,
                            [(http::header::LOCATION, "/file")],
                            "",
                        )
                            .into_response()
                    }),
                );
            let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
            let addr = listener.local_addr().unwrap();
            tokio::spawn(async move {
                axum::serve(listener, app).await.unwrap();
            });

            let url = format!("http://{}/redirect", addr);
            let dest = std::env::temp_dir().join(format!(
                "rong_dl_redirect_test_{}.bin",
                std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap()
                    .subsec_nanos()
            ));

            download(DownloadOptions::new(&url, &dest), None)
                .await
                .expect("download should follow redirect");

            let written = tokio::fs::read(&dest).await.expect("file should exist");
            assert_eq!(written, content);
            let _ = tokio::fs::remove_file(&dest).await;
        });
    }

    #[test]
    fn download_abort_survives_redirect() {
        let _guard = crate::client::test_guard();
        let handle = crate::RongExecutor::global().handle();
        handle.block_on(async {
            use axum::Router;
            use axum::response::IntoResponse;
            use axum::routing::get;

            let app = Router::new()
                .route(
                    "/slow-file",
                    get(|| async {
                        tokio::time::sleep(Duration::from_millis(300)).await;
                        "redirected download"
                    }),
                )
                .route(
                    "/redirect",
                    get(|| async {
                        (
                            http::StatusCode::FOUND,
                            [(http::header::LOCATION, "/slow-file")],
                            "",
                        )
                            .into_response()
                    }),
                );
            let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
            let addr = listener.local_addr().unwrap();
            tokio::spawn(async move {
                axum::serve(listener, app).await.unwrap();
            });

            let url = format!("http://{}/redirect", addr);
            let dest = std::env::temp_dir().join(format!(
                "rong_dl_redirect_abort_test_{}.bin",
                std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap()
                    .subsec_nanos()
            ));
            let (abort_tx, abort_rx) = oneshot::channel();

            let rx = spawn_download(DownloadOptions::new(&url, &dest), Some(abort_rx))
                .expect("should queue download");
            tokio::time::sleep(Duration::from_millis(50)).await;
            let _ = abort_tx.send(());

            let err = rx
                .await
                .expect("channel dropped")
                .expect_err("download should abort");
            assert!(
                err.contains("aborted"),
                "expected abort error, got: {}",
                err
            );
            let _ = tokio::fs::remove_file(&dest).await;
        });
    }

    #[test]
    fn download_redirects_share_request_timeout_budget() {
        let _guard = crate::client::test_guard();
        let handle = crate::RongExecutor::global().handle();
        handle.block_on(async {
            use axum::Router;
            use axum::response::IntoResponse;
            use axum::routing::get;

            let app = Router::new()
                .route(
                    "/slow-file",
                    get(|| async {
                        tokio::time::sleep(Duration::from_millis(100)).await;
                        "redirected download"
                    }),
                )
                .route(
                    "/redirect",
                    get(|| async {
                        tokio::time::sleep(Duration::from_millis(100)).await;
                        (
                            http::StatusCode::FOUND,
                            [(http::header::LOCATION, "/slow-file")],
                            "",
                        )
                            .into_response()
                    }),
                );
            let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
            let addr = listener.local_addr().unwrap();
            tokio::spawn(async move {
                axum::serve(listener, app).await.unwrap();
            });

            let url = format!("http://{}/redirect", addr);
            let dest = std::env::temp_dir().join(format!(
                "rong_dl_redirect_timeout_test_{}.bin",
                std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap()
                    .subsec_nanos()
            ));

            let err = download(
                DownloadOptions::new(&url, &dest)
                    .with_request_timeout(Duration::from_millis(150))
                    .with_connect_timeout(Duration::from_secs(1)),
                None,
            )
            .await
            .expect_err("redirect chain should exhaust the timeout budget");

            assert!(
                err.contains("timeout"),
                "expected timeout error, got: {}",
                err
            );
            let _ = tokio::fs::remove_file(&dest).await;
        });
    }

    #[test]
    fn download_with_timeout_expires() {
        let _guard = crate::client::test_guard();
        let handle = crate::RongExecutor::global().handle();
        handle.block_on(async {
            let addr = spawn_slow_server(300).await;
            let url = format!("http://{}/slow", addr);
            let dest = std::env::temp_dir().join("rong_dl_timeout_test.bin");

            let rx = spawn_download(
                DownloadOptions::new(&url, &dest)
                    .with_request_timeout(Duration::from_millis(10))
                    .with_connect_timeout(Duration::from_secs(1)),
                None,
            )
            .expect("should queue download");
            let err = rx
                .await
                .expect("channel dropped")
                .expect_err("should time out");
            assert!(
                err.contains("timeout"),
                "expected timeout error, got: {}",
                err
            );
        });
    }
}