[][src]Crate http_tools

The http_tools crate aims to provide additional functions on the types found in the http crate. This crate aims to only provide functions that do not cause heap allocations and to be as lightweight as possible. Additonally the crate is compatiable with any library or web framework that also uses the http crate.


The crate provides tools that expand upon the Request and Response types found in the http crate. allowing for the quick creation of an http router.

use http_tools::request::{Extension, Filter};

// standard Http::request::Request
    // Creates an Option<&Request>, each fiter returns Some(req) if it passes and None if it fails
    // match the path /item/{} where {} is a wild card
    // request has the method of type POST
    // The header has the key content-type with a value of application/x-www-form-urlencoded
    .filter_header("content-type", "application/x-www-form-urlencoded")
    // The {} wild card can be used to filter headers aswell
    .filter_header("content-length", "{}")
    // The query has the key cool with the value rust
    .filter_query("cool", "rust")
    // the wild card {} can be used in queries, filters do not decode uri encodings
    .filter_query("also+cool", "{}")
    // custom filters can be applied, and will be given the request and return a bool
    .filter_custom(|req| req.extensions().get::<i32>().is_some())
    // The request has a scheme of https
    // filters simply return std Option where Some means pass and None means failed
    .and_then(|_request| Some("I passed the test!"));

Routing Service

The crate provides some helpful macros to reduce the boiler plate of creating a routing service

#[macro_use] extern crate http_tools;
use http_tools::request::{Extension, Filter};
use http::request::{Request};
use http::response::{Builder, Response};

// routes the request to the proper handler and returns the response
fn router(req : &Request<()>) -> Response<()> {
    // Handles Post requests to root uri
    handle_fn!(post_handler, req.filter()

    // Handles Get requests to root uri
    handle_fn!(get_handler, req.filter()

    // Handles Get requests to some_service/WILDCARD 
    handle_fn!(service_handler, req.filter()

    // Returns method not found


The crate provides some useful iterators

use http_tools::request::query_iter;

// use the http_tools function to create an iterator given an http::request::Request
for (key, value) in query_iter(&request){
    println!("{} {}", key, value)



Provides functions and implements traits on the http::request::Request type.


Provides functions and implements traits on the http::request::Response type.



Help reduce boilerplate of filtering on multiple handlers