server.rs 33.2 KB
Newer Older
1
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
Niklas Adolfsson's avatar
Niklas Adolfsson committed
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
//
// 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.

27
use std::convert::Infallible;
Maciej Hirsz's avatar
Maciej Hirsz committed
28
use std::future::Future;
29
use std::net::{SocketAddr, TcpListener as StdTcpListener};
Maciej Hirsz's avatar
Maciej Hirsz committed
30
31
32
use std::pin::Pin;
use std::task::{Context, Poll};

33
use crate::response;
34
use crate::response::{internal_error, malformed};
35
use futures_channel::mpsc;
36
37
use futures_util::future::FutureExt;
use futures_util::stream::{StreamExt, TryStreamExt};
38
use futures_util::TryFutureExt;
39
use hyper::body::HttpBody;
40
use hyper::header::{HeaderMap, HeaderValue};
41
use hyper::server::conn::AddrStream;
Maciej Hirsz's avatar
Maciej Hirsz committed
42
use hyper::server::{conn::AddrIncoming, Builder as HyperBuilder};
43
44
use hyper::service::{make_service_fn, Service};
use hyper::{Body, Error as HyperError, Method};
Maciej Hirsz's avatar
Maciej Hirsz committed
45
use jsonrpsee_core::error::{Error, GenericTransportError};
46
use jsonrpsee_core::http_helpers::{self, read_body};
47
use jsonrpsee_core::logger::{self, HttpLogger as Logger};
48
use jsonrpsee_core::server::access_control::AccessControl;
49
50
use jsonrpsee_core::server::helpers::{prepare_error, MethodResponse};
use jsonrpsee_core::server::helpers::{BatchResponse, BatchResponseBuilder};
Maciej Hirsz's avatar
Maciej Hirsz committed
51
use jsonrpsee_core::server::resource_limiting::Resources;
52
use jsonrpsee_core::server::rpc_module::{MethodKind, Methods};
53
use jsonrpsee_core::tracing::{rx_log_from_json, rx_log_from_str, tx_log_from_str, RpcTracing};
54
use jsonrpsee_core::TEN_MB_SIZE_BYTES;
55
use jsonrpsee_types::error::{ErrorCode, ErrorObject, BATCHES_NOT_SUPPORTED_CODE, BATCHES_NOT_SUPPORTED_MSG};
56
use jsonrpsee_types::{Id, Notification, Params, Request};
57
use serde_json::value::RawValue;
Alexandru Vasile's avatar
Fix fmt    
Alexandru Vasile committed
58
use std::error::Error as StdError;
59
use tokio::net::{TcpListener, ToSocketAddrs};
60
use tower::layer::util::Identity;
61
use tower::Layer;
62
use tracing_futures::Instrument;
Niklas Adolfsson's avatar
Niklas Adolfsson committed
63

64
65
type Notif<'a> = Notification<'a, Option<&'a RawValue>>;

Niklas Adolfsson's avatar
Niklas Adolfsson committed
66
/// Builder to create JSON-RPC HTTP server.
67
#[derive(Debug)]
68
pub struct Builder<B = Identity, L = ()> {
69
	/// Access control based on HTTP headers.
Niklas Adolfsson's avatar
Niklas Adolfsson committed
70
	access_control: AccessControl,
Maciej Hirsz's avatar
Maciej Hirsz committed
71
	resources: Resources,
Niklas Adolfsson's avatar
Niklas Adolfsson committed
72
	max_request_body_size: u32,
73
	max_response_body_size: u32,
74
	batch_requests_supported: bool,
75
76
	/// Custom tokio runtime to run the server on.
	tokio_runtime: Option<tokio::runtime::Handle>,
77
	logger: L,
78
	max_log_length: u32,
79
	health_api: Option<HealthApi>,
80
	service_builder: tower::ServiceBuilder<B>,
Maciej Hirsz's avatar
Maciej Hirsz committed
81
82
83
84
85
}

impl Default for Builder {
	fn default() -> Self {
		Self {
86
			access_control: AccessControl::default(),
Maciej Hirsz's avatar
Maciej Hirsz committed
87
			max_request_body_size: TEN_MB_SIZE_BYTES,
88
			max_response_body_size: TEN_MB_SIZE_BYTES,
89
			batch_requests_supported: true,
Maciej Hirsz's avatar
Maciej Hirsz committed
90
91
			resources: Resources::default(),
			tokio_runtime: None,
92
			logger: (),
93
			max_log_length: 4096,
94
			health_api: None,
95
			service_builder: tower::ServiceBuilder::new(),
Maciej Hirsz's avatar
Maciej Hirsz committed
96
97
		}
	}
Niklas Adolfsson's avatar
Niklas Adolfsson committed
98
99
}

Niklas Adolfsson's avatar
Niklas Adolfsson committed
100
impl Builder {
Maciej Hirsz's avatar
Maciej Hirsz committed
101
102
103
104
105
106
	/// Create a default server builder.
	pub fn new() -> Self {
		Self::default()
	}
}

107
impl<B, L> Builder<B, L> {
108
	/// Add a logger to the builder [`Logger`](../jsonrpsee_core/logger/trait.Logger.html).
Maciej Hirsz's avatar
Maciej Hirsz committed
109
	///
110
111
	/// # Examples
	///
Maciej Hirsz's avatar
Maciej Hirsz committed
112
	/// ```
113
	/// use std::{time::Instant, net::SocketAddr};
114
	/// use hyper::Request;
Maciej Hirsz's avatar
Maciej Hirsz committed
115
	///
116
	/// use jsonrpsee_core::logger::{HttpLogger, Headers, MethodKind, Params};
Maciej Hirsz's avatar
Maciej Hirsz committed
117
118
	/// use jsonrpsee_http_server::HttpServerBuilder;
	///
Maciej Hirsz's avatar
Maciej Hirsz committed
119
	/// #[derive(Clone)]
120
	/// struct MyLogger;
Maciej Hirsz's avatar
Maciej Hirsz committed
121
	///
122
	/// impl HttpLogger for MyLogger {
Maciej Hirsz's avatar
Maciej Hirsz committed
123
124
	///     type Instant = Instant;
	///
125
126
	///     // Called once the HTTP request is received, it may be a single JSON-RPC call
	///     // or batch.
127
	///     fn on_request(&self, _remote_addr: SocketAddr, _request: &Request<hyper::Body>) -> Instant {
Maciej Hirsz's avatar
Maciej Hirsz committed
128
129
	///         Instant::now()
	///     }
Niklas Adolfsson's avatar
Niklas Adolfsson committed
130
	///
131
132
	///     // Called once a single JSON-RPC method call is processed, it may be called multiple times
	///     // on batches.
133
134
	///     fn on_call(&self, method_name: &str, params: Params, kind: MethodKind) {
	///         println!("Call to method: '{}' params: {:?}, kind: {}", method_name, params, kind);
135
136
137
138
139
140
141
	///     }
	///
	///     // Called once a single JSON-RPC call is completed, it may be called multiple times
	///     // on batches.
	///     fn on_result(&self, method_name: &str, success: bool, started_at: Instant) {
	///         println!("Call to '{}' took {:?}", method_name, started_at.elapsed());
	///     }
Maciej Hirsz's avatar
Maciej Hirsz committed
142
	///
Niklas Adolfsson's avatar
Niklas Adolfsson committed
143
	///     // Called the entire JSON-RPC is completed, called on once for both single calls or batches.
144
145
	///     fn on_response(&self, result: &str, started_at: Instant) {
	///         println!("complete JSON-RPC response: {}, took: {:?}", result, started_at.elapsed());
Maciej Hirsz's avatar
Maciej Hirsz committed
146
147
148
	///     }
	/// }
	///
149
	/// let builder = HttpServerBuilder::new().set_logger(MyLogger);
Maciej Hirsz's avatar
Maciej Hirsz committed
150
	/// ```
151
	pub fn set_logger<T: Logger>(self, logger: T) -> Builder<B, T> {
Maciej Hirsz's avatar
Maciej Hirsz committed
152
		Builder {
153
			access_control: self.access_control,
Maciej Hirsz's avatar
Maciej Hirsz committed
154
			max_request_body_size: self.max_request_body_size,
155
			max_response_body_size: self.max_response_body_size,
156
			batch_requests_supported: self.batch_requests_supported,
Maciej Hirsz's avatar
Maciej Hirsz committed
157
158
			resources: self.resources,
			tokio_runtime: self.tokio_runtime,
159
			logger,
160
			max_log_length: self.max_log_length,
161
			health_api: self.health_api,
162
			service_builder: self.service_builder,
Maciej Hirsz's avatar
Maciej Hirsz committed
163
164
165
		}
	}

Niklas Adolfsson's avatar
Niklas Adolfsson committed
166
167
168
169
170
	/// Sets the maximum size of a request body in bytes (default is 10 MiB).
	pub fn max_request_body_size(mut self, size: u32) -> Self {
		self.max_request_body_size = size;
		self
	}
Niklas Adolfsson's avatar
Niklas Adolfsson committed
171

172
173
174
175
176
177
	/// Sets the maximum size of a response body in bytes (default is 10 MiB).
	pub fn max_response_body_size(mut self, size: u32) -> Self {
		self.max_response_body_size = size;
		self
	}

Niklas Adolfsson's avatar
Niklas Adolfsson committed
178
179
180
181
182
	/// Sets access control settings.
	pub fn set_access_control(mut self, acl: AccessControl) -> Self {
		self.access_control = acl;
		self
	}
Niklas Adolfsson's avatar
Niklas Adolfsson committed
183

184
185
186
187
188
189
190
	/// Enables or disables support of [batch requests](https://www.jsonrpc.org/specification#batch).
	/// By default, support is enabled.
	pub fn batch_requests_supported(mut self, supported: bool) -> Self {
		self.batch_requests_supported = supported;
		self
	}

191
192
193
	/// Register a new resource kind. Errors if `label` is already registered, or if the number of
	/// registered resources on this server instance would exceed 8.
	///
194
	/// See the module documentation for [`resource_limiting`](../jsonrpsee_utils/server/resource_limiting/index.html#resource-limiting)
195
	/// for details.
Maciej Hirsz's avatar
Maciej Hirsz committed
196
197
198
199
200
201
	pub fn register_resource(mut self, label: &'static str, capacity: u16, default: u16) -> Result<Self, Error> {
		self.resources.register(label, capacity, default)?;

		Ok(self)
	}

202
203
204
	/// Configure a custom [`tokio::runtime::Handle`] to run the server on.
	///
	/// Default: [`tokio::spawn`]
205
	pub fn custom_tokio_runtime(mut self, rt: tokio::runtime::Handle) -> Self {
206
		self.tokio_runtime = Some(rt);
207
		self
208
209
	}

210
	/// Enable health endpoint.
211
212
213
214
215
216
217
218
	/// Allows you to expose one of the methods under GET /<path> The method will be invoked with no parameters.
	/// Error returned from the method will be converted to status 500 response.
	/// Expects a tuple with (</path>, <rpc-method-name>).
	///
	/// Fails if the path is missing `/`.
	pub fn health_api(mut self, path: impl Into<String>, method: impl Into<String>) -> Result<Self, Error> {
		let path = path.into();

Niklas Adolfsson's avatar
Niklas Adolfsson committed
219
		if !path.starts_with('/') {
220
221
222
			return Err(Error::Custom(format!("Health endpoint path must start with `/` to work, got: {}", path)));
		}

Niklas Adolfsson's avatar
Niklas Adolfsson committed
223
		self.health_api = Some(HealthApi { path, method: method.into() });
224
		Ok(self)
225
226
	}

227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
	/// Configure a custom [`tower::ServiceBuilder`] middleware for composing layers to be applied to the RPC service.
	///
	/// Default: No tower layers are applied to the RPC service.
	///
	/// # Examples
	///
	/// ```rust
	///
	/// use std::time::Duration;
	/// use std::net::SocketAddr;
	/// use jsonrpsee_http_server::HttpServerBuilder;
	///
	/// #[tokio::main]
	/// async fn main() {
	///     let builder = tower::ServiceBuilder::new()
	///         .timeout(Duration::from_secs(2));
	///
	///     let server = HttpServerBuilder::new()
	///         .set_middleware(builder)
	///         .build("127.0.0.1:0".parse::<SocketAddr>().unwrap())
	///         .await
	///         .unwrap();
	/// }
	/// ```
	pub fn set_middleware<T>(self, service_builder: tower::ServiceBuilder<T>) -> Builder<T, L> {
		Builder {
			access_control: self.access_control,
			max_request_body_size: self.max_request_body_size,
			max_response_body_size: self.max_response_body_size,
			batch_requests_supported: self.batch_requests_supported,
			resources: self.resources,
			tokio_runtime: self.tokio_runtime,
			logger: self.logger,
			max_log_length: self.max_log_length,
			health_api: self.health_api,
			service_builder,
		}
	}

266
267
	/// Finalizes the configuration of the server with customized TCP settings on the socket and on hyper.
	///
268
269
	/// # Examples
	///
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
	/// ```rust
	/// use jsonrpsee_http_server::HttpServerBuilder;
	/// use socket2::{Domain, Socket, Type};
	/// use std::net::TcpListener;
	///
	/// #[tokio::main]
	/// async fn main() {
	///   let addr = "127.0.0.1:0".parse().unwrap();
	///   let domain = Domain::for_address(addr);
	///   let socket = Socket::new(domain, Type::STREAM, None).unwrap();
	///   socket.set_nonblocking(true).unwrap();
	///
	///   let address = addr.into();
	///   socket.bind(&address).unwrap();
	///   socket.listen(4096).unwrap();
	///
	///   let listener: TcpListener = socket.into();
	///   let local_addr = listener.local_addr().ok();
	///
	///   // hyper does some settings on the provided socket, ensure that nothing breaks our "expected settings".
	///
	///   let listener = hyper::Server::from_tcp(listener)
	///     .unwrap()
	///     .tcp_sleep_on_accept_errors(true)
	///     .tcp_keepalive(None)
	///     .tcp_nodelay(true);
	///
	///   let server = HttpServerBuilder::new().build_from_hyper(listener, addr).unwrap();
	/// }
	/// ```
	pub fn build_from_hyper(
		self,
		listener: hyper::server::Builder<AddrIncoming>,
		local_addr: SocketAddr,
304
	) -> Result<Server<B, L>, Error> {
305
		Ok(Server {
306
			access_control: self.access_control,
307
			listener,
308
309
310
			local_addr: Some(local_addr),
			max_request_body_size: self.max_request_body_size,
			max_response_body_size: self.max_response_body_size,
311
			batch_requests_supported: self.batch_requests_supported,
312
313
			resources: self.resources,
			tokio_runtime: self.tokio_runtime,
314
			logger: self.logger,
315
			max_log_length: self.max_log_length,
316
			health_api: self.health_api,
317
			service_builder: self.service_builder,
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
		})
	}

	/// Finalizes the configuration of the server with customized TCP settings on the socket.
	/// Note, that [`hyper`] might overwrite some of the TCP settings on the socket
	/// if you want full-control of socket settings use [`Builder::build_from_hyper`] instead.
	///
	/// ```rust
	/// use jsonrpsee_http_server::HttpServerBuilder;
	/// use socket2::{Domain, Socket, Type};
	/// use std::time::Duration;
	///
	/// #[tokio::main]
	/// async fn main() {
	///   let addr = "127.0.0.1:0".parse().unwrap();
	///   let domain = Domain::for_address(addr);
	///   let socket = Socket::new(domain, Type::STREAM, None).unwrap();
	///   socket.set_nonblocking(true).unwrap();
	///
	///   let address = addr.into();
	///   socket.bind(&address).unwrap();
	///
	///   socket.listen(4096).unwrap();
	///
	///   let server = HttpServerBuilder::new().build_from_tcp(socket).unwrap();
	/// }
	/// ```
345
	pub fn build_from_tcp(self, listener: impl Into<StdTcpListener>) -> Result<Server<B, L>, Error> {
346
347
348
349
350
351
		let listener = listener.into();
		let local_addr = listener.local_addr().ok();

		let listener = hyper::Server::from_tcp(listener)?;

		Ok(Server {
352
			listener,
353
354
355
356
			local_addr,
			access_control: self.access_control,
			max_request_body_size: self.max_request_body_size,
			max_response_body_size: self.max_response_body_size,
357
			batch_requests_supported: self.batch_requests_supported,
358
359
			resources: self.resources,
			tokio_runtime: self.tokio_runtime,
360
			logger: self.logger,
361
			max_log_length: self.max_log_length,
362
			health_api: self.health_api,
363
			service_builder: self.service_builder,
364
365
366
		})
	}

367
	/// Finalizes the configuration of the server.
368
369
370
371
372
373
374
375
376
377
	///
	/// ```rust
	/// #[tokio::main]
	/// async fn main() {
	///   let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
	///   let occupied_addr = listener.local_addr().unwrap();
	///   let addrs: &[std::net::SocketAddr] = &[
	///       occupied_addr,
	///       "127.0.0.1:0".parse().unwrap(),
	///   ];
378
379
	///   assert!(jsonrpsee_http_server::HttpServerBuilder::default().build(occupied_addr).await.is_err());
	///   assert!(jsonrpsee_http_server::HttpServerBuilder::default().build(addrs).await.is_ok());
380
381
	/// }
	/// ```
382
	pub async fn build(self, addrs: impl ToSocketAddrs) -> Result<Server<B, L>, Error> {
383
		let listener = TcpListener::bind(addrs).await?.into_std()?;
Niklas Adolfsson's avatar
Niklas Adolfsson committed
384
385

		let local_addr = listener.local_addr().ok();
386
387
388
		let listener = hyper::Server::from_tcp(listener)?.tcp_nodelay(true);

		Ok(Server {
389
			listener,
390
391
392
393
			local_addr,
			access_control: self.access_control,
			max_request_body_size: self.max_request_body_size,
			max_response_body_size: self.max_response_body_size,
394
			batch_requests_supported: self.batch_requests_supported,
395
396
			resources: self.resources,
			tokio_runtime: self.tokio_runtime,
397
			logger: self.logger,
398
			max_log_length: self.max_log_length,
399
			health_api: self.health_api,
400
			service_builder: self.service_builder,
401
		})
Niklas Adolfsson's avatar
Niklas Adolfsson committed
402
	}
Niklas Adolfsson's avatar
Niklas Adolfsson committed
403
}
Niklas Adolfsson's avatar
Niklas Adolfsson committed
404

405
406
407
408
409
410
#[derive(Debug, Clone)]
struct HealthApi {
	path: String,
	method: String,
}

411
/// Handle used to run or stop the server.
412
#[derive(Debug)]
413
pub struct ServerHandle {
414
	stop_sender: mpsc::Sender<()>,
415
	pub(crate) handle: Option<tokio::task::JoinHandle<()>>,
416
417
}

418
impl ServerHandle {
419
	/// Requests server to stop. Returns an error if server was already stopped.
420
	pub fn stop(mut self) -> Result<tokio::task::JoinHandle<()>, Error> {
421
		let stop = self.stop_sender.try_send(()).map(|_| self.handle.take());
422
423
424
425
		match stop {
			Ok(Some(handle)) => Ok(handle),
			_ => Err(Error::AlreadyStopped),
		}
426
427
428
	}
}

429
430
431
432
433
434
435
436
437
438
439
440
441
impl Future for ServerHandle {
	type Output = ();

	fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
		let handle = match &mut self.handle {
			Some(handle) => handle,
			None => return Poll::Ready(()),
		};

		handle.poll_unpin(cx).map(|_| ())
	}
}

442
/// Data required by the server to handle requests.
443
#[derive(Debug, Clone)]
444
struct ServiceData<L> {
445
	/// Remote server address.
446
	remote_addr: SocketAddr,
447
448
449
450
451
452
	/// Registered server methods.
	methods: Methods,
	/// Access control.
	acl: AccessControl,
	/// Tracker for currently used resources on the server.
	resources: Resources,
453
454
	/// User provided logger.
	logger: L,
455
456
457
458
459
460
461
462
463
464
465
466
467
468
	/// Health API.
	health_api: Option<HealthApi>,
	/// Max request body size.
	max_request_body_size: u32,
	/// Max response body size.
	max_response_body_size: u32,
	/// Max length for logging for request and response
	///
	/// Logs bigger than this limit will be truncated.
	max_log_length: u32,
	/// Whether batch requests are supported by this server or not.
	batch_requests_supported: bool,
}

469
470
impl<L: Logger> ServiceData<L> {
	/// Default behavior for handling the RPC requests.
Alexandru Vasile's avatar
Alexandru Vasile committed
471
	async fn handle_request(
472
473
		self,
		request: hyper::Request<hyper::Body>,
474
	) -> Result<hyper::Response<hyper::Body>, Infallible> {
475
		let ServiceData {
Alexandru Vasile's avatar
Alexandru Vasile committed
476
477
478
479
			remote_addr,
			methods,
			acl,
			resources,
480
			logger,
Alexandru Vasile's avatar
Alexandru Vasile committed
481
482
483
484
485
486
487
			health_api,
			max_request_body_size,
			max_response_body_size,
			max_log_length,
			batch_requests_supported,
		} = self;

488
		let request_start = logger.on_request(remote_addr, &request);
Alexandru Vasile's avatar
Alexandru Vasile committed
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545

		let keys = request.headers().keys().map(|k| k.as_str());
		let cors_request_headers = http_helpers::get_cors_request_headers(request.headers());

		let host = match http_helpers::read_header_value(request.headers(), "host") {
			Some(origin) => origin,
			None => return Ok(malformed()),
		};
		let maybe_origin = http_helpers::read_header_value(request.headers(), "origin");

		if let Err(e) = acl.verify_host(host) {
			tracing::warn!("Denied request: {:?}", e);
			return Ok(response::host_not_allowed());
		}

		if let Err(e) = acl.verify_origin(maybe_origin, host) {
			tracing::warn!("Denied request: {:?}", e);
			return Ok(response::invalid_allow_origin());
		}

		if let Err(e) = acl.verify_headers(keys, cors_request_headers) {
			tracing::warn!("Denied request: {:?}", e);
			return Ok(response::invalid_allow_headers());
		}

		// Only `POST` and `OPTIONS` methods are allowed.
		match *request.method() {
			// An OPTIONS request is a CORS preflight request. We've done our access check
			// above so we just need to tell the browser that the request is OK.
			Method::OPTIONS => {
				let origin = match maybe_origin {
					Some(origin) => origin,
					None => return Ok(malformed()),
				};

				let allowed_headers = acl.allowed_headers().to_cors_header_value();
				let allowed_header_bytes = allowed_headers.as_bytes();

				let res = hyper::Response::builder()
					.header("access-control-allow-origin", origin)
					.header("access-control-allow-methods", "POST")
					.header("access-control-allow-headers", allowed_header_bytes)
					.body(hyper::Body::empty())
					.unwrap_or_else(|e| {
						tracing::error!("Error forming preflight response: {}", e);
						internal_error()
					});

				Ok(res)
			}
			// The actual request. If it's a CORS request we need to remember to add
			// the access-control-allow-origin header (despite preflight) to allow it
			// to be read in a browser.
			Method::POST if content_type_is_json(&request) => {
				let origin = return_origin_if_different_from_host(request.headers()).cloned();
				let mut res = process_validated_request(ProcessValidatedRequest {
					request,
546
					logger,
Alexandru Vasile's avatar
Alexandru Vasile committed
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
					methods,
					resources,
					max_request_body_size,
					max_response_body_size,
					max_log_length,
					batch_requests_supported,
					request_start,
				})
				.await?;

				if let Some(origin) = origin {
					res.headers_mut().insert("access-control-allow-origin", origin);
				}
				Ok(res)
			}
			Method::GET => match health_api.as_ref() {
				Some(health) if health.path.as_str() == request.uri().path() => {
					process_health_request(
						health,
566
						logger,
Alexandru Vasile's avatar
Alexandru Vasile committed
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
						methods,
						max_response_body_size,
						request_start,
						max_log_length,
					)
					.await
				}
				_ => Ok(response::method_not_allowed()),
			},
			// Error scenarios:
			Method::POST => Ok(response::unsupported_content_type()),
			_ => Ok(response::method_not_allowed()),
		}
	}
}

583
584
585
586
/// JsonRPSee service compatible with `tower`.
///
/// # Note
/// This is similar to [`hyper::service::service_fn`].
Alexandru Vasile's avatar
Alexandru Vasile committed
587
#[derive(Debug)]
588
pub struct TowerService<L> {
589
	inner: ServiceData<L>,
590
591
}

592
impl<L: Logger> hyper::service::Service<hyper::Request<hyper::Body>> for TowerService<L> {
593
	type Response = hyper::Response<hyper::Body>;
594
595
596
597
598
599

	// NOTE(lexnv): The `handle_request` method returns `Result<_, Infallible>`.
	// This is because the RPC service will always return a valid HTTP response (ie return `Ok(_)`).
	//
	// The following associated type is required by the `impl<B, U, L: Logger> Server<B, L>` bounds.
	// It satisfies the server's bounds when the `tower::ServiceBuilder<B>` is not set (ie `B: Identity`).
600
	type Error = Box<dyn StdError + Send + Sync + 'static>;
601

602
603
604
605
606
607
608
609
610
	type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;

	/// Opens door for back pressure implementation.
	fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>> {
		Poll::Ready(Ok(()))
	}

	fn call(&mut self, request: hyper::Request<hyper::Body>) -> Self::Future {
		let data = self.inner.clone();
611
612
613
614
		// Note that `handle_request` will never return error.
		// The dummy error is set in place to satisfy the server's trait bounds regarding the
		// `tower::ServiceBuilder` and the error will never be mapped.
		Box::pin(data.handle_request(request).map_err(|_| "".into()))
615
616
617
	}
}

618
619
/// An HTTP JSON RPC server.
#[derive(Debug)]
620
pub struct Server<B = Identity, L = ()> {
Niklas Adolfsson's avatar
Niklas Adolfsson committed
621
	/// Hyper server.
622
	listener: HyperBuilder<AddrIncoming>,
Niklas Adolfsson's avatar
Niklas Adolfsson committed
623
624
625
626
	/// Local address
	local_addr: Option<SocketAddr>,
	/// Max request body size.
	max_request_body_size: u32,
627
628
	/// Max response body size.
	max_response_body_size: u32,
629
630
631
632
	/// Max length for logging for request and response
	///
	/// Logs bigger than this limit will be truncated.
	max_log_length: u32,
633
634
	/// Whether batch requests are supported by this server or not.
	batch_requests_supported: bool,
635
	/// Access control.
Niklas Adolfsson's avatar
Niklas Adolfsson committed
636
	access_control: AccessControl,
637
	/// Tracker for currently used resources on the server.
Maciej Hirsz's avatar
Maciej Hirsz committed
638
	resources: Resources,
639
640
	/// Custom tokio runtime to run the server on.
	tokio_runtime: Option<tokio::runtime::Handle>,
641
	logger: L,
642
	health_api: Option<HealthApi>,
643
	service_builder: tower::ServiceBuilder<B>,
Niklas Adolfsson's avatar
Niklas Adolfsson committed
644
}
Niklas Adolfsson's avatar
Niklas Adolfsson committed
645

646
647
648
649
650
651
652
653
654
655
656
657
658
659
impl<B, U, L: Logger> Server<B, L>
where
	B: Layer<TowerService<L>> + Send + 'static,
	<B as Layer<TowerService<L>>>::Service: Send
		+ Service<
			hyper::Request<Body>,
			Response = hyper::Response<U>,
			Error = Box<(dyn StdError + Send + Sync + 'static)>,
		>,
	<<B as Layer<TowerService<L>>>::Service as Service<hyper::Request<Body>>>::Future: Send,
	U: HttpBody + Send + 'static,
	<U as HttpBody>::Error: Send + Sync + StdError,
	<U as HttpBody>::Data: Send,
{
Niklas Adolfsson's avatar
Niklas Adolfsson committed
660
	/// Returns socket address to which the server is bound.
661
662
	pub fn local_addr(&self) -> Result<SocketAddr, Error> {
		self.local_addr.ok_or_else(|| Error::Custom("Local address not found".into()))
Niklas Adolfsson's avatar
Niklas Adolfsson committed
663
664
	}

Niklas Adolfsson's avatar
Niklas Adolfsson committed
665
	/// Start the server.
666
	pub fn start(mut self, methods: impl Into<Methods>) -> Result<ServerHandle, Error> {
Niklas Adolfsson's avatar
Niklas Adolfsson committed
667
		let max_request_body_size = self.max_request_body_size;
668
		let max_response_body_size = self.max_response_body_size;
669
		let max_log_length = self.max_log_length;
670
		let acl = self.access_control;
671
672
		let (tx, mut rx) = mpsc::channel(1);
		let listener = self.listener;
Maciej Hirsz's avatar
Maciej Hirsz committed
673
		let resources = self.resources;
674
		let logger = self.logger;
675
		let batch_requests_supported = self.batch_requests_supported;
Maciej Hirsz's avatar
Maciej Hirsz committed
676
		let methods = methods.into().initialize_resources(&resources)?;
677
		let health_api = self.health_api;
Niklas Adolfsson's avatar
Niklas Adolfsson committed
678

679
		let make_service = make_service_fn(move |conn: &AddrStream| {
680
			let service = TowerService {
681
				inner: ServiceData {
682
					remote_addr: conn.remote_addr(),
683
684
685
					methods: methods.clone(),
					acl: acl.clone(),
					resources: resources.clone(),
686
					logger: logger.clone(),
687
688
689
690
691
692
693
694
					health_api: health_api.clone(),
					max_request_body_size,
					max_response_body_size,
					max_log_length,
					batch_requests_supported,
				},
			};

695
696
			let server = self.service_builder.service(service);

697
			// For every request the `TowerService` is calling into `ServiceData::handle_request`
698
			// where the RPSee bare implementation resides.
699
			async move { Ok::<_, HyperError>(server) }
Niklas Adolfsson's avatar
Niklas Adolfsson committed
700
		});
Niklas Adolfsson's avatar
Niklas Adolfsson committed
701

702
703
704
705
706
707
		let rt = match self.tokio_runtime.take() {
			Some(rt) => rt,
			None => tokio::runtime::Handle::current(),
		};

		let handle = rt.spawn(async move {
708
			let server = listener.serve(make_service);
709
710
711
			let _ = server.with_graceful_shutdown(async move { rx.next().await.map_or((), |_| ()) }).await;
		});

712
		Ok(ServerHandle { handle: Some(handle), stop_sender: tx })
Niklas Adolfsson's avatar
Niklas Adolfsson committed
713
714
715
	}
}

716
717
718
719
720
721
722
723
724
725
726
727
728
729
// Checks the origin and host headers. If they both exist, return the origin if it does not match the host.
// If one of them doesn't exist (origin most probably), or they are identical, return None.
fn return_origin_if_different_from_host(headers: &HeaderMap) -> Option<&HeaderValue> {
	if let (Some(origin), Some(host)) = (headers.get("origin"), headers.get("host")) {
		if origin != host {
			Some(origin)
		} else {
			None
		}
	} else {
		None
	}
}

Niklas Adolfsson's avatar
Niklas Adolfsson committed
730
/// Checks that content type of received request is valid for JSON-RPC.
731
732
fn content_type_is_json(request: &hyper::Request<hyper::Body>) -> bool {
	is_json(request.headers().get("content-type"))
Niklas Adolfsson's avatar
Niklas Adolfsson committed
733
734
}

Niklas Adolfsson's avatar
Niklas Adolfsson committed
735
736
737
/// Returns true if the `content_type` header indicates a valid JSON message.
fn is_json(content_type: Option<&hyper::header::HeaderValue>) -> bool {
	match content_type.and_then(|val| val.to_str().ok()) {
738
		Some(content)
Niklas Adolfsson's avatar
Niklas Adolfsson committed
739
740
741
742
743
			if content.eq_ignore_ascii_case("application/json")
				|| content.eq_ignore_ascii_case("application/json; charset=utf-8")
				|| content.eq_ignore_ascii_case("application/json;charset=utf-8") =>
		{
			true
Niklas Adolfsson's avatar
Niklas Adolfsson committed
744
		}
Niklas Adolfsson's avatar
Niklas Adolfsson committed
745
		_ => false,
Niklas Adolfsson's avatar
Niklas Adolfsson committed
746
747
	}
}
748

749
struct ProcessValidatedRequest<L: Logger> {
750
	request: hyper::Request<hyper::Body>,
751
	logger: L,
752
753
754
	methods: Methods,
	resources: Resources,
	max_request_body_size: u32,
755
	max_response_body_size: u32,
756
	max_log_length: u32,
757
	batch_requests_supported: bool,
758
	request_start: L::Instant,
759
760
761
}

/// Process a verified request, it implies a POST request with content type JSON.
762
763
async fn process_validated_request<L: Logger>(
	input: ProcessValidatedRequest<L>,
764
) -> Result<hyper::Response<hyper::Body>, Infallible> {
765
766
	let ProcessValidatedRequest {
		request,
767
		logger,
768
769
770
771
772
773
774
775
776
		methods,
		resources,
		max_request_body_size,
		max_response_body_size,
		max_log_length,
		batch_requests_supported,
		request_start,
	} = input;

777
778
	let (parts, body) = request.into_parts();

779
	let (body, is_single) = match read_body(&parts.headers, body, max_request_body_size).await {
780
		Ok(r) => r,
781
		Err(GenericTransportError::TooLarge) => return Ok(response::too_large(max_request_body_size)),
782
783
784
785
786
787
788
789
790
		Err(GenericTransportError::Malformed) => return Ok(response::malformed()),
		Err(GenericTransportError::Inner(e)) => {
			tracing::error!("Internal error reading request body: {}", e);
			return Ok(response::internal_error());
		}
	};

	// Single request or notification
	if is_single {
791
792
		let call = CallData {
			conn_id: 0,
793
			logger: &logger,
794
795
796
797
798
799
800
			methods: &methods,
			max_response_body_size,
			max_log_length,
			resources: &resources,
			request_start,
		};
		let response = process_single_request(body, call).await;
801
		logger.on_response(&response.result, request_start);
802
803
804
805
806
807
808
809
		Ok(response::ok_response(response.result))
	}
	// Batch of requests or notifications
	else if !batch_requests_supported {
		let err = MethodResponse::error(
			Id::Null,
			ErrorObject::borrowed(BATCHES_NOT_SUPPORTED_CODE, &BATCHES_NOT_SUPPORTED_MSG, None),
		);
810
		logger.on_response(&err.result, request_start);
811
812
813
814
815
816
817
818
		Ok(response::ok_response(err.result))
	}
	// Batch of requests or notifications
	else {
		let response = process_batch_request(Batch {
			data: body,
			call: CallData {
				conn_id: 0,
819
				logger: &logger,
820
821
822
823
824
825
826
827
				methods: &methods,
				max_response_body_size,
				max_log_length,
				resources: &resources,
				request_start,
			},
		})
		.await;
828
		logger.on_response(&response.result, request_start);
829
830
831
		Ok(response::ok_response(response.result))
	}
}
832

833
async fn process_health_request<L: Logger>(
834
	health_api: &HealthApi,
835
	logger: L,
836
837
	methods: Methods,
	max_response_body_size: u32,
838
	request_start: L::Instant,
839
	max_log_length: u32,
840
) -> Result<hyper::Response<hyper::Body>, Infallible> {
841
	let trace = RpcTracing::method_call(&health_api.method);
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
	async {
		tx_log_from_str("HTTP health API", max_log_length);
		let response = match methods.method_with_name(&health_api.method) {
			None => MethodResponse::error(Id::Null, ErrorObject::from(ErrorCode::MethodNotFound)),
			Some((_name, method_callback)) => match method_callback.inner() {
				MethodKind::Sync(callback) => {
					(callback)(Id::Number(0), Params::new(None), max_response_body_size as usize)
				}
				MethodKind::Async(callback) => {
					(callback)(Id::Number(0), Params::new(None), 0, max_response_body_size as usize, None).await
				}
				MethodKind::Subscription(_) | MethodKind::Unsubscription(_) => {
					MethodResponse::error(Id::Null, ErrorObject::from(ErrorCode::InternalError))
				}
			},
		};
858

859
		rx_log_from_str(&response.result, max_log_length);
860
861
		logger.on_result(&health_api.method, response.success, request_start);
		logger.on_response(&response.result, request_start);
862

863
864
865
866
867
		if response.success {
			#[derive(serde::Deserialize)]
			struct RpcPayload<'a> {
				#[serde(borrow)]
				result: &'a serde_json::value::RawValue,
868
			}
869

870
871
872
873
874
			let payload: RpcPayload = serde_json::from_str(&response.result)
				.expect("valid JSON-RPC response must have a result field and be valid JSON; qed");
			Ok(response::ok_response(payload.result.to_string()))
		} else {
			Ok(response::internal_error())
875
		}
876
	}
877
878
	.instrument(trace.into_span())
	.await
879
880
881
}

#[derive(Debug, Clone)]
882
struct Batch<'a, L: Logger> {
883
	data: Vec<u8>,
884
	call: CallData<'a, L>,
885
886
887
}

#[derive(Debug, Clone)]
888
struct CallData<'a, L: Logger> {
889
	conn_id: usize,
890
	logger: &'a L,
891
892
893
894
	methods: &'a Methods,
	max_response_body_size: u32,
	max_log_length: u32,
	resources: &'a Resources,
895
	request_start: L::Instant,
896
897
898
}

#[derive(Debug, Clone)]
899
struct Call<'a, L: Logger> {
900
901
	params: Params<'a>,
	name: &'a str,
902
	call: CallData<'a, L>,
903
904
905
906
907
908
	id: Id<'a>,
}

// Batch responses must be sent back as a single message so we read the results from each
// request in the batch and read the results off of a new channel, `rx_batch`, and then send the
// complete batch response back to the client over `tx`.
909
async fn process_batch_request<L>(b: Batch<'_, L>) -> BatchResponse
910
where
911
	L: Logger,
912
913
914
915
916
917
918
919
920
{
	let Batch { data, call } = b;

	if let Ok(batch) = serde_json::from_slice::<Vec<Request>>(&data) {
		let max_response_size = call.max_response_body_size;
		let batch = batch.into_iter().map(|req| Ok((req, call.clone())));

		let batch_stream = futures_util::stream::iter(batch);

921
		let trace = RpcTracing::batch();
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
		return async {
			let batch_response = batch_stream
				.try_fold(
					BatchResponseBuilder::new_with_limit(max_response_size as usize),
					|batch_response, (req, call)| async move {
						let params = Params::new(req.params.map(|params| params.get()));
						let response = execute_call(Call { name: &req.method, params, id: req.id, call }).await;
						batch_response.append(&response)
					},
				)
				.await;

			match batch_response {
				Ok(batch) => batch.finish(),
				Err(batch_err) => batch_err,
			}
		}
		.instrument(trace.into_span())
		.await;
941
942
943
944
945
	}

	if let Ok(batch) = serde_json::from_slice::<Vec<Notif>>(&data) {
		return if !batch.is_empty() {
			BatchResponse { result: "".to_string(), success: true }
946
		} else {
947
948
949
			BatchResponse::error(Id::Null, ErrorObject::from(ErrorCode::InvalidRequest))
		};
	}
950

951
952
953
954
955
956
957
	// "If the batch rpc call itself fails to be recognized as an valid JSON or as an
	// Array with at least one value, the response from the Server MUST be a single
	// Response object." – The Spec.
	let (id, code) = prepare_error(&data);
	BatchResponse::error(id, ErrorObject::from(code))
}

958
async fn process_single_request<L: Logger>(data: Vec<u8>, call: CallData<'_, L>) -> MethodResponse {
959
960
	if let Ok(req) = serde_json::from_slice::<Request>(&data) {
		let trace = RpcTracing::method_call(&req.method);
961
962
963
964
965
966
967
968
969
		async {
			rx_log_from_json(&req, call.max_log_length);
			let params = Params::new(req.params.map(|params| params.get()));
			let name = &req.method;
			let id = req.id;
			execute_call(Call { name, params, id, call }).await
		}
		.instrument(trace.into_span())
		.await
970
971
	} else if let Ok(req) = serde_json::from_slice::<Notif>(&data) {
		let trace = RpcTracing::notification(&req.method);
972
973
		let span = trace.into_span();
		let _enter = span.enter();
974
		rx_log_from_json(&req, call.max_log_length);
975

976
977
978
979
980
		MethodResponse { result: String::new(), success: true }
	} else {
		let (id, code) = prepare_error(&data);
		MethodResponse::error(id, ErrorObject::from(code))
	}
981
}
982

983
async fn execute_call<L: Logger>(c: Call<'_, L>) -> MethodResponse {
984
	let Call { name, id, params, call } = c;
985
	let CallData { resources, methods, logger, max_response_body_size, max_log_length, conn_id, request_start } = call;
986
987

	let response = match methods.method_with_name(name) {
988
		None => {
989
			logger.on_call(name, params.clone(), logger::MethodKind::Unknown);
990
991
			MethodResponse::error(id, ErrorObject::from(ErrorCode::MethodNotFound))
		}
992
		Some((name, method)) => match &method.inner() {
993
			MethodKind::Sync(callback) => {
994
				logger.on_call(name, params.clone(), logger::MethodKind::MethodCall);
995
996
997
998
999
1000

				match method.claim(name, resources) {
					Ok(guard) => {
						let r = (callback)(id, params, max_response_body_size as usize);
						drop(guard);
						r
For faster browsing, not all history is shown. View entire blame