pub fn headers_valid(headers: &[Header], message_type: MessageType) -> Res<()> {
    let mut method_value: Option<&str> = None;
    let mut pseudo_state = EnumSet::new();
    for header in headers {
        let is_pseudo = track_pseudo(header.name(), &mut pseudo_state, message_type)?;

        let mut bytes = header.name().bytes();
        if is_pseudo {
            if header.name() == ":method" {
                method_value = Some(header.value());
            }
            let _ = bytes.next();
        }

        if bytes.any(|b| matches!(b, 0 | 0x10 | 0x13 | 0x3a | 0x41..=0x5a)) {
            return Err(Error::InvalidHeader); // illegal characters.
        }
    }
    // Clear the regular header bit, since we only check pseudo headers below.
    pseudo_state.remove(PseudoHeaderState::Regular);
    let pseudo_header_mask = match message_type {
        MessageType::Response => enum_set!(PseudoHeaderState::Status),
        MessageType::Request => {
            if method_value == Some("CONNECT") {
                PseudoHeaderState::Method | PseudoHeaderState::Authority
            } else {
                PseudoHeaderState::Method | PseudoHeaderState::Scheme | PseudoHeaderState::Path
            }
        }
    };

    if (MessageType::Request == message_type)
        && pseudo_state.contains(PseudoHeaderState::Protocol)
        && method_value != Some("CONNECT")
    {
        return Err(Error::InvalidHeader);
    }

    if pseudo_state & pseudo_header_mask != pseudo_header_mask {
        return Err(Error::InvalidHeader);
    }

    Ok(())
}
/// Checks if request/response headers are well formed, i.e. contain
/// allowed pseudo headers and in a right order, etc.
/// # Errors
/// Returns an error if headers are not well formed.
Checks if request/response headers are well formed, i.e. contain
allowed pseudo headers and in a right order, etc.
Checks if request / response headers are well formed i . e . contain allowed pseudo headers and in a right order etc .
pub fn headers_valid(headers: &[Header], message_type: MessageType) -> Res<()> {
    let mut method_value: Option<&str> = None;
    let mut pseudo_state = EnumSet::new();
    for header in headers {
        let is_pseudo = track_pseudo(header.name(), &mut pseudo_state, message_type)?;
        let mut bytes = header.name().bytes();
        if is_pseudo {
            if header.name() == ":method" {
                method_value = Some(header.value());
            }
            let _ = bytes.next();
        }
        if bytes.any(|b| matches!(b, 0 | 0x10 | 0x13 | 0x3a | 0x41..=0x5a)) {
            return Err(Error::InvalidHeader); // illegal characters.
        }
    }
    // Clear the regular header bit, since we only check pseudo headers below.
    pseudo_state.remove(PseudoHeaderState::Regular);
    let pseudo_header_mask = match message_type {
        MessageType::Response => enum_set!(PseudoHeaderState::Status),
        MessageType::Request => {
            if method_value == Some("CONNECT") {
                PseudoHeaderState::Method | PseudoHeaderState::Authority
            } else {
                PseudoHeaderState::Method | PseudoHeaderState::Scheme | PseudoHeaderState::Path
            }
        }
    };
    if (MessageType::Request == message_type)
        && pseudo_state.contains(PseudoHeaderState::Protocol)
        && method_value != Some("CONNECT")
    {
        return Err(Error::InvalidHeader);
    }
    if pseudo_state & pseudo_header_mask != pseudo_header_mask {
        return Err(Error::InvalidHeader);
    }
    Ok(())
}
pub fn headers_valid ( headers : & [ Header ] , message_type : MessageType ) -> Res < ( ) > { let mut method_value : Option < & str > = None ; let mut pseudo_state = EnumSet :: new ( ) ; for header in headers { let is_pseudo = track_pseudo ( header . name ( ) , & mut pseudo_state , message_type ) ? ; let mut bytes = header . name ( ) . bytes ( ) ; if is_pseudo { if header . name ( ) == " " { method_value = Some ( header . value ( ) ) ; } let _ = bytes . next ( ) ; } if bytes . any ( | b | matches ! ( b 0 0x10 0x13 0x3a 0x41 0x5a ) ) { return Err ( Error :: InvalidHeader ) ; // illegal characters. } } // Clear the regular header bit, since we only check pseudo headers below. pseudo_state . remove ( PseudoHeaderState :: Regular ) ; let pseudo_header_mask = match message_type { MessageType :: Response => enum_set ! ( PseudoHeaderState Status ) , MessageType :: Request => { if method_value == Some ( " " ) { PseudoHeaderState :: Method | PseudoHeaderState :: Authority } else { PseudoHeaderState :: Method | PseudoHeaderState :: Scheme | PseudoHeaderState :: Path } } } ; if ( MessageType :: Request == message_type ) && pseudo_state . contains ( PseudoHeaderState :: Protocol ) && method_value != Some ( " " ) { return Err ( Error :: InvalidHeader ) ; } if pseudo_state & pseudo_header_mask != pseudo_header_mask { return Err ( Error :: InvalidHeader ) ; } Ok ( ( ) ) }
Checks if request/response headers are well formed, i.e.
Checks if request / response headers are well formed i . e .
headers
&[Header]
message_type
MessageType
errors
Returns an error if headers are not well formed.
Returns an error if headers are not well formed .