Unverified Commit 4316ec7d authored by Niklas Adolfsson's avatar Niklas Adolfsson Committed by GitHub
Browse files

refactor utils crate (#264)

parent 375df101
......@@ -14,7 +14,7 @@ hyper14-rustls = { package = "hyper-rustls", version = "0.22", optional = true }
hyper14 = { package = "hyper", version = "0.14", features = ["client", "http1", "http2", "tcp"], optional = true }
hyper13 = { package = "hyper", version = "0.13", optional = true }
jsonrpsee-types = { path = "../types", version = "0.2.0-alpha.4" }
jsonrpsee-utils = { path = "../utils", version = "0.2.0-alpha.4", default-features = false, optional = true }
jsonrpsee-utils = { path = "../utils", version = "0.2.0-alpha.4", optional = true }
log = "0.4"
serde = { version = "1.0", default-features = false, features = ["derive"] }
serde_json = "1.0"
......@@ -25,8 +25,8 @@ fnv = "1"
[features]
default = ["tokio1"]
tokio1 = ["hyper14", "hyper14-rustls", "jsonrpsee-utils/hyper14"]
tokio02 = ["hyper13", "hyper13-rustls", "jsonrpsee-utils/hyper13"]
tokio1 = ["hyper14", "hyper14-rustls", "jsonrpsee-utils/hyper_14"]
tokio02 = ["hyper13", "hyper13-rustls", "jsonrpsee-utils/hyper_13"]
[dev-dependencies]
jsonrpsee-test-utils = { path = "../test-utils" }
......
......@@ -9,7 +9,7 @@
use hyper::client::{Client, HttpConnector};
use hyper_rustls::HttpsConnector;
use jsonrpsee_types::{error::GenericTransportError, jsonrpc};
use jsonrpsee_utils::http::hyper_helpers;
use jsonrpsee_utils::hyper_helpers;
use thiserror::Error;
const CONTENT_TYPE_JSON: &str = "application/json";
......
......@@ -10,12 +10,15 @@ license = "MIT"
anyhow = "1"
hyper = { version = "0.14", features = ["stream", "server", "http1", "http2", "tcp"] }
jsonrpsee-types = { path = "../types", version = "0.2.0-alpha.4" }
jsonrpsee-utils = { path = "../utils", version = "0.2.0-alpha.4" }
jsonrpsee-utils = { path = "../utils", version = "0.2.0-alpha.4", features = ["server"] }
globset = "0.4"
lazy_static = "1.4"
log = "0.4"
serde = { version = "1", default-features = false, features = ["derive"] }
serde_json = "1"
socket2 = "0.4"
tokio = { version = "1", features = ["full"] }
unicase = "2.6.0"
[dev-dependencies]
env_logger = "0.8"
......
......@@ -26,8 +26,8 @@
//! CORS handling utility functions
use crate::http::hosts::{Host, Port};
use crate::http::matcher::{Matcher, Pattern};
use crate::access_control::hosts::{Host, Port};
use crate::access_control::matcher::{Matcher, Pattern};
use lazy_static::lazy_static;
use std::collections::HashSet;
use std::{fmt, ops};
......@@ -323,7 +323,7 @@ lazy_static! {
#[cfg(test)]
mod tests {
use super::*;
use crate::http::hosts::Host;
use crate::access_control::hosts::Host;
use std::iter;
#[test]
......
......@@ -26,9 +26,7 @@
//! Host header validation.
use crate::http::matcher::{Matcher, Pattern};
use std::collections::HashSet;
use std::net::SocketAddr;
use crate::access_control::matcher::{Matcher, Pattern};
const SPLIT_PROOF: &str = "split always returns non-empty iterator.";
......@@ -135,6 +133,7 @@ impl Pattern for Host {
impl std::ops::Deref for Host {
type Target = str;
fn deref(&self) -> &Self::Target {
&self.as_string
}
......@@ -169,17 +168,6 @@ pub fn is_host_valid(host: Option<&str>, allow_hosts: &AllowHosts) -> bool {
}
}
/// Updates given list of hosts with the address.
pub fn update(hosts: Option<Vec<Host>>, address: &SocketAddr) -> Option<Vec<Host>> {
hosts.map(|current_hosts| {
let mut new_hosts = current_hosts.into_iter().collect::<HashSet<_>>();
let address = address.to_string();
new_hosts.insert(address.clone().into());
new_hosts.insert(address.replace("127.0.0.1", "localhost").into());
new_hosts.into_iter().collect()
})
}
/// Allowed hosts for http header 'host'
#[derive(Clone)]
pub enum AllowHosts {
......
......@@ -24,12 +24,17 @@
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//! Access control based on http headers
//! Access control based on HTTP headers
mod cors;
mod hosts;
mod matcher;
pub use cors::{AccessControlAllowHeaders, AccessControlAllowOrigin};
pub use hosts::{AllowHosts, Host};
use crate::http::cors::{AccessControlAllowHeaders, AccessControlAllowOrigin};
use crate::http::hosts::{AllowHosts, Host};
use crate::http::{cors, hosts, hyper_helpers};
use hyper::header;
use jsonrpsee_utils::hyper_helpers;
/// Define access on control on HTTP layer.
#[derive(Clone)]
......
......@@ -24,11 +24,12 @@
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
mod access_control;
mod module;
mod response;
mod server;
pub use jsonrpsee_utils::http::access_control::{AccessControl, AccessControlBuilder};
pub use access_control::{AccessControl, AccessControlBuilder, AllowHosts, Host};
pub use module::{RpcContextModule, RpcModule};
pub use server::{Builder as HttpServerBuilder, Server as HttpServer};
......
use jsonrpsee_types::error::Error;
use jsonrpsee_types::v2::{traits::RpcMethod, RpcError, RpcParams};
use jsonrpsee_utils::server_utils::{send_response, Methods};
use jsonrpsee_utils::server::{send_response, Methods};
use serde::Serialize;
use std::sync::Arc;
......
......@@ -26,6 +26,7 @@
use crate::module::RpcModule;
use crate::response;
use crate::AccessControl;
use anyhow::anyhow;
use hyper::{
server::{conn::AddrIncoming, Builder as HyperBuilder},
......@@ -38,8 +39,7 @@ use jsonrpsee_types::v2::error::{
PARSE_ERROR_MSG,
};
use jsonrpsee_types::v2::{JsonRpcInvalidRequest, JsonRpcRequest, RpcError, RpcParams};
use jsonrpsee_utils::http::{access_control::AccessControl, hyper_helpers::read_response_to_body};
use jsonrpsee_utils::server_utils::send_error;
use jsonrpsee_utils::{hyper_helpers::read_response_to_body, server::send_error};
use serde::Serialize;
use socket2::{Domain, Socket, Type};
use std::{
......
......@@ -7,22 +7,22 @@ edition = "2018"
license = "MIT"
[dependencies]
anyhow = "1"
futures = "0.3"
globset = "0.4"
anyhow = { version = "1", optional = true }
futures = { version = "0.3", default-features = false, optional = true}
hyper13 = { package = "hyper", version = "0.13", default-features = false, features = ["stream"], optional = true }
hyper14 = { package = "hyper", version = "0.14", default-features = false, features = ["stream"], optional = true }
jsonrpsee-types = { path = "../types", version = "0.2.0-alpha.4" }
lazy_static = "1.4"
log = "0.4"
rustc-hash = "1"
serde = { version = "1.0", default-features = false, features = ["derive"] }
serde_json = { version = "1", features = ["raw_value"] }
tokio = { version = "1", features = ["sync"] }
unicase = "2.6"
jsonrpsee-types = { path = "../types", version = "0.2.0-alpha.4", optional = true }
log = { version = "0.4", optional = true }
rustc-hash = { version = "1", optional = true }
serde = { version = "1.0", default-features = false, features = ["derive"], optional = true }
serde_json = { version = "1", features = ["raw_value"], optional = true }
tokio = { version = "1", features = ["sync"], optional = true }
[features]
default = ["hyper14"]
default = []
hyper_13 = ["hyper13", "futures", "jsonrpsee-types"]
hyper_14 = ["hyper14", "futures", "jsonrpsee-types"]
server = ["anyhow", "tokio", "futures", "jsonrpsee-types", "rustc-hash", "serde", "serde_json", "log"]
[dev-dependencies]
serde_json = "1.0"
......
// Copyright 2019 Parity Technologies (UK) Ltd.
//
// 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.
//! HTTP Server utilities for the `jsonrpsee` library
pub mod access_control;
pub mod cors;
pub mod hosts;
pub mod hyper_helpers;
mod matcher;
pub use matcher::Pattern;
......@@ -5,17 +5,16 @@
#[cfg(all(feature = "hyper13", feature = "hyper14"))]
compile_error!("feature `hyper13` and `hyper14` are mutably exclusive");
#[cfg(not(any(feature = "hyper13", feature = "hyper14")))]
compile_error!("feature `hyper13` or `hyper14` must be enabled for this crate");
#[cfg(all(feature = "hyper13", not(feature = "hyper14")))]
extern crate hyper13 as hyper;
#[cfg(all(feature = "hyper14", not(feature = "hyper13")))]
extern crate hyper14 as hyper;
/// Helpers for HTTP.
pub mod http;
/// Shared hyper helpers.
#[cfg(any(feature = "hyper13", feature = "hyper14"))]
pub mod hyper_helpers;
/// Helpers for JSON-RPC servers.
pub mod server_utils;
/// Shared code for JSON-RPC servers.
#[cfg(feature = "server")]
pub mod server;
......@@ -14,7 +14,7 @@ bs58 = "0.4"
fnv = "1"
futures = "0.3"
jsonrpsee-types = { path = "../types", version = "0.2.0-alpha.4" }
jsonrpsee-utils = { path = "../utils", version = "0.2.0-alpha.4" }
jsonrpsee-utils = { path = "../utils", version = "0.2.0-alpha.4", features = ["server"] }
log = "0.4"
parking_lot = "0.11"
rand = "0.8"
......
......@@ -43,7 +43,7 @@ use tokio_util::compat::TokioAsyncReadCompatExt;
use jsonrpsee_types::v2::error::{METHOD_NOT_FOUND_CODE, METHOD_NOT_FOUND_MSG};
use jsonrpsee_types::v2::{JsonRpcInvalidRequest, JsonRpcRequest, RpcError, RpcParams, TwoPointZero};
use jsonrpsee_types::v2::{JsonRpcNotification, JsonRpcNotificationParams};
use jsonrpsee_utils::server_utils::{send_error, ConnectionId, Methods};
use jsonrpsee_utils::server::{send_error, ConnectionId, Methods};
mod module;
......
use crate::server::{Methods, RpcError, RpcParams, SubscriptionId, SubscriptionSink};
use crate::server::{RpcError, RpcParams, SubscriptionId, SubscriptionSink};
use jsonrpsee_types::error::Error;
use jsonrpsee_types::v2::traits::RpcMethod;
use jsonrpsee_utils::server_utils::send_response;
use jsonrpsee_utils::server::{send_response, Methods};
use parking_lot::Mutex;
use rustc_hash::FxHashMap;
use serde::Serialize;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment