How can hyper works on an existing stream, when it used as a http client? #3132
-
Dear developers, hello! Is your feature request related to a problem? Please describe. Describe the solution you'd like let (mut client, mut server) = tokio::io::duplex(64);
let connector = MyHttpsConnector::new(KiteTlsStream::new(client));
let client: hyper::Client<_, hyper::Body> = hyper::Client::builder()
.build(connector); As Describe alternatives you've considered Additional context
steps:
emmmm...What should I do? Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
If you already have an IO transport, you can make use of hyper::client::conn. |
Beta Was this translation helpful? Give feedback.
-
Sorry for I placed this issue wrongly. Those interested in this problem can refer to the following code, it works fine. use std::sync::Arc;
use hyper::{client::conn, Body, http::{Request, StatusCode}};
use tokio::net::TcpStream;
use tokio_rustls::rustls;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut root_store = rustls::RootCertStore::empty();
root_store.add_server_trust_anchors(
webpki_roots::TLS_SERVER_ROOTS
.0
.iter()
.map(|ta| {
rustls::OwnedTrustAnchor::from_subject_spki_name_constraints(
ta.subject,
ta.spki,
ta.name_constraints,
)
})
);
let config = rustls::ClientConfig::builder()
.with_safe_defaults()
.with_root_certificates(root_store)
.with_no_client_auth();
let server_name = "example.com".try_into().unwrap();
let connector = tokio_rustls::TlsConnector::from(Arc::new(config));
let stream = TcpStream::connect("example.com:443").await.unwrap();
let mut stream = connector.connect(server_name, stream).await.unwrap();
let (mut request_sender, connection) = conn::handshake(stream).await?;
// spawn a task to poll the connection and drive the HTTP state
tokio::spawn(async move {
if let Err(e) = connection.await {
eprintln!("Error in connection: {}", e);
}
});
let request = Request::builder()
.uri("/robots.txt")
// We need to manually add the host header because SendRequest does not
.header("Host", "example.com")
.method("GET")
.body(Body::from(""))?;
let response = request_sender.send_request(request).await?;
println!("{:#?}", response);
Ok(())
} Thanks for the tip! |
Beta Was this translation helpful? Give feedback.
If you already have an IO transport, you can make use of hyper::client::conn.