| 1 |
OxHTTP |
| 2 |
====== |
| 3 |
|
| 4 |
[](https://github.com/oxigraph/oxhttp/actions) |
| 5 |
[](https://crates.io/crates/oxhttp) |
| 6 |
[](https://docs.rs/oxhttp) |
| 7 |
|
| 8 |
OxHTTP is a simple and naive synchronous implementation of [HTTP 1.1](https://httpwg.org/http-core/) in Rust. |
| 9 |
It provides both a client and a server. |
| 10 |
It does not aim to be a fully-working-in-all-cases HTTP implementation |
| 11 |
but to be only a simple one to be use in simple usecases. |
| 12 |
|
| 13 |
## Client |
| 14 |
|
| 15 |
OxHTTP provides [a client](https://docs.rs/oxhttp/latest/oxhttp/struct.Client.html). |
| 16 |
It aims at following the basic concepts of the [Web Fetch standard](https://fetch.spec.whatwg.org/) without the bits |
| 17 |
specific to web browsers (context, CORS...). |
| 18 |
|
| 19 |
HTTPS is supported behind the disabled by default features. |
| 20 |
To enable it, you need to enable one of the following features: |
| 21 |
|
| 22 |
* `native-tls` to use the current system native implementation. |
| 23 |
* `rustls-ring-webpki` to use [Rustls](https://github.com/rustls/rustls) with |
| 24 |
the [Ring](https://github.com/briansmith/ring) cryptographic library and |
| 25 |
the [Common CA Database](https://www.ccadb.org/). |
| 26 |
* `rustls-ring-native` to use [Rustls](https://github.com/rustls/rustls) with |
| 27 |
the [Ring](https://github.com/briansmith/ring) cryptographic library and the host certificates. |
| 28 |
* `rustls-aws-lc-webpki` to use [Rustls](https://github.com/rustls/rustls) with |
| 29 |
the [AWS Libcrypto for Rust](https://github.com/aws/aws-lc-rs) and |
| 30 |
the [Common CA Database](https://www.ccadb.org/). |
| 31 |
* `rustls-aws-lc-native` to use [Rustls](https://github.com/rustls/rustls) with |
| 32 |
the [AWS Libcrypto for Rust](https://github.com/aws/aws-lc-rs) and the host certificates. |
| 33 |
|
| 34 |
Example: |
| 35 |
|
| 36 |
```rust |
| 37 |
use oxhttp::Client; |
| 38 |
use oxhttp::model::{Body, Request, Method, StatusCode, HeaderName}; |
| 39 |
use oxhttp::model::header::CONTENT_TYPE; |
| 40 |
use std::io::Read; |
| 41 |
|
| 42 |
let client = Client::new(); |
| 43 |
let response = client.request(Request::builder().uri("http://example.com").body(Body::empty()).unwrap()).unwrap(); |
| 44 |
assert_eq!(response.status(), StatusCode::OK); |
| 45 |
assert_eq!(response.headers().get(CONTENT_TYPE).unwrap(), "text/html"); |
| 46 |
|
| 47 |
let body = response.into_body().to_string().unwrap(); |
| 48 |
``` |
| 49 |
|
| 50 |
## Server |
| 51 |
|
| 52 |
OxHTTP provides [a threaded HTTP server](https://docs.rs/oxhttp/latest/oxhttp/struct.Server.html). |
| 53 |
It is still a work in progress. Use at your own risks behind a reverse proxy! |
| 54 |
|
| 55 |
Example: |
| 56 |
|
| 57 |
```rust no_run |
| 58 |
use std::net::{Ipv4Addr, Ipv6Addr}; |
| 59 |
use oxhttp::Server; |
| 60 |
use oxhttp::model::{Body, Response, StatusCode}; |
| 61 |
use std::time::Duration; |
| 62 |
|
| 63 |
// Builds a new server that returns a 404 everywhere except for "/" where it returns the body 'home' |
| 64 |
let mut server = Server::new( | request| { |
| 65 |
if request.uri().path() == "/" { |
| 66 |
Response::builder().body(Body::from("home")).unwrap() |
| 67 |
} else { |
| 68 |
Response::builder().status(StatusCode::NOT_FOUND).body(Body::empty()).unwrap() |
| 69 |
} |
| 70 |
}); |
| 71 |
// We bind the server to localhost on both IPv4 and v6 |
| 72 |
server = server.bind((Ipv4Addr::LOCALHOST, 8080)).bind((Ipv6Addr::LOCALHOST, 8080)); |
| 73 |
// Raise a timeout error if the client does not respond after 10s. |
| 74 |
server = server.with_global_timeout(Duration::from_secs(10)); |
| 75 |
// Limits the max number of concurrent connections to 128. |
| 76 |
server = server.with_max_concurrent_connections(128); |
| 77 |
// We spawn the server and block on it |
| 78 |
server.spawn().unwrap().join().unwrap(); |
| 79 |
``` |
| 80 |
|
| 81 |
## License |
| 82 |
|
| 83 |
This project is licensed under either of |
| 84 |
|
| 85 |
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or |
| 86 |
`<http://www.apache.org/licenses/LICENSE-2.0>`) |
| 87 |
* MIT license ([LICENSE-MIT](LICENSE-MIT) or |
| 88 |
`<http://opensource.org/licenses/MIT>`) |
| 89 |
|
| 90 |
at your option. |
| 91 |
|
| 92 |
### Contribution |
| 93 |
|
| 94 |
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in OxHTTP by you, as |
| 95 |
defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions. |