Created
February 11, 2019 05:28
-
-
Save AlecTaylor/94e72a0759d7733abbfcee8d64ff99b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use mio::{Events, Poll}; | |
use mio_httpc::{CallBuilder, Httpc, HttpcCfg, SimpleCall, Headers, Response}; | |
use url::Url; | |
use failure::Error; | |
#[derive(Clone)] | |
struct StatusHeadersText<'a> { | |
status: u16, | |
headers: Headers<'a>, | |
text: String, | |
} | |
fn do_call<'a>(htp: &mut Httpc, poll: &Poll, mut call: SimpleCall) -> Result<StatusHeadersText<'a>, Error> { | |
let mut opt_resp: Option<String>; | |
let to = ::std::time::Duration::from_millis(100); | |
let mut events = Events::with_capacity(8); | |
/* | |
let last_status : &'a u16; | |
let last_headers: &'a Headers ; | |
*/ | |
let last_response: &'a Response; | |
let last_response_nonref: Response; | |
'outer: loop { | |
poll.poll(&mut events, Some(to))?; | |
for cref in htp.timeout().into_iter() { | |
if call.is_ref(cref) { | |
return Err(format_err!("Request timed out")); | |
} | |
} | |
for ev in events.iter() { | |
let cref = htp.event(&ev); | |
if call.is_call(&cref) && call.perform(htp, &poll)? { | |
let (mut response, body) = match call.finish() { | |
Some(rb) => Ok(rb), | |
None => Err(format_err!("No response")), | |
}?; | |
match String::from_utf8(body.clone()) { | |
Ok(s) => { | |
opt_resp = Some( s); | |
last_response_nonref = response; | |
response = Response::default(); | |
} | |
Err(_) => { | |
return Err(format_err!("Non utf8 body sized: {}", body.len())); | |
} | |
} | |
break 'outer; | |
} | |
} | |
} | |
last_response = &last_response_nonref; | |
match opt_resp { | |
Some(text) => Ok(StatusHeadersText { | |
status: last_response.status.clone(), | |
headers: last_response.headers().clone(), | |
text | |
}), | |
None => Err(format_err!("Empty response")), | |
} | |
} |
Author
AlecTaylor
commented
Feb 11, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment