Skip to main content

sark_core/http/codec/
mod.rs

1pub mod decode;
2pub mod encode;
3mod header_utils;
4
5pub use decode::{BodyKind, DecodeMode, DecodedHead, HeaderScan, ParsedRequestHead, chunked};
6pub use encode::Wire;
7pub use header_utils::{Header, HeaderLookup};
8
9use crate::error::Result;
10
11pub struct Parse;
12
13#[derive(Clone, Copy, PartialEq, Eq, Debug)]
14pub enum BodyFraming {
15    Length(usize),
16    Chunked,
17}
18
19impl HeaderScan {
20    pub fn validate_for_request(&self) -> Result<BodyFraming> {
21        if self.duplicate_content_length {
22            return Err(crate::error::Error::BadRequest(
23                "Multiple Content-Length headers are not allowed".into(),
24            ));
25        }
26
27        if self.has_transfer_encoding {
28            if self.content_length.is_some() {
29                return Err(crate::error::Error::BadRequest(
30                    "Content-Length with Transfer-Encoding is not allowed".into(),
31                ));
32            }
33            if self.is_chunked_transfer {
34                return Ok(BodyFraming::Chunked);
35            }
36            return Err(crate::error::Error::BadRequest(
37                "Transfer-Encoding is not supported".into(),
38            ));
39        }
40
41        Ok(BodyFraming::Length(self.content_length.unwrap_or(0)))
42    }
43}