fastly 0.6.0-beta1

Fastly Compute@Edge API
Documentation
use lazy_static::lazy_static;
use std::sync::RwLock;

// TODO ACF 2020-11-10: for beta releases of the new API, set the initial sizes high because xqd
// doesn't have support for the adaptive buffers yet.

pub(crate) const INITIAL_HEADER_NAME_BUF_SIZE: usize = 8192;
// pub(crate) const INITIAL_HEADER_NAME_BUF_SIZE: usize = 128;
pub(crate) const DEFAULT_MAX_HEADER_NAME_BYTES: usize = 8192;

pub(crate) const INITIAL_HEADER_VALUE_BUF_SIZE: usize = 8192;
// pub(crate) const INITIAL_HEADER_VALUE_BUF_SIZE: usize = 4096;
pub(crate) const DEFAULT_MAX_HEADER_VALUE_BYTES: usize = 8192;

pub(crate) const INITIAL_METHOD_BUF_SIZE: usize = 8192;
// pub(crate) const INITIAL_METHOD_BUF_SIZE: usize = 8;
pub(crate) const DEFAULT_MAX_METHOD_BYTES: usize = 8192;

pub(crate) const INITIAL_URL_BUF_SIZE: usize = 8192;
// pub(crate) const INITIAL_URL_BUF_SIZE: usize = 4096;
pub(crate) const DEFAULT_MAX_URL_BYTES: usize = 8192;

lazy_static! {
    pub(crate) static ref REQUEST_LIMITS: RwLock<RequestLimits> =
        RwLock::new(RequestLimits::default());
}

#[derive(Clone, Copy, Debug)]
pub struct RequestLimits {
    pub(crate) max_header_name_bytes: Option<usize>,
    pub(crate) max_header_value_bytes: Option<usize>,
    pub(crate) max_method_bytes: Option<usize>,
    pub(crate) max_url_bytes: Option<usize>,
}

impl RequestLimits {
    const fn default() -> Self {
        RequestLimits {
            max_header_name_bytes: Some(DEFAULT_MAX_HEADER_NAME_BYTES),
            max_header_value_bytes: Some(DEFAULT_MAX_HEADER_VALUE_BYTES),
            max_method_bytes: Some(DEFAULT_MAX_METHOD_BYTES),
            max_url_bytes: Some(DEFAULT_MAX_URL_BYTES),
        }
    }

    pub fn set_all_default() {
        *REQUEST_LIMITS.write().unwrap() = RequestLimits::default();
    }

    pub fn set_all_disabled() {
        *REQUEST_LIMITS.write().unwrap() = RequestLimits {
            max_header_name_bytes: None,
            max_header_value_bytes: None,
            max_method_bytes: None,
            max_url_bytes: None,
        };
    }

    pub fn get_max_header_name_bytes() -> Option<usize> {
        REQUEST_LIMITS.read().unwrap().max_header_name_bytes
    }

    pub fn set_max_header_name_bytes(max: Option<usize>) {
        REQUEST_LIMITS.write().unwrap().max_header_name_bytes = max;
    }

    pub fn get_max_header_value_bytes() -> Option<usize> {
        REQUEST_LIMITS.read().unwrap().max_header_value_bytes
    }

    pub fn set_max_header_value_bytes(max: Option<usize>) {
        REQUEST_LIMITS.write().unwrap().max_header_value_bytes = max;
    }

    pub fn get_max_method_bytes() -> Option<usize> {
        REQUEST_LIMITS.read().unwrap().max_method_bytes
    }

    pub fn set_max_method_bytes(max: Option<usize>) {
        REQUEST_LIMITS.write().unwrap().max_method_bytes = max;
    }

    pub fn get_max_url_bytes() -> Option<usize> {
        REQUEST_LIMITS.read().unwrap().max_url_bytes
    }

    pub fn set_max_url_bytes(max: Option<usize>) {
        REQUEST_LIMITS.write().unwrap().max_url_bytes = max;
    }
}

lazy_static! {
    pub(crate) static ref RESPONSE_LIMITS: RwLock<ResponseLimits> =
        RwLock::new(ResponseLimits::default());
}

#[derive(Clone, Copy, Debug)]
pub struct ResponseLimits {
    pub(crate) max_header_name_bytes: Option<usize>,
    pub(crate) max_header_value_bytes: Option<usize>,
}

impl ResponseLimits {
    const fn default() -> Self {
        ResponseLimits {
            max_header_name_bytes: None,
            max_header_value_bytes: None,
        }
    }

    pub fn set_all_default() {
        *RESPONSE_LIMITS.write().unwrap() = ResponseLimits::default();
    }

    pub fn set_all_disabled() {
        *RESPONSE_LIMITS.write().unwrap() = ResponseLimits {
            max_header_name_bytes: None,
            max_header_value_bytes: None,
        };
    }

    pub fn get_max_header_name_bytes() -> Option<usize> {
        RESPONSE_LIMITS.read().unwrap().max_header_name_bytes
    }

    pub fn set_max_header_name_bytes(max: Option<usize>) {
        RESPONSE_LIMITS.write().unwrap().max_header_name_bytes = max;
    }

    pub fn get_max_header_value_bytes() -> Option<usize> {
        RESPONSE_LIMITS.read().unwrap().max_header_value_bytes
    }

    pub fn set_max_header_value_bytes(max: Option<usize>) {
        RESPONSE_LIMITS.write().unwrap().max_header_value_bytes = max;
    }
}