hpip 0.1.2

Host These Things Please - a modern async HTTP file server
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
use std::collections::BTreeSet;
use std::collections::btree_map::{BTreeMap, Entry as BTreeMapEntry};
use std::env;
use std::ffi::OsString;
use std::net::IpAddr;
use std::num::NonZeroU64;
use std::path::PathBuf;
use std::str::FromStr;

use cidr::IpCidr;
use clap::Parser;

use crate::error::Error;

#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum LogLevel {
    All,
    NoServeStatus,
    NoStartup,
    NoAuth,
}

impl From<u8> for LogLevel {
    fn from(raw: u8) -> LogLevel {
        match raw {
            0 => LogLevel::All,
            1 => LogLevel::NoServeStatus,
            2 => LogLevel::NoStartup,
            _ => LogLevel::NoAuth,
        }
    }
}

#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum WebDavLevel {
    No,
    MkColMoveOnly,
    All,
}

/// Host These Things Please - a modern async HTTP file server
#[derive(Parser, Debug)]
#[command(name = "hpip", version, about)]
struct Cli {
    /// Directory to host. Default: current working directory
    #[arg(default_value = ".")]
    dir: String,

    /// Port to use. Default: first free port from 8000 up
    #[arg(short, long)]
    port: Option<u16>,

    /// Address to bind to. Default: 0.0.0.0
    #[arg(short = 'a', long = "address")]
    address: Option<String>,

    /// Temporary directory. Default: $TEMP
    #[arg(short = 't', long = "temp-dir")]
    temp_dir: Option<String>,

    /// Return this file instead of a 404 for a GET. Default: generated response
    #[arg(long = "404")]
    fallback_404: Option<String>,

    /// Don't follow symlinks. Default: false
    #[arg(short = 's', long = "no-follow-symlinks")]
    no_follow_symlinks: bool,

    /// Restrict/sandbox where symlinks lead to only the direct descendants of the hosted directory
    #[arg(short = 'r', long = "sandbox-symlinks")]
    sandbox_symlinks: bool,

    /// Allow for write operations. Default: false
    #[arg(short = 'w', long = "allow-write")]
    allow_write: bool,

    /// Never generate dir listings. Default: false
    #[arg(short = 'l', long = "no-listings")]
    no_listings: bool,

    /// Do not automatically use index files. Default: false
    #[arg(short = 'i', long = "no-indices")]
    no_indices: bool,

    /// Do not encode filesystem files. Default: false
    #[arg(short = 'e', long = "no-encode")]
    no_encode: bool,

    /// Consume at most FS_LIMIT space for encoded filesystem files
    #[arg(long = "encoded-filesystem")]
    encoded_filesystem: Option<String>,

    /// Consume at most GEN_LIMIT memory for encoded generated responses
    #[arg(long = "encoded-generated")]
    encoded_generated: Option<String>,

    /// Prune cached encoded data older than MAX_AGE
    #[arg(long = "encoded-prune")]
    encoded_prune: Option<String>,

    /// Allow stripping index extensions from served paths. Default: false
    #[arg(short = 'x', long = "strip-extensions")]
    strip_extensions: bool,

    /// Suppress increasing amounts of output
    #[arg(short = 'q', long = "quiet", action = clap::ArgAction::Count)]
    quiet: u8,

    /// Don't prefix logs with the timestamp
    #[arg(short = 'Q', long = "quiet-time")]
    quiet_time: bool,

    /// Don't colourise the log output
    #[arg(short = 'c', long = "no-colour")]
    no_colour: bool,

    /// Handle WebDAV requests (MKCOL, MOVE, PROPFIND, COPY, PROPPATCH). Default: false
    #[arg(short = 'd', long = "webdav")]
    webdav: bool,

    /// Allow WebDAV MKCOL and MOVE only. Default: false
    #[arg(short = 'D', long = "convenient-webdav")]
    convenient_webdav: bool,

    /// Allow requesting tar and ZIP archives. Default: false
    #[arg(short = 'A', long = "archives")]
    archives: bool,

    /// Data for HTTPS, identity file. Password in HTTP_SSL_PASS env var, otherwise empty
    #[arg(long = "ssl")]
    ssl: Option<String>,

    /// Generate a one-off TLS certificate
    #[arg(long = "gen-ssl", conflicts_with = "ssl")]
    gen_ssl: bool,

    /// Data for global authentication
    #[arg(long = "auth")]
    auth: Option<String>,

    /// Generate a one-off username:password set for global authentication
    #[arg(long = "gen-auth", conflicts_with = "auth")]
    gen_auth: bool,

    /// Data for authentication under PATH
    #[arg(long = "path-auth", num_args = 1)]
    path_auth: Vec<String>,

    /// Generate a one-off username:password set for authentication under PATH
    #[arg(long = "gen-path-auth", num_args = 1)]
    gen_path_auth: Vec<String>,

    /// Treat HEADER-NAME as proxy forwarded-for header when request comes from CIDR
    #[arg(long = "proxy", num_args = 1)]
    proxy: Vec<String>,

    /// Treat HEADER-NAME as proxy X-Original-URL header for redirects when request comes from CIDR
    #[arg(long = "proxy-redir", num_args = 1)]
    proxy_redir: Vec<String>,

    /// Always return MIME-TYPE for files with EXTENSION
    #[arg(short = 'm', long = "mime-type", num_args = 1)]
    mime_type: Vec<String>,

    /// Limit each request to returning BYTES per second, or 0 for unlimited. Default: 0
    #[arg(long = "request-bandwidth")]
    request_bandwidth: Option<String>,

    /// Headers to add to every response
    #[arg(short = 'H', long = "header", num_args = 1)]
    header: Vec<String>,
}

/// All configurable values for the application.
#[derive(Debug, Clone)]
pub struct Options {
    pub hosted_directory: (String, PathBuf),
    pub port: Option<u16>,
    pub bind_address: IpAddr,
    pub follow_symlinks: bool,
    pub sandbox_symlinks: bool,
    pub temp_directory: (String, PathBuf),
    pub generate_listings: bool,
    pub check_indices: bool,
    pub strip_extensions: bool,
    pub try_404: Option<PathBuf>,
    pub allow_writes: bool,
    pub encode_fs: bool,
    pub encoded_filesystem_limit: Option<u64>,
    pub encoded_generated_limit: Option<u64>,
    pub encoded_prune: Option<u64>,
    pub loglevel: LogLevel,
    pub log_time: bool,
    pub log_colour: bool,
    pub webdav: WebDavLevel,
    pub archives: bool,
    pub tls_data: Option<((String, PathBuf), String)>,
    pub generate_tls: bool,
    pub path_auth_data: BTreeMap<String, Option<String>>,
    pub generate_path_auth: BTreeSet<String>,
    pub proxies: BTreeMap<IpCidr, String>,
    pub proxy_redirs: BTreeMap<IpCidr, String>,
    pub mime_type_overrides: BTreeMap<OsString, String>,
    pub request_bandwidth: Option<NonZeroU64>,
    pub additional_headers: Vec<(String, Vec<u8>)>,
}

impl Options {
    pub fn parse() -> Result<Options, Error> {
        let cli = Cli::parse();

        let dir = &cli.dir;
        let dir_pb = std::fs::canonicalize(dir)
            .map_err(|_| Error(format!("Directory to host \"{}\" not found", dir)))?;
        if !dir_pb.is_dir() {
            return Err(Error(format!(
                "Directory to host \"{}\" not actually a directory",
                dir
            )));
        }

        let follow_symlinks = !cli.no_follow_symlinks;

        let bind_address = cli
            .address
            .as_deref()
            .map(IpAddr::from_str)
            .transpose()
            .map_err(|_| Error("Invalid bind address".into()))?
            .unwrap_or_else(|| "0.0.0.0".parse().unwrap());

        let mut path_auth_data = BTreeMap::new();
        if let Some(ref root_auth) = cli.auth {
            Options::validate_credentials(root_auth)?;
            path_auth_data.insert(
                String::new(),
                Some(Options::normalise_credentials(root_auth)),
            );
        }

        for pa in &cli.path_auth {
            let (path, auth) = Options::decode_path_credentials(pa)?;
            match path_auth_data.entry(path) {
                BTreeMapEntry::Occupied(oe) => {
                    return Err(Error(format!(
                        "Credentials for path \"/{}\" already present",
                        oe.key()
                    )));
                }
                BTreeMapEntry::Vacant(ve) => {
                    ve.insert(auth.map(|s| Options::normalise_credentials(&s)));
                }
            }
        }

        let mut generate_path_auth = BTreeSet::new();
        if cli.gen_auth {
            generate_path_auth.insert(String::new());
        }

        for gpa in &cli.gen_path_auth {
            let path = Options::normalise_path(gpa);
            if path_auth_data.contains_key(&path) {
                return Err(Error(format!(
                    "Credentials for path \"/{}\" already present",
                    path
                )));
            }
            if !generate_path_auth.insert(path.clone()) {
                return Err(Error(format!(
                    "Credentials for path \"/{}\" already present",
                    path
                )));
            }
        }

        let proxies: BTreeMap<IpCidr, String> = cli
            .proxy
            .iter()
            .map(|s| Options::proxy_parse(s))
            .collect::<Result<_, _>>()?;

        let proxy_redirs: BTreeMap<IpCidr, String> = cli
            .proxy_redir
            .iter()
            .map(|s| Options::proxy_parse(s))
            .collect::<Result<_, _>>()?;

        let mime_type_overrides: BTreeMap<OsString, String> = cli
            .mime_type
            .iter()
            .map(|s| Options::mime_type_override_parse(s))
            .collect::<Result<_, _>>()?;

        let additional_headers: Vec<(String, Vec<u8>)> = cli
            .header
            .iter()
            .map(|s| Options::header_parse(s))
            .collect::<Result<_, _>>()?;

        let webdav = std::cmp::max(
            if cli.webdav {
                WebDavLevel::All
            } else {
                WebDavLevel::No
            },
            if cli.convenient_webdav {
                WebDavLevel::MkColMoveOnly
            } else {
                WebDavLevel::No
            },
        );

        Ok(Options {
            hosted_directory: (dir.to_string(), dir_pb.clone()),
            port: cli.port,
            bind_address,
            follow_symlinks,
            sandbox_symlinks: follow_symlinks && cli.sandbox_symlinks,
            temp_directory: {
                let (temp_s, temp_pb) = if let Some(ref tmpdir) = cli.temp_dir {
                    (
                        tmpdir.to_string(),
                        std::fs::canonicalize(tmpdir).map_err(|_| {
                            Error(format!("Temporary directory \"{}\" not found", tmpdir))
                        })?,
                    )
                } else {
                    ("$TEMP".to_string(), env::temp_dir())
                };
                let suffix = dir_pb
                    .to_string_lossy()
                    .replace(r"\\?\", "")
                    .replace(':', "")
                    .replace('\\', "/")
                    .replace('/', "-");
                let suffix = if suffix.len() >= 255 - 5 {
                    format!("hpip-{}", blake3::hash(suffix.as_bytes()).to_hex())
                } else {
                    format!(
                        "hpip{}{}",
                        if suffix.starts_with('-') { "" } else { "-" },
                        suffix
                    )
                };
                (
                    format!(
                        "{}{}{}",
                        temp_s,
                        if temp_s.ends_with('/') || temp_s.ends_with('\\') {
                            ""
                        } else {
                            "/"
                        },
                        suffix
                    ),
                    temp_pb.join(&suffix),
                )
            },
            generate_listings: !cli.no_listings,
            check_indices: !cli.no_indices,
            strip_extensions: cli.strip_extensions,
            try_404: cli.fallback_404.map(PathBuf::from),
            allow_writes: cli.allow_write,
            encode_fs: !cli.no_encode,
            encoded_filesystem_limit: cli
                .encoded_filesystem
                .as_deref()
                .map(Options::size_parse)
                .transpose()?,
            encoded_generated_limit: cli
                .encoded_generated
                .as_deref()
                .map(Options::size_parse)
                .transpose()?,
            encoded_prune: cli
                .encoded_prune
                .as_deref()
                .map(Options::age_parse)
                .transpose()?,
            loglevel: LogLevel::from(cli.quiet),
            log_time: !cli.quiet_time,
            log_colour: !cli.no_colour,
            webdav,
            archives: cli.archives,
            tls_data: cli.ssl.map(|id| {
                let id_pb = std::fs::canonicalize(&id).expect("TLS identity file not found");
                ((id, id_pb), env::var("HTTP_SSL_PASS").unwrap_or_default())
            }),
            generate_tls: cli.gen_ssl,
            path_auth_data,
            generate_path_auth,
            proxies,
            proxy_redirs,
            mime_type_overrides,
            request_bandwidth: cli
                .request_bandwidth
                .as_deref()
                .map(Options::bandwidth_parse)
                .transpose()?
                .flatten(),
            additional_headers,
        })
    }

    fn validate_credentials(s: &str) -> Result<(), Error> {
        if match s.split_once(':') {
            Some((u, p)) => !u.is_empty() && !p.contains(':'),
            None => !s.is_empty(),
        } {
            Ok(())
        } else {
            Err(Error(format!(
                "Global authentication credentials \"{}\" need be in format \"username[:password]\"",
                s
            )))
        }
    }

    fn decode_path_credentials(s: &str) -> Result<(String, Option<String>), Error> {
        let (path, creds) = s.split_once('=').ok_or_else(|| {
            Error(format!(
                "Per-path authentication credentials \"{}\" need be in format \"path=[username[:password]]\"",
                s
            ))
        })?;

        let path = Options::normalise_path(path);
        if creds.is_empty() {
            Ok((path, None))
        } else {
            if let Some((u, p)) = creds.split_once(':')
                && (u.is_empty() || p.contains(':'))
            {
                return Err(Error(format!(
                    "Per-path authentication credentials \"{}\" need be in format \"path=[username[:password]]\"",
                    s
                )));
            }
            Ok((path, Some(creds.to_string())))
        }
    }

    fn normalise_path(path: &str) -> String {
        let mut frags = vec![];
        for fragment in path.split(['/', '\\']) {
            match fragment {
                "" | "." => {}
                ".." => {
                    frags.pop();
                }
                _ => frags.push(fragment),
            }
        }

        let mut ret =
            String::with_capacity(frags.iter().map(|s| s.len()).sum::<usize>() + frags.len());
        for frag in frags {
            ret.push_str(frag);
            ret.push('/');
        }
        ret.pop();
        ret
    }

    fn normalise_credentials(creds: &str) -> String {
        if creds.ends_with(':') {
            creds[0..creds.len() - 1].to_string()
        } else {
            creds.to_string()
        }
    }

    pub fn size_parse(s: &str) -> Result<u64, Error> {
        let mut s = s;
        if matches!(s.as_bytes().last(), Some(b'b' | b'B')) {
            s = &s[..s.len() - 1];
        }
        let mul: u64 = match s.as_bytes().last() {
            Some(b'k' | b'K') => 1024,
            Some(b'm' | b'M') => 1024 * 1024,
            Some(b'g' | b'G') => 1024 * 1024 * 1024,
            Some(b't' | b'T') => 1024 * 1024 * 1024 * 1024,
            Some(b'p' | b'P') => 1024 * 1024 * 1024 * 1024 * 1024,
            _ => 1,
        };
        if mul != 1 {
            s = &s[..s.len() - 1];
        }
        s.parse::<u64>()
            .map(|size| size * mul)
            .map_err(|e| Error(format!("{} not a valid size: {}", s, e)))
    }

    pub fn age_parse(s: &str) -> Result<u64, Error> {
        let mut s = s;
        let (mul, trim) = match s.as_bytes().last() {
            Some(b's') => (1, true),
            Some(b'm') => (60, true),
            Some(b'h') => (60 * 60, true),
            Some(b'd') => (60 * 60 * 24, true),
            _ => (1, false),
        };
        if trim {
            s = &s[..s.len() - 1];
        }
        s.parse::<u64>()
            .map(|age| age * mul)
            .map_err(|e| Error(format!("{} not a valid age: {}", s, e)))
    }

    fn proxy_parse(s: &str) -> Result<(IpCidr, String), Error> {
        match s.find(':') {
            None => Err(Error(format!("{} not in HEADER-NAME:CIDR format", s))),
            Some(0) => Err(Error(format!("{} sets invalid zero-length header", s))),
            Some(col_idx) => {
                let cidr: IpCidr = s[col_idx + 1..]
                    .parse()
                    .map_err(|e| Error(format!("{} not a valid CIDR: {}", &s[col_idx + 1..], e)))?;
                Ok((cidr, s[..col_idx].to_string()))
            }
        }
    }

    fn bandwidth_parse(s: &str) -> Result<Option<NonZeroU64>, Error> {
        let s = s.trim();
        let multiplier_b = s
            .as_bytes()
            .last()
            .ok_or_else(|| Error(format!("\"{}\" bandwidth specifier empty", s)))?;
        let multiplier_order = match multiplier_b {
            b'k' | b'K' => 1u32,
            b'm' | b'M' => 2,
            b'g' | b'G' => 3,
            b't' | b'T' => 4,
            b'p' | b'P' => 5,
            b'e' | b'E' => 6,
            _ => 0,
        };
        let (multiplier, s) = match multiplier_order {
            0 => (1u64, s),
            mo => {
                let base: u64 = if (*multiplier_b as char).is_uppercase() {
                    1024
                } else {
                    1000
                };
                (base.pow(mo), &s[..s.len() - 1])
            }
        };

        let number =
            u64::from_str(s).map_err(|e| Error(format!("\"{}\" not bandwidth size: {}", s, e)))?;
        Ok(NonZeroU64::new(number.checked_mul(multiplier).ok_or_else(
            || Error(format!("{} * {} too big", number, multiplier)),
        )?))
    }

    fn mime_type_override_parse(s: &str) -> Result<(OsString, String), Error> {
        match s.find(':') {
            None => Err(Error(format!("{} not in EXTENSION:MIME-TYPE format", s))),
            Some(col_idx) => {
                let mime_s = &s[col_idx + 1..];
                // Basic MIME type validation
                if !mime_s.contains('/') {
                    return Err(Error(format!("{} not a valid MIME type", mime_s)));
                }
                Ok((OsString::from(&s[..col_idx]), mime_s.to_string()))
            }
        }
    }

    fn header_parse(s: &str) -> Result<(String, Vec<u8>), Error> {
        s.split_once(':')
            .and_then(|(hn, mut hd)| {
                hd = hd.trim_start();
                if !hn.is_empty() && !hd.is_empty() {
                    Some((hn, hd))
                } else {
                    None
                }
            })
            .map(|(hn, hd)| (hn.to_string(), hd.as_bytes().to_vec()))
            .ok_or_else(|| Error(format!("\"{}\" invalid header format", s)))
    }
}