| 1 |
#[cfg(feature = "read-http")] |
| 2 |
use crate::{Error, ReadSeek, Res}; |
| 3 |
#[cfg(feature = "read-http")] |
| 4 |
use std::borrow::BorrowMut; |
| 5 |
|
| 6 |
pub const HTAB: u8 = 0x09; |
| 7 |
#[cfg(feature = "read-http")] |
| 8 |
pub const NL: u8 = 0x0a; |
| 9 |
#[cfg(feature = "read-http")] |
| 10 |
pub const CR: u8 = 0x0d; |
| 11 |
pub const SP: u8 = 0x20; |
| 12 |
pub const COMMA: u8 = 0x2c; |
| 13 |
#[cfg(feature = "read-http")] |
| 14 |
pub const SLASH: u8 = 0x2f; |
| 15 |
#[cfg(feature = "read-http")] |
| 16 |
pub const COLON: u8 = 0x3a; |
| 17 |
#[cfg(feature = "read-http")] |
| 18 |
pub const SEMICOLON: u8 = 0x3b; |
| 19 |
|
| 20 |
pub fn is_ows(x: u8) -> bool { |
| 21 |
x == SP || x == HTAB |
| 22 |
} |
| 23 |
|
| 24 |
pub fn trim_ows(v: &[u8]) -> &[u8] { |
| 25 |
for s in 0..v.len() { |
| 26 |
if !is_ows(v[s]) { |
| 27 |
for e in (s..v.len()).rev() { |
| 28 |
if !is_ows(v[e]) { |
| 29 |
return &v[s..=e]; |
| 30 |
} |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
&v[..0] |
| 35 |
} |
| 36 |
|
| 37 |
#[cfg(feature = "read-http")] |
| 38 |
pub fn downcase(n: &mut [u8]) { |
| 39 |
for i in n { |
| 40 |
if *i >= 0x41 && *i <= 0x5a { |
| 41 |
*i += 0x20; |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
pub fn index_of(v: u8, line: &[u8]) -> Option<usize> { |
| 47 |
for (i, x) in line.iter().enumerate() { |
| 48 |
if *x == v { |
| 49 |
return Some(i); |
| 50 |
} |
| 51 |
} |
| 52 |
None |
| 53 |
} |
| 54 |
|
| 55 |
#[cfg(feature = "read-http")] |
| 56 |
pub fn split_at(v: u8, mut line: Vec<u8>) -> Option<(Vec<u8>, Vec<u8>)> { |
| 57 |
index_of(v, &line).map(|i| { |
| 58 |
let tail = line.split_off(i + 1); |
| 59 |
let _ = line.pop(); |
| 60 |
(line, tail) |
| 61 |
}) |
| 62 |
} |
| 63 |
|
| 64 |
#[cfg(feature = "read-http")] |
| 65 |
pub fn read_line<T, R>(r: &mut T) -> Res<Vec<u8>> |
| 66 |
where |
| 67 |
T: BorrowMut<R> + ?Sized, |
| 68 |
R: ReadSeek + ?Sized, |
| 69 |
{ |
| 70 |
let mut buf = Vec::new(); |
| 71 |
r.borrow_mut().read_until(NL, &mut buf)?; |
| 72 |
let tail = buf.pop(); |
| 73 |
if tail != Some(NL) { |
| 74 |
return Err(Error::Truncated); |
| 75 |
} |
| 76 |
if buf.pop().ok_or(Error::Missing(CR))? == CR { |
| 77 |
Ok(buf) |
| 78 |
} else { |
| 79 |
Err(Error::Missing(CR)) |
| 80 |
} |
| 81 |
} |