aboutsummaryrefslogtreecommitdiffstats
path: root/cms-backd/src/args.rs
blob: e552b02502d10541d7e60d4d2870fd7bfc365280 (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
// -*- coding: utf-8 -*-
//
// Simple CMS
//
// Copyright (C) 2011-2024 Michael Büsch <m@bues.ch>
//
// Licensed under the Apache License version 2.0
// or the MIT license, at your option.
// SPDX-License-Identifier: Apache-2.0 OR MIT

use crate::{cookie::Cookie, query::Query};
use cms_ident::CheckedIdent;

pub fn html_safe_escape(text: &str) -> String {
    html_escape::encode_safe(text).to_string()
}

pub struct CmsGetArgs {
    pub _host: String,
    pub path: CheckedIdent,
    pub _cookie: Cookie,
    pub query: Query,
    pub https: bool,
}

impl CmsGetArgs {
    pub fn protocol_str(&self) -> &str {
        if self.https {
            "https"
        } else {
            "http"
        }
    }
}

pub struct CmsPostArgs {
    pub body: Vec<u8>,
    pub body_mime: String,
}

pub fn get_query_var(get: &CmsGetArgs, variable_name: &str, escape: bool) -> String {
    if let Some(index) = variable_name.find('_') {
        let qname = &variable_name[index + 1..];
        if !qname.is_empty() {
            let qvalue = get.query.get_str(qname).unwrap_or_default();
            if escape {
                return html_safe_escape(&qvalue);
            } else {
                return qvalue;
            }
        }
    }
    Default::default()
}

// vim: ts=4 sw=4 expandtab
bues.ch cgit interface