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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use std::future::Future;
use futures::future::BoxFuture;
use futures::FutureExt;
use hyper::body::Buf;
use hyper::header::HeaderValue;
use hyper::http::request::Parts as RequestParts;
use hyper::http::response::Parts as ResponseParts;
use hyper::http::Error as HttpError;
use hyper::server::conn::AddrIncoming;
use hyper::server::{Builder, Server};
use hyper::{Body, HeaderMap, Request, Response, StatusCode};
use lazy_static::lazy_static;
use serde_json::{Map, Value};
use url::Host;
use yajrc::{AnyRpcMethod, GenericRpcMethod, Id, RpcError, RpcRequest, RpcResponse};
use crate::{Context, Metadata};
lazy_static! {
#[cfg(feature = "cbor")]
static ref CBOR_INTERNAL_ERROR: Vec<u8> =
serde_cbor::to_vec(&RpcResponse::<AnyRpcMethod<'static>>::from(yajrc::INTERNAL_ERROR)).unwrap();
static ref JSON_INTERNAL_ERROR: Vec<u8> =
serde_json::to_vec(&RpcResponse::<AnyRpcMethod<'static>>::from(yajrc::INTERNAL_ERROR)).unwrap();
}
pub fn make_builder<Ctx: Context>(ctx: &Ctx) -> Builder<AddrIncoming> {
let addr = match ctx.host() {
Host::Ipv4(ip) => (ip, ctx.port()).into(),
Host::Ipv6(ip) => (ip, ctx.port()).into(),
Host::Domain(localhost) if localhost == "localhost" => ([127, 0, 0, 1], ctx.port()).into(),
_ => ([0, 0, 0, 0], ctx.port()).into(),
};
Server::bind(&addr)
}
pub async fn make_request(
req_parts: &RequestParts,
req_body: Body,
) -> Result<RpcRequest<GenericRpcMethod<String, Map<String, Value>>>, RpcError> {
let body = hyper::body::aggregate(req_body).await?.reader();
let rpc_req: RpcRequest<GenericRpcMethod<String, Map<String, Value>>>;
#[cfg(feature = "cbor")]
if req_parts
.headers
.get("content-type")
.and_then(|h| h.to_str().ok())
== Some("application/cbor")
{
rpc_req = serde_cbor::from_reader(body)?;
} else {
rpc_req = serde_json::from_reader(body)?;
}
#[cfg(not(feature = "cbor"))]
{
rpc_req = serde_json::from_reader(body)?;
}
Ok(rpc_req)
}
pub fn to_response<F: Fn(i32) -> StatusCode>(
req_headers: &HeaderMap<HeaderValue>,
mut res_parts: ResponseParts,
res: Result<(Option<Id>, Result<Value, RpcError>), RpcError>,
status_code_fn: F,
) -> Result<Response<Body>, HttpError> {
let rpc_res: RpcResponse = match res {
Ok((id, result)) => RpcResponse { id, result },
Err(e) => e.into(),
};
let body;
#[cfg(feature = "cbor")]
if req_headers
.get("accept")
.and_then(|h| h.to_str().ok())
.iter()
.flat_map(|s| s.split(","))
.map(|s| s.trim())
.any(|s| s == "application/cbor")
{
res_parts
.headers
.insert("content-type", HeaderValue::from_static("application/cbor"));
body = serde_cbor::to_vec(&rpc_res).unwrap_or_else(|_| CBOR_INTERNAL_ERROR.clone());
} else {
res_parts
.headers
.insert("content-type", HeaderValue::from_static("application/json"));
body = serde_json::to_vec(&rpc_res).unwrap_or_else(|_| JSON_INTERNAL_ERROR.clone());
}
#[cfg(not(feature = "cbor"))]
{
res_parts
.headers
.insert("content-type", HeaderValue::from_static("application/json"));
body = serde_json::to_vec(&rpc_res).unwrap_or_else(|_| JSON_INTERNAL_ERROR.clone());
}
res_parts.headers.insert(
"content-length",
HeaderValue::from_str(&format!("{}", body.len()))?,
);
res_parts.status = match &rpc_res.result {
Ok(_) => StatusCode::OK,
Err(e) => status_code_fn(e.code),
};
Ok(Response::from_parts(res_parts, body.into()))
}
pub type DynMiddleware<Metadata> = Box<
dyn for<'a> Fn(
&'a mut Request<Body>,
Metadata,
)
-> BoxFuture<'a, Result<Result<DynMiddlewareStage2, Response<Body>>, HttpError>>
+ Send
+ Sync,
>;
pub fn noop<M: Metadata>() -> DynMiddleware<M> {
Box::new(|_, _| async { Ok(Ok(noop2())) }.boxed())
}
pub type DynMiddlewareStage2 = Box<
dyn for<'a> FnOnce(
&'a mut RequestParts,
&'a mut RpcRequest<GenericRpcMethod<String, Map<String, Value>>>,
) -> BoxFuture<
'a,
Result<Result<DynMiddlewareStage3, Response<Body>>, HttpError>,
> + Send
+ Sync,
>;
pub fn noop2() -> DynMiddlewareStage2 {
Box::new(|_, _| async { Ok(Ok(noop3())) }.boxed())
}
pub type DynMiddlewareStage3 = Box<
dyn for<'a> FnOnce(
&'a mut ResponseParts,
&'a mut Result<Value, RpcError>,
) -> BoxFuture<
'a,
Result<Result<DynMiddlewareStage4, Response<Body>>, HttpError>,
> + Send
+ Sync,
>;
pub fn noop3() -> DynMiddlewareStage3 {
Box::new(|_, _| async { Ok(Ok(noop4())) }.boxed())
}
pub type DynMiddlewareStage4 = Box<
dyn for<'a> FnOnce(&'a mut Response<Body>) -> BoxFuture<'a, Result<(), HttpError>>
+ Send
+ Sync,
>;
pub fn noop4() -> DynMiddlewareStage4 {
Box::new(|_| async { Ok(()) }.boxed())
}
pub fn constrain_middleware<
'a,
'b,
'c,
'd,
M: Metadata,
ReqFn: Fn(&'a mut Request<Body>, M) -> ReqFut + Clone,
ReqFut: Future<Output = Result<Result<RpcReqFn, Response<Body>>, HttpError>> + 'a,
RpcReqFn: FnOnce(
&'b mut RequestParts,
&'b mut RpcRequest<GenericRpcMethod<String, Map<String, Value>>>,
) -> RpcReqFut,
RpcReqFut: Future<Output = Result<Result<RpcResFn, Response<Body>>, HttpError>> + 'b,
RpcResFn: FnOnce(&'c mut ResponseParts, &'c mut Result<Value, RpcError>) -> RpcResFut,
RpcResFut: Future<Output = Result<Result<ResFn, Response<Body>>, HttpError>> + 'c,
ResFn: FnOnce(&'d mut Response<Body>) -> ResFut,
ResFut: Future<Output = Result<(), HttpError>> + 'd,
>(
f: ReqFn,
) -> ReqFn {
f
}