14 stable releases
1.2.0 | Jan 1, 2024 |
---|---|
1.1.11 | Dec 29, 2023 |
1.1.5 | Nov 19, 2023 |
#920 in Network programming
Used in 2 crates
45KB
1K
SLoC
WHDP - Wizards hypermedia protocol parser
A library to parse the raw string into a workable type and vice versa.
Documentation:
Explanation:
Http is a text-based protocol. It follows a rather simple format
Requests:
Method Request-URI HTTP-Version CRLF
headers CRLF
message-body
Response:
HTTP-Version Status-Code Reason-Phrase CRLF
headers CRLF
message-body
Usage:
Import the library into your Cargo.toml
[dependencies]
whdp = "1.2.0"
Then just use the TryRequest
trait to parse it to a Request
use std::io::Write;
use std::net::TcpListener;
use whdp::{Response, TryRequest};
fn main() {
let server = TcpListener::bind("0.0.0.0:8080").unwrap();
let mut resp = Response::default();
for inco in server.incoming() {
let mut inco = inco.unwrap();
println!("{}", inco.try_to_request().unwrap()); // don't unwrap immediatly first look if there is an error
let _ = inco.write_all(resp.to_string().as_bytes());
}
}
And / Or if you want to create a Response use the ResponseBuilder
or the resp_presets
module
use std::io::Write;
use std::net::TcpListener;
use whdp::{HttpVersion, ResponseBuilder};
use whdp::presets::ok;
fn main() {
let server = TcpListener::bind("0.0.0.0:8080").unwrap();
let resp = ResponseBuilder::new()
.with_body("Hello, World".into())
.with_status(ok())
.with_version(HttpVersion::OnePointOne)
.with_empty_headers()
.build().unwrap();
for inco in server.incoming() {
let mut inco = inco.unwrap();
let _ = inco.write_all(resp.to_string().as_bytes());
}
}
Dependencies
~53KB