Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
parity
Mirrored projects
jsonrpsee
Commits
4316ec7d
Unverified
Commit
4316ec7d
authored
Apr 12, 2021
by
Niklas Adolfsson
Committed by
GitHub
Apr 12, 2021
Browse files
refactor utils crate (#264)
parent
375df101
Changes
18
Hide whitespace changes
Inline
Side-by-side
http-client/Cargo.toml
View file @
4316ec7d
...
...
@@ -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"
}
...
...
http-client/src/transport.rs
View file @
4316ec7d
...
...
@@ -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"
;
...
...
http-server/Cargo.toml
View file @
4316ec7d
...
...
@@ -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"
...
...
utils/src/http
/cors.rs
→
http-server/src/access_control
/cors.rs
View file @
4316ec7d
...
...
@@ -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]
...
...
utils/src/http
/hosts.rs
→
http-server/src/access_control
/hosts.rs
View file @
4316ec7d
...
...
@@ -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
{
...
...
utils/src/http
/matcher.rs
→
http-server/src/access_control
/matcher.rs
View file @
4316ec7d
File moved
utils/src/http
/access_control.rs
→
http-server/src
/access_control
/mod
.rs
View file @
4316ec7d
...
...
@@ -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)]
...
...
http-server/src/lib.rs
View file @
4316ec7d
...
...
@@ -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
};
...
...
http-server/src/module.rs
View file @
4316ec7d
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
;
...
...
http-server/src/server.rs
View file @
4316ec7d
...
...
@@ -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
::{
...
...
utils/Cargo.toml
View file @
4316ec7d
...
...
@@ -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"
...
...
utils/src/http/mod.rs
deleted
100644 → 0
View file @
375df101
// 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
;
utils/src/
http/
hyper_helpers.rs
→
utils/src/hyper_helpers.rs
View file @
4316ec7d
File moved
utils/src/lib.rs
View file @
4316ec7d
...
...
@@ -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
;
utils/src/server
_utils
.rs
→
utils/src/server.rs
View file @
4316ec7d
File moved
ws-server/Cargo.toml
View file @
4316ec7d
...
...
@@ -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"
...
...
ws-server/src/server.rs
View file @
4316ec7d
...
...
@@ -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
;
...
...
ws-server/src/server/module.rs
View file @
4316ec7d
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
;
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment