Internals have been shuffled around such that Request and Reponse are now given only a mutable reference to the stream, instead of being allowed to consume it. This allows the server to re-use the streams if keep-alive is true. A task pool is used, and the number of the threads can currently be adjusted by using the `listen_threads()` method on Server. [breaking-change]
		
			
				
	
	
		
			56 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			56 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
| #![feature(macro_rules, phase)]
 | |
| 
 | |
| extern crate hyper;
 | |
| #[phase(plugin, link)] extern crate log;
 | |
| 
 | |
| use std::io::util::copy;
 | |
| use std::io::net::ip::Ipv4Addr;
 | |
| 
 | |
| use hyper::{Get, Post};
 | |
| use hyper::header::common::ContentLength;
 | |
| use hyper::server::{Server, Request, Response};
 | |
| 
 | |
| macro_rules! try_return(
 | |
|     ($e:expr) => {{
 | |
|         match $e {
 | |
|             Ok(v) => v,
 | |
|             Err(e) => { error!("Error: {}", e); return; }
 | |
|         }
 | |
|     }}
 | |
| )
 | |
| 
 | |
| fn echo(mut req: Request, mut res: Response) {
 | |
|     match req.uri {
 | |
|         hyper::uri::AbsolutePath(ref path) => match (&req.method, path.as_slice()) {
 | |
|             (&Get, "/") | (&Get, "/echo") => {
 | |
|                 let out = b"Try POST /echo";
 | |
| 
 | |
|                 res.headers_mut().set(ContentLength(out.len()));
 | |
|                 let mut res = try_return!(res.start());
 | |
|                 try_return!(res.write(out));
 | |
|                 try_return!(res.end());
 | |
|                 return;
 | |
|             },
 | |
|             (&Post, "/echo") => (), // fall through, fighting mutable borrows
 | |
|             _ => {
 | |
|                 *res.status_mut() = hyper::status::NotFound;
 | |
|                 try_return!(res.start().and_then(|res| res.end()));
 | |
|                 return;
 | |
|             }
 | |
|         },
 | |
|         _ => {
 | |
|             try_return!(res.start().and_then(|res| res.end()));
 | |
|             return;
 | |
|         }
 | |
|     };
 | |
| 
 | |
|     let mut res = try_return!(res.start());
 | |
|     try_return!(copy(&mut req, &mut res));
 | |
|     try_return!(res.end());
 | |
| }
 | |
| 
 | |
| fn main() {
 | |
|     let server = Server::http(Ipv4Addr(127, 0, 0, 1), 1337);
 | |
|     server.listen(echo).unwrap();
 | |
| }
 |