#http

oxhttp

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

7 releases

0.1.6 Mar 18, 2023
0.1.5 Aug 16, 2022
0.1.4 Jan 24, 2022
0.1.3 Dec 5, 2021
0.1.1 Sep 30, 2021

#2228 in Network programming

Download history 137/week @ 2022-12-05 277/week @ 2022-12-12 340/week @ 2022-12-19 297/week @ 2022-12-26 309/week @ 2023-01-02 78/week @ 2023-01-09 115/week @ 2023-01-16 103/week @ 2023-01-23 74/week @ 2023-01-30 173/week @ 2023-02-06 187/week @ 2023-02-13 251/week @ 2023-02-20 246/week @ 2023-02-27 156/week @ 2023-03-06 325/week @ 2023-03-13 173/week @ 2023-03-20

970 downloads per month
Used in 2 crates

MIT/Apache

120KB
2.5K 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 native-tls feature (to use the current system native implementation) or rustls feature (to use Rustls).

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 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()
    }
});
// Raise a timeout error if the client does not respond after 10s.
server.set_global_timeout(Duration::from_secs(10));
// Listen to localhost:8080
server.listen(("localhost", 8080)).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.5–7MB
~152K SLoC