Module hyper::client::conn[][src]

This is supported on crate feature client and (crate features http1 or http2) only.

Lower-level client connection API.

The types in this module are to provide a lower-level API based around a single connection. Connecting to a host, pooling connections, and the like are not handled at this level. This module provides the building blocks to customize those things externally.

If don’t have need to manage connections yourself, consider using the higher-level Client API.

Example

A simple example that uses the SendRequest struct to talk HTTP over a Tokio TCP stream

use http::{Request, StatusCode};
use hyper::{client::conn::Builder, Body};
use tokio::net::TcpStream;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let target_stream = TcpStream::connect("example.com:80").await?;

    let (mut request_sender, connection) = Builder::new()
        .handshake::<TcpStream, Body>(target_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()
    // 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?;
    assert!(response.status() == StatusCode::OK);
    Ok(())
}

Structs

Builder

A builder to configure an HTTP connection.

Connection

A future that processes all HTTP state for the IO object.

Parts

Deconstructed parts of a Connection.

ResponseFuture

A future returned by SendRequest::send_request.

SendRequest

The sender side of an established connection.

Functions

handshake

Returns a handshake future over some IO.