diff --git a/Cargo.toml b/Cargo.toml index cce7a796c..aaf8eb176 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tiny_http" -version = "0.5.9" +version = "0.6.0" authors = ["pierre.krieger1708@gmail.com", "Corey Farwell "] description = "Low level HTTP server library" @@ -14,14 +14,14 @@ default = [] ssl = ["openssl"] [dependencies] -ascii = "0.7" +ascii = "0.8" chunked_transfer = "0.3" encoding = "0.2" -openssl = { version = "0.7", optional = true } -url = "0.2" -chrono = "0.2.15" -log = "0.3" +openssl = { version = "0.10", optional = true } +url = "1.7" +chrono = "0.4" +log = "0.4" [dev-dependencies] rustc-serialize = "0.3" -sha1 = "0.2.0" +sha1 = "0.6.0" diff --git a/src/client.rs b/src/client.rs index 1648d0253..3eb502a06 100644 --- a/src/client.rs +++ b/src/client.rs @@ -235,7 +235,7 @@ impl Iterator for ClientConnection { { let connection_header = rq.headers().iter() .find(|h| h.field.equiv(&"Connection")) - .map(|h| AsRef::::as_ref(h.value.as_ref())); + .map(|h| h.value.as_str()); let lowercase = connection_header.map(|h| h.to_ascii_lowercase()); diff --git a/src/common.rs b/src/common.rs index 984c39b10..e6d002886 100644 --- a/src/common.rs +++ b/src/common.rs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use ascii::{AsciiString, AsciiStr}; +use ascii::{AsciiString, AsciiStr, FromAsciiError}; use std::ascii::AsciiExt; use std::fmt::{self, Display, Formatter}; use std::str::{FromStr}; @@ -232,7 +232,7 @@ impl Display for Header { pub struct HeaderField(AsciiString); impl HeaderField { - pub fn from_bytes(bytes: B) -> Result where B: Into> + AsRef<[u8]> { + pub fn from_bytes(bytes: B) -> Result> where B: Into> + AsRef<[u8]> { AsciiString::from_ascii(bytes).map(HeaderField) } @@ -432,12 +432,12 @@ impl From<(u8, u8)> for HTTPVersion { } /// Represents the current date, expressed in RFC 1123 format, e.g. Sun, 06 Nov 1994 08:49:37 GMT pub struct HTTPDate { - d: DateTime + d: DateTime } impl HTTPDate { pub fn new() -> HTTPDate { - HTTPDate {d: UTC::now(),} + HTTPDate {d: Utc::now(),} } } diff --git a/src/lib.rs b/src/lib.rs index a925b0b97..edebfbf4c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -257,19 +257,18 @@ impl Server { let ssl: Option = match config.ssl { #[cfg(feature = "ssl")] Some(mut config) => { - use std::io::Cursor; use openssl::ssl; use openssl::x509::X509; - use openssl::crypto::pkey::PKey; - use openssl::ssl::SSL_VERIFY_NONE; + use openssl::pkey::PKey; + use openssl::ssl::SslVerifyMode; - let mut ctxt = try!(SslContext::new(ssl::SslMethod::Sslv23)); + let mut ctxt = try!(SslContext::builder(ssl::SslMethod::tls())); try!(ctxt.set_cipher_list("DEFAULT")); - let certificate = try!(X509::from_pem(&mut Cursor::new(&config.certificate[..]))); + let certificate = try!(X509::from_pem(&config.certificate[..])); try!(ctxt.set_certificate(&certificate)); - let private_key = try!(PKey::private_key_from_pem(&mut Cursor::new(&config.private_key[..]))); + let private_key = try!(PKey::private_key_from_pem(&config.private_key[..])); try!(ctxt.set_private_key(&private_key)); - ctxt.set_verify(SSL_VERIFY_NONE, None); + ctxt.set_verify(SslVerifyMode::NONE); try!(ctxt.check_private_key()); // let's wipe the certificate and private key from memory, because we're @@ -277,7 +276,7 @@ impl Server { for b in &mut config.certificate { *b = 0; } for b in &mut config.private_key { *b = 0; } - Some(ctxt) + Some(ctxt.build()) }, #[cfg(not(feature = "ssl"))] Some(_) => return Err("Building a server with SSL requires enabling the `ssl` feature \ @@ -306,9 +305,10 @@ impl Server { }, #[cfg(feature = "ssl")] Some(ref ssl) => { + let ssl = openssl::ssl::Ssl::new(ssl).expect("Couldn't create ssl"); // trying to apply SSL over the connection // if an error occurs, we just close the socket and resume listening - let sock = match openssl::ssl::SslStream::accept(ssl, sock) { + let sock = match ssl.accept(sock) { Ok(s) => s, Err(_) => continue }; diff --git a/src/request.rs b/src/request.rs index 29c7eb69d..965f85fe1 100644 --- a/src/request.rs +++ b/src/request.rs @@ -132,7 +132,7 @@ pub fn new_request(secure: bool, method: Method, path: String, // true if the client sent a `Expect: 100-continue` header let expects_continue = { - match headers.iter().find(|h: &&Header| h.field.equiv(&"Expect")).map(|h| AsRef::::as_ref(h.value.as_ref())) { + match headers.iter().find(|h: &&Header| h.field.equiv(&"Expect")).map(|h| h.value.as_str()) { None => false, Some(v) if v.eq_ignore_ascii_case("100-continue") => true, @@ -142,7 +142,7 @@ pub fn new_request(secure: bool, method: Method, path: String, // true if the client sent a `Connection: upgrade` header let connection_upgrade = { - match headers.iter().find(|h: &&Header| h.field.equiv(&"Connection")).map(|h| AsRef::::as_ref(h.value.as_ref())) { + match headers.iter().find(|h: &&Header| h.field.equiv(&"Connection")).map(|h| h.value.as_str()) { Some(v) if v.to_ascii_lowercase().contains("upgrade") => true, _ => false diff --git a/src/response.rs b/src/response.rs index afb97d6ef..c40c12fec 100644 --- a/src/response.rs +++ b/src/response.rs @@ -102,7 +102,7 @@ fn write_message_header(mut writer: W, http_version: &HTTPVersion, for header in headers.iter() { try!(writer.write_all(header.field.as_str().as_ref())); try!(write!(&mut writer, ": ")); - try!(writer.write_all(header.value.as_ref().as_ref())); + try!(writer.write_all(header.value.as_str().as_ref())); try!(write!(&mut writer, "\r\n")); } diff --git a/src/util/refined_tcp_stream.rs b/src/util/refined_tcp_stream.rs index 7a3df15c2..474afbbed 100644 --- a/src/util/refined_tcp_stream.rs +++ b/src/util/refined_tcp_stream.rs @@ -16,6 +16,8 @@ use std::io::{Read, Write}; use std::io::Result as IoResult; use std::net::{SocketAddr, TcpStream, Shutdown}; +#[cfg(feature = "ssl")] +use std::sync::{Arc, Mutex}; #[cfg(feature = "ssl")] use openssl::ssl::SslStream; @@ -28,7 +30,7 @@ pub struct RefinedTcpStream { pub enum Stream { Http(TcpStream), #[cfg(feature = "ssl")] - Https(SslStream), + Https(Arc>>), } impl From for Stream { @@ -42,7 +44,7 @@ impl From for Stream { impl From> for Stream { #[inline] fn from(stream: SslStream) -> Stream { - Stream::Https(stream) + Stream::Https(Arc::new(Mutex::new(stream))) } } @@ -55,7 +57,7 @@ impl RefinedTcpStream { let read = match stream { Stream::Http(ref stream) => Stream::Http(stream.try_clone().unwrap()), #[cfg(feature = "ssl")] - Stream::Https(ref stream) => Stream::Https(stream.try_clone().unwrap()), + Stream::Https(ref stream) => Stream::Https(stream.clone()), }; let read = RefinedTcpStream { @@ -87,7 +89,7 @@ impl RefinedTcpStream { match self.stream { Stream::Http(ref mut stream) => stream.peer_addr(), #[cfg(feature = "ssl")] - Stream::Https(ref mut stream) => stream.get_ref().peer_addr(), + Stream::Https(ref mut stream) => stream.lock().unwrap().get_ref().peer_addr(), } } } @@ -99,7 +101,7 @@ impl Drop for RefinedTcpStream { // ignoring outcome Stream::Http(ref mut stream) => stream.shutdown(Shutdown::Read).ok(), #[cfg(feature = "ssl")] - Stream::Https(ref mut stream) => stream.get_mut().shutdown(Shutdown::Read).ok(), + Stream::Https(ref mut stream) => stream.lock().unwrap().get_mut().shutdown(Shutdown::Read).ok(), }; } @@ -108,7 +110,7 @@ impl Drop for RefinedTcpStream { // ignoring outcome Stream::Http(ref mut stream) => stream.shutdown(Shutdown::Write).ok(), #[cfg(feature = "ssl")] - Stream::Https(ref mut stream) => stream.get_mut().shutdown(Shutdown::Write).ok(), + Stream::Https(ref mut stream) => stream.lock().unwrap().get_mut().shutdown(Shutdown::Write).ok(), }; } } @@ -119,7 +121,7 @@ impl Read for RefinedTcpStream { match self.stream { Stream::Http(ref mut stream) => stream.read(buf), #[cfg(feature = "ssl")] - Stream::Https(ref mut stream) => stream.read(buf), + Stream::Https(ref mut stream) => stream.lock().unwrap().read(buf), } } } @@ -129,7 +131,7 @@ impl Write for RefinedTcpStream { match self.stream { Stream::Http(ref mut stream) => stream.write(buf), #[cfg(feature = "ssl")] - Stream::Https(ref mut stream) => stream.write(buf), + Stream::Https(ref mut stream) => stream.lock().unwrap().write(buf), } } @@ -137,7 +139,7 @@ impl Write for RefinedTcpStream { match self.stream { Stream::Http(ref mut stream) => stream.flush(), #[cfg(feature = "ssl")] - Stream::Https(ref mut stream) => stream.flush(), + Stream::Https(ref mut stream) => stream.lock().unwrap().flush(), } } }