#http #client-server #http-server #http-basic #synchronous #rustls

oxhttp

Very simple implementation of HTTP 1.1 (both client and server)

13 releases

0.2.0 Mar 23, 2024
0.2.0-alpha.3 Dec 7, 2023
0.2.0-alpha.2 Nov 18, 2023
0.1.6 Mar 18, 2023
0.1.2 Nov 3, 2021

#42 in HTTP client

Download history 282/week @ 2024-03-15 715/week @ 2024-03-22 509/week @ 2024-03-29 315/week @ 2024-04-05 211/week @ 2024-04-12 249/week @ 2024-04-19 153/week @ 2024-04-26 302/week @ 2024-05-03 255/week @ 2024-05-10 321/week @ 2024-05-17 202/week @ 2024-05-24 177/week @ 2024-05-31 155/week @ 2024-06-07 305/week @ 2024-06-14 218/week @ 2024-06-21 56/week @ 2024-06-28

758 downloads per month
Used in 5 crates

MIT/Apache

130KB
3K SLoC

OxHTTP

actions status Latest Version Released API docs

OxHTTP is a simple and naive synchronous implementation of HTTP 1.1 in Rust. It provides both a client and a server. It does not aim to be a fully-working-in-all-cases HTTP implementation but to be only a naive one to be use in simple usecases.

Client

OxHTTP provides a client. It aims at following the basic concepts of the Web Fetch standard without the bits specific to web browsers (context, CORS...).

HTTPS is supported behind the disabled by default features. To enable it you need to enable one of the following features:

Example:

use oxhttp::Client;
use oxhttp::model::{Request, Method, Status, HeaderName};
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; charset=UTF-8");

let body = response.into_body().to_string().unwrap();

Server

OxHTTP provides a threaded HTTP server. It is still a work in progress. Use at your own risks behind a reverse proxy!

Example:

use std::net::{Ipv4Addr, Ipv6Addr};
use oxhttp::Server;
use oxhttp::model::{Response, Status};
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")
} else {
Response::builder(Status::NOT_FOUND).build()
}
});
// We bind the server to localhost on both IPv4 and v6
server = server.bind((Ipv4Addr::LOCALHOST, 8080)).bind((Ipv6Addr::LOCALHOST, 8080));
// Raise a timeout error if the client does not respond after 10s.
server = server.with_global_timeout(Duration::from_secs(10));
// Limits the max number of concurrent connections to 128.
server = server.with_max_concurrent_connections(128);
// We spawn the server and block on it
server.spawn().unwrap().join().unwrap();

License

This project is licensed under either of

  • Apache License, Version 2.0, (LICENSE-APACHE or <http://www.apache.org/licenses/LICENSE-2.0>)
  • MIT license (LICENSE-MIT or <http://opensource.org/licenses/MIT>)

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in OxHTTP by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~1–19MB
~390K SLoC