1 unstable release

Uses old Rust 2015

0.5.0 Feb 1, 2017

#34 in #response-headers

MIT license

5KB
51 lines

uhttp_response_header -- HTTP response header lines

Documentation

This crate provides a simple formatter for building the lines of an HTTP response header. The result can be written directly into a TcpStream or any other object that implements Write.

Example

use uhttp_response_header::HeaderLines;
use std::io::{Cursor, Write};

let mut buf = [0; 40];
let mut cursor = Cursor::new(&mut buf[..]);

// Write a header with response code `200` and a `Host: iana.org` header field.
{
    let mut h = HeaderLines::new(&mut cursor);
    write!(h.line(), "{} {}", "HTTP/1.1", "200 OK").unwrap();
    write!(h.line(), "Host: {}", "iana.org").unwrap();
}

// Now write the body.
write!(&mut cursor, "hello").unwrap();

assert_eq!(cursor.into_inner(), &b"HTTP/1.1 200 OK\r\nHost: iana.org\r\n\r\nhello"[..]);

Usage

This crate can be used through cargo by adding it as a dependency in Cargo.toml:

[dependencies]
uhttp_response_header = "0.5.0"

and importing it in the crate root:

extern crate uhttp_response_header;

No runtime deps