~boringcactus/gemifedi

ref: b5f2b8c75384306ecf3dcb9fc45ef62065bb3f00 gemifedi/src/gemini_util.rs -rw-r--r-- 1.3 KiB
b5f2b8c7 — Melody Horn bump to v0.3.0 1 year, 7 months ago
                                                                                
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
use maj::{
    gemini as gemtext,
    Response as GeminiResponse,
    server::{
        Request as GeminiRequest,
    },
};

pub trait RequestExt {
    fn cert_hash(&self) -> Option<String>;
}

fn cert_hash(certificate: &rustls::Certificate) -> String {
    use sha2::Digest;
    format!("{:x}", sha2::Sha256::digest(certificate.as_ref()))
}

impl RequestExt for GeminiRequest {
    fn cert_hash(&self) -> Option<String> {
        match &self.certs {
            Some(cert) => Some(cert_hash(&cert[0])),
            None => None,
        }
    }
}

pub fn strip_html(html: &str) -> Vec<gemtext::Node> {
    crate::html2gemtext::parse_html(html)
}

pub fn temp_redirect<I: Into<String>>(dest: I) -> GeminiResponse {
    let mut redirect = GeminiResponse::perm_redirect(dest.into());
    redirect.status = maj::StatusCode::TemporaryRedirect;
    redirect
}

pub fn apply_global_template(nodes: Vec<gemtext::Node>) -> Vec<gemtext::Node> {
    let mut result = vec![
    ];
    result.extend(nodes);
    let footer = gemtext::Builder::new()
        .link("/", Some("timeline".to_string()))
        .link("/logout", Some("logout".to_string()))
        .link("/about", Some(format!("about gemifedi v{}", env!("CARGO_PKG_VERSION"))))
        .build();
    result.extend(footer);
    result
}