pub struct Client { /* private fields */ }
Expand description

This struct represents a client which has connected to the µHTTP server.microhttp

If an instance of this struct is dropped, the connection is closed.

Implementations

Return the address of the requesting client, for example “1.2.3.4:9435”.

Return the request the client made or None if the client didn’t make any or an invalid one.

Note: At the moment, only HTTP GET is supported. Any other requests will not be collected.

Send a HTTP 200 OK response to the client + the provided data. The data may be an empty array, for example the following implementation echos all requests except “/hello”:

Consider using respond_ok_chunked for sending file-backed data.

use micro_http_server::MicroHTTP;
use std::io::*;
let server = MicroHTTP::new("127.0.0.1:4000").expect("Could not create server.");
let mut client = server.next_client().unwrap().unwrap();
let request_str: String = client.request().as_ref().unwrap().clone();

match request_str.as_ref() {
	"/hello" => client.respond_ok(&[]),
    _ => client.respond_ok(request_str.as_bytes())  // Echo request
};

Send a HTTP 200 OK response to the client + the provided data. The data may be any type implementing Read and will be read in chunks. This is useful for serving file-backed data that should not be loaded into memory all at once.

use micro_http_server::MicroHTTP;
use std::io::*;
use std::fs::*;
let server = MicroHTTP::new("127.0.0.1:4000").expect("Could not create server.");
let mut client = server.next_client().unwrap().unwrap();
client.request();

let mut file_handle = OpenOptions::new()
	.read(true)
	.write(false)
	.open("/some/local/file")
	.unwrap();
let file_len = file_handle.metadata().unwrap().len() as usize;

client.respond_ok_chunked(file_handle, file_len);

Send response data to the client.

This is similar to respond_ok, but you may control the details yourself.

Consider using respond_chunked for sending file-backed data.

Parameters
  • status_code: Select the status code of the response, e.g. 200 OK.
  • data: Data to transmit. May be empty.
  • headers: Additional headers to add to the response. May be empty.

Calling respond("200 OK", data, &vec!()) is the same as calling respond_ok(data).

Send repsonse data to the client.

This is similar to respond_ok_chunked, but you may control the details yourself.

Parameters
  • status_code: Select the status code of the response, e.ge 200 OK.
  • data: Data to transmit. May be empty
  • content_size: Size of the data to transmit in bytes
  • headers: Additional headers to add to the response. May be empty.

Calling respond_chunked("200 OK", data, content_size, &vec!()) is the same as calling repsond_ok_chunked(data, content_size).

Trait Implementations

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.