Skip to content

Commit 19ea081

Browse files
committed
Add http. Initially expose shio::http::Method and expose as a facade.
1 parent a903fb4 commit 19ea081

File tree

13 files changed

+110
-53
lines changed

13 files changed

+110
-53
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ fn hello(ctx: Context) -> Response {
3636

3737
fn main() {
3838
Shio::default()
39-
.route((Method::Get, "/", hello_world))
40-
.route((Method::Get, "/{name}", hello))
39+
.route((Method::GET, "/", hello_world))
40+
.route((Method::GET, "/{name}", hello))
4141
.run(":7878").unwrap();
4242
}
4343
```

examples/hello/src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ fn main() {
3131
// Construct a _default_ `Shio` service, mount the `index` handler, and
3232
// run indefinitely on port `7878` (by default, binds to both `0.0.0.0` and `::0`).
3333
Shio::default()
34-
.route((Method::Get, "/", hello_world))
35-
.route((Method::Get, "/{name}", hello))
34+
.route((Method::GET, "/", hello_world))
35+
.route((Method::GET, "/{name}", hello))
3636
.run(":7878")
3737
.unwrap();
3838

@@ -46,8 +46,8 @@ fn main() {
4646
4747
let mut router = shio::router::Router::new();
4848
49-
router.route((Method::Get, "/", hello_world));
50-
router.route((Method::Get, "/{name}", hello));
49+
router.route((Method::GET, "/", hello_world));
50+
router.route((Method::GET, "/{name}", hello));
5151
5252
let mut service = Shio::new(router);
5353

examples/json/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fn index(ctx: Context) -> BoxFuture<Response, errors::Error> {
5454

5555
fn main() {
5656
Shio::default()
57-
.route((Method::Post, "/", index))
57+
.route((Method::POST, "/", index))
5858
.run(":7878")
5959
.unwrap();
6060
}

examples/managed_state/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn hello(context: Context) -> Response {
2424
fn main() {
2525
Shio::default()
2626
.manage::<SharedCounter>(AtomicUsize::default())
27-
.route((Method::Get, "/", hello))
27+
.route((Method::GET, "/", hello))
2828
.run(":7878")
2929
.unwrap();
3030
}

examples/postgres/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ fn main() {
9191
pretty_env_logger::init().unwrap();
9292

9393
Shio::default()
94-
.route((Method::Get, "/{name}", index))
94+
.route((Method::GET, "/{name}", index))
9595
.run(":7878")
9696
.unwrap();
9797
}

examples/redirect/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ fn index(_: Context) -> Response {
2020

2121
fn main() {
2222
Shio::default()
23-
.route((Method::Get, "/", index))
24-
.route((Method::Get, "/redirect", redirect_to))
25-
.route((Method::Get, "/redirected", redirected))
23+
.route((Method::GET, "/", index))
24+
.route((Method::GET, "/redirect", redirect_to))
25+
.route((Method::GET, "/redirected", redirected))
2626
.run(":7878")
2727
.unwrap();
2828
}

examples/stateful/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl shio::Handler for HandlerWithState {
2525

2626
fn main() {
2727
Shio::default()
28-
.route((Method::Get, "/", HandlerWithState::default()))
28+
.route((Method::GET, "/", HandlerWithState::default()))
2929
.run(":7878")
3030
.unwrap();
3131
}

examples/templates_askama/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn hello(ctx: Context) -> Result<Response, askama::Error> {
2020

2121
fn main() {
2222
Shio::default()
23-
.route((Method::Get, "/hello/{name}", hello))
23+
.route((Method::GET, "/hello/{name}", hello))
2424
.run(":7878")
2525
.unwrap()
2626
}

lib/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ tokio-proto = "0.1.1"
2020
regex = "0.2"
2121
log = "0.3"
2222
unsafe-any = "0.4.2"
23+
http = "0.1"
2324

2425
[features]
2526
default = []

lib/src/http.rs

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,63 @@
22
//!
33
//! This module exports types that map to HTTP concepts or to the underlying HTTP library
44
//! when needed.
5+
#![allow(non_upper_case_globals)]
56

6-
// HTTP Methods
7-
pub use hyper::Method;
7+
use hyper;
8+
use http_types;
9+
10+
// NOTE: This facade is only around to not completely break current usage of 0.2.
11+
// It will be removed in 0.4.
12+
13+
/// The Request Method (VERB).
14+
#[derive(Debug, PartialEq, Eq)]
15+
pub struct Method(http_types::Method);
16+
17+
impl Method {
18+
pub const HEAD: Method = Method(http_types::Method::HEAD);
19+
pub const OPTIONS: Method = Method(http_types::Method::OPTIONS);
20+
pub const GET: Method = Method(http_types::Method::GET);
21+
pub const POST: Method = Method(http_types::Method::POST);
22+
pub const PUT: Method = Method(http_types::Method::PUT);
23+
pub const PATCH: Method = Method(http_types::Method::PATCH);
24+
pub const DELETE: Method = Method(http_types::Method::DELETE);
25+
26+
#[deprecated(since = "0.2.0", note = "use `Method::HEAD` instead")]
27+
pub const Head: Method = Method(http_types::Method::HEAD);
28+
29+
#[deprecated(since = "0.2.0", note = "use `Method::OPTIONS` instead")]
30+
pub const Options: Method = Method(http_types::Method::OPTIONS);
31+
32+
#[deprecated(since = "0.2.0", note = "use `Method::GET` instead")]
33+
pub const Get: Method = Method(http_types::Method::GET);
34+
35+
#[deprecated(since = "0.2.0", note = "use `Method::POST` instead")]
36+
pub const Post: Method = Method(http_types::Method::POST);
37+
38+
#[deprecated(since = "0.2.0", note = "use `Method::PUT` instead")]
39+
pub const Put: Method = Method(http_types::Method::PUT);
40+
41+
#[deprecated(since = "0.2.0", note = "use `Method::PATCH` instead")]
42+
pub const Patch: Method = Method(http_types::Method::PATCH);
43+
44+
#[deprecated(since = "0.2.0", note = "use `Method::DELETE` instead")]
45+
pub const Delete: Method = Method(http_types::Method::DELETE);
46+
47+
// Temporary until Hyper 0.12
48+
pub(crate) fn to_hyper_method(&self) -> hyper::Method {
49+
match *self {
50+
Self::OPTIONS => hyper::Method::Options,
51+
Self::HEAD => hyper::Method::Head,
52+
Self::GET => hyper::Method::Get,
53+
Self::POST => hyper::Method::Post,
54+
Self::PUT => hyper::Method::Put,
55+
Self::PATCH => hyper::Method::Patch,
56+
Self::DELETE => hyper::Method::Delete,
57+
58+
_ => unimplemented!()
59+
}
60+
}
61+
}
862

963
// HTTP Headers
1064
pub use hyper::header;

lib/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![cfg_attr(feature = "nightly", feature(specialization))]
44

55
extern crate futures;
6+
extern crate http as http_types;
67
extern crate hyper;
78
#[macro_use]
89
extern crate log;

lib/src/router/mod.rs

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl Router {
5555
/// # use shio::{Method, Response, StatusCode};
5656
/// # use shio::router::Router;
5757
/// # let mut router = Router::new();
58-
/// router.route((Method::Get, "/users", |_| {
58+
/// router.route((Method::GET, "/users", |_| {
5959
/// // [...]
6060
/// # Response::with(StatusCode::NoContent)
6161
/// }));
@@ -127,8 +127,8 @@ mod tests {
127127
use hyper;
128128

129129
use super::{Parameters, Router};
130-
use {Context, Handler, Response, State, StatusCode};
131-
use Method::*;
130+
use {Context, Handler, Response, State};
131+
use http::{Method, StatusCode};
132132

133133
// Empty handler to use for route tests
134134
fn empty_handler(_: Context) -> Response {
@@ -139,44 +139,44 @@ mod tests {
139139
#[test]
140140
fn test_static_get() {
141141
let mut router = Router::new();
142-
router.add((Get, "/hello", empty_handler));
142+
router.add((Method::GET, "/hello", empty_handler));
143143

144-
assert!(router.find(&Get, "/hello").is_some());
145-
assert!(router.find(&Get, "/aa").is_none());
146-
assert!(router.find(&Get, "/hello/asfa").is_none());
144+
assert!(router.find(&hyper::Method::Get, "/hello").is_some());
145+
assert!(router.find(&hyper::Method::Get, "/aa").is_none());
146+
assert!(router.find(&hyper::Method::Get, "/hello/asfa").is_none());
147147
}
148148

149149
/// Test for _some_ match for static in PUT, POST, DELETE
150150
#[test]
151151
fn test_static_put_post_del() {
152152
let mut router = Router::new();
153-
router.add((Put, "/hello", empty_handler));
154-
router.add((Post, "/hello", empty_handler));
155-
router.add((Delete, "/hello", empty_handler));
156-
157-
assert!(router.find(&Get, "/hello").is_none());
158-
assert!(router.find(&Put, "/hello").is_some());
159-
assert!(router.find(&Post, "/hello").is_some());
160-
assert!(router.find(&Delete, "/hello").is_some());
153+
router.add((Method::PUT, "/hello", empty_handler));
154+
router.add((Method::POST, "/hello", empty_handler));
155+
router.add((Method::DELETE, "/hello", empty_handler));
156+
157+
assert!(router.find(&hyper::Method::Get, "/hello").is_none());
158+
assert!(router.find(&hyper::Method::Put, "/hello").is_some());
159+
assert!(router.find(&hyper::Method::Post, "/hello").is_some());
160+
assert!(router.find(&hyper::Method::Delete, "/hello").is_some());
161161
}
162162

163163
/// Test for the correct match for static
164164
#[test]
165165
fn test_static_find() {
166166
// Correct match
167167
let mut router = Router::new();
168-
router.add((Get, "/aa", empty_handler));
169-
router.add((Get, "/hello", empty_handler));
168+
router.add((Method::GET, "/aa", empty_handler));
169+
router.add((Method::GET, "/hello", empty_handler));
170170

171171
// FIXME: This section currently matches against regex
172172
// This is an implementation detail; store the source strings and we'll
173173
// match against that
174174
assert_eq!(
175-
router.find(&Get, "/hello").unwrap().pattern().as_str(),
175+
router.find(&hyper::Method::Get, "/hello").unwrap().pattern().as_str(),
176176
"^/hello$"
177177
);
178178
assert_eq!(
179-
router.find(&Get, "/aa").unwrap().pattern().as_str(),
179+
router.find(&hyper::Method::Get, "/aa").unwrap().pattern().as_str(),
180180
"^/aa$"
181181
);
182182
}
@@ -185,19 +185,19 @@ mod tests {
185185
#[test]
186186
fn test_param_get() {
187187
let mut router = Router::new();
188-
router.add((Get, "/user/{id}", empty_handler));
188+
router.add((Method::GET, "/user/{id}", empty_handler));
189189

190-
assert!(router.find(&Get, "/user/asfa").is_some());
191-
assert!(router.find(&Get, "/user/profile").is_some());
192-
assert!(router.find(&Get, "/user/3289").is_some());
193-
assert!(router.find(&Get, "/user").is_none());
190+
assert!(router.find(&hyper::Method::Get, "/user/asfa").is_some());
191+
assert!(router.find(&hyper::Method::Get, "/user/profile").is_some());
192+
assert!(router.find(&hyper::Method::Get, "/user/3289").is_some());
193+
assert!(router.find(&hyper::Method::Get, "/user").is_none());
194194
}
195195

196196
/// Test for segment parameter value
197197
#[test]
198198
fn test_param_get_value() {
199199
let mut router = Router::new();
200-
router.add((Get, "/user/{id}", |context: Context| {
200+
router.add((Method::GET, "/user/{id}", |context: Context| {
201201
// FIXME: We should have an assert that we got here
202202
assert_eq!(&context.get::<Parameters>()["id"], "3289");
203203

@@ -209,7 +209,7 @@ mod tests {
209209
// TODO: It should much easier to make a test context
210210
// Perhaps `Request::build ( .. )` should be a thing?
211211
// Proxied as `Context::build ( .. )` ?
212-
let (request, data) = ::service::from_hyper_request(hyper::Request::new(Get, "/user/3289".parse().unwrap()));
212+
let (request, data) = ::service::from_hyper_request(hyper::Request::new(hyper::Method::Get, "/user/3289".parse().unwrap()));
213213
let context = Context::new(core.handle(), request, State::default(), data);
214214

215215
let work = router.call(context);
@@ -221,19 +221,19 @@ mod tests {
221221
#[test]
222222
fn test_param_custom_get() {
223223
let mut router = Router::new();
224-
router.add((Get, "/static/{file: .+}", empty_handler));
224+
router.add((Method::GET, "/static/{file: .+}", empty_handler));
225225

226-
assert!(router.find(&Get, "/static").is_none());
227-
assert!(router.find(&Get, "/static/").is_none());
228-
assert!(router.find(&Get, "/static/blah").is_some());
229-
assert!(router.find(&Get, "/static/rahrahrah").is_some());
226+
assert!(router.find(&hyper::Method::Get, "/static").is_none());
227+
assert!(router.find(&hyper::Method::Get, "/static/").is_none());
228+
assert!(router.find(&hyper::Method::Get, "/static/blah").is_some());
229+
assert!(router.find(&hyper::Method::Get, "/static/rahrahrah").is_some());
230230
}
231231

232232
/// Test for segment parameter value
233233
#[test]
234234
fn test_param_get_custom() {
235235
let mut router = Router::new();
236-
router.add((Get, "/static/{filename: .*}", |context: Context| {
236+
router.add((Method::GET, "/static/{filename: .*}", |context: Context| {
237237
// FIXME: We should have an assert that we got here
238238
assert_eq!(
239239
&context.get::<Parameters>()["filename"],
@@ -245,7 +245,7 @@ mod tests {
245245

246246
let mut core = Core::new().unwrap();
247247

248-
let (request, data) = ::service::from_hyper_request(hyper::Request::new(Get, "/static/path/to/file/is/here".parse().unwrap()));
248+
let (request, data) = ::service::from_hyper_request(hyper::Request::new(hyper::Method::Get, "/static/path/to/file/is/here".parse().unwrap()));
249249
let context = Context::new(core.handle(), request, State::default(), data);
250250

251251
let work = router.call(context);

lib/src/router/route.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use hyper::{self, Method};
44
use futures::IntoFuture;
55

66
use context::Context;
7+
use http;
78
use handler::{BoxHandler, Handler};
89
use response::Response;
910
use router::pattern::Pattern;
@@ -27,7 +28,7 @@ impl Route {
2728
/// # Response::new()
2829
/// });
2930
/// ```
30-
pub fn new<P, H>(method: Method, pattern: P, handler: H) -> Self
31+
pub fn new<P, H>(method: http::Method, pattern: P, handler: H) -> Self
3132
where
3233
P: Into<Pattern>,
3334
H: Handler + 'static,
@@ -36,7 +37,7 @@ impl Route {
3637
Self {
3738
handler: handler.into_box(),
3839
pattern: pattern.into(),
39-
method,
40+
method: method.to_hyper_method(),
4041
}
4142
}
4243

@@ -56,13 +57,13 @@ impl Route {
5657
}
5758
}
5859

59-
impl<P, H> From<(Method, P, H)> for Route
60+
impl<P, H> From<(http::Method, P, H)> for Route
6061
where
6162
P: Into<Pattern>,
6263
H: Handler + 'static,
6364
<H::Result as IntoFuture>::Error: fmt::Debug + Send,
6465
{
65-
fn from(arguments: (Method, P, H)) -> Self {
66+
fn from(arguments: (http::Method, P, H)) -> Self {
6667
Self::new(arguments.0, arguments.1, arguments.2)
6768
}
6869
}

0 commit comments

Comments
 (0)