whdp

Crates.iowhdp
lib.rswhdp
version1.2.0
sourcesrc
created_at2023-11-18 19:10:00.553297
updated_at2024-01-01 13:00:56.603897
descriptionWizards hypermedia document parser
homepage
repositoryhttps://github.com/AdrisGithub/whdp
max_upload_size
id1040672
size44,625
Adriiii (AdrisGithub)

documentation

README

WHDP - Wizards hypermedia protocol parser

A library to parse the raw string into a workable type and vice versa.

Latest version Documentation Reliability Rating Quality Gate Status Technical Debt

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());
    }
}

Commit count: 164

cargo fmt