mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-09 04:39:24 +00:00
Replace reqwest with simple-request
reqwest was replaced with hyper and hyper-rustls within monero-serai due to reqwest *solely* offering a connection pool API. In the process, it was demonstrated how quickly we can achieve equivalent functionality to reqwest for our use cases with a fraction of the code. This adds our own reqwest alternative to the tree, applying it to both bitcoin-serai and message-queue. By doing so, bitcoin-serai decreases its tree by 21 packages and the processor by 18. Cargo.lock decreases by 8 dependencies, solely adding simple-request. Notably removed is openssl-sys and openssl. One noted decrease functionality is the requirement on the system having installed CA certificates. While we could fallback to the rustls certificates if the system doesn't have any, that's blocked by https://github.com/rustls/hyper-rustls/pulls/228.
This commit is contained in:
25
common/request/Cargo.toml
Normal file
25
common/request/Cargo.toml
Normal file
@@ -0,0 +1,25 @@
|
||||
[package]
|
||||
name = "simple-request"
|
||||
version = "0.1.0"
|
||||
description = "A simple HTTP(S) request library"
|
||||
license = "MIT"
|
||||
repository = "https://github.com/serai-dex/serai/tree/develop/common/simple-request"
|
||||
authors = ["Luke Parker <lukeparker5132@gmail.com>"]
|
||||
keywords = ["http", "https", "async", "request", "ssl"]
|
||||
edition = "2021"
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
all-features = true
|
||||
rustdoc-args = ["--cfg", "docsrs"]
|
||||
|
||||
[dependencies]
|
||||
# Deprecated here means to enable deprecated warnings, not to restore deprecated APIs
|
||||
hyper = { version = "0.14", default-features = false, features = ["http1", "tcp", "client", "backports", "deprecated"] }
|
||||
hyper-rustls = { version = "0.24", default-features = false, features = ["http1", "native-tokio"] }
|
||||
tokio = { version = "1", default-features = false }
|
||||
|
||||
zeroize = { version = "1", optional = true }
|
||||
base64ct = { version = "1", features = ["alloc"], optional = true }
|
||||
|
||||
[features]
|
||||
basic-auth = ["zeroize", "base64ct"]
|
||||
21
common/request/LICENSE
Normal file
21
common/request/LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023 Luke Parker
|
||||
|
||||
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.
|
||||
7
common/request/README.md
Normal file
7
common/request/README.md
Normal file
@@ -0,0 +1,7 @@
|
||||
# Simple Request
|
||||
|
||||
A simple alternative to reqwest, supporting HTTPS, intended to support a
|
||||
majority of use cases with a fraction of the dependency tree.
|
||||
|
||||
This library is built directly around `hyper`, `hyper-rustls`, and does require
|
||||
`tokio`. Support for `async-std` would be welcome.
|
||||
97
common/request/src/lib.rs
Normal file
97
common/request/src/lib.rs
Normal file
@@ -0,0 +1,97 @@
|
||||
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
||||
#![doc = include_str!("../README.md")]
|
||||
|
||||
use hyper_rustls::{HttpsConnectorBuilder, HttpsConnector};
|
||||
use hyper::{
|
||||
StatusCode,
|
||||
header::{HeaderValue, HeaderMap},
|
||||
body::{Buf, Body},
|
||||
Response as HyperResponse,
|
||||
client::HttpConnector,
|
||||
};
|
||||
pub use hyper::{self, Request};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Response(HyperResponse<Body>);
|
||||
impl Response {
|
||||
pub fn status(&self) -> StatusCode {
|
||||
self.0.status()
|
||||
}
|
||||
pub fn headers(&self) -> &HeaderMap<HeaderValue> {
|
||||
self.0.headers()
|
||||
}
|
||||
pub async fn body(self) -> Result<impl std::io::Read, hyper::Error> {
|
||||
Ok(hyper::body::aggregate(self.0.into_body()).await?.reader())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
enum Connection {
|
||||
ConnectionPool(hyper::Client<HttpsConnector<HttpConnector>>),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Client {
|
||||
connection: Connection,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
InvalidHost,
|
||||
Hyper(hyper::Error),
|
||||
}
|
||||
|
||||
impl Client {
|
||||
fn https_builder() -> HttpsConnector<HttpConnector> {
|
||||
HttpsConnectorBuilder::new().with_native_roots().https_or_http().enable_http1().build()
|
||||
}
|
||||
|
||||
pub fn with_connection_pool() -> Client {
|
||||
Client {
|
||||
connection: Connection::ConnectionPool(hyper::Client::builder().build(Self::https_builder())),
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
fn without_connection_pool() -> Client {}
|
||||
*/
|
||||
|
||||
pub async fn request(&self, mut request: Request<Body>) -> Result<Response, Error> {
|
||||
if request.headers().get(hyper::header::HOST).is_none() {
|
||||
let host = request.uri().host().ok_or(Error::InvalidHost)?.to_string();
|
||||
request
|
||||
.headers_mut()
|
||||
.insert(hyper::header::HOST, HeaderValue::from_str(&host).map_err(|_| Error::InvalidHost)?);
|
||||
}
|
||||
|
||||
#[cfg(feature = "basic-auth")]
|
||||
if request.headers().get(hyper::header::AUTHORIZATION).is_none() {
|
||||
if let Some(authority) = request.uri().authority() {
|
||||
let authority = authority.as_str();
|
||||
if authority.contains('@') {
|
||||
// Decode the username and password from the URI
|
||||
let mut userpass = authority.split('@').next().unwrap().to_string();
|
||||
// If the password is "", the URI may omit :, yet the authentication will still expect it
|
||||
if !userpass.contains(':') {
|
||||
userpass.push(':');
|
||||
}
|
||||
|
||||
use zeroize::Zeroize;
|
||||
use base64ct::{Encoding, Base64};
|
||||
|
||||
let mut encoded = Base64::encode_string(userpass.as_bytes());
|
||||
userpass.zeroize();
|
||||
request.headers_mut().insert(
|
||||
hyper::header::AUTHORIZATION,
|
||||
HeaderValue::from_str(&format!("Basic {encoded}")).unwrap(),
|
||||
);
|
||||
encoded.zeroize();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Response(match &self.connection {
|
||||
Connection::ConnectionPool(client) => client.request(request).await.map_err(Error::Hyper)?,
|
||||
}))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user