hyper_simple_server/
exports.rs

1
2
3#![ allow (dead_code) ]
4#![ allow (unused_import_braces) ]
5
6
7
8
9use crate::prelude::*;
10
11
12
13
14#[ cfg (feature = "http") ]
15pub use http::{
16		
17		request::Request,
18		request::Parts as RequestParts,
19		request::Builder as RequestBuilder,
20		
21		response::Response,
22		response::Parts as ResponseParts,
23		response::Builder as ResponseBuilder,
24		
25		uri::Uri,
26		uri::Scheme,
27		uri::Authority,
28		uri::PathAndQuery,
29		
30		version::Version,
31		method::Method,
32		status::StatusCode,
33		
34		header::HeaderMap,
35		header::HeaderName,
36		header::HeaderValue,
37		
38		header::AsHeaderName,
39		header::IntoHeaderName,
40		
41		Extensions,
42};
43
44#[ cfg (feature = "http-body") ]
45pub use http_body::{
46		
47		Body as BodyTrait,
48		SizeHint as BodySizeHint,
49		Data as BodyData,
50		Trailers as BodyTrailers,
51	};
52
53#[ cfg (feature = "bytes") ]
54pub use bytes::{
55		
56		Bytes,
57		Buf,
58	};
59
60#[ cfg (feature = "hyper") ]
61pub use ::hyper::{
62		
63		body::Body,
64	};
65
66#[ cfg (feature = "tokio--rt") ]
67pub use ::tokio::{
68		
69		runtime::Runtime,
70	};
71
72
73
74
75#[ cfg (feature = "http") ]
76pub type Headers = HeaderMap<HeaderValue>;
77
78
79
80
81#[ derive (Copy, Clone) ]
82#[ cfg (feature = "hss-extensions") ]
83pub enum ContentType {
84	
85	// https://docs.rs/headers/0.3.3/headers/struct.ContentType.html
86	// https://docs.rs/mime/0.3.16/mime/#constants
87	
88	Text,
89	Html,
90	Css,
91	Js,
92	
93	Json,
94	Xml,
95	
96	Png,
97	Jpeg,
98	Svg,
99	Icon,
100	
101	FontTtf,
102	FontOtf,
103	FontWoff,
104	FontWoff2,
105	
106	Unknown,
107	
108}
109
110
111#[ cfg (feature = "hss-extensions") ]
112impl ContentType {
113	
114	pub fn to_str (&self) -> &'static str {
115		match self {
116			
117			ContentType::Text => "text/plain; charset=utf-8",
118			ContentType::Html => "text/html; charset=utf-8",
119			ContentType::Css => "text/css; charset=utf-8",
120			ContentType::Js => "application/javascript; charset=utf-8",
121			
122			ContentType::Json => "application/json; charset=utf-8",
123			ContentType::Xml => "application/xml; charset=utf-8",
124			
125			ContentType::Png => "image/png",
126			ContentType::Jpeg => "image/jpeg",
127			ContentType::Svg => "image/svg+xml",
128			ContentType::Icon => "image/vnd.microsoft.icon",
129			
130			ContentType::FontTtf => "font/ttf",
131			ContentType::FontOtf => "font/otf",
132			ContentType::FontWoff => "font/woff",
133			ContentType::FontWoff2 => "font/woff2",
134			
135			ContentType::Unknown => "application/octet-stream",
136		}
137	}
138	
139	pub fn from_str (_string : &str) -> Option<Self> {
140		match _string {
141			
142			"text/plain; charset=utf-8" | "text/plain" =>
143				Some (ContentType::Text),
144			"text/html; charset=utf-8" | "text/html" =>
145				Some (ContentType::Html),
146			"text/css; charset=utf-8" | "text/css" =>
147				Some (ContentType::Css),
148			"application/javascript; charset=utf-8" | "application/javascript" =>
149				Some (ContentType::Js),
150			
151			"application/json; charset=utf-8" | "application/json" =>
152				Some (ContentType::Json),
153			"application/xml; charset=utf-8" | "application/xml" =>
154				Some (ContentType::Xml),
155			
156			"image/png" =>
157				Some (ContentType::Png),
158			"image/jpeg" =>
159				Some (ContentType::Jpeg),
160			"image/svg+xml" =>
161				Some (ContentType::Svg),
162			"image/vnd.microsoft.icon" =>
163				Some (ContentType::Icon),
164			
165			"font/ttf" =>
166				Some (ContentType::FontTtf),
167			"font/otf" =>
168				Some (ContentType::FontOtf),
169			"font/woff" =>
170				Some (ContentType::FontWoff),
171			"font/woff2" =>
172				Some (ContentType::FontWoff2),
173			
174			"application/octet-stream" =>
175				Some (ContentType::Unknown),
176			
177			_ =>
178				None,
179		}
180	}
181}
182
183
184#[ cfg (feature = "hss-extensions") ]
185impl Into<HeaderValue> for ContentType {
186	fn into (self) -> HeaderValue {
187		#[ allow (unsafe_code) ]
188		unsafe {
189			HeaderValue::from_maybe_shared_unchecked (Bytes::from_static (self.to_str () .as_bytes ()))
190		}
191	}
192}
193
194
195
196
197#[ allow (clippy::declare_interior_mutable_const) ]
198#[ cfg (feature = "hss-extensions") ]
199pub mod consts {
200	
201	
202	macro_rules! def_const {
203		( $_type : ty => $_from : ident => $( $_id : ident ),+ $(,)? ) => {
204			$(
205				pub const $_id : $_type = ::http::$_from::$_id;
206			)+
207		};
208		( $_type : ty => $( $_id : ident ),+ $(,)? ) => {
209			$(
210				pub const $_id : $_type = <$_type>::$_id;
211			)+
212		};
213	}
214	
215	
216	def_const! (super::Version => HTTP_09, HTTP_10, HTTP_11, HTTP_2, HTTP_3);
217	def_const! (super::Method => GET, POST, PUT, DELETE, HEAD, OPTIONS, CONNECT, PATCH, TRACE);
218	
219	
220	// NOTE:  https://docs.rs/http/0.2.3/src/http/status.rs.html#323-515
221	// x-selection co | sed -r -e '\#^\s*$#d' -e 's#^\s+##' -e '\#^//#d' -e 's#\([0-9]+,\s*([^,]+).*$#\1#' -e 's#^#\t\t\t#' -e 's#$#,#' | LC_ALL=C sort | x-selection pi
222	def_const! (super::StatusCode =>
223			ACCEPTED,
224			ALREADY_REPORTED,
225			BAD_GATEWAY,
226			BAD_REQUEST,
227			CONFLICT,
228			CONTINUE,
229			CREATED,
230			EXPECTATION_FAILED,
231			FAILED_DEPENDENCY,
232			FORBIDDEN,
233			FOUND,
234			GATEWAY_TIMEOUT,
235			GONE,
236			HTTP_VERSION_NOT_SUPPORTED,
237			IM_A_TEAPOT,
238			IM_USED,
239			INSUFFICIENT_STORAGE,
240			INTERNAL_SERVER_ERROR,
241			LENGTH_REQUIRED,
242			LOCKED,
243			LOOP_DETECTED,
244			METHOD_NOT_ALLOWED,
245			MISDIRECTED_REQUEST,
246			MOVED_PERMANENTLY,
247			MULTIPLE_CHOICES,
248			MULTI_STATUS,
249			NETWORK_AUTHENTICATION_REQUIRED,
250			NON_AUTHORITATIVE_INFORMATION,
251			NOT_ACCEPTABLE,
252			NOT_EXTENDED,
253			NOT_FOUND,
254			NOT_IMPLEMENTED,
255			NOT_MODIFIED,
256			NO_CONTENT,
257			OK,
258			PARTIAL_CONTENT,
259			PAYLOAD_TOO_LARGE,
260			PAYMENT_REQUIRED,
261			PERMANENT_REDIRECT,
262			PRECONDITION_FAILED,
263			PRECONDITION_REQUIRED,
264			PROCESSING,
265			PROXY_AUTHENTICATION_REQUIRED,
266			RANGE_NOT_SATISFIABLE,
267			REQUEST_HEADER_FIELDS_TOO_LARGE,
268			REQUEST_TIMEOUT,
269			RESET_CONTENT,
270			SEE_OTHER,
271			SERVICE_UNAVAILABLE,
272			SWITCHING_PROTOCOLS,
273			TEMPORARY_REDIRECT,
274			TOO_MANY_REQUESTS,
275			UNAUTHORIZED,
276			UNAVAILABLE_FOR_LEGAL_REASONS,
277			UNPROCESSABLE_ENTITY,
278			UNSUPPORTED_MEDIA_TYPE,
279			UPGRADE_REQUIRED,
280			URI_TOO_LONG,
281			USE_PROXY,
282			VARIANT_ALSO_NEGOTIATES,
283		);
284	
285	
286	// https://docs.rs/http/0.2.3/src/http/header/name.rs.html#146-965
287	// x-selection co | sed -r -e '\#^\s*$#d' -e 's#^\s+##' -e '\#^//#d' -e 's#\([a-zA-Z0-9]+,\s*([^,]+).*$#\1#' -e 's#^#\t\t\t#' -e 's#$#,#' | LC_ALL=C sort | x-selection pi
288	def_const! (super::HeaderName => header =>
289			ACCEPT,
290			ACCEPT_CHARSET,
291			ACCEPT_ENCODING,
292			ACCEPT_LANGUAGE,
293			ACCEPT_RANGES,
294			ACCESS_CONTROL_ALLOW_CREDENTIALS,
295			ACCESS_CONTROL_ALLOW_HEADERS,
296			ACCESS_CONTROL_ALLOW_METHODS,
297			ACCESS_CONTROL_ALLOW_ORIGIN,
298			ACCESS_CONTROL_EXPOSE_HEADERS,
299			ACCESS_CONTROL_MAX_AGE,
300			ACCESS_CONTROL_REQUEST_HEADERS,
301			ACCESS_CONTROL_REQUEST_METHOD,
302			AGE,
303			ALLOW,
304			ALT_SVC,
305			AUTHORIZATION,
306			CACHE_CONTROL,
307			CONNECTION,
308			CONTENT_DISPOSITION,
309			CONTENT_ENCODING,
310			CONTENT_LANGUAGE,
311			CONTENT_LENGTH,
312			CONTENT_LOCATION,
313			CONTENT_RANGE,
314			CONTENT_SECURITY_POLICY,
315			CONTENT_SECURITY_POLICY_REPORT_ONLY,
316			CONTENT_TYPE,
317			COOKIE,
318			DATE,
319			DNT,
320			ETAG,
321			EXPECT,
322			EXPIRES,
323			FORWARDED,
324			FROM,
325			HOST,
326			IF_MATCH,
327			IF_MODIFIED_SINCE,
328			IF_NONE_MATCH,
329			IF_RANGE,
330			IF_UNMODIFIED_SINCE,
331			LAST_MODIFIED,
332			LINK,
333			LOCATION,
334			MAX_FORWARDS,
335			ORIGIN,
336			PRAGMA,
337			PROXY_AUTHENTICATE,
338			PROXY_AUTHORIZATION,
339			PUBLIC_KEY_PINS,
340			PUBLIC_KEY_PINS_REPORT_ONLY,
341			RANGE,
342			REFERER,
343			REFERRER_POLICY,
344			REFRESH,
345			RETRY_AFTER,
346			SEC_WEBSOCKET_ACCEPT,
347			SEC_WEBSOCKET_EXTENSIONS,
348			SEC_WEBSOCKET_KEY,
349			SEC_WEBSOCKET_PROTOCOL,
350			SEC_WEBSOCKET_VERSION,
351			SERVER,
352			SET_COOKIE,
353			STRICT_TRANSPORT_SECURITY,
354			TE,
355			TRAILER,
356			TRANSFER_ENCODING,
357			UPGRADE,
358			UPGRADE_INSECURE_REQUESTS,
359			USER_AGENT,
360			VARY,
361			VIA,
362			WARNING,
363			WWW_AUTHENTICATE,
364			X_CONTENT_TYPE_OPTIONS,
365			X_DNS_PREFETCH_CONTROL,
366			X_FRAME_OPTIONS,
367			X_XSS_PROTECTION,
368		);
369}
370