wmproxy/prot/
mod.rs

1// Copyright 2022 - 2023 Wenmeng See the COPYRIGHT
2// file at the top-level directory of this distribution.
3// 
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8// 
9// Author: tickbh
10// -----
11// Created Date: 2023/09/22 09:59:56
12
13
14mod flag;
15mod create;
16mod close;
17mod data;
18mod frame;
19mod kind;
20mod mapping;
21mod token;
22
23pub use flag::ProtFlag;
24pub use kind::ProtKind;
25pub use create::ProtCreate;
26pub use close::ProtClose;
27pub use data::ProtData;
28pub use mapping::ProtMapping;
29pub use token::ProtToken;
30pub use frame::{ProtFrame, ProtFrameHeader};
31
32use webparse::{Buf, BufMut};
33
34use crate::ProxyResult;
35
36fn read_short_string<T: Buf>(buf: &mut T) -> ProxyResult<String> {
37    if buf.remaining() < 1 {
38        return Err(crate::ProxyError::TooShort);
39    }
40    let len = buf.get_u8() as usize;
41    if buf.remaining() < len {
42        return Err(crate::ProxyError::TooShort);
43    } else if len == 0 {
44        return Ok(String::new());
45    }
46    let s = String::from_utf8_lossy(&buf.chunk()[0..len]).to_string();
47    buf.advance(len);
48    Ok(s)
49}
50
51fn write_short_string<T: Buf + BufMut>(buf: &mut T, val: &str) -> ProxyResult<usize> {
52    let bytes = val.as_bytes();
53    if bytes.len() > 255 {
54        return Err(crate::ProxyError::TooShort);
55    }
56    let mut size = 0;
57    size += buf.put_u8(bytes.len() as u8);
58    size += buf.put_slice(bytes);
59    Ok(size)
60}