Skip to content

Commit

Permalink
Merge pull request #14 from PureW/master
Browse files Browse the repository at this point in the history
Fix broken tls-build and adding test of tls-functionality.
  • Loading branch information
Nazarii Sheremet authored Jan 2, 2018
2 parents 90b0cc0 + ae73bbd commit d8fb0da
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub const C_TYPE: [&'static str; 3] = [
pub const SEP: &'static str = "\r\n";

pub const DEF_PORT: u16 = 80;
#[cfg(feature = "native_tls")]
#[cfg(feature = "native-tls")]
pub const DEF_SSL_PORT: u16 = 443;
pub const DEF_ACCEPT: &'static str = "*/*";
pub const DEF_CONN: &'static str = "close";
Expand Down
26 changes: 13 additions & 13 deletions src/err.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
extern crate serde_json;
#[cfg(feature = "native_tls")]
#[cfg(feature = "native-tls")]
extern crate native_tls;

use std::fmt;
use std::error;
use std::io;
#[cfg(feature = "native_tls")]
#[cfg(feature = "native-tls")]
use std::net::TcpStream;
use std::num::ParseIntError;
use url::ParseError;
#[cfg(feature = "native_tls")]
#[cfg(feature = "native-tls")]
use native_tls::HandshakeError;

#[derive(Debug)]
pub enum HttpError {
Parse(ParseError),
IO(io::Error),
Json(serde_json::Error),
#[cfg(feature = "native_tls")]
#[cfg(feature = "native-tls")]
TLS(native_tls::Error),
#[cfg(feature = "native_tls")]
#[cfg(feature = "native-tls")]
SSL(HandshakeError<TcpStream>),
ParseInt(ParseIntError),
MissingFeature(String),
Expand All @@ -43,14 +43,14 @@ impl From<serde_json::Error> for HttpError {
}
}

#[cfg(feature = "native_tls")]
#[cfg(feature = "native-tls")]
impl From<native_tls::Error> for HttpError {
fn from(err: native_tls::Error) -> HttpError {
HttpError::TLS(err)
}
}

#[cfg(feature = "native_tls")]
#[cfg(feature = "native-tls")]
impl From<HandshakeError<TcpStream>> for HttpError {
fn from(err: HandshakeError<TcpStream>) -> HttpError {
HttpError::SSL(err)
Expand All @@ -69,9 +69,9 @@ impl fmt::Display for HttpError {
HttpError::Parse(ref err) => write!(f, "Parse error: {}", err),
HttpError::IO(ref err) => write!(f, "Parse error: {}", err),
HttpError::Json(ref err) => write!(f, "Parse error: {}", err),
#[cfg(feature = "native_tls")]
#[cfg(feature = "native-tls")]
HttpError::TLS(ref err) => write!(f, "Parse error: {}", err),
#[cfg(feature = "native_tls")]
#[cfg(feature = "native-tls")]
HttpError::SSL(ref err) => write!(f, "Parse error: {}", err),
HttpError::ParseInt(ref err) => write!(f, "Parse error: {}", err),
HttpError::MissingFeature(ref err) => write!(f, "Missing feature: {}", err),
Expand All @@ -85,9 +85,9 @@ impl error::Error for HttpError {
HttpError::Parse(ref err) => err.description(),
HttpError::IO(ref err) => err.description(),
HttpError::Json(ref err) => err.description(),
#[cfg(feature = "native_tls")]
#[cfg(feature = "native-tls")]
HttpError::TLS(ref err) => err.description(),
#[cfg(feature = "native_tls")]
#[cfg(feature = "native-tls")]
HttpError::SSL(ref err) => err.description(),
HttpError::ParseInt(ref err) => err.description(),
HttpError::MissingFeature(ref err) => err,
Expand All @@ -99,9 +99,9 @@ impl error::Error for HttpError {
HttpError::Parse(ref err) => Some(err),
HttpError::IO(ref err) => Some(err),
HttpError::Json(ref err) => Some(err),
#[cfg(feature = "native_tls")]
#[cfg(feature = "native-tls")]
HttpError::TLS(ref err) => Some(err),
#[cfg(feature = "native_tls")]
#[cfg(feature = "native-tls")]
HttpError::SSL(ref err) => Some(err),
HttpError::ParseInt(ref err) => Some(err),
HttpError::MissingFeature(ref _err) => None,
Expand Down
20 changes: 11 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
extern crate url;
extern crate rand;
extern crate serde_json;
#[cfg(feature = "native_tls")]
#[cfg(feature = "native-tls")]
extern crate native_tls;

use std::net::TcpStream;
Expand All @@ -19,7 +19,7 @@ use url::{Url, ParseError};
use consts::*;
use err::HttpError;
use response::*;
#[cfg(feature = "native_tls")]
#[cfg(feature = "native-tls")]
use native_tls::TlsConnector;

mod err;
Expand Down Expand Up @@ -257,16 +257,16 @@ impl HTTP {
stream.write_all(request.as_bytes())?;
stream.read_to_string(&mut self.response_str)?;
} else {
self.tls_transport(url)?;
self.response_str = self.tls_transport(request, url)?;
}

response = self.response_str.clone();
let resp = Response::new(response).unwrap();
Ok(resp)
}

#[cfg(feature = "native_tls")]
fn tls_transport(&self, url: &str) -> Result<(), HttpError> {
#[cfg(feature = "native-tls")]
fn tls_transport(&self, request: String, url: &str) -> Result<String, HttpError> {
let port = match self.url.port() {
Some(p) => p,
None => DEF_SSL_PORT,
Expand All @@ -277,13 +277,15 @@ impl HTTP {
let mut stream = connector.connect(&self.host, stream)?;

stream.write_all(request.as_bytes())?;
stream.read_to_string(&mut self.response_str)?;
let mut buf = String::new();
stream.read_to_string(&mut buf)?;
Ok(buf)
}

#[cfg(not(feature = "native_tls"))]
fn tls_transport(&self, _url: &str) -> Result<(), HttpError> {
#[cfg(not(feature = "native-tls"))]
fn tls_transport(&self, _request: String, _url: &str) -> Result<String, HttpError> {
Err(HttpError::MissingFeature(
"Lib not compiled with feature native_tls active".into(),
"Lib not compiled with feature native-tls active".into(),
))
}
/// Create Reqeust String
Expand Down
9 changes: 9 additions & 0 deletions tests/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ mod test {
let response = http.get().send().unwrap();
let string = response.as_str();

assert!(!string.is_empty(), "Response shouldn't be empty");
}
#[cfg(feature = "native-tls")]
#[test]
fn get_tls_response() {
let mut http = HTTP::new("https://google.com/").unwrap();
let response = http.get().send().unwrap();
let string = response.as_str();

assert!(!string.is_empty(), "Response shouldn't be empty");
}
}

0 comments on commit d8fb0da

Please sign in to comment.