Skip to content

Commit 3cd48b4

Browse files
committed
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.
1 parent a37e6b5 commit 3cd48b4

File tree

109 files changed

+1005
-14412
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+1005
-14412
lines changed

Diff for: .travis.yml

-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ matrix:
99
- rust: beta
1010
- rust: stable
1111
env: HYPER_DOCS=1
12-
- rust: stable
13-
env: FEATURES="--no-default-features"
14-
- rust: stable
15-
env: FEATURES="--features compat"
1612
- rust: 1.21.0
1713

1814
cache:

Diff for: Cargo.toml

+1-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ base64 = "0.9"
2525
bytes = "0.4.4"
2626
futures = "0.1.17"
2727
futures-cpupool = "0.1.6"
28-
http = { version = "0.1", optional = true }
28+
http = "0.1.5"
2929
httparse = "1.0"
3030
iovec = "0.1"
3131
language-tags = "0.2"
@@ -46,7 +46,4 @@ spmc = "0.2"
4646
url = "1.0"
4747

4848
[features]
49-
default = []
5049
nightly = []
51-
raw_status = []
52-
compat = [ "http" ]

Diff for: benches/end_to_end.rs

+11-16
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ use futures::{Future, Stream};
1212
use tokio_core::reactor::{Core, Handle};
1313
use tokio_core::net::TcpListener;
1414

15-
use hyper::client;
16-
use hyper::header::{ContentLength, ContentType};
17-
use hyper::Method;
15+
use hyper::{Body, Method, Request, Response};
1816

1917

2018
#[bench]
@@ -30,7 +28,7 @@ fn get_one_at_a_time(b: &mut test::Bencher) {
3028
b.bytes = 160 * 2 + PHRASE.len() as u64;
3129
b.iter(move || {
3230
let work = client.get(url.clone()).and_then(|res| {
33-
res.body().for_each(|_chunk| {
31+
res.into_body().into_stream().for_each(|_chunk| {
3432
Ok(())
3533
})
3634
});
@@ -54,12 +52,11 @@ fn post_one_at_a_time(b: &mut test::Bencher) {
5452
let post = "foo bar baz quux";
5553
b.bytes = 180 * 2 + post.len() as u64 + PHRASE.len() as u64;
5654
b.iter(move || {
57-
let mut req = client::Request::new(Method::Post, url.clone());
58-
req.headers_mut().set(ContentLength(post.len() as u64));
59-
req.set_body(post);
60-
55+
let mut req = Request::new(post.into());
56+
*req.method_mut() = Method::POST;
57+
*req.uri_mut() = url.clone();
6158
let work = client.request(req).and_then(|res| {
62-
res.body().for_each(|_chunk| {
59+
res.into_body().into_stream().for_each(|_chunk| {
6360
Ok(())
6461
})
6562
});
@@ -71,22 +68,20 @@ fn post_one_at_a_time(b: &mut test::Bencher) {
7168
static PHRASE: &'static [u8] = include_bytes!("../CHANGELOG.md"); //b"Hello, World!";
7269

7370
fn spawn_hello(handle: &Handle) -> SocketAddr {
74-
use hyper::server::{const_service, service_fn, NewService, Request, Response};
71+
use hyper::server::{const_service, service_fn, NewService};
7572
let addr = "127.0.0.1:0".parse().unwrap();
7673
let listener = TcpListener::bind(&addr, handle).unwrap();
7774
let addr = listener.local_addr().unwrap();
7875

7976
let handle2 = handle.clone();
8077
let http = hyper::server::Http::<hyper::Chunk>::new();
8178

82-
let service = const_service(service_fn(|req: Request| {
83-
req.body()
79+
let service = const_service(service_fn(|req: Request<Body>| {
80+
req.into_body()
81+
.into_stream()
8482
.concat2()
8583
.map(|_| {
86-
Response::<hyper::Body>::new()
87-
.with_header(ContentLength(PHRASE.len() as u64))
88-
.with_header(ContentType::plaintext())
89-
.with_body(PHRASE)
84+
Response::new(Body::from(PHRASE))
9085
})
9186
}));
9287

Diff for: benches/server.rs

+21-21
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ use std::io::{Read, Write};
1010
use std::net::{TcpListener, TcpStream};
1111
use std::sync::mpsc;
1212

13-
use futures::{future, stream, Future};
13+
use futures::{future, stream, Future, Stream};
1414
use futures::sync::oneshot;
1515

16-
use hyper::header::{ContentLength, ContentType, TransferEncoding};
17-
use hyper::server::{self, Service};
16+
use hyper::{Body, Request, Response};
17+
use hyper::server::Service;
1818

1919
macro_rules! bench_server {
2020
($b:ident, $header:expr, $body:expr) => ({
@@ -65,37 +65,37 @@ fn body(b: &'static [u8]) -> hyper::Body {
6565

6666
#[bench]
6767
fn throughput_fixedsize_small_payload(b: &mut test::Bencher) {
68-
bench_server!(b, ContentLength(13), || body(b"Hello, World!"))
68+
bench_server!(b, ("content-length", "13"), || body(b"Hello, World!"))
6969
}
7070

7171
#[bench]
7272
fn throughput_fixedsize_large_payload(b: &mut test::Bencher) {
73-
bench_server!(b, ContentLength(1_000_000), || body(&[b'x'; 1_000_000]))
73+
bench_server!(b, ("content-length", "1000000"), || body(&[b'x'; 1_000_000]))
7474
}
7575

7676
#[bench]
7777
fn throughput_fixedsize_many_chunks(b: &mut test::Bencher) {
78-
bench_server!(b, ContentLength(1_000_000), || {
78+
bench_server!(b, ("content-length", "1000000"), || {
7979
static S: &'static [&'static [u8]] = &[&[b'x'; 1_000] as &[u8]; 1_000] as _;
80-
stream::iter_ok(S.iter())
80+
Body::wrap_stream(stream::iter_ok(S.iter()).map(|&s| s))
8181
})
8282
}
8383

8484
#[bench]
8585
fn throughput_chunked_small_payload(b: &mut test::Bencher) {
86-
bench_server!(b, TransferEncoding::chunked(), || body(b"Hello, World!"))
86+
bench_server!(b, ("transfer-encoding", "chunked"), || body(b"Hello, World!"))
8787
}
8888

8989
#[bench]
9090
fn throughput_chunked_large_payload(b: &mut test::Bencher) {
91-
bench_server!(b, TransferEncoding::chunked(), || body(&[b'x'; 1_000_000]))
91+
bench_server!(b, ("transfer-encoding", "chunked"), || body(&[b'x'; 1_000_000]))
9292
}
9393

9494
#[bench]
9595
fn throughput_chunked_many_chunks(b: &mut test::Bencher) {
96-
bench_server!(b, TransferEncoding::chunked(), || {
96+
bench_server!(b, ("transfer-encoding", "chunked"), || {
9797
static S: &'static [&'static [u8]] = &[&[b'x'; 1_000] as &[u8]; 1_000] as _;
98-
stream::iter_ok(S.iter())
98+
Body::wrap_stream(stream::iter_ok(S.iter()).map(|&s| s))
9999
})
100100
}
101101

@@ -177,26 +177,26 @@ fn raw_tcp_throughput_large_payload(b: &mut test::Bencher) {
177177
tx.send(()).unwrap();
178178
}
179179

180-
struct BenchPayload<H, F> {
181-
header: H,
180+
struct BenchPayload<F> {
181+
header: (&'static str, &'static str),
182182
body: F,
183183
}
184184

185-
impl<H, F, B> Service for BenchPayload<H, F>
185+
impl<F, B> Service for BenchPayload<F>
186186
where
187-
H: hyper::header::Header + Clone,
188187
F: Fn() -> B,
189188
{
190-
type Request = server::Request;
191-
type Response = server::Response<B>;
189+
type Request = Request<Body>;
190+
type Response = Response<B>;
192191
type Error = hyper::Error;
193192
type Future = future::FutureResult<Self::Response, hyper::Error>;
194193
fn call(&self, _req: Self::Request) -> Self::Future {
195194
future::ok(
196-
server::Response::new()
197-
.with_header(self.header.clone())
198-
.with_header(ContentType::plaintext())
199-
.with_body((self.body)())
195+
Response::builder()
196+
.header(self.header.0, self.header.1)
197+
.header("content-type", "text/plain")
198+
.body((self.body)())
199+
.unwrap()
200200
)
201201
}
202202
}

Diff for: examples/client.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::io::{self, Write};
1111
use futures::Future;
1212
use futures::stream::Stream;
1313

14-
use hyper::Client;
14+
use hyper::{Body, Client, Request};
1515

1616
fn main() {
1717
pretty_env_logger::init();
@@ -25,7 +25,7 @@ fn main() {
2525
};
2626

2727
let url = url.parse::<hyper::Uri>().unwrap();
28-
if url.scheme() != Some("http") {
28+
if url.scheme_part().map(|s| s.as_ref()) != Some("http") {
2929
println!("This example only works with 'http' URLs.");
3030
return;
3131
}
@@ -34,11 +34,13 @@ fn main() {
3434
let handle = core.handle();
3535
let client = Client::new(&handle);
3636

37-
let work = client.get(url).and_then(|res| {
37+
let mut req = Request::new(Body::empty());
38+
*req.uri_mut() = url;
39+
let work = client.request(req).and_then(|res| {
3840
println!("Response: {}", res.status());
39-
println!("Headers: \n{}", res.headers());
41+
println!("Headers: {:#?}", res.headers());
4042

41-
res.body().for_each(|chunk| {
43+
res.into_parts().1.for_each(|chunk| {
4244
io::stdout().write_all(&chunk).map_err(From::from)
4345
})
4446
}).map(|_| {

Diff for: examples/hello.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ extern crate hyper;
33
extern crate futures;
44
extern crate pretty_env_logger;
55

6-
use hyper::header::{ContentLength, ContentType};
7-
use hyper::server::{Http, Response, const_service, service_fn};
6+
use hyper::{Body, Response};
7+
use hyper::server::{Http, const_service, service_fn};
88

99
static PHRASE: &'static [u8] = b"Hello World!";
1010

@@ -13,10 +13,7 @@ fn main() {
1313
let addr = ([127, 0, 0, 1], 3000).into();
1414

1515
let new_service = const_service(service_fn(|_| {
16-
Ok(Response::<hyper::Body>::new()
17-
.with_header(ContentLength(PHRASE.len() as u64))
18-
.with_header(ContentType::plaintext())
19-
.with_body(PHRASE))
16+
Ok(Response::new(Body::from(PHRASE)))
2017
}));
2118

2219
let server = Http::new()

Diff for: examples/multi_server.rs

+13-14
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,31 @@ extern crate pretty_env_logger;
77
use futures::{Future, Stream};
88
use futures::future::FutureResult;
99

10-
use hyper::{Get, StatusCode};
10+
use hyper::{Body, Method, Request, Response, StatusCode};
1111
use tokio_core::reactor::Core;
12-
use hyper::header::ContentLength;
13-
use hyper::server::{Http, Service, Request, Response};
12+
use hyper::server::{Http, Service};
1413

1514
static INDEX1: &'static [u8] = b"The 1st service!";
1615
static INDEX2: &'static [u8] = b"The 2nd service!";
1716

1817
struct Srv(&'static [u8]);
1918

2019
impl Service for Srv {
21-
type Request = Request;
22-
type Response = Response;
20+
type Request = Request<Body>;
21+
type Response = Response<Body>;
2322
type Error = hyper::Error;
24-
type Future = FutureResult<Response, hyper::Error>;
23+
type Future = FutureResult<Response<Body>, hyper::Error>;
2524

26-
fn call(&self, req: Request) -> Self::Future {
27-
futures::future::ok(match (req.method(), req.path()) {
28-
(&Get, "/") => {
29-
Response::new()
30-
.with_header(ContentLength(self.0.len() as u64))
31-
.with_body(self.0)
25+
fn call(&self, req: Request<Body>) -> Self::Future {
26+
futures::future::ok(match (req.method(), req.uri().path()) {
27+
(&Method::GET, "/") => {
28+
Response::new(self.0.into())
3229
},
3330
_ => {
34-
Response::new()
35-
.with_status(StatusCode::NotFound)
31+
Response::builder()
32+
.status(StatusCode::NOT_FOUND)
33+
.body(Body::empty())
34+
.unwrap()
3635
}
3736
})
3837
}

Diff for: examples/params.rs

+27-30
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ extern crate url;
66

77
use futures::{Future, Stream};
88

9-
use hyper::{Get, Post, StatusCode};
10-
use hyper::header::ContentLength;
11-
use hyper::server::{Http, Service, Request, Response};
9+
use hyper::{Body, Method, Request, Response, StatusCode};
10+
use hyper::server::{Http, Service};
1211

1312
use std::collections::HashMap;
1413
use url::form_urlencoded;
@@ -20,20 +19,18 @@ static NOTNUMERIC: &[u8] = b"Number field is not numeric";
2019
struct ParamExample;
2120

2221
impl Service for ParamExample {
23-
type Request = Request;
24-
type Response = Response;
22+
type Request = Request<Body>;
23+
type Response = Response<Body>;
2524
type Error = hyper::Error;
2625
type Future = Box<Future<Item = Self::Response, Error = Self::Error>>;
2726

28-
fn call(&self, req: Request) -> Self::Future {
29-
match (req.method(), req.path()) {
30-
(&Get, "/") | (&Get, "/post") => {
31-
Box::new(futures::future::ok(Response::new()
32-
.with_header(ContentLength(INDEX.len() as u64))
33-
.with_body(INDEX)))
27+
fn call(&self, req: Request<Body>) -> Self::Future {
28+
match (req.method(), req.uri().path()) {
29+
(&Method::GET, "/") | (&Method::GET, "/post") => {
30+
Box::new(futures::future::ok(Response::new(INDEX.into())))
3431
},
35-
(&Post, "/post") => {
36-
Box::new(req.body().concat2().map(|b| {
32+
(&Method::POST, "/post") => {
33+
Box::new(req.into_parts().1.concat2().map(|b| {
3734
// Parse the request body. form_urlencoded::parse
3835
// always succeeds, but in general parsing may
3936
// fail (for example, an invalid post of json), so
@@ -52,25 +49,25 @@ impl Service for ParamExample {
5249
let name = if let Some(n) = params.get("name") {
5350
n
5451
} else {
55-
return Response::new()
56-
.with_status(StatusCode::UnprocessableEntity)
57-
.with_header(ContentLength(MISSING.len() as u64))
58-
.with_body(MISSING);
52+
return Response::builder()
53+
.status(StatusCode::UNPROCESSABLE_ENTITY)
54+
.body(MISSING.into())
55+
.unwrap();
5956
};
6057
let number = if let Some(n) = params.get("number") {
6158
if let Ok(v) = n.parse::<f64>() {
6259
v
6360
} else {
64-
return Response::new()
65-
.with_status(StatusCode::UnprocessableEntity)
66-
.with_header(ContentLength(NOTNUMERIC.len() as u64))
67-
.with_body(NOTNUMERIC);
61+
return Response::builder()
62+
.status(StatusCode::UNPROCESSABLE_ENTITY)
63+
.body(NOTNUMERIC.into())
64+
.unwrap();
6865
}
6966
} else {
70-
return Response::new()
71-
.with_status(StatusCode::UnprocessableEntity)
72-
.with_header(ContentLength(MISSING.len() as u64))
73-
.with_body(MISSING);
67+
return Response::builder()
68+
.status(StatusCode::UNPROCESSABLE_ENTITY)
69+
.body(MISSING.into())
70+
.unwrap();
7471
};
7572

7673
// Render the response. This will often involve
@@ -80,14 +77,14 @@ impl Service for ParamExample {
8077
// responses such as InternalServiceError may be
8178
// needed here, too.
8279
let body = format!("Hello {}, your number is {}", name, number);
83-
Response::new()
84-
.with_header(ContentLength(body.len() as u64))
85-
.with_body(body)
80+
Response::new(body.into())
8681
}))
8782
},
8883
_ => {
89-
Box::new(futures::future::ok(Response::new()
90-
.with_status(StatusCode::NotFound)))
84+
Box::new(futures::future::ok(Response::builder()
85+
.status(StatusCode::NOT_FOUND)
86+
.body(Body::empty())
87+
.unwrap()))
9188
}
9289
}
9390
}

0 commit comments

Comments
 (0)