client.rs |
|
51496 |
conn.rs |
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](super) API.
## Example
A simple example that uses the `SendRequest` struct to talk HTTP over a Tokio TCP stream
```no_run
# #[cfg(all(feature = "client", feature = "http1", feature = "runtime"))]
# mod rt {
use tower::ServiceExt;
use http::{Request, StatusCode};
use hyper::{client::conn, 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) = conn::handshake(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);
// To send via the same connection again, it may not work as it may not be ready,
// so we have to wait until the request_sender becomes ready.
request_sender.ready().await?;
let request = Request::builder()
.header("Host", "example.com")
.method("GET")
.body(Body::from(""))?;
let response = request_sender.send_request(request).await?;
assert!(response.status() == StatusCode::OK);
Ok(())
}
# }
``` |
37770 |
connect |
|
|
dispatch.rs |
|
13079 |
mod.rs |
HTTP Client
There are two levels of APIs provided for construct HTTP clients:
- The higher-level [`Client`](Client) type.
- The lower-level [`conn`](conn) module.
# Client
The [`Client`](Client) is the main way to send HTTP requests to a server.
The default `Client` provides these things on top of the lower-level API:
- A default **connector**, able to resolve hostnames and connect to
destinations over plain-text TCP.
- A **pool** of existing connections, allowing better performance when
making multiple requests to the same hostname.
- Automatic setting of the `Host` header, based on the request `Uri`.
- Automatic request **retries** when a pooled connection is closed by the
server before any bytes have been written.
Many of these features can configured, by making use of
[`Client::builder`](Client::builder).
## Example
For a small example program simply fetching a URL, take a look at the
[full client example](https://github.com/hyperium/hyper/blob/master/examples/client.rs).
```
# #[cfg(all(feature = "tcp", feature = "client", any(feature = "http1", feature = "http2")))]
# async fn fetch_httpbin() -> hyper::Result<()> {
use hyper::{body::HttpBody as _, Client, Uri};
let client = Client::new();
// Make a GET /ip to 'http://httpbin.org'
let res = client.get(Uri::from_static("http://httpbin.org/ip")).await?;
// And then, if the request gets a response...
println!("status: {}", res.status());
// Concatenate the body stream into a single buffer...
let buf = hyper::body::to_bytes(res).await?;
println!("body: {:?}", buf);
# Ok(())
# }
# fn main () {}
``` |
2137 |
pool.rs |
|
33471 |
service.rs |
Utilities used to interact with the Tower ecosystem.
This module provides `Connect` which hook-ins into the Tower ecosystem. |
2627 |
tests.rs |
|
9453 |