use serde::Serialize;
use crate::util::{Bytes, BytesMut};
pub(crate) fn is_valid_target(
method: &super::Method,
version: super::Version,
uri: &urly::Url,
) -> bool {
if uri.validate_target().is_err() {
return false;
}
if method == super::Method::CONNECT {
!uri.is_absolute()
&& uri.authority().is_some()
&& uri.path().is_empty()
&& uri.query().is_none()
} else if version != super::Version::HTTP_2 && uri.is_absolute() {
uri.authority().is_some()
} else {
(version == super::Version::HTTP_2 || uri.authority().is_none())
&& (uri.path().as_str().starts_with('/')
|| (uri.path() == "*" && uri.query().is_none() && method == super::Method::OPTIONS))
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub(crate) enum TargetPolicy {
Raw,
Validate,
Normalize,
}
pub(crate) fn process_request_target(
policy: TargetPolicy,
method: &super::Method,
version: super::Version,
uri: &mut urly::Url,
) -> Result<(), super::error::DecodeError> {
if uri.path() == "*" && method != super::Method::OPTIONS {
return Err(super::error::DecodeError::InvalidInput(
"asterisk-form request-target is only valid for OPTIONS",
));
}
if policy == TargetPolicy::Raw {
return Ok(());
}
if !is_valid_target(method, version, uri) {
return Err(super::error::DecodeError::InvalidInput(
"invalid request-target",
));
}
if policy == TargetPolicy::Validate {
return Ok(());
}
if version == super::Version::HTTP_2 && uri.path() == "*" {
*uri = urly::Url::from_static("*");
} else {
uri.normalize()?;
}
Ok(())
}
pub(crate) fn json_body<T: ?Sized + Serialize>(value: &T) -> serde_json::Result<Bytes> {
let mut buf = BytesMut::new();
serde_json::to_writer(&mut buf, value)?;
Ok(buf.freeze())
}
pub(crate) fn take_trimmed(buf: &mut BytesMut) -> Bytes {
let len = buf.len();
if buf.capacity() > len + len / 4 {
let body = Bytes::copy_from_slice(buf);
buf.clear();
body
} else {
buf.take()
}
}
#[cfg(feature = "cookie")]
pub(crate) fn push_cookie(buf: &mut Vec<u8>, name: &str, value: &str) {
use urly::quoting::{Component, quote};
if !buf.is_empty() {
buf.extend_from_slice(b"; ");
}
buf.extend_from_slice(quote(name, Component::Opaque).as_bytes());
buf.push(b'=');
buf.extend_from_slice(quote(value, Component::Opaque).as_bytes());
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn trimmed() {
let mut buf = BytesMut::with_capacity(1024);
buf.extend_from_slice(&[1; 1000]);
let ptr = buf.as_ptr();
let body = take_trimmed(&mut buf);
assert_eq!(body, [1; 1000][..]);
assert_eq!(body.as_ptr(), ptr);
assert!(buf.is_empty());
buf.reserve(1024);
buf.extend_from_slice(&[2; 512]);
let ptr = buf.as_ptr();
let body = take_trimmed(&mut buf);
assert_eq!(body, [2; 512][..]);
assert_ne!(body.as_ptr(), ptr);
assert!(buf.is_empty());
}
#[test]
fn json_body_matches_to_string() {
let small = vec![1, 2, 3];
let large: Vec<String> = (0..1000).map(|i| format!("item-{i}")).collect();
let map: std::collections::BTreeMap<_, _> = (0..50).map(|i| (i.to_string(), i)).collect();
assert_eq!(
json_body(&small).unwrap(),
serde_json::to_string(&small).unwrap()
);
assert_eq!(
json_body(&large).unwrap(),
serde_json::to_string(&large).unwrap()
);
assert_eq!(
json_body(&map).unwrap(),
serde_json::to_string(&map).unwrap()
);
assert_eq!(json_body("").unwrap(), "\"\"");
assert!(json_body(&small).unwrap().is_inline());
}
#[test]
fn json_body_error() {
let mut map = std::collections::HashMap::new();
map.insert(vec![1u8], 1);
assert!(json_body(&map).is_err());
}
}