haprox_rs/protocol_v2/autogen/
mod.rs

1/*-
2 * haprox-rs - a HaProxy protocol parser.
3 * 
4 * Copyright 2025 (c) Aleksandr Morozov
5 * The scram-rs crate can be redistributed and/or modified
6 * under the terms of either of the following licenses:
7 *
8 *   1. the Mozilla Public License Version 2.0 (the “MPL”) OR
9 *
10 *   2. The MIT License (MIT)
11 *                     
12 *   3. EUROPEAN UNION PUBLIC LICENCE v. 1.2 EUPL © the European Union 2007, 2016
13 */
14
15use autogen_alpn::{AlpnType, ALPNS_LIST};
16
17
18
19pub mod autogen_alpn;
20
21/// A structure which describes the ALPNs.
22#[derive(Clone, Debug, PartialEq, Eq)]
23pub struct AlpnProtocolId
24{
25    pub ident: AlpnType,
26    pub ident_seq: &'static [u8],
27    pub ident_str: &'static str,
28    pub descr: &'static str,
29}
30
31impl AlpnProtocolId
32{
33    /// Returns the static reference to Alpn protocol ID record.
34    pub 
35    fn get_by_alpn_type(altp: AlpnType) -> &'static AlpnProtocolId
36    {
37        return &ALPNS_LIST[altp as usize];
38    }
39
40    /// Returns the statuc reference to Apln protocol ID record by the string identification
41    /// i.e http/1.0
42    pub 
43    fn get_by_ident_str(ident: &str) -> Option<&'static AlpnProtocolId>
44    {
45        let idx = ALPNS_LIST.binary_search_by_key(&ident,|v| v.ident_str).ok()?;
46
47        return Some(&ALPNS_LIST[idx]);
48    }
49
50    /// Returns the statuc reference to Apln protocol ID record by the string identification
51    /// i.e b"\x68\x32\x63" -> h2c
52    pub 
53    fn get_by_ident_seq(ident: &[u8]) -> Option<&'static AlpnProtocolId>
54    {
55        let idx = ALPNS_LIST.binary_search_by_key(&ident,|v| v.ident_seq).ok()?;
56
57        return Some(&ALPNS_LIST[idx]);
58    }
59}
60
61#[cfg(test)]
62mod tests
63{
64    use crate::protocol_v2::autogen::autogen_alpn::AlpnType;
65
66    use super::AlpnProtocolId;
67
68    #[test]
69    fn test_get_by_ident_str()
70    {
71        let a1 = AlpnProtocolId::get_by_ident_str("h2c");
72        assert_eq!(Some(AlpnProtocolId::get_by_alpn_type(AlpnType::Http2OverTcp)), a1);
73
74        let a1 = AlpnProtocolId::get_by_ident_str("http/1.0");
75        assert_eq!(Some(AlpnProtocolId::get_by_alpn_type(AlpnType::Http10)), a1);
76
77        let a1 = AlpnProtocolId::get_by_ident_str("https/1.0");
78        assert_eq!(None, a1);
79
80        let a1 = AlpnProtocolId::get_by_ident_seq(b"\x68\x32\x63");
81        assert_eq!(Some(AlpnProtocolId::get_by_alpn_type(AlpnType::Http2OverTcp)), a1);
82
83        let a1 = AlpnProtocolId::get_by_ident_seq(b"\x68\x74\x74\x70\x2f\x31\x2e\x30");
84        assert_eq!(Some(AlpnProtocolId::get_by_alpn_type(AlpnType::Http10)), a1);
85    }
86}