https://github.com/tickbh/wmhttp
http1.1/http2 server/client by rust
https://github.com/tickbh/wmhttp
client http http-client http-requests http-server http2 httpclient https server
Last synced: 30 days ago
JSON representation
http1.1/http2 server/client by rust
- Host: GitHub
- URL: https://github.com/tickbh/wmhttp
- Owner: tickbh
- License: apache-2.0
- Created: 2023-08-31T01:54:31.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-02-14T06:51:00.000Z (11 months ago)
- Last Synced: 2025-04-10T09:53:56.644Z (9 months ago)
- Topics: client, http, http-client, http-requests, http-server, http2, httpclient, https, server
- Language: Rust
- Homepage:
- Size: 550 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# wenmeng
一个包含http1.1及http2的服务器及客户端的实现, 依赖tokio实现
## 使用方法
简单的hello world示例
```rust
use std::{env, error::Error};
use tokio::net::TcpListener;
use webparse::{Request, Response};
use wmhttp::{self, ProtResult, RecvStream, Server, RecvRequest};
#[tokio::main]
async fn main() -> Result<(), Box> {
let addr = env::args()
.nth(1)
.unwrap_or_else(|| "127.0.0.1:8080".to_string());
let server = TcpListener::bind(&addr).await?;
println!("Listening on: {}", addr);
loop {
let (stream, _) = server.accept().await?;
tokio::spawn(async move {
let mut server = Server::new(stream);
async fn operate(req: RecvRequest) -> ProtResult>> {
let response = Response::builder()
.version(req.version().clone())
.body("Hello World".to_string())?;
Ok(Some(response))
}
let _ = server.incoming(operate).await;
});
}
}
```
## 客户端使用方法
> http1/http2通用, recv可以接收多个返回及服务端的推送信息
```rust
use webparse::Request;
use wmhttp::{Client, ProtResult};
async fn test_http2() -> ProtResult<()> {
let url = "http://nghttp2.org/"; //"http://127.0.0.1:8080/"
let req = Request::builder().method("GET").url(url).body("").unwrap();
let client = Client::builder().connect(url).await.unwrap();
let (mut recv, sender) = client.send2(req.into_type()).await?;
let mut res = recv.recv().await.unwrap();
res.body_mut().wait_all().await;
println!("res = {}", res);
let req = Request::builder()
.method("GET")
.url(url.to_owned() + "blog/")
.body("")
.unwrap();
sender.send(req.into_type()).await?;
let res = recv.recv().await.unwrap();
println!("res = {}", res);
Ok(())
}
```
## License
Apache License, Version 2.0 ([LICENSE-APACHE](./LICENSE) or [https://apache.org/licenses/LICENSE-2.0](https://apache.org/licenses/LICENSE-2.0))