Crate mio_httpc

source ·
Expand description

mio_httpc is an async http client that runs on top of mio only.

For convenience it also provides CallBuilder::exec for a simple one-line blocking HTTP call.

Except CallBuilder::exec no call will block, not even for DNS resolution as it is implemented internally to avoid blocking.

For https to work you must specify one of the TLS implementations using features: rtls (rustls), native, openssl. Default build will fail on any https URI.

CallBuilder also has URL construction functions (host/path_segm/query/set_https/auth/https) which will take care of url-safe encoding.

mio_httpc does a minimal amount of allocation and in general works with buffers you provide and an internal pool of buffers that get reused on new calls.

§Examples

extern crate mio_httpc;
extern crate mio;

use mio_httpc::{CallBuilder,Httpc};
use mio::{Poll,Events};

let mut poll = Poll::new().unwrap();
let mut htp = Httpc::new(10,None);
let mut call = CallBuilder::get()
    .url("https://www.reddit.com").expect("Invalid url")
    .timeout_ms(500)
    .simple_call(&mut htp, poll.registry())?;

let to = ::std::time::Duration::from_millis(100);
let mut events = Events::with_capacity(8);
'outer: loop {
    poll.poll(&mut events, Some(to)).unwrap();
    for cref in htp.timeout().into_iter() {
       if call.is_ref(cref) {
           println!("Request timed out");
           call.abort(&mut htp);
           break 'outer;
       }
   }

   for ev in events.iter() {
       let cref = htp.event(&ev);

       if call.is_call(&cref) {
           if call.perform(&mut htp, poll.registry())? {
               let (resp,body) = call.finish().ok_or(mio_httpc::Error::Other("unexpected state"))?;
               if let Ok(s) = String::from_utf8(body) {
                   println!("Body: {}",s);
               }
               break 'outer;
           }
       }
   }
}
extern crate mio_httpc;
use mio_httpc::CallBuilder;

// One line blocking call.

let (response_meta, body) = CallBuilder::get().timeout_ms(5000).url("http://www.example.com")?.exec()?;

Structs§

  • Call structure.
  • Used to start a call and get a Call for it.
  • Reference to call. Used for matching mio Token with call. If you have lots of calls, you can use this as a key in a HashMap (you probably want fnv HashMap).
  • A single header
  • Iterator over headers in response
  • Top level configuration for mio_http.
  • Simplified API for non-streaming requests and responses. If body exists it needs to be provided to Request. If response has a body it is returned in Response.
  • WebSocket interface.

Enums§

Functions§

  • Make use of the TLS implementation’s crypto hashing functions. Not picking any TLS implementation as a feature means hash will not work also and will always return an empty vec.

Type Aliases§