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
#![allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]

use core::fmt;
use std::{
    env, fs,
    path::{Path, PathBuf},
    process,
    str::FromStr,
    sync::Arc,
    time::Duration,
};

use jsonrs::Json;
pub use pool::PoolConfig;

use crate::{
    Result,
    log::{self},
    log_info, log_warn,
};

#[derive(Clone, Debug)]
pub enum Preset {
    Read,
    ReadWrite,
    Post,
}

#[derive(Clone)]
pub struct ServerConfig {
    pub port: u16,
    pub pool_conf: PoolConfig,
    pub keep_alive_timeout: Duration,
    pub keep_alive_requests: u16,
    pub log_file: Option<String>,
    pub setup_lib: Option<String>,
    pub preset: Preset,

    #[cfg(feature = "tls")]
    pub tls_config: Option<Arc<rustls::ServerConfig>>,
}

impl fmt::Debug for ServerConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut deb = f.debug_struct("ServerConfig");
        deb.field("port", &self.port)
            .field("pool_conf", &self.pool_conf)
            .field("keep_alive_timeout", &self.keep_alive_timeout)
            .field("keep_alive_requests", &self.keep_alive_requests)
            .field("setup_lib", &self.setup_lib)
            .field("preset", &self.preset)
            .field("log_file", &self.log_file);

        #[cfg(feature = "tls")]
        deb.field("tls", &self.tls_config.is_some());

        deb.finish()
    }
}

#[cfg(not(test))]
fn get_default_conf_file() -> Option<PathBuf> {
    if let Ok(path) = env::var("XDG_CONFIG_HOME") {
        let mut p = PathBuf::new();
        p.push(path);
        p.push("http-srv");
        p.push("config.json");
        Some(p)
    } else if let Ok(path) = env::var("HOME") {
        let mut p = PathBuf::new();
        p.push(path);
        p.push(".config");
        p.push("http-srv");
        p.push("config.json");
        Some(p)
    } else {
        None
    }
}

#[cfg(test)]
fn get_default_conf_file() -> Option<PathBuf> {
    None
}

#[cfg(feature = "tls")]
#[allow(clippy::unwrap_used)]
fn get_tls_config(cert: Option<String>, pkey: Option<String>) -> Result<Arc<rustls::ServerConfig>> {
    use rustls::pki_types::{CertificateDer, PrivateKeyDer, pem::PemObject};

    let Some(cert) = cert else {
        return Err("Missing certificate file".into());
    };
    let Some(pkey) = pkey else {
        return Err("Missing private key file".into());
    };

    let certs = CertificateDer::pem_file_iter(cert)
        .unwrap()
        .map(|cert| cert.unwrap())
        .collect();
    let private_key = PrivateKeyDer::from_pem_file(pkey).unwrap();
    let config = rustls::ServerConfig::builder()
        .with_no_client_auth()
        .with_single_cert(certs, private_key)
        .map_err(|err| format!("rustls: {err}"))?;

    Ok(Arc::new(config))
}

fn parse_preset(arg: &str, is_from_cli: bool) -> Result<Preset> {
    Ok(match arg {
        "read" => Preset::Read,
        "readwrite" => Preset::ReadWrite,
        "post" => Preset::Post,
        p => {
            return Err(format!(
                "Unknown argument for \"{}preset\": {p}.\
                Valid values: read, readwrite, post",
                if is_from_cli { "--" } else { "" }
            )
            .into());
        }
    })
}

/// [`crate::HttpServer`] configuration
///
/// # Example
/// ```
/// use http_srv::ServerConfig;
/// use pool::PoolConfig;
///
/// let pool_conf = PoolConfig::builder()
///                 .n_workers(120_u16)
///                 .build();
/// let conf =
/// ServerConfig::default()
///     .port(8080)
///     .pool_config(pool_conf);
/// ```
impl ServerConfig {
    /// Parse the configuration from the command line args
    pub fn parse<S: AsRef<str>>(args: &[S]) -> Result<Self> {
        const CONFIG_FILE_ARG: &str = "--conf";

        let mut conf = Self::default();

        let mut conf_file = get_default_conf_file();

        /* Parse the --config-file before the rest */
        let mut first_pass = args.iter();
        while let Some(arg) = first_pass.next() {
            if arg.as_ref() == CONFIG_FILE_ARG {
                let fname = first_pass
                    .next()
                    .ok_or_else(|| format!("Missing argument for \"{CONFIG_FILE_ARG}\""))?;
                let filename = PathBuf::from(fname.as_ref());
                if filename.exists() {
                    conf_file = Some(filename);
                } else {
                    log_warn!(
                        "Config path: {} doesn't exist",
                        filename.as_os_str().to_str().unwrap_or("[??]")
                    );
                }
            }
        }

        if let Some(cfile) = conf_file {
            conf.parse_conf_file(&cfile)?;
        }

        let mut pool_conf_builder = PoolConfig::builder();

        #[cfg(feature = "tls")]
        let mut tls = false;
        #[cfg(feature = "tls")]
        let mut cert: Option<String> = None;
        #[cfg(feature = "tls")]
        let mut privkey: Option<String> = None;

        let mut args = args.iter();
        while let Some(arg) = args.next() {
            macro_rules! parse_next {
                () => {
                    args.next_parse().ok_or_else(|| {
                        format!("Missing or incorrect argument for \"{}\"", arg.as_ref())
                    })?
                };
                (as $t:ty) => {{
                    let _next: $t = parse_next!();
                    _next
                }};
            }

            match arg.as_ref() {
                "-p" | "--port" => conf.port = parse_next!(),
                "-n" | "-n-workers" => {
                    pool_conf_builder.set_n_workers(parse_next!(as u16));
                }
                "-d" | "--dir" => {
                    let path: String = parse_next!();
                    env::set_current_dir(Path::new(&path))?;
                }
                "-k" | "--keep-alive" => {
                    let timeout = parse_next!();
                    conf.keep_alive_timeout = Duration::from_secs_f32(timeout);
                }
                "-r" | "--keep-alive-requests" => conf.keep_alive_requests = parse_next!(),
                "-l" | "--log" => conf.log_file = Some(parse_next!()),
                "--license" => license(),
                "--log-level" => {
                    let n: u8 = parse_next!();
                    log::set_level(n.try_into()?);
                }
                "--preset" => {
                    let arg = args.next().ok_or("Missing argument for \"--preset\"")?;
                    conf.preset = parse_preset(arg.as_ref(), true)?;
                }

                "--setup-lib" => conf.setup_lib = Some(parse_next!()),

                #[cfg(feature = "tls")]
                "--tls" => tls = true,

                #[cfg(feature = "tls")]
                "--cert-file" => cert = Some(parse_next!()),

                #[cfg(feature = "tls")]
                "--private-key" => privkey = Some(parse_next!()),

                CONFIG_FILE_ARG => {
                    let _ = args.next();
                }
                "-h" | "--help" => help(),
                unknown => return Err(format!("Unknow argument: {unknown}").into()),
            }
        }

        conf.pool_conf = pool_conf_builder.build();

        #[cfg(feature = "tls")]
        if conf.tls_config.is_none() && tls {
            conf.tls_config = Some(get_tls_config(cert, privkey)?);
        }

        log_info!("{conf:#?}");
        Ok(conf)
    }
    #[allow(clippy::too_many_lines)]
    fn parse_conf_file(&mut self, conf_file: &Path) -> crate::Result<()> {
        if !conf_file.exists() {
            return Ok(());
        }
        let conf_str = conf_file.as_os_str().to_str().unwrap_or("");
        let f = fs::read_to_string(conf_file).unwrap_or_else(|err| {
            eprintln!("Error reading config file \"{conf_str}\": {err}");
            std::process::exit(1);
        });
        let json = Json::deserialize(&f).unwrap_or_else(|err| {
            eprintln!("Error parsing config file: {err}");
            std::process::exit(1);
        });
        log_info!("Parsing config file: {conf_str}");
        let Json::Object(obj) = json else {
            return Err("Expected json object".into());
        };

        #[cfg(feature = "tls")]
        let mut tls = false;

        #[cfg(feature = "tls")]
        let mut cert: Option<String> = None;

        #[cfg(feature = "tls")]
        let mut privkey: Option<String> = None;

        for (k, v) in obj {
            macro_rules! num {
                () => {
                    num!(v)
                };
                ($v:ident) => {
                    $v.number().ok_or_else(|| {
                        format!("Parsing config file ({conf_str}): Expected number for \"{k}\"")
                    })?
                };
                ($v:ident as $t:ty) => {{
                    let _n = num!($v);
                    _n as $t
                }};
            }
            macro_rules! bool {
                ($v:ident) => {
                    $v.boolean().ok_or_else(|| {
                        format!("Parsing config file ({conf_str}): Expected boolean for \"{k}\"")
                    })?
                };
            }
            macro_rules! string {
                ($v:ident) => {
                    $v.string()
                        .ok_or_else(|| {
                            format!("Parsing config file ({conf_str}): Expected string for \"{k}\"")
                        })?
                        .to_string()
                };
                () => {
                    string!(v)
                };
            }
            macro_rules! obj {
                () => {
                    v.object().ok_or_else(|| {
                        format!("Parsing config file ({conf_str}): Expected object for \"{k}\"")
                    })?
                };
            }

            macro_rules! path {
                ($v:ident) => {{
                    let path: String = string!($v);
                    path.replacen(
                        '~',
                        env::var("HOME").as_ref().map(String::as_str).unwrap_or("~"),
                        1,
                    )
                }};

                () => {
                    path!(v)
                };
            }

            match &*k {
                "port" => self.port = num!() as u16,
                "root_dir" => {
                    let path = path!();
                    env::set_current_dir(Path::new(&path))?;
                }
                "keep_alive_timeout" => self.keep_alive_timeout = Duration::from_secs_f64(num!()),
                "keep_alive_requests" => self.keep_alive_requests = num!() as u16,
                "log_file" => self.log_file = Some(string!()),
                "log_level" => {
                    let n = num!(v as u8);
                    log::set_level(n.try_into()?);
                }
                "preset" => {
                    let arg = string!(v);
                    self.preset = parse_preset(arg.as_str(), false)?;
                }
                #[cfg(feature = "tls")]
                "tls" => {
                    for (k, v) in obj!() {
                        match &**k {
                            "enabled" => tls = bool!(v),
                            "cert_file" => cert = Some(path!(v)),
                            "private_key" => privkey = Some(path!(v)),
                            _ => log_warn!(
                                "Parsing config file ({conf_str}): Unexpected key: \"{k}\""
                            ),
                        }
                    }
                }
                "setup_lib" if self.setup_lib.is_none() => {
                    self.setup_lib = Some(path!(v));
                }
                "pool_config" => {
                    for (k, v) in obj!() {
                        match &**k {
                            "n_workers" => self.pool_conf.n_workers = num!(v as u16),
                            "pending_buffer_size" => {
                                let n = v.number().map(|n| n as u16);
                                self.pool_conf.incoming_buf_size = n;
                            }
                            _ => log_warn!(
                                "Parsing config file ({conf_str}): Unexpected key: \"{k}\""
                            ),
                        }
                    }
                }
                _ => log_warn!("Parsing config file ({conf_str}): Unexpected key: \"{k}\""),
            }
        }

        #[cfg(feature = "tls")]
        if tls {
            self.tls_config = Some(get_tls_config(cert, privkey)?);
        }

        Ok(())
    }
    #[inline]
    #[must_use]
    pub fn pool_config(mut self, conf: PoolConfig) -> Self {
        self.pool_conf = conf;
        self
    }
    #[inline]
    #[must_use]
    pub fn port(mut self, port: u16) -> Self {
        self.port = port;
        self
    }
    #[inline]
    #[must_use]
    pub fn keep_alive_timeout(mut self, timeout: Duration) -> Self {
        self.keep_alive_timeout = timeout;
        self
    }
    #[inline]
    #[must_use]
    pub fn keep_alive_requests(mut self, n: u16) -> Self {
        self.keep_alive_requests = n;
        self
    }

    #[inline]
    #[must_use]
    pub fn preset(mut self, p: Preset) -> Self {
        self.preset = p;
        self
    }
}

fn help() -> ! {
    /* FIXME: Don't output tls options if the tls feature is disabled */
    println!(
        "\
http-srv: Copyright (C) 2025 Saúl Valdelvira

This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, version 3.
Use http-srv --license to read a copy of the GPL v3

USAGE: http-srv [-p <port>] [-n <n-workers>] [-d <working-dir>]
PARAMETERS:
    -p, --port <port>    TCP Port to listen for requests
    -n, --n-workers <n>  Number of concurrent workers
    -d, --dir <working-dir>  Root directory of the server
    -k, --keep-alive <sec>   Keep alive seconds
    -r, --keep-alive-requests <num> Keep alive max requests
    -l, --log <file>   Set log file
    -h, --help      Display this help message
    --log-level <n> Set log level
    --setup-lib <file> Load the given file to setup the server
    --conf <file>   Use the given config file instead of the default one
    --license       Output the license of this program
    --preset <read|readwrite|post>  Sets a default handler preset
        read: Only GET and HEAD methods are allowed
        readwrite: GET, HEAD, POST and DELETE are allowed
        post: Only POST is allowed

    --tls           Enable TLS
    --cert-file     Certificate file for TLS
    --private-key   Private key for TLS
EXAMPLES:
  http-srv -p 8080 -d /var/html
  http-srv -d ~/desktop -n 1024 --keep-alive 120
  http-srv --log /var/log/http-srv.log"
    );
    process::exit(0);
}

fn license() -> ! {
    println!(include_str!("../COPYING"));
    process::exit(0);
}

trait ParseIterator {
    fn next_parse<T: FromStr>(&mut self) -> Option<T>;
}

impl<I, R: AsRef<str>> ParseIterator for I
where
    I: Iterator<Item = R>,
{
    fn next_parse<T: FromStr>(&mut self) -> Option<T> {
        self.next()?.as_ref().parse().ok()
    }
}

impl Default for ServerConfig {
    /// Default configuration
    ///
    /// - Port: 80
    /// - Preset: Read
    /// - Nº Workers: 1024
    /// - Keep Alive Timeout: 0s (Disabled)
    /// - Keep Alove Requests: 10000
    #[inline]
    fn default() -> Self {
        Self {
            port: 80,
            pool_conf: PoolConfig::default(),
            keep_alive_timeout: Duration::from_secs(0),
            keep_alive_requests: 10000,
            log_file: None,
            setup_lib: None,
            preset: Preset::Read,
            #[cfg(feature = "tls")]
            tls_config: None,
        }
    }
}

#[cfg(test)]
mod test {
    #![allow(clippy::unwrap_used)]

    use crate::ServerConfig;

    #[test]
    fn valid_args() {
        let conf = vec!["-p".to_string(), "80".to_string()];
        ServerConfig::parse(&conf).unwrap();
    }

    macro_rules! expect_err {
        ($conf:expr , $msg:literal) => {
            match ServerConfig::parse(&$conf) {
                Ok(c) => panic!("Didn't panic: {c:#?}"),
                Err(msg) => assert_eq!(msg.get_message(), $msg),
            }
        };
    }

    #[test]
    fn unknown() {
        let conf = vec!["?"];
        expect_err!(conf, "Unknow argument: ?");
    }

    #[test]
    fn missing() {
        let conf = vec!["-n"];
        expect_err!(conf, "Missing or incorrect argument for \"-n\"");
    }

    #[test]
    fn parse_error() {
        let conf = vec!["-p", "abc"];
        expect_err!(conf, "Missing or incorrect argument for \"-p\"");
    }
}