micro_http_server/lib.rs
1#![deny(missing_docs)]
2
3//! # Micro HTTP Server
4//!
5//! The micro HTTP server - may also be called µHTTP - is a small asynchronous HTTP server
6//! implementation without Futures or
7//! any other overly complicated stuff; therefore, it is ideal for quick prototyping
8//! or API-like requests (e.g. exchaning JSON data).
9//!
10//! µHTTP does not support any kind of load balancing or threading - you
11//! would have to implement this yourself if you want it.
12//!
13//! At the moment, µHTTP only supports GET requests; if you need PUT/POST/ etc.,
14//! feel free to create an issue or a pull request!
15//!
16//! # Example
17//!
18//! ```
19//! use std::{io::{Read,Write},net::TcpStream};
20//! use micro_http_server::MicroHTTP;
21//!
22//! // Create a server on 127.0.0.1:3000.
23//! let server = MicroHTTP::new("127.0.0.1:3000").expect("Could not create server.");
24//! println!("[Server] Waiting for a client @ 127.0.0.1:3000...");
25//!
26//! // Client side: Connect to it and request a file.
27//! let mut connection = TcpStream::connect("127.0.0.1:3000")
28//! .expect("Could not reach server");
29//! println!("[Client] Connected! - Requesting /cat.txt...");
30//! connection.write("GET /cat.txt\r\n\r\n".as_bytes());
31//!
32//! {
33//! // Server side: Get client and send a response.
34//! let mut client = server.next_client().unwrap().unwrap();
35//! println!("[Server] Client requested: {}", client.request().as_ref().unwrap());
36//! let bytes_written = client.respond_ok("Cats are nice.\n".as_bytes()).unwrap();
37//! println!("[Server] Sent {} bytes to the client.", bytes_written);
38//! } // client is dropped here to close the TcpStream.
39//!
40//! // Client side: Read response
41//! let mut buf = String::new();
42//! connection.read_to_string(&mut buf);
43//! println!("[Client] Content of cat.txt: {}", buf);
44//! ```
45
46#[macro_use] extern crate log;
47
48mod microhttp;
49mod client;
50
51pub use microhttp::MicroHTTP;
52pub use client::Client;
53
54#[cfg(not(target_os="windows"))]
55fn os_windows() -> bool { false }
56
57#[cfg(target_os="windows")]
58fn os_windows() -> bool { true }