1
2
3use crate::prelude::*;
4
5
6
7
8#[ cfg (feature = "hss-handler") ]
9pub trait Handler
10 where
11 Self : Send + Sync + 'static,
12{
13 type Future : Future<Output = ServerResult<Response<Self::ResponseBody>>> + Send + 'static;
14 type ResponseBody : BodyTrait<Data = Bytes, Error = Self::ResponseBodyError> + Send + Sync + 'static;
15 type ResponseBodyError : Error + Send + Sync + 'static;
16
17 fn handle (&self, _request : Request<Body>) -> Self::Future;
18
19 fn wrap (self) -> HandlerWrapper<Self> where Self : Sized {
20 HandlerWrapper (self)
21 }
22}
23
24
25
26
27#[ cfg (feature = "hss-handler") ]
28pub struct HandlerWrapper <H : Handler> (H);
29
30#[ cfg (feature = "hss-handler") ]
31impl <H> hyper::Service<Request<Body>> for HandlerWrapper<H>
32 where
33 H : Handler,
34{
35 type Future = H::Future;
36 type Response = Response<H::ResponseBody>;
37 type Error = ServerError;
38
39 fn poll_ready (&mut self, _context : &mut Context<'_>) -> Poll<ServerResult> {
40 Poll::Ready (Ok (()))
41 }
42
43 fn call (&mut self, _request : Request<Body>) -> Self::Future {
44 self.0.handle (_request)
45 }
46}
47
48
49
50
51#[ cfg (feature = "hss-handler") ]
52pub struct HandlerFnAsync <C, F, RB>
53 where
54 C : Fn (Request<Body>) -> F + Send + Sync + 'static,
55 F : Future<Output = ServerResult<Response<RB>>> + Send + 'static,
56 RB : BodyTrait<Data = Bytes> + Send + Sync + 'static,
57 RB::Error : Error + Send + Sync + 'static,
58{
59 function : C,
60 phantom : PhantomData<fn(RB)>,
61}
62
63
64#[ cfg (feature = "hss-handler") ]
65impl <C, F, RB> Handler for HandlerFnAsync<C, F, RB>
66 where
67 C : Fn (Request<Body>) -> F + Send + Sync + 'static,
68 F : Future<Output = ServerResult<Response<RB>>> + Send + 'static,
69 RB : BodyTrait<Data = Bytes> + Send + Sync + 'static,
70 RB::Error : Error + Send + Sync + 'static,
71{
72 type Future = F;
73 type ResponseBody = RB;
74 type ResponseBodyError = RB::Error;
75
76 fn handle (&self, _request : Request<Body>) -> Self::Future {
77 (self.function) (_request)
78 }
79}
80
81
82#[ cfg (feature = "hss-handler") ]
83impl <C, F, RB> From<C> for HandlerFnAsync<C, F, RB>
84 where
85 C : Fn (Request<Body>) -> F + Send + Sync + 'static,
86 F : Future<Output = ServerResult<Response<RB>>> + Send + 'static,
87 RB : BodyTrait<Data = Bytes> + Send + Sync + 'static,
88 RB::Error : Error + Send + Sync + 'static,
89{
90 fn from (_function : C) -> Self {
91 Self {
92 function : _function,
93 phantom : PhantomData,
94 }
95 }
96}
97
98
99
100
101#[ cfg (feature = "hss-handler") ]
102pub struct HandlerFnSync <C, RB>
103 where
104 C : Fn (Request<Body>) -> ServerResult<Response<RB>> + Send + Sync + 'static,
105 RB : BodyTrait<Data = Bytes> + Send + Sync + 'static,
106 RB::Error : Error + Send + Sync + 'static,
107{
108 function : C,
109 phantom : PhantomData<fn(RB)>,
110}
111
112
113#[ cfg (feature = "hss-handler") ]
114impl <C, RB> Handler for HandlerFnSync<C, RB>
115 where
116 C : Fn (Request<Body>) -> ServerResult<Response<RB>> + Send + Sync + 'static,
117 RB : BodyTrait<Data = Bytes> + Send + Sync + 'static,
118 RB::Error : Error + Send + Sync + 'static,
119{
120 type Future = future::Ready<ServerResult<Response<RB>>>;
121 type ResponseBody = RB;
122 type ResponseBodyError = RB::Error;
123
124 fn handle (&self, _request : Request<Body>) -> Self::Future {
125 future::ready ((self.function) (_request))
126 }
127}
128
129
130#[ cfg (feature = "hss-handler") ]
131impl <C, RB> From<C> for HandlerFnSync<C, RB>
132 where
133 C : Fn (Request<Body>) -> ServerResult<Response<RB>> + Send + Sync + 'static,
134 RB : BodyTrait<Data = Bytes> + Send + Sync + 'static,
135 RB::Error : Error + Send + Sync + 'static,
136{
137 fn from (_function : C) -> Self {
138 Self {
139 function : _function,
140 phantom : PhantomData,
141 }
142 }
143}
144
145
146
147
148#[ cfg (feature = "hss-handler") ]
149pub trait HandlerDyn
150 where
151 Self : Send + Sync + 'static,
152{
153 fn handle (&self, _request : Request<Body>) -> HandlerFutureDynBox;
154}
155
156
157#[ cfg (feature = "hss-handler") ]
158impl <H> HandlerDyn for H
159 where
160 H : Handler + Send + Sync + 'static,
161 H::Future : Future<Output = ServerResult<Response<H::ResponseBody>>> + Send + 'static,
162{
163 fn handle (&self, _request : Request<Body>) -> HandlerFutureDynBox {
164 let _future = Handler::handle (self, _request);
165 let _future = _future.map_ok (|_response| _response.map (BodyDynBox::new));
166 HandlerFutureDynBox::new (_future)
167 }
168}
169
170
171
172
173#[ derive (Clone) ]
174#[ cfg (feature = "hss-handler") ]
175pub struct HandlerDynArc (Arc<dyn HandlerDyn>);
176
177
178#[ cfg (feature = "hss-handler") ]
179impl HandlerDynArc {
180
181 pub fn new (_handler : impl HandlerDyn) -> Self {
182 HandlerDynArc (Arc::new (_handler))
183 }
184
185 pub fn delegate (&self, _request : Request<Body>) -> HandlerFutureDynBox {
186 self.0.handle (_request)
187 }
188
189 pub fn from_arc (_handler : Arc<dyn HandlerDyn>) -> Self {
190 HandlerDynArc (_handler)
191 }
192
193 pub fn into_arc (self) -> Arc<dyn HandlerDyn> {
194 self.0
195 }
196
197 pub fn clone_arc (&self) -> Arc<dyn HandlerDyn> {
198 self.0.clone ()
199 }
200}
201
202
203#[ cfg (feature = "hss-handler") ]
204impl Handler for HandlerDynArc {
205
206 type Future = HandlerFutureDynBox;
207 type ResponseBody = BodyDynBox;
208 type ResponseBodyError = ServerError;
209
210 fn handle (&self, _request : Request<Body>) -> Self::Future {
211 self.delegate (_request)
212 }
213}
214
215
216
217
218#[ cfg (feature = "hss-handler") ]
219pub struct HandlerFutureDynBox (Pin<Box<dyn Future<Output = ServerResult<Response<BodyDynBox>>> + Send>>);
220
221
222#[ cfg (feature = "hss-handler") ]
223impl Future for HandlerFutureDynBox {
224
225 type Output = ServerResult<Response<BodyDynBox>>;
226
227 fn poll (self : Pin<&mut Self>, _context : &mut Context<'_>) -> Poll<Self::Output> {
228 let _self = Pin::into_inner (self);
229 _self.0.as_mut () .poll (_context)
230 }
231}
232
233
234#[ cfg (feature = "hss-handler") ]
235impl HandlerFutureDynBox {
236
237 pub fn new <F> (_future : F) -> Self
238 where
239 F : Future<Output = ServerResult<Response<BodyDynBox>>> + Send + 'static
240 {
241 Self (Box::pin (_future))
242 }
243
244 pub fn ready (_result : ServerResult<Response<BodyDynBox>>) -> Self {
245 Self::new (future::ready (_result))
246 }
247
248 pub fn ready_response (_response : Response<BodyDynBox>) -> Self {
249 Self::ready (Ok (_response))
250 }
251
252 pub fn ready_error (_error : ServerError) -> Self {
253 Self::ready (Err (_error))
254 }
255}
256
257
258#[ cfg (feature = "hss-handler") ]
259#[ cfg (feature = "hss-extensions") ]
260impl <B> From<Response<B>> for HandlerFutureDynBox
261 where
262 B : BodyTrait<Data = Bytes> + Send + Sync + 'static,
263 B::Error : Error + Send + Sync + 'static,
264{
265 fn from (_response : Response<B>) -> Self {
266 Self::ready_response (_response.map (BodyDynBox::new))
267 }
268}
269
270
271#[ cfg (feature = "hss-handler") ]
272#[ cfg (feature = "hss-extensions") ]
273impl From<ServerError> for HandlerFutureDynBox {
274 fn from (_error : ServerError) -> Self {
275 Self::ready_error (_error)
276 }
277}
278
279
280
281
282#[ cfg (feature = "hss-handler") ]
283#[ cfg (feature = "hss-extensions") ]
284pub trait HandlerSimpleAsync
285 where
286 Self : Send + Sync + 'static,
287{
288 type Future : Future<Output = ServerResult<Response<Body>>> + Send + 'static;
289
290 fn handle (&self, _request : Request<Body>) -> Self::Future;
291
292 fn wrap (self) -> HandlerSimpleAsyncWrapper<Self> where Self : Sized {
293 HandlerSimpleAsyncWrapper (self)
294 }
295}
296
297
298#[ cfg (feature = "hss-handler") ]
299#[ cfg (feature = "hss-extensions") ]
300pub struct HandlerSimpleAsyncWrapper <H> (H)
301 where
302 H : HandlerSimpleAsync,
303;
304
305
306#[ cfg (feature = "hss-handler") ]
307#[ cfg (feature = "hss-extensions") ]
308impl <H> HandlerSimpleAsyncWrapper<H>
309 where
310 H : HandlerSimpleAsync,
311{
312 pub fn new (_handler : H) -> Self {
313 Self (_handler)
314 }
315}
316
317
318#[ cfg (feature = "hss-handler") ]
319#[ cfg (feature = "hss-extensions") ]
320impl <H> Handler for HandlerSimpleAsyncWrapper<H>
321 where
322 H : HandlerSimpleAsync,
323 H::Future : Unpin,
324{
325 type Future = HandlerSimpleAsyncWrapperFuture<H::Future>;
326 type ResponseBody = BodyWrapper<Body>;
327 type ResponseBodyError = ServerError;
328
329 fn handle (&self, _request : Request<Body>) -> Self::Future {
330 let _future = HandlerSimpleAsync::handle (&self.0, _request);
331 let _future = HandlerSimpleAsyncWrapperFuture (_future);
332 _future
333 }
334}
335
336
337#[ cfg (feature = "hss-handler") ]
338#[ cfg (feature = "hss-extensions") ]
339pub struct HandlerSimpleAsyncWrapperFuture <F> (F)
340 where
341 F : Future<Output = ServerResult<Response<Body>>> + Send + 'static + Unpin,
342;
343
344#[ cfg (feature = "hss-handler") ]
345#[ cfg (feature = "hss-extensions") ]
346impl <F> Future for HandlerSimpleAsyncWrapperFuture<F>
347 where
348 F : Future<Output = ServerResult<Response<Body>>> + Send + 'static + Unpin,
349{
350 type Output = ServerResult<Response<BodyWrapper<Body>>>;
351
352 fn poll (self : Pin<&mut Self>, _context : &mut Context<'_>) -> Poll<Self::Output> {
353 let _self = Pin::into_inner (self);
354 let _delegate = Pin::new (&mut _self.0);
355 let _poll = _delegate.poll (_context);
356 let _poll = _poll.map_ok (|_response| _response.map (BodyWrapper::new));
357 _poll
358 }
359}
360
361
362
363
364#[ cfg (feature = "hss-handler") ]
365#[ cfg (feature = "hss-extensions") ]
366pub trait HandlerSimpleSync
367 where
368 Self : Send + Sync + 'static,
369{
370 fn handle (&self, _request : &Request<Body>, _response : &mut Response<Body>) -> ServerResult;
371
372 fn wrap (self) -> HandlerSimpleSyncWrapper<Self> where Self : Sized {
373 HandlerSimpleSyncWrapper (self)
374 }
375}
376
377
378#[ cfg (feature = "hss-handler") ]
379#[ cfg (feature = "hss-extensions") ]
380pub struct HandlerSimpleSyncWrapper <H> (H)
381 where
382 H : HandlerSimpleSync,
383;
384
385
386#[ cfg (feature = "hss-handler") ]
387#[ cfg (feature = "hss-extensions") ]
388impl <H> HandlerSimpleSyncWrapper<H>
389 where
390 H : HandlerSimpleSync,
391{
392 pub fn new (_handler : H) -> Self {
393 Self (_handler)
394 }
395}
396
397
398#[ cfg (feature = "hss-handler") ]
399#[ cfg (feature = "hss-extensions") ]
400impl <H> Handler for HandlerSimpleSyncWrapper<H>
401 where
402 H : HandlerSimpleSync,
403{
404 type Future = future::Ready<ServerResult<Response<Self::ResponseBody>>>;
405 type ResponseBody = BodyWrapper<Body>;
406 type ResponseBodyError = ServerError;
407
408 fn handle (&self, _request : Request<Body>) -> Self::Future {
409 let mut _response = Response::new (Body::empty ());
410 match HandlerSimpleSync::handle (&self.0, &_request, &mut _response) {
411 Ok (()) =>
412 future::ready (Ok (_response.map (BodyWrapper::new))),
413 Err (_error) =>
414 future::ready (Err (_error)),
415 }
416 }
417}
418
419
420
421
422#[ cfg (feature = "hss-handler") ]
423#[ cfg (feature = "hss-extensions") ]
424pub struct BodyWrapper <B> (B)
425 where
426 B : BodyTrait<Data = Bytes> + Send + 'static + Unpin,
427 B::Error : Error + Send + Sync + 'static,
428;
429
430
431#[ cfg (feature = "hss-handler") ]
432#[ cfg (feature = "hss-extensions") ]
433impl <B> BodyTrait for BodyWrapper<B>
434 where
435 B : BodyTrait<Data = Bytes> + Send + 'static + Unpin,
436 B::Error : Error + Send + Sync + 'static,
437{
438 type Data = Bytes;
439 type Error = ServerError;
440
441 fn poll_data (self : Pin<&mut Self>, _context : &mut Context<'_>) -> Poll<Option<ServerResult<Bytes>>> {
442 let _future = self.delegate_pin_mut () .poll_data (_context);
443 let _future = _future.map (|_option| _option.map (|_result| _result.map_err (|_error| _error.wrap (0x4e33a117))));
444 _future
445 }
446
447 fn poll_trailers (self : Pin<&mut Self>, _context : &mut Context<'_>) -> Poll<ServerResult<Option<Headers>>> {
448 let _future = self.delegate_pin_mut () .poll_trailers (_context);
449 let _future = _future.map (|_result| _result.map_err (|_error| _error.wrap (0x3a25b983)));
450 _future
451 }
452
453 fn is_end_stream (&self) -> bool {
454 self.delegate () .is_end_stream ()
455 }
456
457 fn size_hint (&self) -> BodySizeHint {
458 self.delegate () .size_hint ()
459 }
460}
461
462
463#[ cfg (feature = "hss-handler") ]
464#[ cfg (feature = "hss-extensions") ]
465impl <B> BodyWrapper<B>
466 where
467 B : BodyTrait<Data = Bytes> + Send + 'static + Unpin,
468 B::Error : Error + Send + Sync + 'static,
469{
470 pub fn new (_body : B) -> Self {
471 Self (_body)
472 }
473
474 fn delegate_pin_mut (self : Pin<&mut Self>) -> Pin<&mut B> {
475 let _self = Pin::into_inner (self);
476 Pin::new (&mut _self.0)
477 }
478
479 fn delegate (&self) -> Pin<&B> {
480 Pin::new (&self.0)
481 }
482}
483
484
485
486
487#[ cfg (feature = "hss-handler") ]
488pub trait BodyDyn
489 where
490 Self : Send + 'static,
491{
492 fn poll_data (self : Pin<&mut Self>, _context : &mut Context<'_>) -> Poll<Option<ServerResult<Bytes>>>;
493 fn poll_trailers (self : Pin<&mut Self>, _context : &mut Context<'_>) -> Poll<ServerResult<Option<Headers>>>;
494
495 fn is_end_stream (&self) -> bool;
496 fn size_hint (&self) -> BodySizeHint;
497}
498
499
500#[ cfg (feature = "hss-handler") ]
501impl <B> BodyDyn for B
502 where
503 B : BodyTrait<Data = Bytes> + Send + 'static,
504 B::Error : Error + Send + Sync + 'static,
505{
506 fn poll_data (self : Pin<&mut Self>, _context : &mut Context<'_>) -> Poll<Option<ServerResult<Bytes>>> {
507 let _future = BodyTrait::poll_data (self, _context);
508 let _future = _future.map (|_option| _option.map (|_result| _result.map_err (|_error| _error.wrap (0xd89897d4))));
509 _future
510 }
511
512 fn poll_trailers (self : Pin<&mut Self>, _context : &mut Context<'_>) -> Poll<ServerResult<Option<Headers>>> {
513 let _future = BodyTrait::poll_trailers (self, _context);
514 let _future = _future.map (|_result| _result.map_err (|_error| _error.wrap (0x8adea6a0)));
515 _future
516 }
517
518 fn is_end_stream (&self) -> bool {
519 BodyTrait::is_end_stream (self)
520 }
521
522 fn size_hint (&self) -> BodySizeHint {
523 BodyTrait::size_hint (self)
524 }
525}
526
527
528
529
530#[ cfg (feature = "hss-handler") ]
531pub struct BodyDynBox (Pin<Box<dyn BodyDyn + Sync>>);
532
533
534#[ cfg (feature = "hss-handler") ]
535impl BodyTrait for BodyDynBox {
536
537 type Data = Bytes;
538 type Error = ServerError;
539
540 fn poll_data (self : Pin<&mut Self>, _context : &mut Context<'_>) -> Poll<Option<ServerResult<Bytes>>> {
541 self.delegate_pin_mut () .poll_data (_context)
542 }
543
544 fn poll_trailers (self : Pin<&mut Self>, _context : &mut Context<'_>) -> Poll<ServerResult<Option<Headers>>> {
545 self.delegate_pin_mut () .poll_trailers (_context)
546 }
547
548 fn is_end_stream (&self) -> bool {
549 self.delegate () .is_end_stream ()
550 }
551
552 fn size_hint (&self) -> BodySizeHint {
553 self.delegate () .size_hint ()
554 }
555}
556
557
558#[ cfg (feature = "hss-handler") ]
559impl BodyDynBox {
560
561 pub fn new (_body : impl BodyDyn + Sync) -> Self {
562 Self (Box::pin (_body))
563 }
564
565 fn delegate_pin_mut (self : Pin<&mut Self>) -> Pin<&mut dyn BodyDyn> {
566 let _self = Pin::into_inner (self);
567 _self.0.as_mut ()
568 }
569
570 fn delegate (&self) -> Pin<&dyn BodyDyn> {
571 self.0.as_ref ()
572 }
573}
574