pub fn create<B: Into<Body>>(request: Request<B>, options: &Options) -> Result<(CurlRequest, impl Future<Output=Result<Response<Body>, Error>>), Error> {
    // Set up the plumbing...
    let (future_tx, future_rx) = oneshot::channel();
    let (request_parts, request_body) = request.into_parts();

    let mut easy = curl::easy::Easy2::new(CurlHandler {
        state: Arc::new(RequestState::new(options.clone())),
        future: Some(future_tx),
        request_body: request_body.into(),
        version: None,
        status_code: None,
        headers: http::HeaderMap::default(),
    });

    easy.verbose(log_enabled!(Level::Trace))?;
    easy.signal(false)?;
    easy.buffer_size(options.buffer_size)?;

    if let Some(timeout) = options.timeout {
        easy.timeout(timeout)?;
    }

    easy.connect_timeout(options.connect_timeout)?;

    easy.tcp_nodelay(options.tcp_nodelay)?;
    if let Some(interval) = options.tcp_keepalive {
        easy.tcp_keepalive(true)?;
        easy.tcp_keepintvl(interval)?;
    } else {
        easy.tcp_keepalive(false)?;
    }

    match options.redirect_policy {
        RedirectPolicy::None => {
            easy.follow_location(false)?;
        }
        RedirectPolicy::Follow => {
            easy.follow_location(true)?;
        }
        RedirectPolicy::Limit(max) => {
            easy.follow_location(true)?;
            easy.max_redirections(max)?;
        }
    }

    if let Some(limit) = options.max_upload_speed {
        easy.max_send_speed(limit)?;
    }

    if let Some(limit) = options.max_download_speed {
        easy.max_recv_speed(limit)?;
    }

    // Set a preferred HTTP version to negotiate.
    easy.http_version(match options.preferred_http_version {
        Some(http::Version::HTTP_10) => curl::easy::HttpVersion::V10,
        Some(http::Version::HTTP_11) => curl::easy::HttpVersion::V11,
        Some(http::Version::HTTP_2) => curl::easy::HttpVersion::V2,
        _ => curl::easy::HttpVersion::Any,
    })?;

    if let Some(ref proxy) = options.proxy {
        easy.proxy(&format!("{}", proxy))?;
    }

    if let Some(addrs) = &options.dns_servers {
        let dns_string = addrs.iter()
            .map(ToString::to_string)
            .collect::<Vec<_>>()
            .join(",");
        if let Err(e) = easy.dns_servers(&dns_string) {
            warn!("DNS servers could not be configured: {}", e);
        }
    }

    // Configure SSL options.
    if let Some(ciphers) = &options.ssl_ciphers {
        easy.ssl_cipher_list(&ciphers.join(":"))?;
    }
    if let Some(cert) = &options.ssl_client_certificate {
        easy.ssl_client_certificate(cert)?;
    }

    // Set the request data according to the request given.
    easy.custom_request(request_parts.method.as_str())?;
    easy.url(&request_parts.uri.to_string())?;

    let mut headers = curl::easy::List::new();
    for (name, value) in request_parts.headers.iter() {
        let header = format!("{}: {}", name.as_str(), value.to_str().unwrap());
        headers.append(&header)?;
    }
    easy.http_headers(headers)?;

    // Enable automatic response decompression.
    easy.accept_encoding("")?;

    // If the request body is non-empty, tell curl that we are going to upload something.
    if !easy.get_ref().request_body.is_empty() {
        easy.upload(true)?;

        if let Some(len) = easy.get_ref().request_body.len() {
            // If we know the size of the request body up front, tell curl about it.
            easy.in_filesize(len as u64)?;
        }
    }

    let future_rx = future_rx.then(|result| match result {
        Ok(result) => result,
        Err(_) => {
            error!("request canceled by agent; this should never happen!");
            Err(Error::Canceled)
        },
    }).map(|response| response.map(Body::from_reader));

    Ok((CurlRequest(easy), future_rx))
}
/// Create a new curl request.
Create a new curl request.
Create a new curl request .
pub fn create<B: Into<Body>>(request: Request<B>, options: &Options) -> Result<(CurlRequest, impl Future<Output=Result<Response<Body>, Error>>), Error> {
    // Set up the plumbing...
    let (future_tx, future_rx) = oneshot::channel();
    let (request_parts, request_body) = request.into_parts();
    let mut easy = curl::easy::Easy2::new(CurlHandler {
        state: Arc::new(RequestState::new(options.clone())),
        future: Some(future_tx),
        request_body: request_body.into(),
        version: None,
        status_code: None,
        headers: http::HeaderMap::default(),
    });
    easy.verbose(log_enabled!(Level::Trace))?;
    easy.signal(false)?;
    easy.buffer_size(options.buffer_size)?;
    if let Some(timeout) = options.timeout {
        easy.timeout(timeout)?;
    }
    easy.connect_timeout(options.connect_timeout)?;
    easy.tcp_nodelay(options.tcp_nodelay)?;
    if let Some(interval) = options.tcp_keepalive {
        easy.tcp_keepalive(true)?;
        easy.tcp_keepintvl(interval)?;
    } else {
        easy.tcp_keepalive(false)?;
    }
    match options.redirect_policy {
        RedirectPolicy::None => {
            easy.follow_location(false)?;
        }
        RedirectPolicy::Follow => {
            easy.follow_location(true)?;
        }
        RedirectPolicy::Limit(max) => {
            easy.follow_location(true)?;
            easy.max_redirections(max)?;
        }
    }
    if let Some(limit) = options.max_upload_speed {
        easy.max_send_speed(limit)?;
    }
    if let Some(limit) = options.max_download_speed {
        easy.max_recv_speed(limit)?;
    }
    // Set a preferred HTTP version to negotiate.
    easy.http_version(match options.preferred_http_version {
        Some(http::Version::HTTP_10) => curl::easy::HttpVersion::V10,
        Some(http::Version::HTTP_11) => curl::easy::HttpVersion::V11,
        Some(http::Version::HTTP_2) => curl::easy::HttpVersion::V2,
        _ => curl::easy::HttpVersion::Any,
    })?;
    if let Some(ref proxy) = options.proxy {
        easy.proxy(&format!("{}", proxy))?;
    }
    if let Some(addrs) = &options.dns_servers {
        let dns_string = addrs.iter()
            .map(ToString::to_string)
            .collect::<Vec<_>>()
            .join(",");
        if let Err(e) = easy.dns_servers(&dns_string) {
            warn!("DNS servers could not be configured: {}", e);
        }
    }
    // Configure SSL options.
    if let Some(ciphers) = &options.ssl_ciphers {
        easy.ssl_cipher_list(&ciphers.join(":"))?;
    }
    if let Some(cert) = &options.ssl_client_certificate {
        easy.ssl_client_certificate(cert)?;
    }
    // Set the request data according to the request given.
    easy.custom_request(request_parts.method.as_str())?;
    easy.url(&request_parts.uri.to_string())?;
    let mut headers = curl::easy::List::new();
    for (name, value) in request_parts.headers.iter() {
        let header = format!("{}: {}", name.as_str(), value.to_str().unwrap());
        headers.append(&header)?;
    }
    easy.http_headers(headers)?;
    // Enable automatic response decompression.
    easy.accept_encoding("")?;
    // If the request body is non-empty, tell curl that we are going to upload something.
    if !easy.get_ref().request_body.is_empty() {
        easy.upload(true)?;
        if let Some(len) = easy.get_ref().request_body.len() {
            // If we know the size of the request body up front, tell curl about it.
            easy.in_filesize(len as u64)?;
        }
    }
    let future_rx = future_rx.then(|result| match result {
        Ok(result) => result,
        Err(_) => {
            error!("request canceled by agent; this should never happen!");
            Err(Error::Canceled)
        },
    }).map(|response| response.map(Body::from_reader));
    Ok((CurlRequest(easy), future_rx))
}
pub fn create < B : Into < Body > > ( request : Request < B > , options : & Options ) -> Result < ( CurlRequest , impl Future < Output = Result < Response < Body > , Error > > ) , Error > { // Set up the plumbing... let ( future_tx , future_rx ) = oneshot :: channel ( ) ; let ( request_parts , request_body ) = request . into_parts ( ) ; let mut easy = curl :: easy :: Easy2 :: new ( CurlHandler { state : Arc :: new ( RequestState :: new ( options . clone ( ) ) ) , future : Some ( future_tx ) , request_body : request_body . into ( ) , version : None , status_code : None , headers : http :: HeaderMap :: default ( ) , } ) ; easy . verbose ( log_enabled ! ( Level Trace ) ) ? ; easy . signal ( false ) ? ; easy . buffer_size ( options . buffer_size ) ? ; if let Some ( timeout ) = options . timeout { easy . timeout ( timeout ) ? ; } easy . connect_timeout ( options . connect_timeout ) ? ; easy . tcp_nodelay ( options . tcp_nodelay ) ? ; if let Some ( interval ) = options . tcp_keepalive { easy . tcp_keepalive ( true ) ? ; easy . tcp_keepintvl ( interval ) ? ; } else { easy . tcp_keepalive ( false ) ? ; } match options . redirect_policy { RedirectPolicy :: None => { easy . follow_location ( false ) ? ; } RedirectPolicy :: Follow => { easy . follow_location ( true ) ? ; } RedirectPolicy :: Limit ( max ) => { easy . follow_location ( true ) ? ; easy . max_redirections ( max ) ? ; } } if let Some ( limit ) = options . max_upload_speed { easy . max_send_speed ( limit ) ? ; } if let Some ( limit ) = options . max_download_speed { easy . max_recv_speed ( limit ) ? ; } // Set a preferred HTTP version to negotiate. easy . http_version ( match options . preferred_http_version { Some ( http :: Version :: HTTP_10 ) => curl :: easy :: HttpVersion :: V10 , Some ( http :: Version :: HTTP_11 ) => curl :: easy :: HttpVersion :: V11 , Some ( http :: Version :: HTTP_2 ) => curl :: easy :: HttpVersion :: V2 , _ => curl :: easy :: HttpVersion :: Any , } ) ? ; if let Some ( ref proxy ) = options . proxy { easy . proxy ( & format ! ( " " proxy ) ) ? ; } if let Some ( addrs ) = & options . dns_servers { let dns_string = addrs . iter ( ) . map ( ToString :: to_string ) . collect :: < Vec < _ > > ( ) . join ( " " ) ; if let Err ( e ) = easy . dns_servers ( & dns_string ) { warn ! ( " " e ) ; } } // Configure SSL options. if let Some ( ciphers ) = & options . ssl_ciphers { easy . ssl_cipher_list ( & ciphers . join ( " " ) ) ? ; } if let Some ( cert ) = & options . ssl_client_certificate { easy . ssl_client_certificate ( cert ) ? ; } // Set the request data according to the request given. easy . custom_request ( request_parts . method . as_str ( ) ) ? ; easy . url ( & request_parts . uri . to_string ( ) ) ? ; let mut headers = curl :: easy :: List :: new ( ) ; for ( name , value ) in request_parts . headers . iter ( ) { let header = format ! ( " " name as_str ( ) value to_str ( ) unwrap ( ) ) ; headers . append ( & header ) ? ; } easy . http_headers ( headers ) ? ; // Enable automatic response decompression. easy . accept_encoding ( " " ) ? ; // If the request body is non-empty, tell curl that we are going to upload something. if ! easy . get_ref ( ) . request_body . is_empty ( ) { easy . upload ( true ) ? ; if let Some ( len ) = easy . get_ref ( ) . request_body . len ( ) { // If we know the size of the request body up front, tell curl about it. easy . in_filesize ( len as u64 ) ? ; } } let future_rx = future_rx . then ( | result | match result { Ok ( result ) => result , Err ( _ ) => { error ! ( " " ) ; Err ( Error :: Canceled ) } , } ) . map ( | response | response . map ( Body :: from_reader ) ) ; Ok ( ( CurlRequest ( easy ) , future_rx ) ) }
Create a new curl request.
Create a new curl request .
request
Request<B>
options
&Options
request
Request<B>
options
&Options