rust_web_server/header/
mod.rs1use crate::client_hint::ClientHint;
2use crate::cors::Cors;
3use crate::ext::date_time_ext::DateTimeExt;
4use crate::ext::string_ext::StringExt;
5use crate::range::Range;
6use crate::request::Request;
7use crate::symbol::SYMBOL;
8
9#[cfg(test)]
10mod tests;
11
12pub mod content_disposition;
13
14#[cfg(test)]
15mod example;
16
17#[derive(PartialEq, Eq, Clone, Debug)]
18pub struct Header {
19 pub name: String,
20 pub value: String,
21}
22
23impl Header {
24
25 pub fn as_string(&self) -> String {
26 let formatted = format!("{}: {}", self.name, self.value);
27 formatted
28 }
29
30 pub const _ACCESS_CONTROL_REQUEST_METHOD: &'static str = "Access-Control-Request-Method";
31 pub const _ACCESS_CONTROL_REQUEST_HEADERS: &'static str = "Access-Control-Request-Headers";
32 pub const _ACCESS_CONTROL_ALLOW_ORIGIN: &'static str = "Access-Control-Allow-Origin";
33 pub const _ACCESS_CONTROL_ALLOW_METHODS: &'static str = "Access-Control-Allow-Methods";
34 pub const _ACCESS_CONTROL_ALLOW_HEADERS: &'static str = "Access-Control-Allow-Headers";
35 pub const _ACCESS_CONTROL_ALLOW_CREDENTIALS: &'static str = "Access-Control-Allow-Credentials";
36 pub const _ACCESS_CONTROL_MAX_AGE: &'static str = "Access-Control-Max-Age";
37 pub const _ACCESS_CONTROL_EXPOSE_HEADERS: &'static str = "Access-Control-Expose-Headers";
38
39 pub const _ACCEPT: &'static str = "Accept";
40 pub const _ACCEPT_RANGES: &'static str = "Accept-Ranges";
41 pub const _ACCEPT_CH: &'static str = "Accept-CH";
42 pub const _ACCEPT_ENCODING: &'static str = "Accept-Encoding";
43 pub const _ACCEPT_LANGUAGE: &'static str = "Accept-Language";
44 pub const _ACCEPT_PATCH: &'static str = "Accept-Patch";
45 pub const _ACCEPT_POST: &'static str = "Accept-Post";
46 pub const _AGE: &'static str = "Age";
47 pub const _ALLOW: &'static str = "Allow";
48 pub const _ALT_SVC: &'static str = "Alt-Svc";
49 pub const _AUTHORIZATION: &'static str = "Authorization";
50 pub const _CACHE_CONTROL: &'static str = "Cache-Control";
51 pub const _CLEAR_SITE_DATA: &'static str = "Clear-Site-Data";
52 pub const _CONTENT_TYPE: &'static str = "Content-Type";
53 pub const _CONTENT_LENGTH: &'static str = "Content-Length";
54 pub const _CONTENT_RANGE: &'static str = "Content-Range";
55 pub const _CONTENT_DISPOSITION: &'static str = "Content-Disposition";
56 pub const _CONTENT_ENCODING: &'static str = "Content-Encoding";
57 pub const _CONTENT_LANGUAGE: &'static str = "Content-Language";
58 pub const _CONTENT_LOCATION: &'static str = "Content-Location";
59 pub const _CONTENT_SECURITY_POLICY: &'static str = "Content-Security-Policy";
60 pub const _CONTENT_SECURITY_POLICY_REPORT_ONLY: &'static str = "Content-Security-Policy-Report-Only";
61 pub const _COOKIE: &'static str = "Cookie";
62 pub const _CROSS_ORIGIN_EMBEDDER_POLICY: &'static str = "Cross-Origin-Embedder-Policy";
63 pub const _CROSS_ORIGIN_OPENER_POLICY: &'static str = "Cross-Origin-Opener-Policy";
64 pub const _CROSS_ORIGIN_RESOURCE_POLICY: &'static str = "Cross-Origin-Resource-Policy";
65 pub const _DATE: &'static str = "Date";
66 pub const _DATE_UNIX_EPOCH_NANOS: &'static str = "Date-Unix-Epoch-Nanos";
67 pub const _DEVICE_MEMORY: &'static str = "Device-Memory";
68 pub const _DIGEST: &'static str = "Digest";
69 pub const _DOWNLINK: &'static str = "Downlink";
70 pub const _EARLY_DATA: &'static str = "Early-Data";
71 pub const _ECT: &'static str = "ECT";
72 pub const _ETAG: &'static str = "ETag";
73 pub const _EXPECT: &'static str = "Expect";
74 pub const _EXPIRES: &'static str = "Expires";
75 pub const _FEATURE_POLICY: &'static str = "Feature-Policy";
76 pub const _PERMISSIONS_POLICY: &'static str = "Permissions-Policy";
77 pub const _FORWARDED: &'static str = "Forwarded";
78 pub const _FROM: &'static str = "From";
79 pub const _HOST: &'static str = "Host";
80 pub const _IF_MATCH: &'static str = "If-Match";
81 pub const _IF_MODIFIED_SINCE: &'static str = "If-Modified-Since";
82 pub const _IF_NONE_MATCH: &'static str = "If-None-Match";
83 pub const _IF_RANGE: &'static str = "If-Range";
84 pub const _IF_UNMODIFIED_SINCE: &'static str = "If-Unmodified-Since";
85 pub const _LAST_MODIFIED: &'static str = "Last-Modified";
86 pub const _LAST_MODIFIED_UNIX_EPOCH_NANOS: &'static str = "Last-Modified-Unix-Epoch-Nanos";
87 pub const _LINK: &'static str = "Link";
88 pub const _LOCATION: &'static str = "Location";
89 pub const _MAX_FORWARDS: &'static str = "Max-Forwards";
90 pub const _NEL: &'static str = "NEL";
91 pub const _ORIGIN: &'static str = "Origin";
92 pub const _PROXY_AUTHENTICATE: &'static str = "Proxy-Authenticate";
93 pub const _PROXY_AUTHORIZATION: &'static str = "Proxy-Authorization";
94 pub const _RANGE: &'static str = "Range";
95 pub const _REFERER: &'static str = "Referer";
96 pub const _REFERRER_POLICY: &'static str = "Referrer-Policy";
97 pub const _RETRY_AFTER: &'static str = "Retry-After";
98 pub const _RTT: &'static str = "RTT";
99 pub const _SAVE_DATA: &'static str = "Save-Data";
100 pub const _SEC_CH_UA: &'static str = "Sec-CH-UA";
101 pub const _SEC_CH_UA_ARCH: &'static str = "Sec-CH-UA-Arch";
102 pub const _SEC_CH_UA_BITNESS: &'static str = "Sec-CH-UA-Bitness";
103 pub const _SEC_CH_UA_FULL_VERSION_LIST: &'static str = "Sec-CH-UA-Full-Version-List";
104 pub const _SEC_CH_UA_MOBILE: &'static str = "Sec-CH-UA-Mobile";
105 pub const _SEC_CH_UA_MODEL: &'static str = "Sec-CH-UA-Model";
106 pub const _SEC_CH_UA_PLATFORM: &'static str = "Sec-CH-UA-Platform";
107 pub const _SEC_CH_UA_PLATFORM_VERSION: &'static str = "Sec-CH-UA-Platform-Version";
108 pub const _SEC_FETCH_DEST: &'static str = "Sec-Fetch-Dest";
109 pub const _SEC_FETCH_MODE: &'static str = "Sec-Fetch-Mode";
110 pub const _SEC_FETCH_SITE: &'static str = "Sec-Fetch-Site";
111 pub const _SEC_FETCH_USER: &'static str = "Sec-Fetch-User";
112 pub const _SEC_GPC: &'static str = "Sec-GPC";
113 pub const _SERVER: &'static str = "Server";
114 pub const _SERVER_TIMING: &'static str = "Server-Timing";
115 pub const _SERVICE_WORKER_NAVIGATION_PRELOAD: &'static str = "Service-Worker-Navigation-Preload";
116 pub const _SET_COOKIE: &'static str = "Set-Cookie";
117 pub const _SOURCE_MAP: &'static str = "SourceMap";
118 pub const _STRICT_TRANSPORT_SECURITY: &'static str = "Strict-Transport-Security";
119 pub const _TE: &'static str = "TE";
120 pub const _TIMING_ALLOW_ORIGIN: &'static str = "Timing-Allow-Origin";
121 pub const _TRAILER: &'static str = "Trailer";
122 pub const _TRANSFER_ENCODING: &'static str = "Transfer-Encoding";
123 pub const _UPGRADE: &'static str = "Upgrade";
124 pub const _UPGRADE_INSECURE_REQUESTS: &'static str = "Upgrade-Insecure-Requests";
125 pub const _USER_AGENT: &'static str = "User-Agent";
126 pub const _VARY: &'static str = "Vary";
127 pub const _VIA: &'static str = "Via";
128 pub const _WANT_DIGEST: &'static str = "Want-Digest";
129 pub const _WWW_AUTHENTICATE: &'static str = "WWW-Authenticate";
130 pub const _X_CONTENT_TYPE_OPTIONS: &'static str = "X-Content-Type-Options";
131 pub const _X_CONTENT_TYPE_OPTIONS_VALUE_NOSNIFF: &'static str = "nosniff";
132 pub const _X_FRAME_OPTIONS: &'static str = "X-Frame-Options";
133 pub const _X_FRAME_OPTIONS_VALUE_DENY: &'static str = "DENY";
134 pub const _X_FRAME_OPTIONS_VALUE_SAME_ORIGIN: &'static str = "SAMEORIGIN";
135
136
137
138
139 pub const NAME_VALUE_SEPARATOR: &'static str = ": ";
140
141 pub const _DO_NOT_STORE_CACHE: &'static str = "no-store, no-cache, private, max-age=0, must-revalidate, proxy-revalidate";
142
143 pub fn get_header_list(request: &Request) -> Vec<Header> {
144 let mut header_list : Vec<Header>;
145 let mut vary_value : Vec<String>;
146
147 let cors_vary = Cors::get_vary_header_value();
148 vary_value = vec![cors_vary];
149 let cors_header_list: Vec<Header> = Cors::get_headers(&request);
150 header_list = cors_header_list;
151
152 let client_hint_header = ClientHint::get_accept_client_hints_header();
153 header_list.push(client_hint_header);
154
155 let critical_client_hint_header = ClientHint::get_critical_client_hints_header();
156 header_list.push(critical_client_hint_header);
157
158 let client_hint_vary = ClientHint::get_vary_header_value();
159 vary_value.push(client_hint_vary);
160
161 let vary_header = Header { name: Header::_VARY.to_string(), value: vary_value.join(", ") };
162 header_list.push(vary_header);
163
164 let x_content_type_options_header = Header::get_x_content_type_options_header();
165 header_list.push(x_content_type_options_header);
166
167 let accept_ranges_header = Header::get_accept_ranges_header();
168 header_list.push(accept_ranges_header);
169
170 let x_frame_options_header = Header::get_x_frame_options_header();
171 header_list.push(x_frame_options_header);
172
173 let date_iso_8601_header = Header::get_date_iso_8601_header();
174 header_list.push(date_iso_8601_header);
175
176 let no_cache = Header::get_no_cache_header();
177 header_list.push(no_cache);
178
179 header_list
180 }
181
182
183 pub fn get_x_content_type_options_header() -> Header {
184 Header {
185 name: Header::_X_CONTENT_TYPE_OPTIONS.to_string(),
186 value: Header::_X_CONTENT_TYPE_OPTIONS_VALUE_NOSNIFF.to_string(),
187 }
188 }
189
190 pub fn get_accept_ranges_header() -> Header {
191 Header {
192 name: Header::_ACCEPT_RANGES.to_string(),
193 value: Range::BYTES.to_string(),
194 }
195 }
196
197 pub fn get_x_frame_options_header() -> Header {
198 Header {
199 name: Header::_X_FRAME_OPTIONS.to_string(),
200 value: Header::_X_FRAME_OPTIONS_VALUE_SAME_ORIGIN.to_string(),
201 }
202 }
203
204 pub fn get_date_iso_8601_header() -> Header {
205 Header {
206 name: Header::_DATE_UNIX_EPOCH_NANOS.to_string(),
207 value: DateTimeExt::_now_unix_epoch_nanos().to_string(),
208 }
209 }
210
211 pub fn get_no_cache_header() -> Header {
212 Header {
213 name: Header::_CACHE_CONTROL.to_string(),
214 value: Header::_DO_NOT_STORE_CACHE.to_string(),
215 }
216 }
217
218 pub fn parse_header(raw_header: &str) -> Result<Header, String> {
219 let escaped_header = StringExt::filter_ascii_control_characters(raw_header);
220 let escaped_header = StringExt::truncate_new_line_carriage_return(&escaped_header);
221
222 let boxed_split = escaped_header.split_once(SYMBOL.colon);
223 if boxed_split.is_none() {
224 let message = format!("Unable to parse header: {}", escaped_header);
225 return Err(message)
226 }
227
228 let (name, value) = boxed_split.unwrap();
229
230 let header = Header {
231 name: name.trim().to_string(),
232 value: value.trim().to_string(),
233 };
234
235 Ok(header)
236 }
237
238 pub fn parse(raw_header: &str) -> Result<Header, String> {
239 Header::parse_header(raw_header)
240 }
241
242 pub fn generate(&self) -> String {
243 self.as_string()
244 }
245
246}
247