summaryrefslogtreecommitdiff
path: root/crates/common/src/config/network.rs
blob: b7c3faa5a81935c8a47d369875ce29e50092cfac (plain)
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
/*
 * SPDX-FileCopyrightText: 2020 Stalwart Labs Ltd <hello@stalw.art>
 *
 * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-SEL
 */

use crate::{
    expr::{if_block::IfBlock, tokenizer::TokenMap},
    Network,
};
use utils::config::Config;

use super::*;

pub(crate) const HTTP_VARS: &[u32; 11] = &[
    V_LISTENER,
    V_REMOTE_IP,
    V_REMOTE_PORT,
    V_LOCAL_IP,
    V_LOCAL_PORT,
    V_PROTOCOL,
    V_TLS,
    V_URL,
    V_URL_PATH,
    V_HEADERS,
    V_METHOD,
];

impl Default for Network {
    fn default() -> Self {
        Self {
            security: Default::default(),
            node_id: 0,
            http_response_url: IfBlock::new::<()>(
                "server.http.url",
                [],
                "protocol + '://' + key_get('default', 'hostname') + ':' + local_port",
            ),
            http_allowed_endpoint: IfBlock::new::<()>("server.http.allowed-endpoint", [], "200"),
        }
    }
}

impl Network {
    pub fn parse(config: &mut Config) -> Self {
        let mut network = Network {
            node_id: config.property("cluster.node-id").unwrap_or_default(),
            security: Security::parse(config),
            ..Default::default()
        };
        let token_map = &TokenMap::default().with_variables(HTTP_VARS);

        for (value, key) in [
            (&mut network.http_response_url, "server.http.url"),
            (
                &mut network.http_allowed_endpoint,
                "server.http.allowed-endpoint",
            ),
        ] {
            if let Some(if_block) = IfBlock::try_parse(config, key, token_map) {
                *value = if_block;
            }
        }

        network
    }
}