shine-cli 2.0.3

Give personal automation a reviewable lifecycle
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
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
use anyhow::{Context, Result, bail};
use sha2::{Digest, Sha256};
use std::ffi::{OsStr, OsString};
use std::path::{Component, Path, PathBuf};
use std::time::Duration;
use tokio::fs;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpListener, TcpStream};
use tokio::process::Command;

use crate::config::Config;

const DEFAULT_HOST: &str = "127.0.0.1";
const LAUNCHD_LABEL: &str = "top.biulight.shine.http";
const SYSTEMD_UNIT: &str = "shine-http.service";
const WINDOWS_TASK: &str = "Shine HTTP Server";
const MAX_HEADER_BYTES: usize = 8192;
/// Bounds how long a single connection may take end-to-end (read request + write
/// response), so a slow-loris style local client can't hold a task open forever.
const CONNECTION_TIMEOUT: Duration = Duration::from_secs(30);

// --- Trust boundary ---------------------------------------------------------------------
//
// This server binds loopback-only (`DEFAULT_HOST` = 127.0.0.1) and is never reachable from
// the network, but it has NO authentication of its own: any local OS user account on a
// shared/multi-user machine can connect to `127.0.0.1:<port>` and read any file under
// `http_root()`, bypassing the normal filesystem permissions that would otherwise keep
// other accounts out of this user's home directory. Preset authors must never route secrets
// or otherwise sensitive content through a `dest` that resolves under `~/.shine/http`.
// See docs/kb/architecture/invariants.md § Local HTTP server.

pub async fn handle_install(config: &Config, port: u16) -> Result<()> {
    let root = http_root(config);
    fs::create_dir_all(&root)
        .await
        .with_context(|| format!("creating {}", root.display()))?;

    match std::env::consts::OS {
        "macos" => install_launchd(config, port).await?,
        "linux" => install_systemd(config, port).await?,
        "windows" => install_windows_task(config, port).await?,
        os => bail!("shine serve install is not supported on {os}"),
    }

    println!("Serving {}", root.display());
    println!("URL base: http://{DEFAULT_HOST}:{port}/");
    Ok(())
}

async fn install_launchd(config: &Config, port: u16) -> Result<()> {
    let plist_path = launchd_plist_path(config);
    let parent = plist_path
        .parent()
        .context("launchd plist path must have a parent directory")?;
    fs::create_dir_all(parent)
        .await
        .with_context(|| format!("creating {}", parent.display()))?;

    let log_dir = launchd_log_dir(config);
    fs::create_dir_all(&log_dir)
        .await
        .with_context(|| format!("creating {}", log_dir.display()))?;

    let executable = service_executable(config)?;
    let plist = launchd_plist(&executable, config.shine_dir(), port, &log_dir);
    fs::write(&plist_path, plist)
        .await
        .with_context(|| format!("writing {}", plist_path.display()))?;

    let plist_arg = plist_path.to_string_lossy().to_string();
    let _ = launchctl(&["unload", "-w", &plist_arg]).await;
    launchctl(&["load", "-w", &plist_arg]).await?;

    println!("Installed {LAUNCHD_LABEL}");
    Ok(())
}

async fn install_systemd(config: &Config, port: u16) -> Result<()> {
    let unit_path = systemd_unit_path(config);
    let parent = unit_path
        .parent()
        .context("systemd unit path must have a parent directory")?;
    fs::create_dir_all(parent)
        .await
        .with_context(|| format!("creating {}", parent.display()))?;

    let executable = service_executable(config)?;
    let unit = systemd_unit(&executable, config.shine_dir(), port)?;
    fs::write(&unit_path, unit)
        .await
        .with_context(|| format!("writing {}", unit_path.display()))?;

    systemctl(&["daemon-reload"]).await?;
    systemctl(&["enable", SYSTEMD_UNIT]).await?;
    systemctl(&["restart", SYSTEMD_UNIT]).await?;
    println!("Installed {SYSTEMD_UNIT}");
    Ok(())
}

async fn install_windows_task(config: &Config, port: u16) -> Result<()> {
    let executable = service_executable(config)?;
    let task_run = windows_task_command(&executable, config.shine_dir(), port)?;
    if task_run.encode_utf16().count() > 261 {
        bail!("Windows scheduled task command exceeds the 261-character schtasks limit");
    }
    let run_as = windows_current_user()?;
    let _ = schtasks(&[
        OsStr::new("/End"),
        OsStr::new("/TN"),
        OsStr::new(WINDOWS_TASK),
    ])
    .await;
    schtasks(&[
        OsStr::new("/Create"),
        OsStr::new("/SC"),
        OsStr::new("ONLOGON"),
        OsStr::new("/TN"),
        OsStr::new(WINDOWS_TASK),
        OsStr::new("/TR"),
        OsStr::new(&task_run),
        OsStr::new("/RL"),
        OsStr::new("LIMITED"),
        OsStr::new("/RU"),
        &run_as,
        OsStr::new("/NP"),
        OsStr::new("/F"),
    ])
    .await?;
    schtasks(&[
        OsStr::new("/Run"),
        OsStr::new("/TN"),
        OsStr::new(WINDOWS_TASK),
    ])
    .await?;
    println!("Installed {WINDOWS_TASK}");
    Ok(())
}

pub async fn handle_start(config: &Config, port: u16) -> Result<()> {
    let root = http_root(config);
    fs::create_dir_all(&root)
        .await
        .with_context(|| format!("creating {}", root.display()))?;

    let listener = TcpListener::bind((DEFAULT_HOST, port))
        .await
        .with_context(|| format!("binding {DEFAULT_HOST}:{port}"))?;
    println!("Serving {}", root.display());
    println!("URL base: http://{DEFAULT_HOST}:{port}/");

    loop {
        let (stream, _) = listener.accept().await?;
        let root = root.clone();
        tokio::spawn(async move {
            match tokio::time::timeout(CONNECTION_TIMEOUT, handle_connection(stream, root)).await {
                Ok(Ok(())) => {}
                Ok(Err(e)) => eprintln!("shine serve: connection error: {e:#}"),
                Err(_) => {
                    eprintln!("shine serve: connection timed out after {CONNECTION_TIMEOUT:?}")
                }
            }
        });
    }
}

pub async fn handle_status(config: &Config) -> Result<()> {
    match std::env::consts::OS {
        "macos" => status_launchd(config).await,
        "linux" => status_systemd(config).await,
        "windows" => status_windows_task().await,
        os => bail!("shine serve status is not supported on {os}"),
    }
}

pub async fn handle_uninstall(config: &Config) -> Result<()> {
    match std::env::consts::OS {
        "macos" => uninstall_launchd(config).await,
        "linux" => uninstall_systemd(config).await,
        "windows" => uninstall_windows_task().await,
        os => bail!("shine serve uninstall is not supported on {os}"),
    }
}

async fn status_launchd(config: &Config) -> Result<()> {
    let plist_path = launchd_plist_path(config);
    if !plist_path.exists() {
        println!("Not installed");
        return Ok(());
    }
    match launchctl(&["list", LAUNCHD_LABEL]).await {
        Ok(()) => println!("Installed and running"),
        Err(_) => println!("Installed but not running"),
    }
    println!("Plist: {}", plist_path.display());
    Ok(())
}

async fn status_systemd(config: &Config) -> Result<()> {
    let unit_path = systemd_unit_path(config);
    if !unit_path.exists() {
        println!("Not installed");
        return Ok(());
    }
    match systemctl(&["is-active", "--quiet", SYSTEMD_UNIT]).await {
        Ok(()) => println!("Installed and running"),
        Err(_) => println!("Installed but not running"),
    }
    println!("Unit: {}", unit_path.display());
    Ok(())
}

async fn status_windows_task() -> Result<()> {
    match schtasks(&[
        OsStr::new("/Query"),
        OsStr::new("/TN"),
        OsStr::new(WINDOWS_TASK),
    ])
    .await
    {
        Ok(()) => println!("Installed"),
        Err(_) => println!("Not installed"),
    }
    Ok(())
}

async fn uninstall_launchd(config: &Config) -> Result<()> {
    let plist_path = launchd_plist_path(config);
    if plist_path.exists() {
        let plist_arg = plist_path.to_string_lossy().to_string();
        let _ = launchctl(&["unload", "-w", &plist_arg]).await;
        fs::remove_file(&plist_path)
            .await
            .with_context(|| format!("removing {}", plist_path.display()))?;
        println!("Uninstalled {LAUNCHD_LABEL}");
    } else {
        println!("Not installed");
    }
    Ok(())
}

async fn uninstall_systemd(config: &Config) -> Result<()> {
    let unit_path = systemd_unit_path(config);
    if !unit_path.exists() {
        println!("Not installed");
        return Ok(());
    }
    systemctl(&["disable", "--now", SYSTEMD_UNIT]).await?;
    fs::remove_file(&unit_path)
        .await
        .with_context(|| format!("removing {}", unit_path.display()))?;
    systemctl(&["daemon-reload"]).await?;
    println!("Uninstalled {SYSTEMD_UNIT}");
    Ok(())
}

async fn uninstall_windows_task() -> Result<()> {
    let query = schtasks(&[
        OsStr::new("/Query"),
        OsStr::new("/TN"),
        OsStr::new(WINDOWS_TASK),
    ])
    .await;
    if query.is_err() {
        println!("Not installed");
        return Ok(());
    }
    let _ = schtasks(&[
        OsStr::new("/End"),
        OsStr::new("/TN"),
        OsStr::new(WINDOWS_TASK),
    ])
    .await;
    schtasks(&[
        OsStr::new("/Delete"),
        OsStr::new("/TN"),
        OsStr::new(WINDOWS_TASK),
        OsStr::new("/F"),
    ])
    .await?;
    println!("Uninstalled {WINDOWS_TASK}");
    Ok(())
}

pub fn handle_url(path: &str, port: u16) -> Result<()> {
    println!("{}", public_url(path, port)?);
    Ok(())
}

pub fn public_url(path: &str, port: u16) -> Result<String> {
    let rel = normalize_resource_path(path)?;
    Ok(format!(
        "http://{DEFAULT_HOST}:{port}/{}",
        rel.to_string_lossy()
    ))
}

pub fn http_root(config: &Config) -> PathBuf {
    config.shine_dir().join("http")
}

/// Directory for the launchd service's stdout/stderr logs. Deliberately kept out of
/// `http_root()` (`shine_dir/http`) so log contents are never servable over HTTP, and kept
/// under the user's own `shine_dir` (not a shared path like `/tmp`) so two OS user accounts
/// running `shine serve install` never collide on the same log file.
fn launchd_log_dir(config: &Config) -> PathBuf {
    config.shine_dir().join("run").join("http")
}

fn service_executable(config: &Config) -> Result<PathBuf> {
    if let Some(dest) = &config.self_install_dest {
        return Ok(dest.clone());
    }
    std::env::current_exe().context("failed to resolve current executable path")
}

fn launchd_plist_path(config: &Config) -> PathBuf {
    config
        .home_dir
        .join("Library")
        .join("LaunchAgents")
        .join(format!("{LAUNCHD_LABEL}.plist"))
}

fn launchd_plist(executable: &Path, shine_dir: &Path, port: u16, log_dir: &Path) -> String {
    let executable = xml_escape(&executable.display().to_string());
    let shine_dir = xml_escape(&shine_dir.display().to_string());
    let out_log = xml_escape(&log_dir.join("serve.out.log").display().to_string());
    let err_log = xml_escape(&log_dir.join("serve.err.log").display().to_string());
    format!(
        r#"<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>{LAUNCHD_LABEL}</string>
  <key>ProgramArguments</key>
  <array>
    <string>{executable}</string>
    <string>--config-dir</string>
    <string>{shine_dir}</string>
    <string>serve</string>
    <string>start</string>
    <string>--port</string>
    <string>{port}</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>KeepAlive</key>
  <true/>
  <key>StandardOutPath</key>
  <string>{out_log}</string>
  <key>StandardErrorPath</key>
  <string>{err_log}</string>
</dict>
</plist>
"#
    )
}

fn systemd_unit_path(config: &Config) -> PathBuf {
    let config_home = std::env::var_os("XDG_CONFIG_HOME")
        .map(PathBuf::from)
        .filter(|path| path.is_absolute())
        .unwrap_or_else(|| config.home_dir.join(".config"));
    config_home.join("systemd").join("user").join(SYSTEMD_UNIT)
}

fn systemd_unit(executable: &Path, shine_dir: &Path, port: u16) -> Result<String> {
    let executable = executable
        .to_str()
        .context("shine executable path is not valid UTF-8")?;
    let shine_dir = shine_dir
        .to_str()
        .context("shine config directory is not valid UTF-8")?;
    Ok(format!(
        "[Unit]\nDescription=Shine local HTTP server\n\n[Service]\nType=simple\nExecStart={} --config-dir {} serve start --port {port}\nRestart=on-failure\nRestartSec=2s\n\n[Install]\nWantedBy=default.target\n",
        systemd_quote(executable),
        systemd_quote(shine_dir),
    ))
}

fn systemd_quote(value: &str) -> String {
    let escaped = value
        .replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace('\n', "\\n")
        .replace('\r', "\\r")
        .replace('\t', "\\t")
        .replace('%', "%%")
        .replace('$', "$$");
    format!("\"{escaped}\"")
}

fn windows_task_command(executable: &Path, shine_dir: &Path, port: u16) -> Result<String> {
    let executable = executable
        .to_str()
        .context("shine executable path is not valid Unicode")?;
    let shine_dir = shine_dir
        .to_str()
        .context("shine config directory is not valid Unicode")?;
    Ok([
        executable.to_string(),
        "--config-dir".to_string(),
        shine_dir.to_string(),
        "serve".to_string(),
        "start".to_string(),
        "--port".to_string(),
        port.to_string(),
    ]
    .iter()
    .map(|arg| windows_quote_arg(arg))
    .collect::<Vec<_>>()
    .join(" "))
}

fn windows_current_user() -> Result<OsString> {
    let username = std::env::var_os("USERNAME")
        .filter(|value| !value.is_empty())
        .context("USERNAME is not set; cannot register the current-user scheduled task")?;
    if let Some(domain) = std::env::var_os("USERDOMAIN").filter(|value| !value.is_empty()) {
        let mut qualified = domain;
        qualified.push("\\");
        qualified.push(username);
        Ok(qualified)
    } else {
        Ok(username)
    }
}

// Quote one argv element according to the CommandLineToArgvW rules used by Rust's
// Windows process startup. Backslashes need doubling only when they precede a quote
// or the closing quote.
fn windows_quote_arg(value: &str) -> String {
    let mut quoted = String::from("\"");
    let mut backslashes = 0;
    for ch in value.chars() {
        if ch == '\\' {
            backslashes += 1;
        } else if ch == '"' {
            quoted.push_str(&"\\".repeat(backslashes * 2 + 1));
            quoted.push('"');
            backslashes = 0;
        } else {
            quoted.push_str(&"\\".repeat(backslashes));
            backslashes = 0;
            quoted.push(ch);
        }
    }
    quoted.push_str(&"\\".repeat(backslashes * 2));
    quoted.push('"');
    quoted
}

fn xml_escape(value: &str) -> String {
    value
        .replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
        .replace('\'', "&apos;")
}

async fn launchctl(args: &[&str]) -> Result<()> {
    run_command("launchctl", args.iter().map(OsStr::new)).await
}

async fn systemctl(args: &[&str]) -> Result<()> {
    let args = std::iter::once(OsStr::new("--user")).chain(args.iter().map(OsStr::new));
    run_command("systemctl", args).await
}

async fn schtasks(args: &[&OsStr]) -> Result<()> {
    run_command("schtasks.exe", args.iter().copied()).await
}

async fn run_command<'a>(program: &str, args: impl IntoIterator<Item = &'a OsStr>) -> Result<()> {
    let args: Vec<OsString> = args.into_iter().map(OsStr::to_os_string).collect();
    let output = Command::new(program)
        .args(&args)
        .output()
        .await
        .with_context(|| format!("running {program}"))?;
    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        let stdout = String::from_utf8_lossy(&output.stdout);
        let detail = stderr.trim();
        let detail = if detail.is_empty() {
            stdout.trim()
        } else {
            detail
        };
        if detail.is_empty() {
            bail!("{program} failed with {}", output.status);
        }
        bail!("{program} failed with {}: {detail}", output.status);
    }
    Ok(())
}

async fn handle_connection(mut stream: TcpStream, root: PathBuf) -> Result<()> {
    let request = match read_request(&mut stream).await {
        Ok(request) => request,
        Err(_) => {
            write_response(
                &mut stream,
                400,
                "Bad Request",
                "text/plain",
                b"",
                false,
                None,
            )
            .await?;
            return Ok(());
        }
    };

    if request.method != "GET" && request.method != "HEAD" {
        write_response(
            &mut stream,
            405,
            "Method Not Allowed",
            "text/plain",
            b"",
            false,
            None,
        )
        .await?;
        return Ok(());
    }

    let rel = match normalize_resource_path(&request.path) {
        Ok(rel) => rel,
        Err(_) => {
            write_response(
                &mut stream,
                404,
                "Not Found",
                "text/plain",
                b"",
                false,
                None,
            )
            .await?;
            return Ok(());
        }
    };
    let root_canon = fs::canonicalize(&root).await?;
    let candidate = root.join(rel);
    let file_canon = match fs::canonicalize(&candidate).await {
        Ok(path) => path,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
            write_response(
                &mut stream,
                404,
                "Not Found",
                "text/plain",
                b"",
                false,
                None,
            )
            .await?;
            return Ok(());
        }
        Err(e) => return Err(e.into()),
    };
    if !file_canon.starts_with(&root_canon) || !file_canon.is_file() {
        write_response(
            &mut stream,
            404,
            "Not Found",
            "text/plain",
            b"",
            false,
            None,
        )
        .await?;
        return Ok(());
    }

    let bytes = fs::read(&file_canon).await?;
    let etag = entity_tag(&bytes);
    if request
        .if_none_match
        .as_deref()
        .is_some_and(|value| etag_matches(value, &etag))
    {
        write_response(
            &mut stream,
            304,
            "Not Modified",
            content_type(&file_canon),
            b"",
            true,
            Some(&etag),
        )
        .await?;
        return Ok(());
    }
    write_response(
        &mut stream,
        200,
        "OK",
        content_type(&file_canon),
        &bytes,
        request.method == "HEAD",
        Some(&etag),
    )
    .await?;
    Ok(())
}

struct Request {
    method: String,
    path: String,
    if_none_match: Option<String>,
}

async fn read_request(stream: &mut TcpStream) -> Result<Request> {
    let mut data = Vec::new();
    let mut buf = [0u8; 1024];
    loop {
        let n = stream.read(&mut buf).await?;
        if n == 0 {
            break;
        }
        data.extend_from_slice(&buf[..n]);
        if data.windows(4).any(|w| w == b"\r\n\r\n") {
            break;
        }
        if data.len() > MAX_HEADER_BYTES {
            bail!("request header too large");
        }
    }

    let header = std::str::from_utf8(&data).context("request header must be utf-8")?;
    let line = header.lines().next().context("missing request line")?;
    let mut parts = line.split_whitespace();
    let method = parts.next().context("missing method")?.to_string();
    let target = parts.next().context("missing path")?;
    let path = target
        .split_once('?')
        .map(|(path, _)| path)
        .unwrap_or(target)
        .to_string();
    let if_none_match = header.lines().skip(1).find_map(|line| {
        let (name, value) = line.split_once(':')?;
        name.eq_ignore_ascii_case("if-none-match")
            .then(|| value.trim().to_string())
    });
    Ok(Request {
        method,
        path,
        if_none_match,
    })
}

async fn write_response(
    stream: &mut TcpStream,
    status: u16,
    reason: &str,
    content_type: &str,
    body: &[u8],
    head_only: bool,
    etag: Option<&str>,
) -> Result<()> {
    let cache_headers = cache_headers(etag);
    let headers = format!(
        "HTTP/1.1 {status} {reason}\r\nContent-Length: {}\r\nContent-Type: {content_type}\r\n{cache_headers}Connection: close\r\n\r\n",
        body.len(),
    );
    stream.write_all(headers.as_bytes()).await?;
    if !head_only {
        stream.write_all(body).await?;
    }
    Ok(())
}

fn cache_headers(etag: Option<&str>) -> String {
    let mut headers =
        "Cache-Control: no-cache, max-age=0, must-revalidate\r\nPragma: no-cache\r\n".to_string();
    if let Some(etag) = etag {
        headers.push_str(&format!("ETag: {etag}\r\n"));
    }
    headers
}

fn entity_tag(bytes: &[u8]) -> String {
    format!("\"sha256-{:x}\"", Sha256::digest(bytes))
}

fn etag_matches(if_none_match: &str, etag: &str) -> bool {
    if_none_match
        .split(',')
        .map(str::trim)
        .any(|candidate| candidate == "*" || candidate.trim_start_matches("W/") == etag)
}

fn normalize_resource_path(path: &str) -> Result<PathBuf> {
    let path = path.trim_start_matches('/');
    if path.is_empty() {
        bail!("resource path must not be empty");
    }
    let decoded = percent_decode(path)?;
    let rel = Path::new(&decoded);
    if rel.is_absolute() {
        bail!("resource path must be relative");
    }
    if rel.components().any(|c| !matches!(c, Component::Normal(_))) {
        bail!("resource path must not contain traversal");
    }
    Ok(rel.to_path_buf())
}

fn percent_decode(input: &str) -> Result<String> {
    let bytes = input.as_bytes();
    let mut out = Vec::with_capacity(bytes.len());
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'%' {
            if i + 2 >= bytes.len() {
                bail!("invalid percent encoding");
            }
            let hex = std::str::from_utf8(&bytes[i + 1..i + 3])?;
            out.push(u8::from_str_radix(hex, 16).context("invalid percent encoding")?);
            i += 3;
        } else {
            out.push(bytes[i]);
            i += 1;
        }
    }
    String::from_utf8(out).context("decoded path must be utf-8")
}

fn content_type(path: &Path) -> &'static str {
    match path.extension().and_then(|ext| ext.to_str()) {
        Some("html") => "text/html; charset=utf-8",
        Some("json") => "application/json",
        Some("sgmodule" | "txt" | "toml") => "text/plain; charset=utf-8",
        _ => "application/octet-stream",
    }
}

#[cfg(test)]
mod tests {
    use super::{
        cache_headers, entity_tag, etag_matches, launchd_log_dir, launchd_plist,
        launchd_plist_path, normalize_resource_path, public_url, systemd_unit, systemd_unit_path,
        windows_quote_arg, windows_task_command,
    };
    use crate::config::Config;
    use std::path::{Path, PathBuf};

    #[test]
    fn public_url_uses_single_local_server_root() {
        assert_eq!(
            public_url("/app/surge/custom-rules.sgmodule", 6174).unwrap(),
            "http://127.0.0.1:6174/app/surge/custom-rules.sgmodule"
        );
    }

    #[test]
    fn resource_paths_reject_traversal() {
        assert!(normalize_resource_path("../secret").is_err());
        assert!(normalize_resource_path("app/../secret").is_err());
        assert!(normalize_resource_path("/app/surge/custom-rules.sgmodule").is_ok());
        assert_eq!(
            normalize_resource_path("app/surge/custom-rules.sgmodule").unwrap(),
            Path::new("app/surge/custom-rules.sgmodule")
        );
    }

    #[test]
    fn resource_responses_require_revalidation_and_offer_an_etag() {
        let etag = entity_tag(b"current rules");
        let headers = cache_headers(Some(&etag));
        assert!(headers.contains("Cache-Control: no-cache, max-age=0, must-revalidate"));
        assert!(headers.contains("Pragma: no-cache"));
        assert!(headers.contains(&format!("ETag: {etag}")));
        assert!(etag_matches(&etag, &etag));
        assert!(etag_matches(&format!("W/{etag}, \"older\""), &etag));
        assert!(!etag_matches("\"older\"", &etag));
    }

    #[test]
    fn launchd_plist_runs_the_single_foreground_server() {
        let log_dir = Path::new("/Users/tester/.shine/run/http");
        let plist = launchd_plist(
            Path::new("/opt/shine & tools/shine"),
            Path::new("/Users/tester/.shine & tools"),
            6188,
            log_dir,
        );
        assert!(plist.contains("<string>top.biulight.shine.http</string>"));
        assert!(plist.contains("<string>/opt/shine &amp; tools/shine</string>"));
        assert!(plist.contains("<string>--config-dir</string>"));
        assert!(plist.contains("<string>/Users/tester/.shine &amp; tools</string>"));
        assert!(plist.contains("<string>serve</string>"));
        assert!(plist.contains("<string>start</string>"));
        assert!(plist.contains("<string>--port</string>"));
        assert!(plist.contains("<string>6188</string>"));
    }

    #[cfg(unix)]
    #[test]
    fn launchd_plist_logs_are_scoped_under_the_user_shine_dir_not_shared_tmp() {
        let log_dir = Path::new("/Users/tester/.shine/run/http");
        let plist = launchd_plist(
            Path::new("/opt/shine/shine"),
            Path::new("/Users/tester/.shine"),
            6188,
            log_dir,
        );
        assert!(plist.contains("<string>/Users/tester/.shine/run/http/serve.out.log</string>"));
        assert!(plist.contains("<string>/Users/tester/.shine/run/http/serve.err.log</string>"));
        assert!(!plist.contains("/tmp/"));
    }

    #[test]
    fn launchd_plist_path_lives_under_user_launch_agents() {
        let root = PathBuf::from("/tmp/shine-home");
        let config = Config::new_for_test(&root);
        assert_eq!(
            launchd_plist_path(&config),
            root.join("Library/LaunchAgents/top.biulight.shine.http.plist")
        );
    }

    #[test]
    fn launchd_log_dir_lives_under_shine_dir_run_not_shared_tmp() {
        let root = PathBuf::from("/tmp/shine-home");
        let config = Config::new_for_test(&root);
        assert_eq!(launchd_log_dir(&config), root.join("run").join("http"));
    }

    #[test]
    fn systemd_unit_runs_and_restarts_the_user_server() {
        let unit = systemd_unit(
            Path::new("/opt/shine % tools/shine"),
            Path::new("/home/tester/.shine $ state"),
            6188,
        )
        .unwrap();
        assert!(unit.contains("Type=simple"));
        assert!(unit.contains(
            "ExecStart=\"/opt/shine %% tools/shine\" --config-dir \"/home/tester/.shine $$ state\" serve start --port 6188"
        ));
        assert!(unit.contains("Restart=on-failure"));
        assert!(unit.contains("WantedBy=default.target"));
    }

    #[test]
    fn systemd_unit_path_uses_the_standard_user_unit_suffix() {
        let root = PathBuf::from("/tmp/shine-home");
        let config = Config::new_for_test(&root);
        assert!(systemd_unit_path(&config).ends_with("systemd/user/shine-http.service"));
    }

    #[test]
    fn windows_task_command_preserves_spaces_quotes_and_trailing_backslashes() {
        let command = windows_task_command(
            Path::new(r#"C:\Program Files\Shine\shine.exe"#),
            Path::new(r#"C:\Users\Tester\shine state\"#),
            6199,
        )
        .unwrap();
        assert!(command.starts_with(r#""C:\Program Files\Shine\shine.exe" "--config-dir""#));
        assert!(command.contains(r#""C:\Users\Tester\shine state\\""#));
        assert!(command.ends_with(r#""serve" "start" "--port" "6199""#));
        assert_eq!(
            windows_quote_arg(r#"C:\path with space\"#),
            r#""C:\path with space\\""#
        );
        assert_eq!(windows_quote_arg(r#"a"b"#), r#""a\"b""#);
    }
}