hyper_simple_server/
resources.rs1
2
3use crate::prelude::*;
4
5
6
7
8#[ cfg (feature = "hss-resources") ]
9pub struct FileResource {
10 pub path : path::PathBuf,
11 pub content_type : Option<ContentType>,
12 pub cache : Option<Arc<RwLock<Option<Bytes>>>>,
13}
14
15
16#[ cfg (feature = "hss-resources") ]
17impl FileResource {
18
19 pub fn new (_path : impl AsRef<path::Path>, _content_type : Option<ContentType>, _should_cache : bool) -> Self {
20 FileResource {
21 path : _path.as_ref () .into (),
22 content_type : _content_type,
23 cache : if _should_cache {
24 Some (Arc::new (RwLock::new (None)))
25 } else {
26 None
27 },
28 }
29 }
30
31 pub fn load (&self) -> ServerResult<Bytes> {
32 if let Some (_cache) = self.cache.as_ref () {
33 let _cache = _cache.read () .or_wrap (0x6801d05a) ?;
34 if let Some (_data) = _cache.as_ref () {
35 return Ok (_data.clone ());
36 }
37 drop (_cache);
38 }
39 let _data = fs::read (&self.path) .or_wrap (0x0db95839) ?;
40 let _data = Bytes::from (_data);
41 if let Some (_cache) = self.cache.as_ref () {
42 let mut _cache = _cache.write () .or_panic (0xf103624b);
43 *_cache = Some (_data.clone ());
44 }
45 Ok (_data)
46 }
47
48 pub fn new_text (_path : impl AsRef<path::Path>) -> Self {
49 Self::new (_path, Some (ContentType::Text), false)
50 }
51
52 pub fn new_html (_path : impl AsRef<path::Path>) -> Self {
53 Self::new (_path, Some (ContentType::Html), false)
54 }
55
56 pub fn new_css (_path : impl AsRef<path::Path>) -> Self {
57 Self::new (_path, Some (ContentType::Css), false)
58 }
59
60 pub fn new_js (_path : impl AsRef<path::Path>) -> Self {
61 Self::new (_path, Some (ContentType::Js), false)
62 }
63
64 pub fn new_json (_path : impl AsRef<path::Path>) -> Self {
65 Self::new (_path, Some (ContentType::Json), false)
66 }
67
68 pub fn new_xml (_path : impl AsRef<path::Path>) -> Self {
69 Self::new (_path, Some (ContentType::Xml), false)
70 }
71
72 pub fn new_png (_path : impl AsRef<path::Path>) -> Self {
73 Self::new (_path, Some (ContentType::Png), false)
74 }
75
76 pub fn new_jpeg (_path : impl AsRef<path::Path>) -> Self {
77 Self::new (_path, Some (ContentType::Jpeg), false)
78 }
79
80 pub fn new_svg (_path : impl AsRef<path::Path>) -> Self {
81 Self::new (_path, Some (ContentType::Svg), false)
82 }
83
84 pub fn new_icon (_path : impl AsRef<path::Path>) -> Self {
85 Self::new (_path, Some (ContentType::Icon), false)
86 }
87
88 pub fn response (&self) -> ServerResult<Response<Body>> {
89 let _data = self.load () ?;
90 let _response = Response::new_200_with_body (_data, self.content_type);
91 Ok (_response)
92 }
93}
94
95
96#[ cfg (feature = "hss-handler") ]
97#[ cfg (feature = "hss-resources") ]
98impl Handler for FileResource {
99
100 type Future = future::Ready<ServerResult<Response<Self::ResponseBody>>>;
101 type ResponseBody = BodyWrapper<Body>;
102 type ResponseBodyError = ServerError;
103
104 fn handle (&self, _request : Request<Body>) -> Self::Future {
105 match self.response () {
106 Ok (_response) => {
107 future::ready (Ok (_response.map (BodyWrapper::new)))
108 }
109 Err (_error) =>
110 future::ready (Err (_error)),
111 }
112 }
113}
114
115
116
117
118#[ cfg (feature = "hss-resources") ]
119pub struct BytesResource {
120 pub data : Bytes,
121 pub content_type : Option<ContentType>,
122}
123
124
125#[ cfg (feature = "hss-resources") ]
126impl BytesResource {
127
128 pub fn new (_data : impl Into<Bytes>, _content_type : Option<ContentType>) -> Self {
129 BytesResource {
130 data : _data.into (),
131 content_type : _content_type,
132 }
133 }
134
135 pub fn new_text (_data : impl Into<Bytes>) -> Self {
136 Self::new (_data, Some (ContentType::Text))
137 }
138
139 pub fn new_html (_data : impl Into<Bytes>) -> Self {
140 Self::new (_data, Some (ContentType::Html))
141 }
142
143 pub fn new_css (_data : impl Into<Bytes>) -> Self {
144 Self::new (_data, Some (ContentType::Css))
145 }
146
147 pub fn new_js (_data : impl Into<Bytes>) -> Self {
148 Self::new (_data, Some (ContentType::Js))
149 }
150
151 pub fn new_json (_data : impl Into<Bytes>) -> Self {
152 Self::new (_data, Some (ContentType::Json))
153 }
154
155 pub fn new_xml (_data : impl Into<Bytes>) -> Self {
156 Self::new (_data, Some (ContentType::Xml))
157 }
158
159 pub fn new_png (_data : impl Into<Bytes>) -> Self {
160 Self::new (_data, Some (ContentType::Png))
161 }
162
163 pub fn new_jpeg (_data : impl Into<Bytes>) -> Self {
164 Self::new (_data, Some (ContentType::Jpeg))
165 }
166
167 pub fn new_svg (_data : impl Into<Bytes>) -> Self {
168 Self::new (_data, Some (ContentType::Svg))
169 }
170
171 pub fn new_icon (_data : impl Into<Bytes>) -> Self {
172 Self::new (_data, Some (ContentType::Icon))
173 }
174
175 pub fn load_from_path (_path : impl AsRef<path::Path>, _content_type : Option<ContentType>) -> ServerResult<Self> {
176 let _data = fs::read (_path) ?;
177 Ok (Self::new (_data, _content_type))
178 }
179
180 pub fn response (&self) -> Response<Body> {
181 Response::new_200_with_body (self.data.clone (), self.content_type)
182 }
183}
184
185
186#[ cfg (feature = "hss-handler") ]
187#[ cfg (feature = "hss-resources") ]
188impl Handler for BytesResource {
189
190 type Future = future::Ready<ServerResult<Response<Self::ResponseBody>>>;
191 type ResponseBody = BodyWrapper<Body>;
192 type ResponseBodyError = ServerError;
193
194 fn handle (&self, _request : Request<Body>) -> Self::Future {
195 let _response = self.response ();
196 future::ready (Ok (_response.map (BodyWrapper::new)))
197 }
198}
199
200
201
202
203#[ cfg (feature = "hss-resources") ]
204pub struct EmbeddedResource {
205 pub data : &'static [u8],
206 pub content_type : Option<ContentType>,
207}
208
209
210#[ cfg (feature = "hss-handler") ]
211#[ cfg (feature = "hss-resources") ]
212impl EmbeddedResource {
213
214 pub fn new (_data : &'static (impl AsRef<[u8]> + ?Sized), _content_type : Option<ContentType>) -> Self {
215 let _data = _data.as_ref ();
216 EmbeddedResource {
217 data : _data,
218 content_type : _content_type,
219 }
220 }
221
222 pub const fn new_const (_data : &'static [u8], _content_type : Option<ContentType>) -> Self {
223 EmbeddedResource {
224 data : _data,
225 content_type : _content_type,
226 }
227 }
228
229 pub fn new_text (_data : &'static (impl AsRef<[u8]> + ?Sized)) -> Self {
230 Self::new (_data, Some (ContentType::Text))
231 }
232
233 pub fn new_html (_data : &'static (impl AsRef<[u8]> + ?Sized)) -> Self {
234 Self::new (_data, Some (ContentType::Html))
235 }
236
237 pub fn new_css (_data : &'static (impl AsRef<[u8]> + ?Sized)) -> Self {
238 Self::new (_data, Some (ContentType::Css))
239 }
240
241 pub fn new_js (_data : &'static (impl AsRef<[u8]> + ?Sized)) -> Self {
242 Self::new (_data, Some (ContentType::Js))
243 }
244
245 pub fn new_json (_data : &'static (impl AsRef<[u8]> + ?Sized)) -> Self {
246 Self::new (_data, Some (ContentType::Json))
247 }
248
249 pub fn new_xml (_data : &'static (impl AsRef<[u8]> + ?Sized)) -> Self {
250 Self::new (_data, Some (ContentType::Xml))
251 }
252
253 pub fn new_png (_data : &'static (impl AsRef<[u8]> + ?Sized)) -> Self {
254 Self::new (_data, Some (ContentType::Png))
255 }
256
257 pub fn new_jpeg (_data : &'static (impl AsRef<[u8]> + ?Sized)) -> Self {
258 Self::new (_data, Some (ContentType::Jpeg))
259 }
260
261 pub fn new_svg (_data : &'static (impl AsRef<[u8]> + ?Sized)) -> Self {
262 Self::new (_data, Some (ContentType::Svg))
263 }
264
265 pub fn new_icon (_data : &'static (impl AsRef<[u8]> + ?Sized)) -> Self {
266 Self::new (_data, Some (ContentType::Icon))
267 }
268
269 pub fn response (&self) -> Response<Body> {
270 Response::new_200_with_body (self.data, self.content_type)
271 }
272}
273
274
275#[ cfg (feature = "hss-handler") ]
276#[ cfg (feature = "hss-resources") ]
277impl Handler for EmbeddedResource {
278
279 type Future = future::Ready<ServerResult<Response<Self::ResponseBody>>>;
280 type ResponseBody = BodyWrapper<Body>;
281 type ResponseBodyError = ServerError;
282
283 fn handle (&self, _request : Request<Body>) -> Self::Future {
284 let _response = self.response ();
285 future::ready (Ok (_response.map (BodyWrapper::new)))
286 }
287}
288