use super::*;
pub(crate) fn fill_protocol_request(
config: &RequestConfig,
head: ProtocolRequestHead,
request: &mut Request,
target: &mut String,
) -> Result<(), RequestError> {
let ProtocolRequestHead {
version,
method,
parts,
body,
} = head;
let uri: &Uri = &parts.uri;
write_request_target(uri.path(), uri.query(), target);
let max_path_size: usize = config.get_max_path_size();
if target.len() > max_path_size && max_path_size != DEFAULT_LOW_SECURITY_MAX_PATH_SIZE {
return Err(RequestError::PathTooLong(HttpStatus::URITooLong));
}
let max_body_size: usize = config.get_max_body_size();
if body.len() > max_body_size && max_body_size != DEFAULT_LOW_SECURITY_MAX_BODY_SIZE {
return Err(RequestError::ContentLengthTooLarge(
HttpStatus::PayloadTooLarge,
));
}
check_request_authority(&parts.headers, uri)?;
request.get_mut_querys().clear();
let query_index: Option<usize> = target.find(QUESTION_MARK);
let hash_index: Option<usize> = target.find(HASH);
fill_request_querys(
split_target_query(target, query_index, hash_index),
request.get_mut_querys(),
);
let req_path: &mut String = request.get_mut_path();
req_path.clear();
req_path.push_str(split_target_path(target, query_index, hash_index));
request.set_method(protocol_method(method));
request.set_version(version);
fill_request_headers(&parts.headers, config, request)?;
write_request_host(&parts.headers, uri, request.get_mut_host());
if !body.is_empty() {
*request.get_mut_body() = body;
} else {
request.get_mut_body().clear();
}
Ok(())
}
pub(crate) fn write_request_host(headers: &ProtocolHeaders, uri: &Uri, out: &mut String) {
out.clear();
if let Some(authority) = uri.authority() {
out.push_str(authority.as_str());
return;
}
for name in [HOST, COLON_AUTHORITY] {
if let Some(value) = headers.get(name)
&& let Ok(value) = value.to_str()
{
out.push_str(value);
return;
}
}
}
pub(crate) fn write_request_target(path: &str, query: Option<&str>, target: &mut String) {
target.clear();
match query {
Some(query) => {
target.reserve(path.len() + query.len() + 1);
target.push_str(path);
target.push(QUESTION_MARK);
target.push_str(query);
}
None => {
target.push_str(path);
}
}
}
fn is_valid_authority_value(value: &str) -> bool {
if value.is_empty() {
return false;
}
for byte in value.bytes() {
let forbidden: bool = match byte {
b'@' | b'/' | b'\\' | b'?' | b'#' | b',' | b';' => true,
_ => byte <= 0x20 || byte >= 0x7f,
};
if forbidden {
return false;
}
}
let (host, port): (&str, Option<&str>) = split_authority_port(value);
if host.is_empty() {
return false;
}
if host.starts_with('[') {
let Some(inner) = host
.strip_prefix('[')
.and_then(|s: &str| s.strip_suffix(']'))
else {
return false;
};
if inner.is_empty()
|| !inner
.bytes()
.all(|b: u8| b.is_ascii_hexdigit() || b == b':' || b == b'.')
{
return false;
}
} else if host.contains('[') || host.contains(']') || host.contains(':') {
return false;
}
if let Some(port) = port {
let all_digits: bool = port.bytes().all(|b: u8| b.is_ascii_digit());
if port.is_empty() || !all_digits {
return false;
}
if port.parse::<u16>().is_err() {
return false;
}
}
true
}
fn is_forbidden_h2_header(name: &str, value: &[u8]) -> bool {
match name {
CONNECTION | KEEP_ALIVE | PROXY_CONNECTION | TRANSFER_ENCODING | UPGRADE => true,
TE => !value.eq_ignore_ascii_case(HTTP_TE_TRAILERS.as_bytes()),
_ => false,
}
}
pub(crate) fn has_forbidden_h2_request_header(headers: &ProtocolHeaders) -> bool {
headers
.iter()
.any(|(name, value): (&HeaderName, &HeaderValue)| {
is_forbidden_h2_header(name.as_str(), value.as_bytes())
})
}
pub fn check_request_authority(headers: &ProtocolHeaders, uri: &Uri) -> Result<(), RequestError> {
let host_count: usize = headers.get_all(HOST).iter().count();
if host_count > 1 {
return Err(RequestError::InvalidUrlHost(HttpStatus::BadRequest));
}
let authority: Option<&str> = uri
.authority()
.map(|authority: &Authority| authority.as_str());
let host: Option<&str> = match headers.get(HOST) {
Some(value) => match value.to_str() {
Ok(value) => Some(value),
Err(_) => return Err(RequestError::InvalidUrlHost(HttpStatus::BadRequest)),
},
None => match headers.get(COLON_AUTHORITY) {
Some(value) => match value.to_str() {
Ok(value) => Some(value),
Err(_) => return Err(RequestError::InvalidUrlHost(HttpStatus::BadRequest)),
},
None => None,
},
};
if let Some(authority) = authority
&& !is_valid_authority_value(authority)
{
return Err(RequestError::InvalidUrlHost(HttpStatus::BadRequest));
}
if let Some(host) = host
&& !is_valid_authority_value(host)
{
return Err(RequestError::InvalidUrlHost(HttpStatus::BadRequest));
}
let (Some(authority), Some(host)) = (authority, host) else {
return Ok(());
};
let scheme: Option<&str> = uri.scheme_str();
if !same_authority(authority, host, scheme) {
return Err(RequestError::InvalidUrlHost(HttpStatus::BadRequest));
}
Ok(())
}
pub fn same_authority(authority: &str, host: &str, scheme: Option<&str>) -> bool {
if authority.eq_ignore_ascii_case(host) {
return true;
}
let (auth_host, auth_port): (&str, Option<&str>) = split_authority_port(authority);
let (host_host, host_port): (&str, Option<&str>) = split_authority_port(host);
if !auth_host.eq_ignore_ascii_case(host_host) {
return false;
}
let default_port: Option<&str> = match scheme {
Some(HTTP_SCHEME) => Some(DEFAULT_HTTP_PORT_STR),
Some(HTTPS_SCHEME) => Some(DEFAULT_HTTPS_PORT_STR),
_ => None,
};
let effective_auth_port: Option<&str> = match (auth_port, default_port) {
(Some(port), Some(default_port)) if port == default_port => None,
(port, _) => port,
};
let effective_host_port: Option<&str> = match (host_port, default_port) {
(Some(port), Some(default_port)) if port == default_port => None,
(port, _) => port,
};
effective_auth_port == effective_host_port
}
#[cfg(test)]
pub fn normalize_authority(authority: &str, scheme: Option<&str>) -> (String, Option<String>) {
let (host, port): (&str, Option<&str>) = split_authority_port(authority);
let host: String = host.to_ascii_lowercase();
let port: Option<String> = port.map(|port: &str| port.to_owned());
let default_port: Option<&str> = match scheme {
Some(HTTP_SCHEME) => Some(DEFAULT_HTTP_PORT_STR),
Some(HTTPS_SCHEME) => Some(DEFAULT_HTTPS_PORT_STR),
_ => None,
};
match (port, default_port) {
(Some(port), Some(default_port)) if port == default_port => (host, None),
(port, _) => (host, port),
}
}
pub fn split_authority_port(authority: &str) -> (&str, Option<&str>) {
let authority: &str = authority.trim();
if authority.starts_with('[') {
return match authority.find(']') {
Some(end) => match authority[end + 1..].strip_prefix(':') {
Some(port) => (&authority[..end + 1], Some(port)),
None => (authority, None),
},
None => (authority, None),
};
}
match authority.rsplit_once(':') {
Some((host, port)) if !host.is_empty() && !host.contains(':') => (host, Some(port)),
_ => (authority, None),
}
}
pub fn split_target_query(
target: &str,
query_index: Option<usize>,
hash_index: Option<usize>,
) -> &str {
match (query_index, hash_index) {
(Some(query_index), Some(hash_index)) if hash_index < query_index => EMPTY_STR,
(Some(query_index), Some(hash_index)) => &target[query_index + 1..hash_index],
(Some(query_index), None) => &target[query_index + 1..],
(None, _) => EMPTY_STR,
}
}
pub fn split_target_path(
target: &str,
query_index: Option<usize>,
hash_index: Option<usize>,
) -> &str {
match (query_index, hash_index) {
(Some(query_index), Some(hash_index)) => &target[..query_index.min(hash_index)],
(Some(query_index), None) => &target[..query_index],
(None, Some(hash_index)) => &target[..hash_index],
(None, None) => target,
}
}
pub fn fill_request_querys(query: &str, querys: &mut RequestQuerys) {
if query.is_empty() {
return;
}
let mut param_count: usize = 0;
for pair in query.split(AND) {
if param_count >= PROTOCOL_MAX_QUERY_PARAM_COUNT {
break;
}
if let Some((key, value)) = pair.split_once(EQUAL) {
if !key.is_empty() {
param_count += 1;
if let Some(existing) = querys.get_mut(key) {
existing.push_back(value.to_owned());
} else {
let mut values: VecDeque<String> = VecDeque::with_capacity(1);
values.push_back(value.to_owned());
querys.insert(key.to_owned(), values);
}
}
} else if !pair.is_empty() {
param_count += 1;
if !querys.contains_key(pair) {
querys.insert(pair.to_owned(), VecDeque::new());
}
}
}
}
pub fn protocol_method(method: ProtocolMethod) -> RequestMethod {
if method == ProtocolMethod::GET {
return Method::Get;
}
if method == ProtocolMethod::POST {
return Method::Post;
}
if method == ProtocolMethod::PUT {
return Method::Put;
}
if method == ProtocolMethod::DELETE {
return Method::Delete;
}
if method == ProtocolMethod::PATCH {
return Method::Patch;
}
if method == ProtocolMethod::HEAD {
return Method::Head;
}
if method == ProtocolMethod::OPTIONS {
return Method::Options;
}
if method == ProtocolMethod::CONNECT {
return Method::Connect;
}
if method == ProtocolMethod::TRACE {
return Method::Trace;
}
Method::Unknown(method.as_str().to_owned())
}
pub fn fill_request_headers(
headers: &ProtocolHeaders,
config: &RequestConfig,
request: &mut Request,
) -> Result<(), RequestError> {
let max_header_count: usize = config.get_max_header_count();
let max_header_key_size: usize = config.get_max_header_key_size();
let max_header_value_size: usize = config.get_max_header_value_size();
let target_headers: &mut RequestHeaders = request.get_mut_headers();
let mut header_count: usize = 0;
PROTOCOL_HEADER_POOL.with(|pool: &RefCell<Vec<(String, VecDeque<String>)>>| {
let mut pool_guard: RefMut<'_, Vec<(String, VecDeque<String>)>> = pool.borrow_mut();
for (name, value) in headers.iter() {
let key: &str = name.as_str();
if key.starts_with(PSEUDO_HEADER_PREFIX) {
continue;
}
if is_forbidden_h2_header(key, value.as_bytes()) {
return Err(RequestError::HttpRead(HttpStatus::BadRequest));
}
let Ok(value) = value.to_str() else {
return Err(RequestError::HttpRead(HttpStatus::BadRequest));
};
header_count += 1;
if header_count > max_header_count
&& max_header_count != DEFAULT_LOW_SECURITY_MAX_HEADER_COUNT
{
return Err(RequestError::TooManyHeaders(
HttpStatus::RequestHeaderFieldsTooLarge,
));
}
if key.len() > max_header_key_size
&& max_header_key_size != DEFAULT_LOW_SECURITY_MAX_HEADER_KEY_SIZE
{
return Err(RequestError::HeaderKeyTooLong(HttpStatus::BadRequest));
}
if value.len() > max_header_value_size
&& max_header_value_size != DEFAULT_LOW_SECURITY_MAX_HEADER_VALUE_SIZE
{
return Err(RequestError::HeaderValueTooLong(HttpStatus::BadRequest));
}
if let Some(existing) = target_headers.get_mut(key) {
existing.push_back(value.to_owned());
continue;
}
let (mut pooled_key, mut pooled_values): (String, VecDeque<String>) = pool_guard
.pop()
.unwrap_or_else(|| (String::new(), VecDeque::new()));
pooled_key.clear();
pooled_key.push_str(key);
while pooled_values.len() > 1 {
pooled_values.pop_back();
}
if let Some(slot) = pooled_values.front_mut() {
slot.clear();
slot.push_str(value);
} else {
pooled_values.push_back(value.to_owned());
}
target_headers.insert(pooled_key, pooled_values);
}
Ok(())
})
}
pub fn is_hop_by_hop(name: &str) -> bool {
matches!(
name,
CONNECTION
| KEEP_ALIVE
| PROXY_AUTHENTICATE
| PROXY_AUTHORIZATION
| TE
| TRAILER_HEADER
| TRANSFER_ENCODING
| UPGRADE
)
}
pub fn declared_content_length(
headers: &ProtocolHeaders,
max_body_size: usize,
) -> Result<Option<usize>, RequestError> {
let mut declared: Option<usize> = None;
for value in headers.get_all(CONTENT_LENGTH).iter() {
if declared.is_some() {
return Err(RequestError::InvalidContentLength(HttpStatus::BadRequest));
}
let Ok(value) = value.to_str() else {
return Err(RequestError::InvalidContentLength(HttpStatus::BadRequest));
};
let parsed: usize = parse_ascii_length(value)?;
if parsed > max_body_size && max_body_size != DEFAULT_LOW_SECURITY_MAX_BODY_SIZE {
return Err(RequestError::ContentLengthTooLarge(
HttpStatus::PayloadTooLarge,
));
}
declared = Some(parsed);
}
Ok(declared)
}
#[cold]
pub(crate) fn has_continue_expectation(headers: &ProtocolHeaders) -> bool {
headers
.get_all(EXPECT)
.iter()
.any(|value: &HeaderValue| contains_continue_member(value.as_bytes()))
}
pub(crate) fn contains_continue_member(value: &[u8]) -> bool {
let token: &[u8] = CONTINUE_EXPECT.as_bytes();
let mut start: usize = 0;
let mut quoted: bool = false;
let mut escaped: bool = false;
for (offset, byte) in value.iter().copied().enumerate() {
match byte {
_ if escaped => escaped = false,
HTTP_QUOTED_FIELD_ESCAPE if quoted => escaped = true,
HTTP_QUOTED_FIELD_QUOTE => quoted = !quoted,
COMMA_U8 if !quoted => {
if value[start..offset]
.trim_ascii()
.eq_ignore_ascii_case(token)
{
return true;
}
start = offset + 1;
}
_ => {}
}
}
value[start..].trim_ascii().eq_ignore_ascii_case(token)
}
pub(crate) fn parse_ascii_length(value: &str) -> Result<usize, RequestError> {
let digits: &str = value.trim();
if digits.is_empty() || !digits.bytes().all(|byte: u8| byte.is_ascii_digit()) {
return Err(RequestError::InvalidContentLength(HttpStatus::BadRequest));
}
digits
.parse::<usize>()
.map_err(|_: ParseIntError| RequestError::InvalidContentLength(HttpStatus::BadRequest))
}
pub fn is_bodyless_status(status_code: usize, is_head: bool) -> bool {
is_head
|| status_code < 200
|| status_code == HttpStatus::NoContent.code()
|| status_code == HttpStatus::ResetContent.code()
|| status_code == HttpStatus::NotModified.code()
}
pub fn push_bounded_body(
limit: usize,
body: &mut Vec<u8>,
chunk: Bytes,
) -> Result<(), RequestError> {
if limit != DEFAULT_LOW_SECURITY_MAX_BODY_SIZE && chunk.len() > limit.saturating_sub(body.len())
{
return Err(RequestError::ContentLengthTooLarge(
HttpStatus::PayloadTooLarge,
));
}
body.extend_from_slice(chunk.as_ref());
Ok(())
}
#[cfg(test)]
pub fn request_target(path: &str, query: Option<&str>) -> String {
match query {
Some(query) => {
let mut target: String = String::with_capacity(path.len() + query.len() + 1);
target.push_str(path);
target.push(QUESTION_MARK);
target.push_str(query);
target
}
None => path.to_owned(),
}
}