feat(lib): replace types with those from http crate
BREAKING CHANGE: `Method`, `Request`, `Response`, `StatusCode`, `Version`, and `Uri` have been replaced with types from the `http` crate. The `hyper::header` module is gone for now. Removed `Client::get`, since it needed to construct a `Request<B>` with an empty body. Just use `Client::request` instead. Removed `compat` cargo feature, and `compat` related API.
This commit is contained in:
@@ -4,12 +4,11 @@ extern crate hyper;
|
||||
extern crate pretty_env_logger;
|
||||
|
||||
use futures::{Future, Sink};
|
||||
use futures::sync::{mpsc, oneshot};
|
||||
use futures::sync::oneshot;
|
||||
|
||||
use hyper::{Chunk, Get, StatusCode};
|
||||
use hyper::{Body, Chunk, Method, Request, Response, StatusCode};
|
||||
use hyper::error::Error;
|
||||
use hyper::header::ContentLength;
|
||||
use hyper::server::{Http, Service, Request, Response};
|
||||
use hyper::server::{Http, Service};
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::{self, copy, Read};
|
||||
@@ -18,7 +17,7 @@ use std::thread;
|
||||
static NOTFOUND: &[u8] = b"Not Found";
|
||||
static INDEX: &str = "examples/send_file_index.html";
|
||||
|
||||
fn simple_file_send(f: &str) -> Box<Future<Item = Response, Error = hyper::Error>> {
|
||||
fn simple_file_send(f: &str) -> Box<Future<Item = Response<Body>, Error = hyper::Error>> {
|
||||
// Serve a file by reading it entirely into memory. As a result
|
||||
// this is limited to serving small files, but it is somewhat
|
||||
// simpler with a little less overhead.
|
||||
@@ -31,10 +30,10 @@ fn simple_file_send(f: &str) -> Box<Future<Item = Response, Error = hyper::Error
|
||||
let mut file = match File::open(filename) {
|
||||
Ok(f) => f,
|
||||
Err(_) => {
|
||||
tx.send(Response::new()
|
||||
.with_status(StatusCode::NotFound)
|
||||
.with_header(ContentLength(NOTFOUND.len() as u64))
|
||||
.with_body(NOTFOUND))
|
||||
tx.send(Response::builder()
|
||||
.status(StatusCode::NOT_FOUND)
|
||||
.body(NOTFOUND.into())
|
||||
.unwrap())
|
||||
.expect("Send error on open");
|
||||
return;
|
||||
},
|
||||
@@ -42,14 +41,15 @@ fn simple_file_send(f: &str) -> Box<Future<Item = Response, Error = hyper::Error
|
||||
let mut buf: Vec<u8> = Vec::new();
|
||||
match copy(&mut file, &mut buf) {
|
||||
Ok(_) => {
|
||||
let res = Response::new()
|
||||
.with_header(ContentLength(buf.len() as u64))
|
||||
.with_body(buf);
|
||||
let res = Response::new(buf.into());
|
||||
tx.send(res).expect("Send error on successful file read");
|
||||
},
|
||||
Err(_) => {
|
||||
tx.send(Response::new().with_status(StatusCode::InternalServerError)).
|
||||
expect("Send error on error reading file");
|
||||
tx.send(Response::builder()
|
||||
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
||||
.body(Body::empty())
|
||||
.unwrap())
|
||||
.expect("Send error on error reading file");
|
||||
},
|
||||
};
|
||||
});
|
||||
@@ -60,17 +60,17 @@ fn simple_file_send(f: &str) -> Box<Future<Item = Response, Error = hyper::Error
|
||||
struct ResponseExamples;
|
||||
|
||||
impl Service for ResponseExamples {
|
||||
type Request = Request;
|
||||
type Response = Response;
|
||||
type Request = Request<Body>;
|
||||
type Response = Response<Body>;
|
||||
type Error = hyper::Error;
|
||||
type Future = Box<Future<Item = Self::Response, Error = Self::Error>>;
|
||||
|
||||
fn call(&self, req: Request) -> Self::Future {
|
||||
match (req.method(), req.path()) {
|
||||
(&Get, "/") | (&Get, "/index.html") => {
|
||||
fn call(&self, req: Request<Body>) -> Self::Future {
|
||||
match (req.method(), req.uri().path()) {
|
||||
(&Method::GET, "/") | (&Method::GET, "/index.html") => {
|
||||
simple_file_send(INDEX)
|
||||
},
|
||||
(&Get, "/big_file.html") => {
|
||||
(&Method::GET, "/big_file.html") => {
|
||||
// Stream a large file in chunks. This requires a
|
||||
// little more overhead with two channels, (one for
|
||||
// the response future, and a second for the response
|
||||
@@ -83,16 +83,16 @@ impl Service for ResponseExamples {
|
||||
let mut file = match File::open(INDEX) {
|
||||
Ok(f) => f,
|
||||
Err(_) => {
|
||||
tx.send(Response::new()
|
||||
.with_status(StatusCode::NotFound)
|
||||
.with_header(ContentLength(NOTFOUND.len() as u64))
|
||||
.with_body(NOTFOUND))
|
||||
tx.send(Response::builder()
|
||||
.status(StatusCode::NOT_FOUND)
|
||||
.body(NOTFOUND.into())
|
||||
.unwrap())
|
||||
.expect("Send error on open");
|
||||
return;
|
||||
},
|
||||
};
|
||||
let (mut tx_body, rx_body) = mpsc::channel(1);
|
||||
let res = Response::new().with_body(rx_body);
|
||||
let (mut tx_body, rx_body) = Body::pair();
|
||||
let res = Response::new(rx_body.into());
|
||||
tx.send(res).expect("Send error on successful file read");
|
||||
let mut buf = [0u8; 16];
|
||||
loop {
|
||||
@@ -114,16 +114,18 @@ impl Service for ResponseExamples {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Box::new(rx.map_err(|e| Error::from(io::Error::new(io::ErrorKind::Other, e))))
|
||||
},
|
||||
(&Get, "/no_file.html") => {
|
||||
(&Method::GET, "/no_file.html") => {
|
||||
// Test what happens when file cannot be be found
|
||||
simple_file_send("this_file_should_not_exist.html")
|
||||
},
|
||||
_ => {
|
||||
Box::new(futures::future::ok(Response::new()
|
||||
.with_status(StatusCode::NotFound)))
|
||||
Box::new(futures::future::ok(Response::builder()
|
||||
.status(StatusCode::NOT_FOUND)
|
||||
.body(Body::empty())
|
||||
.unwrap()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user