From a96894c743b44c56c26bfc5f23241f946fa8367d Mon Sep 17 00:00:00 2001 From: Alex <124635195+alexl8819@users.noreply.github.com> Date: Thu, 31 Oct 2024 19:48:19 -0700 Subject: [PATCH] =?UTF-8?q?enables=20http2=20crate=20feature,=20replaces?= =?UTF-8?q?=20http1=20protocol=20with=20http2=20on=20co=E2=80=A6=20(#305)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 4 ++-- src/client.rs | 19 ++++++++----------- src/oauth.rs | 7 ++----- 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4bba848..2fe1456 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ serde = { version = "1.0.193", features = ["derive"] } cookie = "0.18.0" futures-lite = "2.2.0" hyper = { version = "0.14.28", features = ["full"] } -hyper-rustls = "0.24.2" +hyper-rustls = { version = "0.24.2", features = [ "http2" ] } percent-encoding = "2.3.1" route-recognizer = "0.3.1" serde_json = "1.0.108" @@ -65,4 +65,4 @@ path = "src/main.rs" [[bin]] name = "scraper" -path = "src/scraper/main.rs" \ No newline at end of file +path = "src/scraper/main.rs" diff --git a/src/client.rs b/src/client.rs index 2b72482..c174f66 100644 --- a/src/client.rs +++ b/src/client.rs @@ -4,7 +4,7 @@ use futures_lite::future::block_on; use futures_lite::{future::Boxed, FutureExt}; use hyper::client::HttpConnector; use hyper::header::HeaderValue; -use hyper::{body, body::Buf, client, header, Body, Client, Method, Request, Response, Uri}; +use hyper::{body, body::Buf, header, Body, Client, Method, Request, Response, Uri}; use hyper_rustls::HttpsConnector; use libflate::gzip; use log::{error, trace, warn}; @@ -30,9 +30,12 @@ const REDDIT_SHORT_URL_BASE_HOST: &str = "redd.it"; const ALTERNATIVE_REDDIT_URL_BASE: &str = "https://www.reddit.com"; const ALTERNATIVE_REDDIT_URL_BASE_HOST: &str = "www.reddit.com"; +pub static HTTPS_CONNECTOR: Lazy> = Lazy::new(|| { + hyper_rustls::HttpsConnectorBuilder::new().with_native_roots().https_only().enable_http2().build() +}); + pub static CLIENT: Lazy>> = Lazy::new(|| { - let https = hyper_rustls::HttpsConnectorBuilder::new().with_native_roots().https_only().enable_http1().build(); - client::Client::builder().build(https) + Client::builder().build::<_, Body>(HTTPS_CONNECTOR.clone()) }); pub static OAUTH_CLIENT: Lazy> = Lazy::new(|| { @@ -154,10 +157,7 @@ async fn stream(url: &str, req: &Request) -> Result, String let parsed_uri = url.parse::().map_err(|_| "Couldn't parse URL".to_string())?; // Build the hyper client from the HTTPS connector. - let client: Client<_, Body> = { - let https = hyper_rustls::HttpsConnectorBuilder::new().with_native_roots().https_only().enable_http1().build(); - client::Client::builder().build(https) - }; + let client: &Lazy> = &CLIENT; let mut builder = Request::get(parsed_uri); @@ -219,10 +219,7 @@ fn request(method: &'static Method, path: String, redirect: bool, quarantine: bo let url = format!("{base_path}{path}"); // Construct the hyper client from the HTTPS connector. - let client: Client<_, Body> = { - let https = hyper_rustls::HttpsConnectorBuilder::new().with_native_roots().https_only().enable_http1().build(); - client::Client::builder().build(https) - }; + let client: &Lazy> = &CLIENT; let (token, vendor_id, device_id, user_agent, loid) = { let client = OAUTH_CLIENT.load_full(); diff --git a/src/oauth.rs b/src/oauth.rs index 9a5d2f8..80bf318 100644 --- a/src/oauth.rs +++ b/src/oauth.rs @@ -1,7 +1,7 @@ use std::{collections::HashMap, sync::atomic::Ordering, time::Duration}; use crate::{ - client::{OAUTH_CLIENT, OAUTH_IS_ROLLING_OVER, OAUTH_RATELIMIT_REMAINING}, + client::{CLIENT, OAUTH_CLIENT, OAUTH_IS_ROLLING_OVER, OAUTH_RATELIMIT_REMAINING}, oauth_resources::ANDROID_APP_VERSION_LIST, }; use base64::{engine::general_purpose, Engine as _}; @@ -94,10 +94,7 @@ impl Oauth { trace!("Sending token request..."); // Send request - let client: client::Client<_, Body> = { - let https = hyper_rustls::HttpsConnectorBuilder::new().with_native_roots().https_only().enable_http1().build(); - client::Client::builder().build(https) - }; + let client: &once_cell::sync::Lazy> = &CLIENT; let resp = client.request(request).await.ok()?; trace!("Received response with status {} and length {:?}", resp.status(), resp.headers().get("content-length"));