Skip to content

Commit

Permalink
Use http crate
Browse files Browse the repository at this point in the history
  • Loading branch information
Tpt committed Feb 2, 2025
1 parent 9b1c8f2 commit 42fb645
Show file tree
Hide file tree
Showing 14 changed files with 585 additions and 1,811 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "oxhttp"
version = "0.2.7"
version = "0.3.0-dev"
authors = ["Tpt <[email protected]>"]
license = "MIT OR Apache-2.0"
readme = "README.md"
Expand All @@ -15,14 +15,15 @@ rust-version = "1.74"

[dependencies]
flate2 = { version = "1", optional = true }
http = "1.1"
httparse = "1.8"
url = { version = "2.4", optional = true }
native-tls = { version = "0.2.11", optional = true }
rustls = { version = "0.23.16", optional = true, default-features = false, features = ["std", "tls12"] }
rustls-native-certs = { version = "0.8", optional = true }
rustls-pki-types = { version = "1.10", optional = true }
rustls-platform-verifier = { version = "0.5", optional = true }
webpki-roots = { version = "0.26", optional = true }
url = "2.4"

[dev-dependencies]
codspeed-criterion-compat = "2"
Expand All @@ -35,7 +36,7 @@ rustls-ring-webpki = ["rustls/ring", "rustls-pki-types", "webpki-roots"]
rustls-aws-lc-platform-verifier = ["rustls/aws_lc_rs", "rustls-pki-types", "rustls-platform-verifier"]
rustls-aws-lc-native = ["rustls/aws_lc_rs", "rustls-native-certs", "rustls-pki-types"]
rustls-aws-lc-webpki = ["rustls/aws_lc_rs", "rustls-pki-types", "webpki-roots"]
client = []
client = ["dep:url"]
server = []

[[bench]]
Expand Down
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ Example:

```rust
use oxhttp::Client;
use oxhttp::model::{Request, Method, Status, HeaderName};
use oxhttp::model::{Body, Request, Method, StatusCode, HeaderName};
use oxhttp::model::header::CONTENT_TYPE;
use std::io::Read;

let client = Client::new();
let response = client.request(Request::builder(Method::GET, "http://example.com".parse().unwrap()).build()).unwrap();
assert_eq!(response.status(), Status::OK);
assert_eq!(response.header(&HeaderName::CONTENT_TYPE).unwrap().as_ref(), b"text/html");
let response = client.request(Request::builder().uri("http://example.com").body(Body::empty()).unwrap()).unwrap();
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(response.headers().get(CONTENT_TYPE).unwrap(), "text/html");

let body = response.into_body().to_string().unwrap();
```
Expand All @@ -60,15 +61,15 @@ Example:
```rust no_run
use std::net::{Ipv4Addr, Ipv6Addr};
use oxhttp::Server;
use oxhttp::model::{Response, Status};
use oxhttp::model::{Body, Response, StatusCode};
use std::time::Duration;

// Builds a new server that returns a 404 everywhere except for "/" where it returns the body 'home'
let mut server = Server::new( | request| {
if request.url().path() == "/" {
Response::builder(Status::OK).with_body("home")
if request.uri().path() == "/" {
Response::builder().body(Body::from("home")).unwrap()
} else {
Response::builder(Status::NOT_FOUND).build()
Response::builder().status(StatusCode::NOT_FOUND).body(Body::empty()).unwrap()
}
});
// We bind the server to localhost on both IPv4 and v6
Expand Down
30 changes: 18 additions & 12 deletions benches/lib.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
use codspeed_criterion_compat::{criterion_group, criterion_main, Criterion};
use oxhttp::model::{Body, Method, Request, Response, Status};
use oxhttp::model::{Body, Request, Response, Uri};
use oxhttp::{Client, Server};
use std::io;
use std::io::Read;
use std::net::{Ipv4Addr, SocketAddrV4};
use url::Url;

fn client_server_no_body(c: &mut Criterion) {
Server::new(|_| Response::builder(Status::OK).build())
Server::new(|_| Response::builder().body(Body::empty()).unwrap())
.bind(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 3456))
.spawn()
.unwrap();

let client = Client::new();
let url = Url::parse("http://localhost:3456").unwrap();
let uri = Uri::try_from("http://localhost:3456").unwrap();

c.bench_function("client_server_no_body", |b| {
b.iter(|| {
client
.request(Request::builder(Method::GET, url.clone()).build())
.request(Request::builder().uri(uri.clone()).body(()).unwrap())
.unwrap();
})
});
Expand All @@ -28,20 +27,25 @@ fn client_server_fixed_body(c: &mut Criterion) {
Server::new(|request| {
let mut body = Vec::new();
request.body_mut().read_to_end(&mut body).unwrap();
Response::builder(Status::OK).with_body(body)
Response::builder().body(Body::from(body)).unwrap()
})
.bind(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 3457))
.spawn()
.unwrap();

let client = Client::new();
let url = Url::parse("http://localhost:3456").unwrap();
let uri = Uri::try_from("http://localhost:3456").unwrap();
let body = vec![16u8; 1024];

c.bench_function("client_server_fixed_body", |b| {
b.iter(|| {
client
.request(Request::builder(Method::GET, url.clone()).with_body(body.clone()))
.request(
Request::builder()
.uri(uri.clone())
.body(body.clone())
.unwrap(),
)
.unwrap();
})
});
Expand All @@ -51,21 +55,23 @@ fn client_server_chunked_body(c: &mut Criterion) {
Server::new(|request| {
let mut body = Vec::new();
request.body_mut().read_to_end(&mut body).unwrap();
Response::builder(Status::OK).build()
Response::builder().body(Body::empty()).unwrap()
})
.bind(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 3458))
.spawn()
.unwrap();

let client = Client::new();
let url = Url::parse("http://localhost:3456").unwrap();
let uri = Uri::try_from("http://localhost:3456").unwrap();

c.bench_function("client_server_chunked_body", |b| {
b.iter(|| {
client
.request(
Request::builder(Method::GET, url.clone())
.with_body(Body::from_read(ChunkedReader::default())),
Request::builder()
.uri(uri.clone())
.body(Body::from_read(ChunkedReader::default()))
.unwrap(),
)
.unwrap();
})
Expand Down
Loading

0 comments on commit 42fb645

Please sign in to comment.