use super::common::{HttpCookie, HttpHeader};
use super::{Header, Version};
use core::fmt;
use std::fmt::Formatter;
#[derive(Clone, Debug, PartialEq)]
pub struct HttpRequestObservation {
pub version: Version,
pub horder: Vec<Header>,
pub habsent: Vec<Header>,
pub expsw: String,
}
#[derive(Clone, Debug, PartialEq)]
pub struct HttpResponseObservation {
pub version: Version,
pub horder: Vec<Header>,
pub habsent: Vec<Header>,
pub expsw: String,
}
#[derive(Debug, Clone)]
pub struct ObservableHttpRequest {
pub matching: HttpRequestObservation,
pub lang: Option<String>,
pub user_agent: Option<String>,
pub headers: Vec<HttpHeader>,
pub cookies: Vec<HttpCookie>,
pub referer: Option<String>,
pub method: Option<String>,
pub uri: Option<String>,
}
#[derive(Debug, Clone)]
pub struct ObservableHttpResponse {
pub matching: HttpResponseObservation,
pub headers: Vec<HttpHeader>,
pub status_code: Option<u16>,
}
pub trait HttpDisplayFormat {
fn get_version(&self) -> Version;
fn get_horder(&self) -> &[Header];
fn get_habsent(&self) -> &[Header];
fn get_expsw(&self) -> &str;
fn format_http_display(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}:", self.get_version())?;
for (i, h) in self.get_horder().iter().enumerate() {
if i > 0 {
f.write_str(",")?;
}
write!(f, "{h}")?;
}
f.write_str(":")?;
for (i, h) in self.get_habsent().iter().enumerate() {
if i > 0 {
f.write_str(",")?;
}
write!(f, "{h}")?;
}
write!(f, ":{}", self.get_expsw())
}
}
impl HttpDisplayFormat for HttpRequestObservation {
fn get_version(&self) -> Version {
self.version
}
fn get_horder(&self) -> &[Header] {
&self.horder
}
fn get_habsent(&self) -> &[Header] {
&self.habsent
}
fn get_expsw(&self) -> &str {
&self.expsw
}
}
impl HttpDisplayFormat for HttpResponseObservation {
fn get_version(&self) -> Version {
self.version
}
fn get_horder(&self) -> &[Header] {
&self.horder
}
fn get_habsent(&self) -> &[Header] {
&self.habsent
}
fn get_expsw(&self) -> &str {
&self.expsw
}
}
impl fmt::Display for HttpRequestObservation {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
self.format_http_display(f)
}
}
impl fmt::Display for HttpResponseObservation {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
self.format_http_display(f)
}
}
impl HttpDisplayFormat for ObservableHttpRequest {
fn get_version(&self) -> Version {
self.matching.version
}
fn get_horder(&self) -> &[Header] {
&self.matching.horder
}
fn get_habsent(&self) -> &[Header] {
&self.matching.habsent
}
fn get_expsw(&self) -> &str {
&self.matching.expsw
}
}
impl HttpDisplayFormat for ObservableHttpResponse {
fn get_version(&self) -> Version {
self.matching.version
}
fn get_horder(&self) -> &[Header] {
&self.matching.horder
}
fn get_habsent(&self) -> &[Header] {
&self.matching.habsent
}
fn get_expsw(&self) -> &str {
&self.matching.expsw
}
}
impl fmt::Display for ObservableHttpRequest {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
self.format_http_display(f)
}
}
impl fmt::Display for ObservableHttpResponse {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
self.format_http_display(f)
}
}
impl fmt::Display for HttpHeader {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
if let Some(ref value) = self.value {
write!(f, "{}={}", self.name, value)
} else {
write!(f, "{}", self.name)
}
}
}