Skip to content

Commit

Permalink
Merge pull request #149 from Orange-OpenSource/fix/insecure-for-session
Browse files Browse the repository at this point in the history
Fix insecure mode for session
  • Loading branch information
fabricereix authored Feb 7, 2021
2 parents a2ae194 + 89f0968 commit 7650e15
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 22 deletions.
3 changes: 3 additions & 0 deletions integration/ssl/insecure.hurl
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ GET https://localhost:8001/hello

HTTP/1.0 200
```Hello World!```

GET https://localhost:8001/hello
HTTP/1.0 200
51 changes: 29 additions & 22 deletions packages/hurl/src/http/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ pub struct Client {
pub redirect_count: usize,
pub max_redirect: Option<usize>,
pub verbose: bool,
pub verify: bool,
pub proxy: Option<String>,
pub no_proxy: Option<String>,
pub timeout: Duration,
pub connect_timeout: Duration,
pub authorization: Option<String>,
pub accept_encoding: Option<String>,
}
Expand All @@ -80,6 +85,9 @@ impl Client {
pub fn init(options: ClientOptions) -> Client {
let mut h = easy::Easy::new();

// Set handle attributes
// that are not affected by rest

// Activate cookie storage
// with or without persistence (empty string)
h.cookie_file(
Expand All @@ -90,19 +98,6 @@ impl Client {
)
.unwrap();

if let Some(proxy) = options.proxy {
h.proxy(proxy.as_str()).unwrap();
}
if let Some(s) = options.no_proxy {
h.noproxy(s.as_str()).unwrap();
}
h.verbose(options.verbose).unwrap();
h.ssl_verify_host(!options.insecure).unwrap();
h.ssl_verify_peer(!options.insecure).unwrap();

h.timeout(options.timeout).unwrap();
h.connect_timeout(options.connect_timeout).unwrap();

let authorization = options.user.map(|user| base64::encode(user.as_bytes()));
let accept_encoding = options.accept_encoding;
Client {
Expand All @@ -111,19 +106,16 @@ impl Client {
max_redirect: options.max_redirect,
redirect_count: 0,
verbose: options.verbose,
verify: !options.insecure,
authorization,
accept_encoding,
proxy: options.proxy,
no_proxy: options.no_proxy,
timeout: options.timeout,
connect_timeout: options.connect_timeout,
}
}

///
/// reset HTTP hurl client
///
pub fn reset(&mut self) {
self.handle.reset();
self.handle.verbose(self.verbose).unwrap();
}

///
/// Execute an http request
///
Expand All @@ -132,6 +124,21 @@ impl Client {
request: &Request,
redirect_count: usize,
) -> Result<Response, HttpError> {
// set handle attributes
// that have not been set or reset

self.handle.verbose(self.verbose).unwrap();
self.handle.ssl_verify_host(self.verify).unwrap();
self.handle.ssl_verify_peer(self.verify).unwrap();
if let Some(proxy) = self.proxy.clone() {
self.handle.proxy(proxy.as_str()).unwrap();
}
if let Some(s) = self.no_proxy.clone() {
self.handle.noproxy(s.as_str()).unwrap();
}
self.handle.timeout(self.timeout).unwrap();
self.handle.connect_timeout(self.connect_timeout).unwrap();

self.set_url(&request.url, &request.querystring);
self.set_method(&request.method);

Expand Down Expand Up @@ -243,7 +250,7 @@ impl Client {
}
let duration = start.elapsed();
self.redirect_count = redirect_count;
self.reset();
self.handle.reset();

Ok(Response {
version,
Expand Down
25 changes: 25 additions & 0 deletions packages/hurl/tests/libcurl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,3 +774,28 @@ fn test_proxy() {
}

// endregion

#[test]
fn test_insecure() {
let options = ClientOptions {
follow_location: false,
max_redirect: None,
cookie_input_file: None,
proxy: None,
no_proxy: None,
verbose: false,
insecure: true,
timeout: Default::default(),
connect_timeout: Default::default(),
user: None,
accept_encoding: None,
};
let mut client = Client::init(options);
let request = default_get_request("https://localhost:8001/hello".to_string());

let response = client.execute(&request, 0).unwrap();
assert_eq!(response.status, 200);

let response = client.execute(&request, 0).unwrap();
assert_eq!(response.status, 200);
}

0 comments on commit 7650e15

Please sign in to comment.