Skip to content

Commit

Permalink
update hyper to version 1
Browse files Browse the repository at this point in the history
  • Loading branch information
getong committed Jan 5, 2025
1 parent 701778a commit 8fd103f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 39 deletions.
4 changes: 3 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 3 additions & 8 deletions crates/tauri-driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,11 @@ rust-version = "1.77.2"

[dependencies]
anyhow = "1"
hyper = { version = "0.14", features = [
"client",
"http1",
"runtime",
"server",
"stream",
"tcp",
] }
futures = "0.3"
futures-util = "0.3"
http-body-util = "0.1.2"
hyper = { version = "1", features = ["client", "http1", "server"] }
hyper-util = { version = "0.1", features = ["client", "client-legacy", "http1", "server", "tokio"] }
pico-args = "0.5"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
Expand Down
77 changes: 47 additions & 30 deletions crates/tauri-driver/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@
use crate::cli::Args;
use anyhow::Error;
use futures_util::TryFutureExt;
use hyper::header::CONTENT_LENGTH;
use hyper::http::uri::Authority;
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Client, Method, Request, Response, Server};
use http_body_util::{BodyExt, Full};
use hyper::{
body::{Bytes, Incoming},
header::CONTENT_LENGTH,
http::uri::Authority,
service::service_fn,
Method, Request, Response,
};
use hyper_util::{
client::legacy::Client,
rt::{TokioExecutor, TokioIo},
server::conn::auto,
};
use serde::Deserialize;
use serde_json::{json, Map, Value};
use std::convert::Infallible;
use std::path::PathBuf;
use std::process::Child;

type HttpClient = Client<hyper::client::HttpConnector>;
use tokio::net::TcpListener;

const TAURI_OPTIONS: &str = "tauri:options";

Expand Down Expand Up @@ -55,16 +63,16 @@ impl TauriOptions {
}

async fn handle(
client: HttpClient,
mut req: Request<Body>,
client: Client,
mut req: Request<Incoming>,
args: Args,
) -> Result<Response<Body>, Error> {
) -> Result<Response<Full<Bytes>>, Error> {
// manipulate a new session to convert options to the native driver format
if let (&Method::POST, "/session") = (req.method(), req.uri().path()) {
let (mut parts, body) = req.into_parts();

// get the body from the future stream and parse it as json
let body = hyper::body::to_bytes(body).await?;
let body = body.collect().await?.to_bytes().to_vec();
let json: Value = serde_json::from_slice(&body)?;

// manipulate the json to convert from tauri option to native driver options
Expand All @@ -74,7 +82,7 @@ async fn handle(
let bytes = serde_json::to_vec(&json)?;
parts.headers.insert(CONTENT_LENGTH, bytes.len().into());

req = Request::from_parts(parts, bytes.into());
req = Request::from_parts(parts, bytes);
}

client
Expand All @@ -84,7 +92,10 @@ async fn handle(
}

/// Transform the request to a request for the native webdriver server.
fn forward_to_native_driver(mut req: Request<Body>, args: Args) -> Result<Request<Body>, Error> {
fn forward_to_native_driver(
mut req: Request<Incoming>,
args: Args,
) -> Result<Request<Incoming>, Error> {
let host: Authority = {
let headers = req.headers_mut();
headers.remove("host").expect("hyper request has host")
Expand Down Expand Up @@ -171,30 +182,36 @@ pub async fn run(args: Args, mut _driver: Child) -> Result<(), Error> {
let address = std::net::SocketAddr::from(([127, 0, 0, 1], args.port));

// the client we use to proxy requests to the native webdriver
let client = Client::builder()
let client = Client::builder(TokioExecutor::new())
.http1_preserve_header_case(true)
.http1_title_case_headers(true)
.retry_canceled_requests(false)
.build_http();

// pass a copy of the client to the http request handler
let service = make_service_fn(move |_| {
let client = client.clone();
let args = args.clone();
async move {
Ok::<_, Infallible>(service_fn(move |request| {
handle(client.clone(), request, args.clone())
}))
}
});

// set up a http1 server that uses the service we just created
Server::bind(&address)
.http1_title_case_headers(true)
.http1_preserve_header_case(true)
.http1_only(true)
.serve(service)
.await?;
let srv = async move {
let listener = TcpListener::bind(address).await?;
loop {
let (stream, _) = listener.accept().await?;
let io = TokioIo::new(stream);

tokio::task::spawn(async move {
if let Err(err) = auto::Builder::new(TokioExecutor::new())
.http1()
.title_case_headers(true)
.preserve_header_case(true)
.serve_connection(
io,
service_fn(|request| handle(client.clone(), request, args.clone())),
)
.await
{
println!("Error serving connection: {:?}", err);
}
});
}
};
srv.await?;

#[cfg(unix)]
{
Expand Down

0 comments on commit 8fd103f

Please sign in to comment.