Handle gzip/deflate/brotli on all reqwest clients

This commit is contained in:
Mike Dilger 2023-01-26 07:43:33 +13:00
parent 1257891c71
commit e4f1088053
5 changed files with 95 additions and 19 deletions

52
Cargo.lock generated
View File

@ -139,6 +139,21 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "alloc-no-stdlib"
version = "2.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc7bb162ec39d46ab1ca8c77bf72e890535becd1751bb45f64c597edb4c8c6b3"
[[package]]
name = "alloc-stdlib"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94fb8275041c72129eb51b7d0322c29b8387a0386127718b096429201a5d6ece"
dependencies = [
"alloc-no-stdlib",
]
[[package]] [[package]]
name = "android_system_properties" name = "android_system_properties"
version = "0.1.5" version = "0.1.5"
@ -195,6 +210,20 @@ dependencies = [
"parking_lot", "parking_lot",
] ]
[[package]]
name = "async-compression"
version = "0.3.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "942c7cd7ae39e91bde4820d74132e9862e62c2f386c3aa90ccf55949f5bad63a"
dependencies = [
"brotli",
"flate2",
"futures-core",
"memchr",
"pin-project-lite",
"tokio",
]
[[package]] [[package]]
name = "async-executor" name = "async-executor"
version = "1.5.0" version = "1.5.0"
@ -370,6 +399,27 @@ dependencies = [
"objc2-encode", "objc2-encode",
] ]
[[package]]
name = "brotli"
version = "3.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1a0b1dbcc8ae29329621f8d4f0d835787c1c38bb1401979b49d13b0b305ff68"
dependencies = [
"alloc-no-stdlib",
"alloc-stdlib",
"brotli-decompressor",
]
[[package]]
name = "brotli-decompressor"
version = "2.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b6561fd3f895a11e8f72af2cb7d22e08366bebc2b6b57f7744c4bda27034744"
dependencies = [
"alloc-no-stdlib",
"alloc-stdlib",
]
[[package]] [[package]]
name = "bumpalo" name = "bumpalo"
version = "3.12.0" version = "3.12.0"
@ -3027,6 +3077,7 @@ version = "0.11.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21eed90ec8570952d53b772ecf8f206aa1ec9a3d76b2521c56c42973f2d91ee9" checksum = "21eed90ec8570952d53b772ecf8f206aa1ec9a3d76b2521c56c42973f2d91ee9"
dependencies = [ dependencies = [
"async-compression",
"base64 0.21.0", "base64 0.21.0",
"bytes", "bytes",
"encoding_rs", "encoding_rs",
@ -3050,6 +3101,7 @@ dependencies = [
"serde_urlencoded", "serde_urlencoded",
"tokio", "tokio",
"tokio-native-tls", "tokio-native-tls",
"tokio-util",
"tower-service", "tower-service",
"url", "url",
"wasm-bindgen", "wasm-bindgen",

View File

@ -30,7 +30,7 @@ nostr-types = { git = "https://github.com/mikedilger/nostr-types" }
parking_lot = "0.12" parking_lot = "0.12"
rand = "0.8" rand = "0.8"
regex = "1.7" regex = "1.7"
reqwest = { version = "0.11", features = ["json"] } reqwest = { version = "0.11", features = ["brotli", "deflate", "gzip", "json"] }
rusqlite = { version = "0.28", features = ["bundled", "chrono", "serde_json"] } rusqlite = { version = "0.28", features = ["bundled", "chrono", "serde_json"] }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"

View File

@ -11,6 +11,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::RwLock; use std::sync::RwLock;
use tokio::task; use tokio::task;
#[derive(Debug, Default)]
pub struct Fetcher { pub struct Fetcher {
// we don't want new() to fail in lazy_static init, so we just mark it dead if there was an error // we don't want new() to fail in lazy_static init, so we just mark it dead if there was an error
// on creation // on creation
@ -28,13 +29,27 @@ pub struct Fetcher {
impl Fetcher { impl Fetcher {
pub fn new() -> Fetcher { pub fn new() -> Fetcher {
let mut f = Fetcher { let client = match Client::builder()
dead: None, // .user_agent...
cache_dir: PathBuf::new(), // .timeout(std::time::Duration::new(60, 0)) ?
client: Client::new(), // .redirect(reqwest::redirect::Policy::none()) ?
pending: RwLock::new(HashSet::new()), .gzip(true)
failed: RwLock::new(HashMap::new()), .brotli(true)
requests_in_flight: AtomicUsize::new(0), .deflate(true)
.build()
{
Ok(c) => c,
Err(e) => {
return Fetcher {
dead: Some(format!("{}", e)),
..Default::default()
}
}
};
let mut f: Fetcher = Fetcher {
client,
..Default::default()
}; };
// Setup the cache directory // Setup the cache directory

View File

@ -172,6 +172,9 @@ async fn fetch_nip05(user: &str, domain: &str) -> Result<Nip05, Error> {
let nip05_future = reqwest::Client::builder() let nip05_future = reqwest::Client::builder()
.timeout(std::time::Duration::new(60, 0)) .timeout(std::time::Duration::new(60, 0))
.redirect(reqwest::redirect::Policy::none()) // see NIP-05 .redirect(reqwest::redirect::Policy::none()) // see NIP-05
.gzip(true)
.brotli(true)
.deflate(true)
.build()? .build()?
.get(format!( .get(format!(
"https://{}/.well-known/nostr.json?name={}", "https://{}/.well-known/nostr.json?name={}",

View File

@ -95,17 +95,24 @@ impl Minion {
return Err(Error::UrlHasEmptyHostname); return Err(Error::UrlHasEmptyHostname);
} }
let request_nip11_future = reqwest::Client::new() let request_nip11_future = reqwest::Client::builder()
.timeout(std::time::Duration::new(30, 0))
.redirect(reqwest::redirect::Policy::none())
.gzip(true)
.brotli(true)
.deflate(true)
.build()?
.get(format!("https://{}", host)) .get(format!("https://{}", host))
.header("Host", host) .header("Host", host)
.header("Accept", "application/nostr+json") .header("Accept", "application/nostr+json")
.send(); .send();
// Read NIP-11 information // Read NIP-11 information
if let Ok(response) = match request_nip11_future
tokio::time::timeout(std::time::Duration::new(15, 0), request_nip11_future).await? .await?
.json::<RelayInformationDocument>()
.await
{ {
match response.json::<RelayInformationDocument>().await {
Ok(nip11) => { Ok(nip11) => {
tracing::info!("{}: {}", &self.url, nip11); tracing::info!("{}: {}", &self.url, nip11);
self.nip11 = Some(nip11); self.nip11 = Some(nip11);
@ -114,7 +121,6 @@ impl Minion {
tracing::warn!("{}: Unable to parse response as NIP-11: {}", &self.url, e); tracing::warn!("{}: Unable to parse response as NIP-11: {}", &self.url, e);
} }
} }
}
let key: [u8; 16] = rand::random(); let key: [u8; 16] = rand::random();