Crates.io | whdp |
lib.rs | whdp |
version | 1.2.0 |
source | src |
created_at | 2023-11-18 19:10:00.553297 |
updated_at | 2024-01-01 13:00:56.603897 |
description | Wizards hypermedia document parser |
homepage | |
repository | https://github.com/AdrisGithub/whdp |
max_upload_size | |
id | 1040672 |
size | 44,625 |
A library to parse the raw string into a workable type and vice versa.
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
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());
}
}