use crate::accept_query::{matches_media_range, parse_accept_query};
use crate::consts::*;
use http::HeaderMap;
#[derive(Debug, Clone)]
pub struct ResponseInfo {
pub status: u16,
pub content_location: Option<String>,
pub location: Option<String>,
pub accept_query: Vec<String>,
pub allow: Vec<String>,
}
impl ResponseInfo {
pub fn from_headers(status: u16, headers: &HeaderMap) -> Self {
ResponseInfo {
status,
content_location: header_str(headers, HEADER_CONTENT_LOCATION),
location: header_str(headers, HEADER_LOCATION),
accept_query: parse_accept_query(header_values(headers, HEADER_ACCEPT_QUERY)),
allow: parse_header_list(header_values(headers, HEADER_ALLOW)),
}
}
pub fn result_uri(&self) -> Option<&str> {
self.content_location.as_deref()
}
pub fn equivalent_resource_uri(&self) -> Option<&str> {
self.location.as_deref()
}
}
#[derive(Debug, Clone)]
pub struct Discovery {
pub supports_query: bool,
pub allow: Vec<String>,
pub accept_query: Vec<String>,
}
impl Discovery {
pub fn from_headers(headers: &HeaderMap) -> Self {
let allow = parse_header_list(header_values(headers, HEADER_ALLOW));
let accept_query = parse_accept_query(header_values(headers, HEADER_ACCEPT_QUERY));
let supports_query =
!accept_query.is_empty() || allow.iter().any(|method| method == METHOD_QUERY);
Discovery {
supports_query,
allow,
accept_query,
}
}
pub fn accepts_media_type(&self, media_type: &str) -> bool {
self.accept_query
.iter()
.any(|range| matches_media_range(range, media_type))
}
}
pub fn supports_query(headers: &HeaderMap) -> bool {
Discovery::from_headers(headers).supports_query
}
fn header_str(headers: &HeaderMap, name: &str) -> Option<String> {
headers
.get(name)
.and_then(|value| value.to_str().ok())
.map(|s| s.to_string())
}
fn header_values<'a>(headers: &'a HeaderMap, name: &'a str) -> impl Iterator<Item = String> + 'a {
headers
.get_all(name)
.iter()
.filter_map(|value| value.to_str().ok().map(|s| s.to_string()))
}
fn parse_header_list<I: IntoIterator<Item = String>>(values: I) -> Vec<String> {
let mut items = Vec::new();
for value in values {
for part in value.split(',') {
let trimmed = part.trim();
if !trimmed.is_empty() {
items.push(trimmed.to_string());
}
}
}
items
}
#[cfg(test)]
mod tests {
use super::*;
use http::HeaderValue;
fn headers(pairs: &[(&'static str, &str)]) -> HeaderMap {
let mut map = HeaderMap::new();
for (name, value) in pairs {
map.append(*name, HeaderValue::from_str(value).unwrap());
}
map
}
#[test]
fn discovery_from_allow_and_accept_query() {
let h = headers(&[
(HEADER_ALLOW, "GET, QUERY, OPTIONS"),
(
HEADER_ACCEPT_QUERY,
r#""application/jsonpath", application/sql"#,
),
]);
let d = Discovery::from_headers(&h);
assert!(d.supports_query);
assert!(d.accepts_media_type("application/sql"));
assert!(d.accepts_media_type("application/jsonpath"));
assert!(!d.accepts_media_type("text/plain"));
}
#[test]
fn result_vs_equivalent() {
let h = headers(&[
(HEADER_CONTENT_LOCATION, "/results/17"),
(HEADER_LOCATION, "/queries/42"),
]);
let info = ResponseInfo::from_headers(200, &h);
assert_eq!(info.result_uri(), Some("/results/17"));
assert_eq!(info.equivalent_resource_uri(), Some("/queries/42"));
}
}