1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#![warn(missing_debug_implementations, rust_2018_idioms)]
#![allow(clippy::mutex_atomic, clippy::module_inception)]
#![doc(test(attr(deny(rust_2018_idioms, warnings))))]
#![doc(test(attr(allow(unused_extern_crates, unused_variables))))]
use async_std::io::{self, prelude::*};
use async_std::task::{Context, Poll};
use futures::future::TryFuture;
use std::fmt;
use std::pin::Pin;
pin_project_lite::pin_project! {
pub struct Body {
#[pin]
reader: Pin<Box<dyn BufRead + Send + 'static>>,
}
}
impl Body {
pub fn empty() -> Self {
Self {
reader: Box::pin(io::empty()),
}
}
pub fn from_reader(reader: impl BufRead + Unpin + Send + 'static) -> Self {
Self {
reader: Box::pin(reader),
}
}
}
impl Read for Body {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
Pin::new(&mut self.reader).poll_read(cx, buf)
}
}
impl BufRead for Body {
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&'_ [u8]>> {
let this = self.project();
this.reader.poll_fill_buf(cx)
}
fn consume(mut self: Pin<&mut Self>, amt: usize) {
Pin::new(&mut self.reader).consume(amt)
}
}
impl fmt::Debug for Body {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Body").field("reader", &"<hidden>").finish()
}
}
impl From<Vec<u8>> for Body {
fn from(vec: Vec<u8>) -> Body {
Self {
reader: Box::pin(io::Cursor::new(vec)),
}
}
}
impl<R: BufRead + Unpin + Send + 'static> From<Pin<Box<R>>> for Body {
fn from(reader: Pin<Box<R>>) -> Self {
Self { reader }
}
}
pub type Request = http::Request<Body>;
pub type Response = http::Response<Body>;
pub trait HttpService: Send + Sync + 'static {
type Connection: Send + 'static;
type ConnectionFuture: Send + 'static + TryFuture<Ok = Self::Connection>;
fn connect(&self) -> Self::ConnectionFuture;
type ResponseFuture: Send + 'static + TryFuture<Ok = Response>;
fn respond(&self, conn: &mut Self::Connection, req: Request) -> Self::ResponseFuture;
}