huginn_net_http/http/
observable.rs1use super::common::{HttpCookie, HttpHeader};
2use super::{Header, Version};
3use core::fmt;
4use std::fmt::Formatter;
5
6#[derive(Clone, Debug, PartialEq)]
11pub struct HttpRequestObservation {
12 pub version: Version,
14 pub horder: Vec<Header>,
16 pub habsent: Vec<Header>,
18 pub expsw: String,
20}
21
22#[derive(Clone, Debug, PartialEq)]
24pub struct HttpResponseObservation {
25 pub version: Version,
27 pub horder: Vec<Header>,
29 pub habsent: Vec<Header>,
31 pub expsw: String,
33}
34
35#[derive(Debug, Clone)]
38pub struct ObservableHttpRequest {
39 pub matching: HttpRequestObservation,
40 pub lang: Option<String>,
41 pub user_agent: Option<String>,
42 pub headers: Vec<HttpHeader>,
43 pub cookies: Vec<HttpCookie>,
44 pub referer: Option<String>,
45 pub method: Option<String>,
46 pub uri: Option<String>,
47}
48
49#[derive(Debug, Clone)]
50pub struct ObservableHttpResponse {
51 pub matching: HttpResponseObservation,
52 pub headers: Vec<HttpHeader>,
53 pub status_code: Option<u16>,
54}
55
56pub trait HttpDisplayFormat {
62 fn get_version(&self) -> Version;
63 fn get_horder(&self) -> &[Header];
64 fn get_habsent(&self) -> &[Header];
65 fn get_expsw(&self) -> &str;
66
67 fn format_http_display(&self, f: &mut Formatter<'_>) -> fmt::Result {
68 write!(f, "{}:", self.get_version())?;
69
70 for (i, h) in self.get_horder().iter().enumerate() {
71 if i > 0 {
72 f.write_str(",")?;
73 }
74 write!(f, "{h}")?;
75 }
76
77 f.write_str(":")?;
78
79 for (i, h) in self.get_habsent().iter().enumerate() {
80 if i > 0 {
81 f.write_str(",")?;
82 }
83 write!(f, "{h}")?;
84 }
85
86 write!(f, ":{}", self.get_expsw())
87 }
88}
89
90impl HttpDisplayFormat for HttpRequestObservation {
91 fn get_version(&self) -> Version {
92 self.version
93 }
94 fn get_horder(&self) -> &[Header] {
95 &self.horder
96 }
97 fn get_habsent(&self) -> &[Header] {
98 &self.habsent
99 }
100 fn get_expsw(&self) -> &str {
101 &self.expsw
102 }
103}
104
105impl HttpDisplayFormat for HttpResponseObservation {
106 fn get_version(&self) -> Version {
107 self.version
108 }
109 fn get_horder(&self) -> &[Header] {
110 &self.horder
111 }
112 fn get_habsent(&self) -> &[Header] {
113 &self.habsent
114 }
115 fn get_expsw(&self) -> &str {
116 &self.expsw
117 }
118}
119
120impl fmt::Display for HttpRequestObservation {
121 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
122 self.format_http_display(f)
123 }
124}
125
126impl fmt::Display for HttpResponseObservation {
127 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
128 self.format_http_display(f)
129 }
130}
131
132impl HttpDisplayFormat for ObservableHttpRequest {
133 fn get_version(&self) -> Version {
134 self.matching.version
135 }
136 fn get_horder(&self) -> &[Header] {
137 &self.matching.horder
138 }
139 fn get_habsent(&self) -> &[Header] {
140 &self.matching.habsent
141 }
142 fn get_expsw(&self) -> &str {
143 &self.matching.expsw
144 }
145}
146
147impl HttpDisplayFormat for ObservableHttpResponse {
148 fn get_version(&self) -> Version {
149 self.matching.version
150 }
151 fn get_horder(&self) -> &[Header] {
152 &self.matching.horder
153 }
154 fn get_habsent(&self) -> &[Header] {
155 &self.matching.habsent
156 }
157 fn get_expsw(&self) -> &str {
158 &self.matching.expsw
159 }
160}
161
162impl fmt::Display for ObservableHttpRequest {
163 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
164 self.format_http_display(f)
165 }
166}
167
168impl fmt::Display for ObservableHttpResponse {
169 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
170 self.format_http_display(f)
171 }
172}
173
174impl fmt::Display for HttpHeader {
175 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
176 if let Some(ref value) = self.value {
177 write!(f, "{}={}", self.name, value)
178 } else {
179 write!(f, "{}", self.name)
180 }
181 }
182}