//! A not-quite-trivial HTTP server which responds to requests by showing the request and response //! headers and any other information it has. #![crate_name = "info"] extern crate time; extern crate http; use std::io::net::ip::{SocketAddr, Ipv4Addr}; use std::io::Writer; use http::server::{Config, Server, Request, ResponseWriter}; use http::headers::HeaderEnum; use http::headers::content_type::MediaType; #[deriving(Clone)] struct InfoServer; impl Server for InfoServer { fn get_config(&self) -> Config { Config { bind_address: SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: 8001 } } } fn handle_request(&self, r: Request, w: &mut ResponseWriter) { w.headers.date = Some(time::now_utc()); w.headers.content_type = Some(MediaType { type_: String::from_str("text"), subtype: String::from_str("html"), parameters: vec!((String::from_str("charset"), String::from_str("UTF-8"))) }); w.headers.server = Some(String::from_str("Rust Thingummy/0.0-pre")); w.write(b"Rust HTTP server").unwrap(); w.write(b"

Request

").unwrap(); let s = format!("
Method
{}
Host
{}
Request URI
{}
HTTP version
{}
Close connection
{}
", r.method, r.headers.host, r.request_uri, r.version, r.close_connection); w.write(s.as_bytes()).unwrap(); w.write(b"

Extension headers

").unwrap(); w.write(b"").unwrap(); for header in r.headers.iter() { let line = format!("", header.header_name(), header.header_value()); w.write(line.as_bytes()).unwrap(); } w.write(b"
NameValue
{}{}
").unwrap(); w.write(b"

Body

").unwrap();
        w.write(r.body.as_slice()).unwrap();
        w.write(b"
").unwrap(); w.write(b"

Response

").unwrap(); let s = format!("
Status
{}
", w.status); w.write(s.as_bytes()).unwrap(); w.write(b"

Headers

").unwrap(); w.write(b"").unwrap(); { let h = w.headers.clone(); for header in h.iter() { let line = format!("", header.header_name(), header.header_value()); w.write(line.as_bytes()).unwrap(); } } w.write(b"
NameValue
{}{}
").unwrap(); } } fn main() { InfoServer.serve_forever(); }