Struct Http

Source
pub struct Http;

Implementations§

Source§

impl Http

Source

pub fn from(stream: TcpStream) -> Result<(Request, Response)>

Examples found in repository?
examples/calculator.rs (line 115)
114fn init_stream(stream: TcpStream) -> io::Result<()> {
115    let (request, response) = Http::from(stream)?;
116
117    match request.uri.as_str() {
118        "/add" => add(request, response)?,
119        "/sub" => sub(request, response)?,
120        _      => not_found(request, response)?
121    }
122
123    Ok(())
124}
More examples
Hide additional examples
examples/quick_responses.rs (line 114)
113fn init_stream(stream: TcpStream) -> io::Result<()> {
114    let (request, response) = Http::from(stream)?;
115
116    match request.uri.as_str() {
117        "/add" => add(request, response)?,
118        "/sub" => sub(request, response)?,
119        _      => not_found(request, response)?
120    }
121
122    Ok(())
123}
examples/body.rs (line 41)
40fn init_stream(stream: TcpStream) -> io::Result<()> {
41    let (request, response) = Http::from(stream)?;
42
43    match (&request.method, request.uri.as_str()) {
44        (Method::Post, "/say-hi") => say_hi(request, response)?,
45        _                         => not_found(request, response)?
46    }
47
48    Ok(())
49}
examples/hello_world.rs (line 11)
7fn main() -> io::Result<()> {
8    let listener = TcpListener::bind("127.0.0.1:8080")?;
9
10    for stream in listener.incoming().filter_map(Result::ok) {
11        let (request, response) = Http::from(stream)?;
12
13        println!("New Request: ");
14        println!("- method:          {:?}", request.method);
15        println!("- uri:             {:?}", request.uri);
16        println!("- params:          {:?}", request.params);
17        println!("- http_version:    {:?}", request.http_version);
18        println!("- headers:         {:?}", request.headers);
19
20        response.status_code(StatusCode::Ok)
21        .set_header("Content-Type", "text/html").flush()?
22        .add_to_body("<h1>Hello from Rust!</h1>").flush()?;
23    }
24
25    Ok(())
26}

Auto Trait Implementations§

§

impl Freeze for Http

§

impl RefUnwindSafe for Http

§

impl Send for Http

§

impl Sync for Http

§

impl Unpin for Http

§

impl UnwindSafe for Http

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.