Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add HTTP/2 support for WebSocket #373

Merged
merged 11 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ hyper-util = { version = "0.1.10", features = [
"server-auto",
"tokio",
] }
env_logger = "0.11.6"
serde = { version = "1.0", features = ["derive"] }
libflate = "2.0.0"
zstd = { version = "0.13" }
Expand All @@ -190,6 +189,10 @@ tower = { version = "0.5.2", default-features = false, features = ["limit"] }
num_cpus = "1.0"
libc = "0.2"

env_logger = "0.11.6"
tracing = "0.1"
tracing-subscriber = "0.3.19"

[lib]
doctest = false

Expand Down Expand Up @@ -309,7 +312,7 @@ required-features = ["native-roots", "webpki-roots"]
[[example]]
name = "websocket"
path = "examples/websocket.rs"
required-features = ["websocket", "futures-util/std"]
required-features = ["websocket", "http2-tracing", "futures-util/std"]

[[example]]
name = "client_chain"
Expand Down Expand Up @@ -358,3 +361,8 @@ required-features = ["full"]
name = "request_with_interface"
path = "examples/request_with_interface.rs"
required-features = ["full"]

[[example]]
name = "http2_websocket"
path = "examples/http2_websocket.rs"
required-features = ["websocket", "http2-tracing", "futures-util/std"]
4 changes: 3 additions & 1 deletion examples/base_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use rquest::{Client, Impersonate};

#[tokio::main]
async fn main() -> Result<(), rquest::Error> {
env_logger::init_from_env(env_logger::Env::default().default_filter_or("debug"));
tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE)
.init();

// Build a client to impersonate Edge131
let mut client = Client::builder()
Expand Down
53 changes: 53 additions & 0 deletions examples/http2_websocket.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use futures_util::{SinkExt, StreamExt, TryStreamExt};
use http::header;
use rquest::{Client, Impersonate, Message, RequestBuilder};
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), rquest::Error> {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE)
.init();

// Build a client to impersonate Firefox133
let client = Client::builder()
.impersonate(Impersonate::Firefox133)
.danger_accept_invalid_certs(true)
.build()?;

// Use the API you're already familiar with
let websocket = client
.websocket("wss://127.0.0.1:3000/ws")
.configure_request(configure_request)
.http2_only()
.send()
.await?;

assert_eq!(websocket.version(), http::Version::HTTP_2);

let (mut tx, mut rx) = websocket.into_websocket().await?.split();

tokio::spawn(async move {
for i in 1..11 {
tx.send(Message::Text(format!("Hello, World! #{i}")))
.await
.unwrap();
}
});

while let Some(message) = rx.try_next().await? {
match message {
Message::Text(text) => println!("received: {text}"),
_ => {}
}
}

Ok(())
}

/// We can also set HTTP options here
fn configure_request(builder: RequestBuilder) -> RequestBuilder {
builder
.header(header::USER_AGENT, env!("CARGO_PKG_NAME"))
.timeout(Duration::from_secs(10))
}
4 changes: 3 additions & 1 deletion examples/impersonate_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use rquest::{Client, Impersonate, ImpersonateOS};

#[tokio::main]
async fn main() -> Result<(), rquest::Error> {
env_logger::init_from_env(env_logger::Env::default().default_filter_or("trace"));
tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE)
.init();

// Build a client to impersonate Firefox128
let impersonate = Impersonate::builder()
Expand Down
4 changes: 3 additions & 1 deletion examples/impersonate_psk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use rquest::Impersonate;

#[tokio::main]
async fn main() -> Result<(), rquest::Error> {
env_logger::init_from_env(env_logger::Env::default().default_filter_or("trace"));
tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE)
.init();

// Build a client to impersonate Firefox133
let client = rquest::Client::builder()
Expand Down
4 changes: 3 additions & 1 deletion examples/impersonate_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ const HEADER_ORDER: &[HeaderName] = &[

#[tokio::main]
async fn main() -> Result<(), rquest::Error> {
env_logger::init_from_env(env_logger::Env::default().default_filter_or("trace"));
tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE)
.init();

// TLS settings
let tls = TlsSettings::builder()
Expand Down
4 changes: 3 additions & 1 deletion examples/request_with_cookie_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use url::Url;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init_from_env(env_logger::Env::default().default_filter_or("trace"));
tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE)
.init();

let url = Url::parse("https://google.com/")?;

Expand Down
4 changes: 3 additions & 1 deletion examples/request_with_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use rquest::{Client, Impersonate};

#[tokio::main]
async fn main() -> Result<(), rquest::Error> {
env_logger::init_from_env(env_logger::Env::default().default_filter_or("trace"));
tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE)
.init();

// Build a client to impersonate Firefox128
let client = Client::builder()
Expand Down
4 changes: 3 additions & 1 deletion examples/request_with_local_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use std::net::IpAddr;

#[tokio::main]
async fn main() -> Result<(), rquest::Error> {
env_logger::init_from_env(env_logger::Env::default().default_filter_or("trace"));
tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE)
.init();

// Build a client to impersonate Safari18
let client = rquest::Client::builder()
Expand Down
4 changes: 3 additions & 1 deletion examples/request_with_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use rquest::Impersonate;

#[tokio::main]
async fn main() -> Result<(), rquest::Error> {
env_logger::init_from_env(env_logger::Env::default().default_filter_or("trace"));
tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE)
.init();

// Build a client to impersonate Firefox133
let client = rquest::Client::builder()
Expand Down
4 changes: 3 additions & 1 deletion examples/request_with_redirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use rquest::{redirect::Policy, Impersonate};

#[tokio::main]
async fn main() -> Result<(), rquest::Error> {
env_logger::init_from_env(env_logger::Env::default().default_filter_or("trace"));
tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE)
.init();

// Build a client to impersonate Safari18
let client = rquest::Client::builder()
Expand Down
4 changes: 3 additions & 1 deletion examples/request_with_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use rquest::{redirect::Policy, Impersonate};

#[tokio::main]
async fn main() -> Result<(), rquest::Error> {
env_logger::init_from_env(env_logger::Env::default().default_filter_or("trace"));
tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE)
.init();

// Build a client to impersonate Safari18
let client = rquest::Client::builder()
Expand Down
4 changes: 3 additions & 1 deletion examples/set_proxies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use rquest::{Client, Impersonate};

#[tokio::main]
async fn main() -> Result<(), rquest::Error> {
env_logger::init_from_env(env_logger::Env::default().default_filter_or("trace"));
tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE)
.init();

// Build a client to impersonate Chrome130
let mut client = Client::builder()
Expand Down
4 changes: 3 additions & 1 deletion examples/set_redirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use rquest::{redirect::Policy, Impersonate};

#[tokio::main]
async fn main() -> Result<(), rquest::Error> {
env_logger::init_from_env(env_logger::Env::default().default_filter_or("trace"));
tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE)
.init();

// Build a client to impersonate Safari18
let mut client = rquest::Client::builder()
Expand Down
4 changes: 3 additions & 1 deletion examples/set_root_cert_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use std::sync::LazyLock;

#[tokio::main]
async fn main() -> Result<(), rquest::Error> {
env_logger::init_from_env(env_logger::Env::default().default_filter_or("trace"));
tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE)
.init();
use_static_root_certs().await?;
use_dynamic_root_certs().await?;
Ok(())
Expand Down
17 changes: 10 additions & 7 deletions examples/websocket.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
use std::time::Duration;

use futures_util::{SinkExt, StreamExt, TryStreamExt};
use http::header;
use rquest::{Client, Impersonate, Message, RequestBuilder};
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), rquest::Error> {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE)
.init();

// Build a client to impersonate Firefox133
let client = Client::builder()
.impersonate(Impersonate::Firefox133)
.danger_accept_invalid_certs(true)
.build()?;

// Use the API you're already familiar with
let websocket = client
.websocket("wss://echo.websocket.org")
.websocket("wss://127.0.0.1:3000/ws")
.configure_request(configure_request)
.send()
.await?
.into_websocket()
.await?;

let (mut tx, mut rx) = websocket.split();
assert_eq!(websocket.version(), http::Version::HTTP_11);

let (mut tx, mut rx) = websocket.into_websocket().await?.split();

tokio::spawn(async move {
for i in 1..11 {
Expand All @@ -43,7 +47,6 @@ async fn main() -> Result<(), rquest::Error> {
/// We can also set HTTP options here
fn configure_request(builder: RequestBuilder) -> RequestBuilder {
builder
.proxy("http://127.0.0.1:6152")
.header(header::USER_AGENT, env!("CARGO_PKG_NAME"))
.timeout(Duration::from_secs(10))
}
8 changes: 5 additions & 3 deletions src/client/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,7 @@ impl Client {
redirect,
_cookie_store,
network_scheme,
protocal,
) = req.pieces();

if url.scheme() != "http" && url.scheme() != "https" {
Expand Down Expand Up @@ -1357,12 +1358,13 @@ impl Client {

let in_flight = {
let res = InnerRequest::builder()
.network_scheme(network_scheme.clone())
.uri(uri)
.method(method.clone())
.version(version)
.headers(headers.clone())
.headers_order(self.inner.headers_order.as_deref())
.network_scheme(network_scheme.clone())
.extension(protocal)
.body(body);

match res {
Expand Down Expand Up @@ -1944,12 +1946,12 @@ impl PendingRequest {

*self.as_mut().in_flight().get_mut() = {
let res = InnerRequest::builder()
.network_scheme(self.network_scheme.clone())
.uri(uri)
.method(self.method.clone())
.version(self.version)
.headers(self.headers.clone())
.headers_order(self.client.headers_order.as_deref())
.network_scheme(self.network_scheme.clone())
.body(body);

if let Ok(req) = res {
Expand Down Expand Up @@ -2203,12 +2205,12 @@ impl Future for PendingRequest {

*self.as_mut().in_flight().get_mut() = {
let req = InnerRequest::builder()
.network_scheme(self.network_scheme.clone())
.uri(uri)
.method(self.method.clone())
.version(self.version)
.headers(headers.clone())
.headers_order(self.client.headers_order.as_deref())
.network_scheme(self.network_scheme.clone())
.body(body)?;

std::mem::swap(self.as_mut().headers(), &mut headers);
Expand Down
12 changes: 12 additions & 0 deletions src/client/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type PiecesWithCookieStore = (
Option<redirect::Policy>,
(),
NetworkScheme,
Option<hyper2::ext::Protocol>,
);

#[cfg(feature = "cookies")]
Expand All @@ -46,6 +47,7 @@ type PiecesWithCookieStore = (
Option<redirect::Policy>,
Option<Arc<dyn cookie::CookieStore>>,
NetworkScheme,
Option<hyper2::ext::Protocol>,
);

/// A request which can be executed with `Client::execute()`.
Expand All @@ -61,6 +63,7 @@ pub struct Request {
#[cfg(feature = "cookies")]
cookie_store: Option<Arc<dyn cookie::CookieStore>>,
network_scheme: NetworkSchemeBuilder,
protocol: Option<hyper2::ext::Protocol>,
}

/// A builder to construct the properties of a `Request`.
Expand Down Expand Up @@ -88,6 +91,7 @@ impl Request {
#[cfg(feature = "cookies")]
cookie_store: None,
network_scheme: NetworkScheme::builder(),
protocol: None,
}
}

Expand Down Expand Up @@ -194,6 +198,12 @@ impl Request {
&mut self.version
}

/// Set the mutable reference to the protocol.
#[inline]
pub fn protocol_mut(&mut self) -> &mut Option<hyper2::ext::Protocol> {
&mut self.protocol
}

/// Attempt to clone the request.
///
/// `None` is returned if the request can not be cloned, i.e. if the body is a stream.
Expand Down Expand Up @@ -232,6 +242,7 @@ impl Request {
#[cfg(not(feature = "cookies"))]
(),
self.network_scheme.build(),
self.protocol,
)
}
}
Expand Down Expand Up @@ -824,6 +835,7 @@ where
#[cfg(feature = "cookies")]
cookie_store: None,
network_scheme: NetworkScheme::builder(),
protocol: None,
})
}
}
Expand Down
Loading
Loading