Skip to content

Commit

Permalink
chore(*): Upgrade hyper and url versions
Browse files Browse the repository at this point in the history
Also move listen_https to be unconditionally included since openssl
isn't required by hyper anymore.

Pull request: #356
Approved by: Ryman
  • Loading branch information
sfackler authored and homu committed Jul 9, 2016
1 parent ed54dbd commit b77e4ca
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 91 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Expand Up @@ -17,7 +17,7 @@ unstable = ["hyper/nightly", "compiletest_rs"]
ssl = ["hyper/ssl"]

[dependencies]
url = "0.5"
url = "1.0"
time = "0.1"
typemap = "0.3"
plugin = "0.2"
Expand All @@ -30,7 +30,7 @@ lazy_static = "0.1"
modifier = "0.1"

[dependencies.hyper]
version = "=0.8"
version = "0.9"
default-features = false

[dependencies.compiletest_rs]
Expand Down
81 changes: 36 additions & 45 deletions src/nickel.rs
Expand Up @@ -6,6 +6,7 @@ use router::{Router, HttpRouter, Matcher};
use middleware::{MiddlewareStack, Middleware, ErrorHandler};
use server::{Server, ListeningServer};
use hyper::method::Method;
use hyper::net::SslServer;
use hyper::status::StatusCode;

//pre defined middleware
Expand Down Expand Up @@ -248,18 +249,6 @@ impl<D: Sync + Send + 'static> Nickel<D> {
pub fn keep_alive_timeout(&mut self, timeout: Option<Duration>){
self.keep_alive_timeout = timeout;
}
}

#[cfg(feature = "ssl")]
mod ssl {
use std::net::ToSocketAddrs;
use std::env;
use std::error::Error as StdError;
use server::{Server, ListeningServer};
use hyper::status::StatusCode;
use hyper::net::Ssl;

use super::Nickel;

/// Bind and listen for connections on the given host and port.
/// Only accepts SSL connections
Expand All @@ -273,49 +262,51 @@ mod ssl {
/// # extern crate nickel;
/// extern crate hyper;
///
/// # #[cfg(feature = "ssl")]
/// use nickel::Nickel;
/// # #[cfg(feature = "ssl")]
/// use hyper::net::Openssl;
///
/// # #[cfg(feature = "ssl")]
/// # fn main() {
/// let server = Nickel::new();
/// let ssl = Openssl::with_cert_and_key("foo.crt", "key.pem").unwrap();
/// server.listen_https("127.0.0.1:6767", ssl).unwrap();
/// # }
/// # #[cfg(not(feature = "ssl"))]
/// # fn main() {}
/// ```
impl <D: Sync + Send + 'static> Nickel<D> {
pub fn listen_https<T,S>(mut self, addr: T, ssl: S) -> Result<ListeningServer, Box<StdError>>
where T: ToSocketAddrs,
S: Ssl + Send + Clone + 'static {
self.middleware_stack.add_middleware(middleware! {
(StatusCode::NotFound, "File Not Found")
});

let server = Server::new(self.middleware_stack, self.data);

let is_test_harness = env::vars().any(|(ref k, _)| k == "NICKEL_TEST_HARNESS");

let listener = if is_test_harness {
// If we're under a test harness, we'll pass zero to get assigned a random
// port. See http://doc.rust-lang.org/std/net/struct.TcpListener.html#method.bind
try!(server.serve_https("localhost:0",
self.keep_alive_timeout,
self.options.thread_count,
ssl))
} else {
try!(server.serve_https(addr,
self.keep_alive_timeout,
self.options.thread_count,
ssl))
};

if self.options.output_on_listen {
println!("Listening on https://{}", listener.socket());
println!("Ctrl-C to shutdown server");
}


Ok(listener)
pub fn listen_https<T,S>(mut self, addr: T, ssl: S) -> Result<ListeningServer, Box<StdError>>
where T: ToSocketAddrs,
S: SslServer + Send + Clone + 'static {
self.middleware_stack.add_middleware(middleware! {
(StatusCode::NotFound, "File Not Found")
});

let server = Server::new(self.middleware_stack, self.data);

let is_test_harness = env::vars().any(|(ref k, _)| k == "NICKEL_TEST_HARNESS");

let listener = if is_test_harness {
// If we're under a test harness, we'll pass zero to get assigned a random
// port. See http://doc.rust-lang.org/std/net/struct.TcpListener.html#method.bind
try!(server.serve_https("localhost:0",
self.keep_alive_timeout,
self.options.thread_count,
ssl))
} else {
try!(server.serve_https(addr,
self.keep_alive_timeout,
self.options.thread_count,
ssl))
};

if self.options.output_on_listen {
println!("Listening on https://{}", listener.socket());
println!("Ctrl-C to shutdown server");
}

Ok(listener)
}
}

Expand Down
54 changes: 20 additions & 34 deletions src/server.rs
Expand Up @@ -5,6 +5,7 @@ use std::time::Duration;
use hyper::Result as HttpResult;
use hyper::server::{Request, Response, Handler, Listening};
use hyper::server::Server as HyperServer;
use hyper::net::SslServer;

use middleware::MiddlewareStack;
use request;
Expand Down Expand Up @@ -58,41 +59,26 @@ impl<D: Sync + Send + 'static> Server<D> {

listening.map(ListeningServer)
}
}

#[cfg(feature = "ssl")]
mod ssl {
use std::net::ToSocketAddrs;
use std::sync::{Arc};
use std::time::Duration;
use hyper::Result as HttpResult;
use hyper::server::{Handler, Listening};
use hyper::server::Server as HyperServer;
use hyper::net::Ssl;

use super::{Server,ArcServer, ListeningServer};

impl<D: Sync + Send + 'static> Server<D> {
pub fn serve_https<A,S>(self,
addr: A,
keep_alive_timeout: Option<Duration>,
thread_count: Option<usize>,
ssl: S)
-> HttpResult<ListeningServer>
where A: ToSocketAddrs,
S: Ssl + Clone + Send + 'static {
let arc = ArcServer(Arc::new(self));
let mut server = try!(HyperServer::https(addr, ssl));

server.keep_alive(keep_alive_timeout);

let listening = match thread_count {
Some(threads) => server.handle_threads(arc, threads),
None => server.handle(arc),
};

listening.map(ListeningServer)
}
pub fn serve_https<A,S>(self,
addr: A,
keep_alive_timeout: Option<Duration>,
thread_count: Option<usize>,
ssl: S)
-> HttpResult<ListeningServer>
where A: ToSocketAddrs,
S: SslServer + Clone + Send + 'static {
let arc = ArcServer(Arc::new(self));
let mut server = try!(HyperServer::https(addr, ssl));

server.keep_alive(keep_alive_timeout);

let listening = match thread_count {
Some(threads) => server.handle_threads(arc, threads),
None => server.handle(arc),
};

listening.map(ListeningServer)
}
}

Expand Down
15 changes: 5 additions & 10 deletions src/urlencoded.rs
Expand Up @@ -2,7 +2,7 @@ use groupable::Groupable;
use hyper::uri::RequestUri;
use hyper::uri::RequestUri::{Star, AbsoluteUri, AbsolutePath, Authority};
use std::collections::HashMap;
use url::{form_urlencoded, UrlParser};
use url::{form_urlencoded, Url};

type QueryStore = HashMap<String, Vec<String>>;

Expand Down Expand Up @@ -30,20 +30,15 @@ impl Params {
}

pub fn parse(encoded_string : &str) -> Params {
Params(form_urlencoded::parse(encoded_string.as_bytes()).into_iter().group())
Params(form_urlencoded::parse(encoded_string.as_bytes()).into_owned().group())
}

pub fn parse_uri(origin: &RequestUri) -> Params {
let f = |query: Option<&String>| query.map(|q| parse(&*q));
let f = |query: Option<&str>| query.map(|q| parse(&*q));

let result = match *origin {
AbsoluteUri(ref url) => f(url.query.as_ref()),
AbsolutePath(ref s) => UrlParser::new().parse_path(&*s)
// FIXME: If this fails to parse,
// then it really shouldn't have
// reached here.
.ok()
.and_then(|(_, query, _)| f(query.as_ref())),
AbsoluteUri(ref url) => f(url.query()),
AbsolutePath(ref s) => f(s.splitn(2, '?').nth(1)),
Star | Authority(..) => None
};

Expand Down

0 comments on commit b77e4ca

Please sign in to comment.