Skip to content

Commit c035d8c

Browse files
committed
Update examples
1 parent 2d07be4 commit c035d8c

File tree

4 files changed

+66
-4
lines changed

4 files changed

+66
-4
lines changed

examples/client.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
use async_h1::client;
32
use async_std::net::{TcpStream};
43
use http_types::{Error, Method, Request, Url};

src/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl Encoder {
4949
}
5050
}
5151

52-
/// Send an HTTP request over a stream.
52+
/// Opens an HTTP/1.1 connection to a remote host.
5353
pub async fn connect<RW>(mut stream: RW, req: Request) -> Result<Response, Error>
5454
where
5555
RW: Read + Write + Send + Sync + Unpin + 'static,

src/lib.rs

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,71 @@
2424
//!
2525
//! # Example
2626
//!
27+
//! __HTTP client__
28+
//!
29+
//! ```no_run
30+
//! use async_h1::client;
31+
//! use async_std::net::{TcpStream};
32+
//! use http_types::{Error, Method, Request, Url};
33+
//!
34+
//! #[async_std::main]
35+
//! async fn main() -> Result<(), Error> {
36+
//! let stream = TcpStream::connect("127.0.0.1:8080").await?;
37+
//! let peer_addr = stream.peer_addr()?;
38+
//! println!("connecting to {}", peer_addr);
39+
//!
40+
//! for i in 0usize..2 {
41+
//! println!("making request {}/2", i + 1);
42+
//! let url = Url::parse(&format!("http://{}/foo", peer_addr)).unwrap();
43+
//! let req = Request::new(Method::Get, dbg!(url));
44+
//! let res = client::connect(stream.clone(), req).await?;
45+
//! println!("{:?}", res);
46+
//! }
47+
//! Ok(())
48+
//! }
2749
//! ```
28-
//! // tbi
50+
//!
51+
//! __HTTP Server__
52+
//!
53+
//! ```no_run
54+
//! use async_std::net::{TcpStream, TcpListener};
55+
//! use async_std::prelude::*;
56+
//! use async_std::task;
57+
//! use http_types::{Response, StatusCode};
58+
//!
59+
//! #[async_std::main]
60+
//! async fn main() -> http_types::Result<()> {
61+
//! // Open up a TCP connection and create a URL.
62+
//! let listener = TcpListener::bind(("127.0.0.1", 8080)).await?;
63+
//! let addr = format!("http://{}", listener.local_addr()?);
64+
//! println!("listening on {}", addr);
65+
//!
66+
//! // For each incoming TCP connection, spawn a task and call `accept`.
67+
//! let mut incoming = listener.incoming();
68+
//! while let Some(stream) = incoming.next().await {
69+
//! let stream = stream?;
70+
//! let addr = addr.clone();
71+
//! task::spawn(async {
72+
//! if let Err(err) = accept(addr, stream).await {
73+
//! eprintln!("{}", err);
74+
//! }
75+
//! });
76+
//! }
77+
//! Ok(())
78+
//! }
79+
//!
80+
//! // Take a TCP stream, and convert it into sequential HTTP request / response pairs.
81+
//! async fn accept(addr: String, stream: TcpStream) -> http_types::Result<()> {
82+
//! println!("starting new connection from {}", stream.peer_addr()?);
83+
//! async_h1::accept(&addr, stream.clone(), |_req| async move {
84+
//! let mut res = Response::new(StatusCode::Ok);
85+
//! res.insert_header("Content-Type", "text/plain")?;
86+
//! res.set_body("Hello");
87+
//! Ok(res)
88+
//! })
89+
//! .await?;
90+
//! Ok(())
91+
//! }
2992
//! ```
3093
3194
// #![forbid(unsafe_code, rust_2018_idioms)]

src/server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::MAX_HEADERS;
2020
const CR: u8 = b'\r';
2121
const LF: u8 = b'\n';
2222

23-
/// Parse an incoming HTTP connection.
23+
/// Accept a new incoming HTTP/1.1 connection.
2424
///
2525
/// Supports `KeepAlive` requests by default.
2626
pub async fn accept<RW, F, Fut>(addr: &str, mut io: RW, endpoint: F) -> http_types::Result<()>

0 commit comments

Comments
 (0)