Skip to content

Commit

Permalink
enables http2 crate feature, replaces http1 protocol with http2 on co… (
Browse files Browse the repository at this point in the history
  • Loading branch information
alexl8819 authored Nov 1, 2024
1 parent 9aea9c9 commit a96894c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -65,4 +65,4 @@ path = "src/main.rs"

[[bin]]
name = "scraper"
path = "src/scraper/main.rs"
path = "src/scraper/main.rs"
19 changes: 8 additions & 11 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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<HttpsConnector<HttpConnector>> = Lazy::new(|| {
hyper_rustls::HttpsConnectorBuilder::new().with_native_roots().https_only().enable_http2().build()
});

pub static CLIENT: Lazy<Client<HttpsConnector<HttpConnector>>> = 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<ArcSwap<Oauth>> = Lazy::new(|| {
Expand Down Expand Up @@ -154,10 +157,7 @@ async fn stream(url: &str, req: &Request<Body>) -> Result<Response<Body>, String
let parsed_uri = url.parse::<Uri>().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<_, Body>> = &CLIENT;

let mut builder = Request::get(parsed_uri);

Expand Down Expand Up @@ -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<_, Body>> = &CLIENT;

let (token, vendor_id, device_id, user_agent, loid) = {
let client = OAUTH_CLIENT.load_full();
Expand Down
7 changes: 2 additions & 5 deletions src/oauth.rs
Original file line number Diff line number Diff line change
@@ -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 _};
Expand Down Expand Up @@ -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::Client<_, Body>> = &CLIENT;
let resp = client.request(request).await.ok()?;

trace!("Received response with status {} and length {:?}", resp.status(), resp.headers().get("content-length"));
Expand Down

0 comments on commit a96894c

Please sign in to comment.