devsandbox 0.3.2

Manage devcontainer-style sandboxes on docker, podman, or Apple container — many throwaway instances per repo from one TOML config.
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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
//! devcontainer `features` fetch + metadata.
//!
//! A feature is an OCI artifact (e.g. `ghcr.io/devcontainers/features/node:1`):
//! a tar layer holding `devcontainer-feature.json` + `install.sh`. This module
//! parses a feature ref, pulls the artifact from its registry by shelling out to
//! `curl` (mirroring the project's shell-out-to-docker/git philosophy; no new
//! crates), caches the extraction per user, reads the metadata, and computes an
//! install order from `installsAfter`. Building the derived image lives in the
//! `run` command (step 3); this module stops at a [`ResolvedFeature`].

use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};
use std::process::Command;

use anyhow::{anyhow, bail, Context, Result};
use serde::Deserialize;

use crate::config::Mount;

/// The layer mediaType carrying a feature's tarball.
const FEATURE_LAYER_MEDIA_TYPE: &str = "application/vnd.devcontainers.layer.v1+tar";
/// The manifest mediaType we request.
const MANIFEST_MEDIA_TYPE: &str = "application/vnd.oci.image.manifest.v1+json";
/// Cap the TCP/TLS handshake so an unreachable host fails fast instead of
/// blocking a fetch indefinitely.
const CONNECT_TIMEOUT_SECS: &str = "10";
/// Cap a whole small request (headers, manifest, token). Blobs use a stall
/// guard instead so large layers aren't cut off mid-download.
const REQUEST_MAX_TIME_SECS: &str = "60";

/// A parsed OCI feature reference.
///
/// `ghcr.io/devcontainers/features/node:1` becomes `registry = "ghcr.io"`,
/// `path = "devcontainers/features/node"`, `id = "node"`, `tag = "1"`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FeatureRef {
    /// Registry host (with optional port), e.g. `ghcr.io`.
    pub registry: String,
    /// Repository path under the registry, e.g. `devcontainers/features/node`.
    pub path: String,
    /// Last path segment, e.g. `node`.
    pub id: String,
    /// Tag, e.g. `1`. Defaults to `latest` when absent.
    pub tag: String,
}

impl FeatureRef {
    /// Parse an OCI feature ref. Rejects local paths, refs without a registry
    /// host, and https tarball URLs with a clear error.
    pub fn parse(input: &str) -> Result<Self> {
        let input = input.trim();
        if input.is_empty() {
            bail!("empty feature reference");
        }
        if input.starts_with("http://") || input.starts_with("https://") {
            bail!(
                "feature `{input}`: tarball/URL features are not supported; use an OCI ref like `ghcr.io/devcontainers/features/node:1`"
            );
        }
        if input.starts_with('.') || input.starts_with('/') {
            bail!(
                "feature `{input}`: local-path features are not supported; use an OCI ref like `ghcr.io/devcontainers/features/node:1`"
            );
        }

        // The tag is after the last `:` only when that `:` sits after the last
        // `/` — otherwise the colon is a registry port (`localhost:5000/x`).
        let (name, tag) = match input.rfind(':') {
            Some(colon) if colon > input.rfind('/').unwrap_or(0) => {
                (&input[..colon], input[colon + 1..].to_string())
            }
            _ => (input, "latest".to_string()),
        };
        if tag.is_empty() {
            bail!("feature `{input}`: empty tag");
        }

        let mut segments = name.splitn(2, '/');
        let registry = segments
            .next()
            .filter(|s| !s.is_empty())
            .with_context(|| format!("feature `{input}`: missing registry host"))?;
        let path = segments.next().unwrap_or("");

        // A registry host must look like one: it has a `.` (domain) or `:`
        // (host:port) or is `localhost`, and the ref must carry a path after it.
        let looks_like_host =
            registry.contains('.') || registry.contains(':') || registry == "localhost";
        if path.is_empty() || !looks_like_host {
            bail!(
                "feature `{input}`: not an OCI reference (expected `<registry>/<path>[:<tag>]`, e.g. `ghcr.io/devcontainers/features/node:1`)"
            );
        }

        let id = path
            .rsplit('/')
            .next()
            .filter(|s| !s.is_empty())
            .with_context(|| format!("feature `{input}`: empty id"))?
            .to_string();

        Ok(FeatureRef {
            registry: registry.to_string(),
            path: path.to_string(),
            id,
            tag,
        })
    }

    /// The ref with its tag stripped, e.g. `ghcr.io/devcontainers/features/node`.
    /// Used to match `installsAfter` entries, which are written without a tag.
    pub fn without_tag(&self) -> String {
        format!("{}/{}", self.registry, self.path)
    }

    /// The full ref including tag, e.g. `ghcr.io/devcontainers/features/node:1`.
    pub fn full(&self) -> String {
        format!("{}/{}:{}", self.registry, self.path, self.tag)
    }
}

/// Base cache dir (`$XDG_CACHE_HOME/devsandbox/features`, falling back to
/// `$HOME/.cache/devsandbox/features`). Mirrors `State::path`'s style.
fn cache_root() -> Result<PathBuf> {
    let base = std::env::var_os("XDG_CACHE_HOME")
        .map(PathBuf::from)
        .or_else(|| std::env::var_os("HOME").map(|h| PathBuf::from(h).join(".cache")))
        .context("cannot determine user cache dir ($XDG_CACHE_HOME or $HOME)")?;
    Ok(base.join("devsandbox/features"))
}

/// A fresh build-context dir for a sandbox's derived image build, under the
/// features cache root: `<cache_root>/build-<sandbox>`. Recreated each call
/// (any prior contents are removed) so a rebuild starts clean.
pub fn build_context_dir(sandbox: &str) -> Result<PathBuf> {
    let dir = cache_root()?.join(format!("build-{sandbox}"));
    if dir.exists() {
        std::fs::remove_dir_all(&dir)
            .with_context(|| format!("cannot clear {}", dir.display()))?;
    }
    std::fs::create_dir_all(&dir).with_context(|| format!("cannot create {}", dir.display()))?;
    Ok(dir)
}

/// A feature's `devcontainer-feature.json`. Tolerant of unknown fields and
/// missing values so new schema keys don't break parsing.
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FeatureMetadata {
    #[serde(default)]
    pub id: Option<String>,
    #[serde(default)]
    pub version: Option<String>,
    #[serde(default)]
    pub name: Option<String>,
    #[serde(default)]
    pub options: BTreeMap<String, FeatureOption>,
    #[serde(default)]
    pub container_env: BTreeMap<String, String>,
    #[serde(default)]
    pub installs_after: Vec<String>,
    #[serde(default)]
    pub entrypoint: Option<String>,
    /// Mounts the feature asks the container to carry (e.g.
    /// docker-outside-of-docker's host socket bind). Applied at `run`, where a
    /// sandbox mount with the same target takes precedence.
    #[serde(default)]
    pub mounts: Vec<Mount>,
    #[serde(default, rename = "documentationURL")]
    pub documentation_url: Option<String>,
}

/// One entry in a feature's `options` map. Only `default` is acted on for now;
/// other keys (`type`, `description`, `proposals`, `enum`) are tolerated.
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FeatureOption {
    #[serde(default)]
    pub default: Option<serde_json::Value>,
}

impl FeatureMetadata {
    fn parse(json: &str) -> Result<Self> {
        serde_json::from_str(json).context("invalid devcontainer-feature.json")
    }

    fn read(path: &Path) -> Result<Self> {
        let contents = std::fs::read_to_string(path)
            .with_context(|| format!("cannot read {}", path.display()))?;
        Self::parse(&contents).with_context(|| format!("in {}", path.display()))
    }
}

/// A fetched feature: its ref, the cache dir holding the extracted contents, and
/// the parsed metadata.
#[derive(Debug, Clone)]
pub struct ResolvedFeature {
    pub reference: FeatureRef,
    pub dir: PathBuf,
    pub metadata: FeatureMetadata,
}

/// Fetch a feature into the default per-user cache and read its metadata.
pub fn fetch(reference: &FeatureRef) -> Result<ResolvedFeature> {
    fetch_into(reference, &cache_root()?)
}

/// Like [`fetch`] but with an explicit cache root (for tests). The feature is
/// extracted into `<cache_root>/<registry>/<path>/<tag>/`.
pub fn fetch_into(reference: &FeatureRef, cache_root: &Path) -> Result<ResolvedFeature> {
    let dir = cache_root
        .join(&reference.registry)
        .join(&reference.path)
        .join(&reference.tag);
    let metadata_path = dir.join("devcontainer-feature.json");

    // Offline reuse: a populated cache is taken as-is (mutable tags won't
    // auto-refresh; clear the cache dir to force a re-pull).
    if !metadata_path.exists() {
        download_and_extract(reference, &dir)
            .with_context(|| format!("fetching feature `{}`", reference.full()))?;
    }

    let metadata = FeatureMetadata::read(&metadata_path)?;
    Ok(ResolvedFeature {
        reference: reference.clone(),
        dir,
        metadata,
    })
}

/// Pull the manifest + layer blob and extract the tarball into `dir`. Extraction
/// goes to a sibling temp dir that is renamed into place, so a killed fetch never
/// leaves a half-populated cache.
fn download_and_extract(reference: &FeatureRef, dir: &Path) -> Result<()> {
    let token = fetch_token(reference)?;
    let manifest = fetch_manifest(reference, token.as_deref())?;
    let digest = feature_layer_digest(&manifest)
        .with_context(|| format!("feature `{}`: no feature layer in manifest", reference.full()))?;

    let parent = dir
        .parent()
        .ok_or_else(|| anyhow!("invalid cache dir {}", dir.display()))?;
    std::fs::create_dir_all(parent)
        .with_context(|| format!("cannot create {}", parent.display()))?;

    // Temp dir + temp tarball as siblings of the target, for an atomic rename.
    let stamp = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_nanos())
        .unwrap_or(0);
    let tmp_dir = parent.join(format!(".{}-{}.tmp", reference.tag, stamp));
    let tmp_tar = parent.join(format!(".{}-{}.tar", reference.tag, stamp));
    // Best-effort cleanup of leftovers from a previous crash.
    let _ = std::fs::remove_dir_all(&tmp_dir);
    let _ = std::fs::remove_file(&tmp_tar);
    std::fs::create_dir_all(&tmp_dir)
        .with_context(|| format!("cannot create {}", tmp_dir.display()))?;

    let result = (|| -> Result<()> {
        download_blob(reference, &digest, token.as_deref(), &tmp_tar)?;
        // `tar` auto-detects compression (feature layers are gzip tarballs).
        run_checked(
            Command::new("tar").arg("-xf").arg(&tmp_tar).arg("-C").arg(&tmp_dir),
            "tar",
        )?;
        Ok(())
    })();

    if let Err(e) = result {
        let _ = std::fs::remove_dir_all(&tmp_dir);
        let _ = std::fs::remove_file(&tmp_tar);
        return Err(e);
    }
    let _ = std::fs::remove_file(&tmp_tar);

    // Atomic publish. If another concurrent fetch won the race, keep theirs.
    if dir.exists() {
        let _ = std::fs::remove_dir_all(&tmp_dir);
    } else if let Err(e) = std::fs::rename(&tmp_dir, dir) {
        let _ = std::fs::remove_dir_all(&tmp_dir);
        return Err(e).with_context(|| format!("cannot publish cache dir {}", dir.display()));
    }
    Ok(())
}

/// Obtain a pull token if the registry demands one. Requests the manifest
/// anonymously first; on a 401 it parses the `WWW-Authenticate: Bearer …`
/// challenge and fetches a token. Returns `None` when anonymous access works.
fn fetch_token(reference: &FeatureRef) -> Result<Option<String>> {
    let manifest_url = format!(
        "https://{}/v2/{}/manifests/{}",
        reference.registry, reference.path, reference.tag
    );
    let headers = curl_headers(&[
        "-H",
        &format!("Accept: {MANIFEST_MEDIA_TYPE}"),
        &manifest_url,
    ])?;
    let (status, header_lines) = split_status(&headers);
    if status != 401 {
        return Ok(None);
    }
    let challenge = header_lines
        .iter()
        .find_map(|l| l.strip_prefix_ci("www-authenticate:"))
        .map(str::trim)
        .with_context(|| {
            format!("feature `{}`: 401 without a WWW-Authenticate challenge", reference.full())
        })?;
    let challenge = parse_bearer_challenge(challenge, &reference.path)?;
    let token_url = format!(
        "{}?service={}&scope={}",
        challenge.realm,
        urlencode(&challenge.service),
        urlencode(&challenge.scope),
    );
    let body = curl_body(&[&token_url])?;
    let json: serde_json::Value =
        serde_json::from_str(&body).context("token endpoint returned invalid JSON")?;
    let token = json
        .get("token")
        .or_else(|| json.get("access_token"))
        .and_then(|v| v.as_str())
        .with_context(|| format!("feature `{}`: token endpoint had no token", reference.full()))?;
    Ok(Some(token.to_string()))
}

/// GET the manifest JSON, sending the bearer token when present.
fn fetch_manifest(reference: &FeatureRef, token: Option<&str>) -> Result<serde_json::Value> {
    let url = format!(
        "https://{}/v2/{}/manifests/{}",
        reference.registry, reference.path, reference.tag
    );
    let mut args: Vec<String> = vec!["-H".into(), format!("Accept: {MANIFEST_MEDIA_TYPE}")];
    if let Some(token) = token {
        args.push("-H".into());
        args.push(format!("Authorization: Bearer {token}"));
    }
    args.push(url);
    let refs: Vec<&str> = args.iter().map(String::as_str).collect();
    let body = curl_body(&refs)?;
    serde_json::from_str(&body).context("registry returned an invalid manifest")
}

/// GET a blob by digest, following redirects, writing the bytes to `out`.
fn download_blob(
    reference: &FeatureRef,
    digest: &str,
    token: Option<&str>,
    out: &Path,
) -> Result<()> {
    let url = format!(
        "https://{}/v2/{}/blobs/{}",
        reference.registry, reference.path, digest
    );
    let mut cmd = Command::new("curl");
    cmd.arg("-fsSL"); // -L: blob GETs redirect to storage.
    // Fail fast on a dead connect; abort if the transfer stalls (<1 B/s for
    // 30s) rather than a hard cap, so large layers can still complete.
    cmd.arg("--connect-timeout").arg(CONNECT_TIMEOUT_SECS);
    cmd.arg("--speed-limit").arg("1").arg("--speed-time").arg("30");
    if let Some(token) = token {
        cmd.arg("-H").arg(format!("Authorization: Bearer {token}"));
    }
    cmd.arg("-o").arg(out).arg(&url);
    run_checked(&mut cmd, "curl")?;
    Ok(())
}

/// Find the feature layer's digest in a manifest.
fn feature_layer_digest(manifest: &serde_json::Value) -> Option<String> {
    manifest
        .get("layers")?
        .as_array()?
        .iter()
        .find(|layer| {
            layer.get("mediaType").and_then(|v| v.as_str()) == Some(FEATURE_LAYER_MEDIA_TYPE)
        })
        .and_then(|layer| layer.get("digest"))
        .and_then(|v| v.as_str())
        .map(str::to_string)
}

/// A parsed `WWW-Authenticate: Bearer …` challenge.
#[derive(Debug, PartialEq, Eq)]
struct BearerChallenge {
    realm: String,
    service: String,
    scope: String,
}

/// Parse a `Bearer realm="…",service="…",scope="…"` challenge. `scope` defaults
/// to `repository:<path>:pull` when the challenge omits it.
fn parse_bearer_challenge(value: &str, path: &str) -> Result<BearerChallenge> {
    let params = value
        .strip_prefix("Bearer ")
        .or_else(|| value.strip_prefix("bearer "))
        .with_context(|| format!("unsupported auth challenge: {value}"))?;

    let mut map: BTreeMap<String, String> = BTreeMap::new();
    for part in split_challenge_params(params) {
        if let Some((k, v)) = part.split_once('=') {
            let v = v.trim().trim_matches('"');
            map.insert(k.trim().to_ascii_lowercase(), v.to_string());
        }
    }

    let realm = map
        .get("realm")
        .cloned()
        .with_context(|| format!("auth challenge missing realm: {value}"))?;
    let service = map.get("service").cloned().unwrap_or_default();
    let scope = map
        .get("scope")
        .cloned()
        .unwrap_or_else(|| format!("repository:{path}:pull"));
    Ok(BearerChallenge {
        realm,
        service,
        scope,
    })
}

/// Split a challenge parameter list on commas that sit outside quotes, so a
/// `scope="repository:x:pull,push"` value stays intact.
fn split_challenge_params(params: &str) -> Vec<String> {
    let mut out = Vec::new();
    let mut cur = String::new();
    let mut in_quotes = false;
    for c in params.chars() {
        match c {
            '"' => {
                in_quotes = !in_quotes;
                cur.push(c);
            }
            ',' if !in_quotes => {
                out.push(std::mem::take(&mut cur));
            }
            _ => cur.push(c),
        }
    }
    if !cur.trim().is_empty() {
        out.push(cur);
    }
    out
}

/// Minimal percent-encoding for query values (token scope/service).
fn urlencode(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for b in s.bytes() {
        match b {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
                out.push(b as char)
            }
            _ => out.push_str(&format!("%{b:02X}")),
        }
    }
    out
}

/// Run `curl -sS -D - -o /dev/null <args>` and return the captured response
/// headers (status line included), for inspecting a 401 challenge.
fn curl_headers(args: &[&str]) -> Result<String> {
    let mut cmd = Command::new("curl");
    cmd.arg("-sS").arg("-D").arg("-").arg("-o").arg("/dev/null");
    cmd.arg("--connect-timeout").arg(CONNECT_TIMEOUT_SECS);
    cmd.arg("--max-time").arg(REQUEST_MAX_TIME_SECS);
    cmd.args(args);
    capture_stdout(&mut cmd, "curl")
}

/// Run `curl -fsSL <args>` and return the response body.
fn curl_body(args: &[&str]) -> Result<String> {
    let mut cmd = Command::new("curl");
    cmd.arg("-fsSL");
    cmd.arg("--connect-timeout").arg(CONNECT_TIMEOUT_SECS);
    cmd.arg("--max-time").arg(REQUEST_MAX_TIME_SECS);
    cmd.args(args);
    capture_stdout(&mut cmd, "curl")
}

/// Capture stdout; non-zero exit is an error with the first stderr line folded
/// in (mirrors `runtime::output_quiet`).
fn capture_stdout(cmd: &mut Command, name: &str) -> Result<String> {
    let out = cmd
        .output()
        .with_context(|| format!("failed to run {name} (is it installed?)"))?;
    if !out.status.success() {
        let stderr = String::from_utf8_lossy(&out.stderr);
        bail!(
            "{name} exited with status {}: {}",
            out.status.code().unwrap_or(1),
            stderr.lines().next().unwrap_or("non-zero exit").trim()
        );
    }
    Ok(String::from_utf8_lossy(&out.stdout).to_string())
}

/// Run a command, treating a non-zero exit as an error (stderr inherited).
fn run_checked(cmd: &mut Command, name: &str) -> Result<()> {
    let status = cmd
        .status()
        .with_context(|| format!("failed to run {name} (is it installed?)"))?;
    if !status.success() {
        bail!("{name} exited with status {}", status.code().unwrap_or(1));
    }
    Ok(())
}

/// Split a raw HTTP header dump into `(status_code, header_lines)`. `curl -D -`
/// may print multiple blocks on redirects; the last block's status wins.
fn split_status(headers: &str) -> (u16, Vec<&str>) {
    let mut status = 0u16;
    let mut lines = Vec::new();
    for line in headers.lines() {
        let line = line.trim_end();
        if let Some(rest) = line.strip_prefix("HTTP/") {
            // e.g. "HTTP/2 401" or "HTTP/1.1 401 Unauthorized"
            if let Some(code) = rest.split_whitespace().nth(1).and_then(|c| c.parse().ok()) {
                status = code;
                lines.clear();
                continue;
            }
        }
        if !line.is_empty() {
            lines.push(line);
        }
    }
    (status, lines)
}

/// Case-insensitive header-prefix helper.
trait StripPrefixCi {
    fn strip_prefix_ci(&self, prefix: &str) -> Option<&str>;
}

impl StripPrefixCi for &str {
    fn strip_prefix_ci(&self, prefix: &str) -> Option<&str> {
        // Compare bytes before slicing: a match implies the prefix region is
        // ASCII, so the subsequent str slice cannot split a UTF-8 char.
        let head = self.as_bytes().get(..prefix.len())?;
        if head.eq_ignore_ascii_case(prefix.as_bytes()) {
            Some(&self[prefix.len()..])
        } else {
            None
        }
    }
}

/// Order features so every feature installs after the ones it declares in
/// `installsAfter`. Topological sort with an alphabetical (by full ref)
/// tie-break among ready nodes each round, for determinism. `installsAfter`
/// entries not present in the requested set are ignored (soft deps). A cycle is
/// an error naming its members.
///
/// `installsAfter` entries are written without a tag; each is matched against a
/// candidate's ref-sans-tag first, then its bare id (the official CLI matches
/// loosely).
pub fn install_order(features: Vec<ResolvedFeature>) -> Result<Vec<ResolvedFeature>> {
    let keys: Vec<String> = features.iter().map(|f| f.reference.full()).collect();

    // Map each dependency selector (ref-sans-tag or bare id) to the feature
    // key(s) it resolves to, so `installsAfter` can be matched loosely.
    let mut by_sans_tag: BTreeMap<String, String> = BTreeMap::new();
    let mut by_id: BTreeMap<String, Vec<String>> = BTreeMap::new();
    for f in &features {
        by_sans_tag.insert(f.reference.without_tag(), f.reference.full());
        by_id.entry(f.reference.id.clone()).or_default().push(f.reference.full());
    }

    // Edges: `dep -> feature` (dep installs before feature).
    let mut deps: BTreeMap<String, BTreeSet<String>> = keys
        .iter()
        .map(|k| (k.clone(), BTreeSet::new()))
        .collect();
    for f in &features {
        let key = f.reference.full();
        for entry in &f.metadata.installs_after {
            let entry = entry.trim();
            // Match sans-tag first; fall back to bare id.
            if let Some(dep) = by_sans_tag.get(entry) {
                if *dep != key {
                    deps.get_mut(&key).unwrap().insert(dep.clone());
                }
            } else if let Some(dep_keys) = by_id.get(entry) {
                for dep in dep_keys {
                    if *dep != key {
                        deps.get_mut(&key).unwrap().insert(dep.clone());
                    }
                }
            }
            // else: soft dep not in the requested set — ignored.
        }
    }

    let mut features_by_key: BTreeMap<String, ResolvedFeature> =
        features.into_iter().map(|f| (f.reference.full(), f)).collect();

    let mut ordered = Vec::with_capacity(keys.len());
    let mut placed: BTreeSet<String> = BTreeSet::new();
    while placed.len() < keys.len() {
        // Ready = all deps already placed. `deps` keys are sorted (BTreeMap),
        // giving an alphabetical tie-break among ready nodes.
        let ready: Vec<String> = deps
            .iter()
            .filter(|(k, _)| !placed.contains(*k))
            .filter(|(_, d)| d.iter().all(|dep| placed.contains(dep)))
            .map(|(k, _)| k.clone())
            .collect();
        if ready.is_empty() {
            let cycle: Vec<String> = keys.iter().filter(|k| !placed.contains(*k)).cloned().collect();
            bail!("cyclic feature `installsAfter`: {}", cycle.join(", "));
        }
        for key in ready {
            ordered.push(features_by_key.remove(&key).unwrap());
            placed.insert(key);
        }
    }
    Ok(ordered)
}

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

    fn feature(reference: &str, installs_after: &[&str]) -> ResolvedFeature {
        let reference = FeatureRef::parse(reference).unwrap();
        let mut metadata = FeatureMetadata::default();
        metadata.id = Some(reference.id.clone());
        metadata.installs_after = installs_after.iter().map(|s| s.to_string()).collect();
        ResolvedFeature {
            reference,
            dir: PathBuf::new(),
            metadata,
        }
    }

    #[test]
    fn parse_full_ref_with_tag() {
        let r = FeatureRef::parse("ghcr.io/devcontainers/features/node:1").unwrap();
        assert_eq!(r.registry, "ghcr.io");
        assert_eq!(r.path, "devcontainers/features/node");
        assert_eq!(r.id, "node");
        assert_eq!(r.tag, "1");
        assert_eq!(r.without_tag(), "ghcr.io/devcontainers/features/node");
        assert_eq!(r.full(), "ghcr.io/devcontainers/features/node:1");
    }

    #[test]
    fn parse_defaults_tag_to_latest() {
        let r = FeatureRef::parse("ghcr.io/devcontainers/features/go").unwrap();
        assert_eq!(r.tag, "latest");
        assert_eq!(r.id, "go");
    }

    #[test]
    fn parse_registry_with_port() {
        let r = FeatureRef::parse("localhost:5000/team/feat:2").unwrap();
        assert_eq!(r.registry, "localhost:5000");
        assert_eq!(r.path, "team/feat");
        assert_eq!(r.id, "feat");
        assert_eq!(r.tag, "2");
    }

    #[test]
    fn parse_registry_with_port_no_tag() {
        // The colon is a port, not a tag: tag must default to latest.
        let r = FeatureRef::parse("localhost:5000/team/feat").unwrap();
        assert_eq!(r.registry, "localhost:5000");
        assert_eq!(r.path, "team/feat");
        assert_eq!(r.tag, "latest");
    }

    #[test]
    fn parse_rejects_local_paths() {
        for input in ["./x", "../x", "/abs/x"] {
            let err = FeatureRef::parse(input).unwrap_err().to_string();
            assert!(err.contains("local-path"), "{input}: {err}");
        }
    }

    #[test]
    fn parse_rejects_single_segment() {
        for input in ["node", "just-a-name"] {
            assert!(FeatureRef::parse(input).is_err(), "{input} should be rejected");
        }
    }

    #[test]
    fn parse_rejects_url_forms() {
        for input in [
            "https://example.com/feature.tgz",
            "http://example.com/feature.tar",
        ] {
            let err = FeatureRef::parse(input).unwrap_err().to_string();
            assert!(err.contains("tarball") || err.contains("URL"), "{input}: {err}");
        }
    }

    #[test]
    fn parse_rejects_hostless_ref() {
        // First segment has no `.`/`:` and isn't localhost -> not a host.
        let err = FeatureRef::parse("myorg/feature:1").unwrap_err().to_string();
        assert!(err.contains("not an OCI reference"), "{err}");
    }

    #[test]
    fn install_order_soft_dep() {
        // common-utils installs first; node declares installsAfter common-utils.
        let feats = vec![
            feature("ghcr.io/devcontainers/features/node:1", &[
                "ghcr.io/devcontainers/features/common-utils",
            ]),
            feature("ghcr.io/devcontainers/features/common-utils:2", &[]),
        ];
        let ordered = install_order(feats).unwrap();
        let ids: Vec<_> = ordered.iter().map(|f| f.reference.id.as_str()).collect();
        assert_eq!(ids, ["common-utils", "node"]);
    }

    #[test]
    fn install_order_matches_installs_after_by_bare_id() {
        // installsAfter uses a bare id ("common-utils"), not the full path.
        let feats = vec![
            feature("ghcr.io/devcontainers/features/node:1", &["common-utils"]),
            feature("ghcr.io/devcontainers/features/common-utils:2", &[]),
        ];
        let ordered = install_order(feats).unwrap();
        let ids: Vec<_> = ordered.iter().map(|f| f.reference.id.as_str()).collect();
        assert_eq!(ids, ["common-utils", "node"]);
    }

    #[test]
    fn install_order_alphabetical_tie_break() {
        // No deps between them: alphabetical by full ref (git < node < zsh).
        let feats = vec![
            feature("ghcr.io/devcontainers/features/node:1", &[]),
            feature("ghcr.io/devcontainers/features/zsh:1", &[]),
            feature("ghcr.io/devcontainers/features/git:1", &[]),
        ];
        let ordered = install_order(feats).unwrap();
        let ids: Vec<_> = ordered.iter().map(|f| f.reference.id.as_str()).collect();
        assert_eq!(ids, ["git", "node", "zsh"]);
    }

    #[test]
    fn install_order_absent_dep_is_ignored() {
        // installsAfter references a feature not in the set -> ignored, no error.
        let feats = vec![feature(
            "ghcr.io/devcontainers/features/node:1",
            &["ghcr.io/devcontainers/features/common-utils"],
        )];
        let ordered = install_order(feats).unwrap();
        assert_eq!(ordered.len(), 1);
        assert_eq!(ordered[0].reference.id, "node");
    }

    #[test]
    fn install_order_cycle_errors() {
        let feats = vec![
            feature("ghcr.io/x/a:1", &["ghcr.io/x/b"]),
            feature("ghcr.io/x/b:1", &["ghcr.io/x/a"]),
        ];
        let err = install_order(feats).unwrap_err().to_string();
        assert!(err.contains("cyclic"), "{err}");
        assert!(err.contains("ghcr.io/x/a:1") && err.contains("ghcr.io/x/b:1"), "{err}");
    }

    #[test]
    fn metadata_deserializes_from_fixture() {
        // Realistic go-feature snippet with unknown fields, camelCase renames,
        // an option default, containerEnv with ${PATH}, and installsAfter.
        let json = r#"{
            "id": "go",
            "version": "1.3.2",
            "name": "Go",
            "documentationURL": "https://github.com/devcontainers/features/tree/main/src/go",
            "options": {
                "version": {
                    "type": "string",
                    "proposals": ["latest", "none", "1.22"],
                    "default": "latest",
                    "description": "Select or enter a Go version to install"
                }
            },
            "containerEnv": {
                "GOROOT": "/usr/local/go",
                "PATH": "/usr/local/go/bin:${PATH}"
            },
            "installsAfter": [
                "ghcr.io/devcontainers/features/common-utils"
            ],
            "entrypoint": "/usr/local/share/go-init.sh",
            "someFutureUnknownField": {"nested": true}
        }"#;
        let md = FeatureMetadata::parse(json).unwrap();
        assert_eq!(md.id.as_deref(), Some("go"));
        assert_eq!(md.version.as_deref(), Some("1.3.2"));
        assert_eq!(md.name.as_deref(), Some("Go"));
        assert_eq!(
            md.documentation_url.as_deref(),
            Some("https://github.com/devcontainers/features/tree/main/src/go")
        );
        assert_eq!(
            md.options["version"].default.as_ref().and_then(|v| v.as_str()),
            Some("latest")
        );
        assert_eq!(md.container_env["GOROOT"], "/usr/local/go");
        assert_eq!(md.container_env["PATH"], "/usr/local/go/bin:${PATH}");
        assert_eq!(
            md.installs_after,
            vec!["ghcr.io/devcontainers/features/common-utils".to_string()]
        );
        assert_eq!(md.entrypoint.as_deref(), Some("/usr/local/share/go-init.sh"));
    }

    #[test]
    fn metadata_parses_mounts() {
        // Object form as shipped by docker-outside-of-docker.
        let json = r#"{
            "id": "docker-outside-of-docker",
            "mounts": [
                {"source": "/var/run/docker.sock", "target": "/var/run/docker-host.sock", "type": "bind"}
            ]
        }"#;
        let md = FeatureMetadata::parse(json).unwrap();
        assert_eq!(md.mounts.len(), 1);
        let ctx = crate::config::MountContext {
            config_dir: "/cfg",
            workspace_folder: "/w",
            workspace_folder_basename: "w",
            shared_volumes: "/cfg/shared-volumes",
            instance: "i",
        };
        let resolved = md.mounts[0].resolve(&ctx).unwrap();
        assert_eq!(resolved.kind, "bind");
        assert_eq!(resolved.source.as_deref(), Some("/var/run/docker.sock"));
        assert_eq!(resolved.target, "/var/run/docker-host.sock");
    }

    #[test]
    fn bearer_challenge_parses_realm_service_scope() {
        let ch = parse_bearer_challenge(
            r#"Bearer realm="https://ghcr.io/token",service="ghcr.io",scope="repository:devcontainers/features/node:pull""#,
            "devcontainers/features/node",
        )
        .unwrap();
        assert_eq!(ch.realm, "https://ghcr.io/token");
        assert_eq!(ch.service, "ghcr.io");
        assert_eq!(ch.scope, "repository:devcontainers/features/node:pull");
    }

    #[test]
    fn bearer_challenge_defaults_scope() {
        let ch = parse_bearer_challenge(
            r#"Bearer realm="https://auth.example.com/token",service="registry.example.com""#,
            "team/feat",
        )
        .unwrap();
        assert_eq!(ch.realm, "https://auth.example.com/token");
        assert_eq!(ch.service, "registry.example.com");
        assert_eq!(ch.scope, "repository:team/feat:pull");
    }

    #[test]
    fn bearer_challenge_rejects_non_bearer() {
        assert!(parse_bearer_challenge(r#"Basic realm="x""#, "team/feat").is_err());
    }

    #[test]
    fn split_status_takes_last_block() {
        let headers = "HTTP/1.1 401 Unauthorized\r\nwww-authenticate: Bearer realm=\"x\"\r\n\r\nHTTP/2 200\r\ncontent-type: application/json\r\n";
        let (status, lines) = split_status(headers);
        assert_eq!(status, 200);
        assert!(lines.iter().any(|l| l.starts_with("content-type")));
        // The 401 block's headers were cleared when the 200 status arrived.
        assert!(!lines.iter().any(|l| l.to_ascii_lowercase().starts_with("www-authenticate")));
    }

    /// Network-gated: pulls common-utils from ghcr.io into a temp cache. Skips
    /// (with a message) when the registry is unreachable.
    #[test]
    fn fetches_common_utils_from_ghcr() {
        // Probe reachability: a 401 counts as reachable (registry is up).
        // No `-S`: ghcr's /v2/ answers 401, and we don't want curl printing that
        // (harmless) error to the test's stderr — reachability is read from the
        // exit code below, not the message.
        let probe = Command::new("curl")
            .args(["-fs", "-o", "/dev/null", "--max-time", "10", "https://ghcr.io/v2/"])
            .status();
        let reachable = match probe {
            // -f makes a 401 exit non-zero (22); treat exit 22 as reachable too.
            Ok(s) => s.success() || s.code() == Some(22),
            Err(_) => false,
        };
        if !reachable {
            eprintln!("skipping fetches_common_utils_from_ghcr: ghcr.io unreachable");
            return;
        }

        let tmp = std::env::temp_dir().join(format!(
            "devsandbox-feat-test-{}",
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        let reference = FeatureRef::parse("ghcr.io/devcontainers/features/common-utils:2").unwrap();
        let resolved = fetch_into(&reference, &tmp);

        // Clean up regardless of outcome.
        let cleanup = |p: &Path| {
            let _ = std::fs::remove_dir_all(p);
        };

        let resolved = match resolved {
            Ok(r) => r,
            Err(e) => {
                cleanup(&tmp);
                panic!("fetch failed: {e:#}");
            }
        };
        assert!(
            resolved.dir.join("devcontainer-feature.json").exists(),
            "devcontainer-feature.json missing in {}",
            resolved.dir.display()
        );
        assert_eq!(resolved.metadata.id.as_deref(), Some("common-utils"));
        assert!(resolved.dir.join("install.sh").exists(), "install.sh missing");

        cleanup(&tmp);
    }
}