Newer
Older
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
// Copyright (c) 2019 Parity Technologies Limited
//
// Permission is hereby granted, free of charge, to any
// person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the
// Software without restriction, including without
// limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice
// shall be included in all copies or substantial portions
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
use crate::transport::{HttpTransportServer, TransportServerEvent};
use jsonrpsee_types::jsonrpc::{
self,
wrapped::{batches, Notification, Params},
};
use core::{fmt, hash::Hash};
pub type RequestId = u64;
/// Wraps around a "raw server" and adds capabilities.
///
/// See the module-level documentation for more information.
pub struct RawServer {
/// Internal "raw" server.
raw: HttpTransportServer,
/// List of requests that are in the progress of being answered. Each batch is associated with
/// the raw request ID, or with `None` if this raw request has been closed.
///
/// See the documentation of [`BatchesState`][batches::BatchesState] for more information.
batches: batches::BatchesState<Option<RequestId>>,
}
/// Identifier of a request within a `RawServer`.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct RawServerRequestId {
inner: batches::BatchesRequestId,
}
/// Identifier of a subscription within a [`RawServer`](crate::server::RawServer).
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct RawServerSubscriptionId([u8; 32]);
/// Event generated by a [`RawServer`](crate::server::RawServer).
///
/// > **Note**: Holds a borrow of the `RawServer`. Therefore, must be dropped before the `RawServer` can
/// > be dropped.
#[derive(Debug)]
pub enum RawServerEvent<'a> {
/// Request is a notification.
Notification(Notification),
/// Request is a method call.
Request(RawServerRequest<'a>),
}
/// Request received by a [`RawServer`](crate::raw::RawServer).
pub struct RawServerRequest<'a> {
/// Reference to the request within `self.batches`.
inner: batches::BatchesRequest<'a, Option<RequestId>>,
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/// Reference to the corresponding field in `RawServer`.
raw: &'a mut HttpTransportServer,
}
impl RawServer {
/// Starts a [`RawServer`](crate::raw::RawServer) using the given raw server internally.
pub fn new(raw: HttpTransportServer) -> RawServer {
RawServer { raw, batches: batches::BatchesState::new() }
}
}
impl RawServer {
/// Returns a `Future` resolving to the next event that this server generates.
pub async fn next_event<'a>(&'a mut self) -> RawServerEvent<'a> {
let request_id = loop {
match self.batches.next_event() {
None => {}
Some(batches::BatchesEvent::Notification { notification, .. }) => {
return RawServerEvent::Notification(notification)
}
Some(batches::BatchesEvent::Request(inner)) => {
break RawServerRequestId { inner: inner.id() };
}
Some(batches::BatchesEvent::ReadyToSend { response, user_param: Some(raw_request_id) }) => {
let _ = self.raw.finish(&raw_request_id, Some(&response)).await;
continue;
}
Some(batches::BatchesEvent::ReadyToSend { response: _, user_param: None }) => {
// This situation happens if the connection has been closed by the client.
// Clients who close their connection.
continue;
}
};
match self.raw.next_request().await {
TransportServerEvent::Request { id, request } => self.batches.inject(request, Some(id)),
TransportServerEvent::Closed(raw_id) => {
// The client has a closed their connection. We eliminate all traces of the
// raw request ID from our state.
// TODO: this has an O(n) complexity; make sure that this is not attackable
for ud in self.batches.batches() {
if ud.as_ref() == Some(&raw_id) {
*ud = None;
}
}
}
};
};
RawServerEvent::Request(self.request_by_id(&request_id).unwrap())
}
/// Returns a request previously returned by [`next_event`](crate::raw::RawServer::next_event)
/// by its id.
///
/// Note that previous notifications don't have an ID and can't be accessed with this method.
///
/// Returns `None` if the request ID is invalid or if the request has already been answered in
/// the past.
pub fn request_by_id<'a>(&'a mut self, id: &RawServerRequestId) -> Option<RawServerRequest<'a>> {
Some(RawServerRequest { inner: self.batches.request_by_id(id.inner)?, raw: &mut self.raw })
}
}
impl From<HttpTransportServer> for RawServer {
fn from(inner: HttpTransportServer) -> Self {
RawServer::new(inner)
}
}
impl<'a> RawServerRequest<'a> {
/// Returns the id of the request.
///
/// If this request object is dropped, you can retrieve it again later by calling
/// [`request_by_id`](crate::raw::RawServer::request_by_id).
pub fn id(&self) -> RawServerRequestId {
RawServerRequestId { inner: self.inner.id() }
}
/// Returns the id that the client sent out.
// TODO: can return None, which is wrong
pub fn request_id(&self) -> &jsonrpc::Id {
self.inner.request_id()
}
/// Returns the method of this request.
pub fn method(&self) -> &str {
self.inner.method()
}
/// Returns the parameters of the request, as a `jsonrpc::Params`.
pub fn params(&self) -> Params {
self.inner.params()
}
}
impl<'a> RawServerRequest<'a> {
/// Send back a response.
///
/// If this request is part of a batch:
///
/// - If all requests of the batch have been responded to, then the response is actively
/// sent out.
/// - Otherwise, this response is buffered.
///
/// > **Note**: This method is implemented in a way that doesn't wait for long to send the
/// > response. While calling this method will block your entire server, it
/// > should only block it for a short amount of time. See also [the equivalent
/// > method](crate::transport::TransportServer::finish) on the
/// > [`TransportServer`](crate::transport::TransportServer) trait.
///
pub fn respond(self, response: Result<jsonrpc::JsonValue, jsonrpc::Error>) {
self.inner.set_response(response);
//unimplemented!();
// TODO: actually send out response?
}
}
impl<'a> fmt::Debug for RawServerRequest<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RawServerRequest")
.field("request_id", &self.request_id())
.field("method", &self.method())
.field("params", &self.params())
.finish()
}
}