use super::*;
pub(crate) fn take_pooled_context() -> Context {
CONTEXT_POOL.with(|pool: &RefCell<Vec<Context>>| pool.borrow_mut().pop().unwrap_or_default())
}
pub(crate) fn return_pooled_context(mut ctx: Context) {
{
let headers: &mut RequestHeaders = ctx.get_mut_request().get_mut_headers();
if !headers.is_empty() {
PROTOCOL_HEADER_POOL.with(|pool: &RefCell<Vec<(String, VecDeque<String>)>>| {
let mut pool: RefMut<'_, Vec<(String, VecDeque<String>)>> = pool.borrow_mut();
for (key, values) in headers.drain() {
if pool.len() < MAX_POOLED_PROTOCOL_HEADERS
&& key.capacity() <= MAX_POOLED_STRING_CAPACITY
&& values.capacity() <= B_4
&& values
.iter()
.all(|value: &String| value.capacity() <= MAX_POOLED_STRING_CAPACITY)
{
pool.push((key, values));
}
}
});
}
}
ctx.get_mut_request().reset();
if ctx.get_request().get_headers().capacity() > MAX_POOLED_PROTOCOL_HEADERS {
*ctx.get_mut_request().get_mut_headers() = RequestHeaders::default();
}
if ctx.get_request().get_querys().capacity() > MAX_POOLED_PROTOCOL_HEADERS {
*ctx.get_mut_request().get_mut_querys() = RequestQuerys::default();
}
if ctx.get_request().get_body().capacity() > MAX_POOLED_BUFFER_CAPACITY {
*ctx.get_mut_request().get_mut_body() = Vec::new();
}
if ctx.get_request().get_path().capacity() > MAX_POOLED_STRING_CAPACITY {
*ctx.get_mut_request().get_mut_path() = String::new();
}
if ctx.get_request().get_host().capacity() > MAX_POOLED_STRING_CAPACITY {
*ctx.get_mut_request().get_mut_host() = String::new();
}
ctx.get_mut_response().reset();
if ctx.get_response().get_headers().capacity() > MAX_POOLED_PROTOCOL_HEADERS {
*ctx.get_mut_response().get_mut_headers() = ResponseHeaders::default();
}
if ctx.get_response().get_reason_phrase().capacity() > MAX_POOLED_STRING_CAPACITY {
*ctx.get_mut_response().get_mut_reason_phrase() = String::new();
}
if ctx.get_response().get_body().capacity() > MAX_POOLED_BUFFER_CAPACITY {
*ctx.get_mut_response().get_mut_body() = Vec::new();
}
ctx.clear_route_params();
ctx.reset_request_attributes();
ctx.set_original_head(None);
if let Some(target) = ctx.take_original_target() {
return_target_buffer(target);
}
CONTEXT_POOL.with(|pool: &RefCell<Vec<Context>>| {
let mut pool: RefMut<'_, Vec<Context>> = pool.borrow_mut();
if pool.len() < MAX_POOLED_CONTEXTS {
pool.push(ctx);
}
});
}
pub(crate) fn take_protocol_header_entry() -> Option<(String, VecDeque<String>)> {
PROTOCOL_HEADER_POOL
.with(|pool: &RefCell<Vec<(String, VecDeque<String>)>>| pool.borrow_mut().pop())
}
pub(crate) fn take_target_buffer() -> String {
TARGET_BUFFER_POOL.with(|pool: &RefCell<Vec<String>>| {
let mut buffer: String = pool.borrow_mut().pop().unwrap_or_default();
buffer.clear();
buffer
})
}
pub(crate) fn return_target_buffer(mut buffer: String) {
if buffer.capacity() == 0 || buffer.capacity() > MAX_POOLED_STRING_CAPACITY {
return;
}
buffer.clear();
TARGET_BUFFER_POOL.with(|pool: &RefCell<Vec<String>>| {
let mut pool: RefMut<'_, Vec<String>> = pool.borrow_mut();
if pool.len() < MAX_POOLED_CONTEXTS {
pool.push(buffer);
}
});
}
pub(crate) fn take_route_param_entry() -> (String, String) {
ROUTE_PARAM_POOL.with(|pool: &RefCell<Vec<(String, String)>>| {
let mut pair: (String, String) = pool.borrow_mut().pop().unwrap_or_default();
pair.0.clear();
pair.1.clear();
pair
})
}
pub(crate) fn return_route_param_entry(mut pair: (String, String)) {
if pair.0.capacity() > MAX_POOLED_STRING_CAPACITY
|| pair.1.capacity() > MAX_POOLED_STRING_CAPACITY
{
return;
}
pair.0.clear();
pair.1.clear();
ROUTE_PARAM_POOL.with(|pool: &RefCell<Vec<(String, String)>>| {
let mut pool: RefMut<'_, Vec<(String, String)>> = pool.borrow_mut();
if pool.len() < MAX_POOLED_PROTOCOL_HEADERS {
pool.push(pair);
}
});
}
pub(crate) async fn poll_once_protocol<F>(mut future: Pin<&mut F>) -> Option<F::Output>
where
F: Future,
{
poll_fn(
|context: &mut task::Context<'_>| match future.as_mut().poll(context) {
Poll::Ready(value) => Poll::Ready(Some(value)),
Poll::Pending => Poll::Ready(None),
},
)
.await
}
pub(crate) fn hides_entity_length(status_code: usize, _is_head: bool) -> bool {
status_code < HttpStatus::Ok.code() || status_code == HttpStatus::NoContent.code()
}
pub(crate) fn check_body_budget(
announced_len: Option<usize>,
forwarded: usize,
incoming: usize,
) -> bool {
match announced_len {
Some(announced) => forwarded.saturating_add(incoming) > announced,
None => false,
}
}
pub(crate) fn validate_status_code(status_code: usize) -> Result<(), ResponseError> {
let is_code: bool = (MIN_STATUS_CODE..=MAX_STATUS_CODE).contains(&status_code);
if !is_code {
return Err(ResponseError::Send(INVALID_RESPONSE_STATUS.to_owned()));
}
Ok(())
}