haprox_rs/protocol/autogen/
mod.rs

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