Skip to content

Instantly share code, notes, and snippets.

@AlecTaylor
Created February 11, 2019 05:28
Show Gist options
  • Save AlecTaylor/94e72a0759d7733abbfcee8d64ff99b7 to your computer and use it in GitHub Desktop.
Save AlecTaylor/94e72a0759d7733abbfcee8d64ff99b7 to your computer and use it in GitHub Desktop.
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")),
}
}
@AlecTaylor
Copy link
Author

warning: value assigned to `response` is never read
  --> src/lib.rs:58:25
   |
58 |                         response = Response::default();
   |                         ^^^^^^^^
   |
   = note: #[warn(unused_assignments)] on by default
   = help: maybe it is overwritten before being read?

error[E0597]: `last_response_nonref` does not live long enough
  --> src/lib.rs:69:21
   |
28 | fn do_call<'a>(htp: &mut Httpc, poll: &Poll, mut call: SimpleCall) -> Result<StatusHeadersText<'a>, Error> {
   |            -- lifetime `'a` defined here
...
36 |     let last_response: &'a Response;
   |                        ------------ type annotation requires that `last_response_nonref` is borrowed for `'a`
...
69 |     last_response = &last_response_nonref;
   |                     ^^^^^^^^^^^^^^^^^^^^^ borrowed value does not live long enough
...
78 | }
   | - `last_response_nonref` dropped here while still borrowed

error: aborting due to previous error

For more information about this error, try `rustc --explain E0597`.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment