# How to use a custom transporter for this http client?

**URL:** <https://users.rust-lang.org/t/how-to-use-a-custom-transporter-for-this-http-client/54758>\
**Category:** help\
**Created:** [January 28, 2021, 2:32am UTC](https://users.rust-lang.org/t/how-to-use-a-custom-transporter-for-this-http-client/54758 "2021-01-28T02:32:39Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![guerlando](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@guerlando](https://users.rust-lang.org/u/guerlando)\
**Post date:** [January 28, 2021, 2:32am UTC](https://users.rust-lang.org/t/how-to-use-a-custom-transporter-for-this-http-client/54758/1 "2021-01-28T02:32:40Z")

</div>

Hyper's http client can be passed a custom [hyper::client::connect::Connect - Rust](https://docs.rs/hyper/0.14.2/hyper/client/connect/trait.Connect.html) here:

`pub fn build<C, B>(&self, connector: C) -> Client<C, B> where C: Connect + Clone, B: HttpBody + Send, B::Data: Send, `

If we look at

```
impl<S, T> Connect for S where    

S: Service<Uri, Response = T> + Send + 'static,    

S::Error: Into<Box<dyn StdError + Send + Sync>>,    

S::Future: Unpin + Send,    

T: AsyncRead + AsyncWrite + Connection + Unpin + Send + 'static, 

```

I think `Service` is something that can be called and polled to get the request: [hyper::service::Service - Rust](https://docs.rs/hyper/0.14.2/hyper/service/trait.Service.html)

but what about `T`?

I'm confused, the `T` is the `B` in `pub fn build<C, B>(&self, connector: C) -> Client<C, B>`, right? But where is this `B` passed?

So how should I implement `Connect`?

---

<div class="post-metadata">

**Author:** ![guerlando](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@guerlando](https://users.rust-lang.org/u/guerlando)\
**Post date:** [January 28, 2021, 3:23am UTC](https://users.rust-lang.org/t/how-to-use-a-custom-transporter-for-this-http-client/54758/2 "2021-01-28T03:23:21Z")

</div>

I did

```
use hyper::http::{Request, Response, StatusCode};
use hyper::service::Service;
use core::task::{Context, Poll};
use core::future::Future;
use std::pin::Pin;

#[derive(Clone)]
pub struct CustomTransporter;

impl CustomTransporter {
    pub fn new() -> CustomTransporter {
        CustomTransporter{}
    }
}

impl Service<hyper::Uri> for CustomTransporter {
    type Response = Response<Vec<u8>>;
    type Error = hyper::http::Error;
    type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: hyper::Uri) -> Self::Future {
        // create the body
        let body: Vec<u8> = "hello, world!\n"
            .as_bytes()
            .to_owned();
        // Create the HTTP response
        let resp = Response::builder()
            .status(StatusCode::OK)
            .body(body)
            .expect("Unable to create `http::Response`");
         
        // create a response in a future.
        let fut = async {
            Ok(resp)
        };

        // Return the response as an immediate future
        Box::pin(fut)
    }
}

```

but then on

```
let connector = CustomTransporter::new();
let client = Client::builder().build(connector);

```

on the `build` call, I get

```
the trait bound `hyper::Response<std::vec::Vec<u8>>: tokio::io::AsyncRead` is not satisfied

the trait `tokio::io::AsyncRead` is not implemented for `hyper::Response<std::vec::Vec<u8>>`

note: required because of the requirements on the impl of `hyper::client::connect::Connect` for `custom_req::CustomTransporter`rustc(E0277)

```

---

<div class="post-metadata">

**Author:** ![guerlando](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@guerlando](https://users.rust-lang.org/u/guerlando)\
**Post date:** [January 28, 2021, 3:29am UTC](https://users.rust-lang.org/t/how-to-use-a-custom-transporter-for-this-http-client/54758/3 "2021-01-28T03:29:20Z")

</div>

but I think I should implement instead `Service<Request<Vec<u8>>>`

---

<div class="post-metadata">

**Author:** ![alice](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alice/32/9777_2.png) [@alice](https://users.rust-lang.org/u/alice)\
**Post date:** [January 28, 2021, 9:26am UTC](https://users.rust-lang.org/t/how-to-use-a-custom-transporter-for-this-http-client/54758/4 "2021-01-28T09:26:41Z")

</div>

The type of `CustomTransporter::Response` needs to be something that implements the `AsyncRead` trait.

If you just want to return a single vector, you can wrap the vector in an `std::io::Cursor`, which implements the required trait.

Note that when posting error messages, please run `cargo build` to get the full error message rather than the IDE popups.

---

<div class="post-metadata">

**Author:** ![guerlando](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@guerlando](https://users.rust-lang.org/u/guerlando)\
**Post date:** [February 1, 2021, 4:14am UTC](https://users.rust-lang.org/t/how-to-use-a-custom-transporter-for-this-http-client/54758/5 "2021-02-01T04:14:38Z")

</div>

Here's my custom transporter with a `Response` of type `std::io::Cursor`

```
use hyper::http::{Request, Response, StatusCode};
use hyper::service::Service;
use hyper::client::connect::Connect;
use core::task::{Context, Poll};
use core::future::Future;
use std::pin::Pin;
use std::io::Cursor;

#[derive(Clone)]
pub struct CustomTransporter;

impl CustomTransporter {
    pub fn new() -> CustomTransporter {
        CustomTransporter{}
    }
}

impl Service<hyper::Uri> for CustomTransporter {
    type Response = Cursor<std::vec::Vec<u8>>;
    type Error = hyper::http::Error;
    type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: hyper::Uri) -> Self::Future {
        // create the body
        let body: Vec<u8> = "hello, world!\n"
            .as_bytes()
            .to_owned();
        // Create the HTTP response
        let resp = Cursor::new(body.to_vec());
         
        // create a response in a future.
        let fut = async {
            Ok(resp)
        };

        // Return the response as an immediate future
        Box::pin(fut)
    }
}

```

Then I call like this:

```
let connector = CustomTransporter::new();
let client = Client::builder().build(connector);
let mut res = client.get(url).await?;

```

and I get

```
error[E0277]: the trait bound `std::io::Cursor<Vec<u8>>: hyper::client::connect::Connection` is not satisfied
  --> src/main.rs:42:36
   |
42 | let client = Client::builder().build(connector);
   | ^^^^^ the trait `hyper::client::connect::Connection` is not implemented for `std::io::Cursor<Vec<u8>>`
   |
   = note: required because of the requirements on the impl of `hyper::client::connect::Connect` for `CustomTransporter`

```

By looking at the Connect trait, [hyper::client::connect::Connect - Rust](https://docs.rs/hyper/0.14.2/hyper/client/connect/trait.Connect.html), I'm confused as to what is needed.

I tried doing simply

```
impl Connect for CustomTransporter {
    
}

```

but then I get a conflict with the other `impl`

---

<div class="post-metadata">

**Author:** ![guerlando](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@guerlando](https://users.rust-lang.org/u/guerlando)\
**Post date:** [February 2, 2021, 1:42am UTC](https://users.rust-lang.org/t/how-to-use-a-custom-transporter-for-this-http-client/54758/6 "2021-02-02T01:42:02Z")

</div>

I implemented `Connection` for `CustomTransporter`, like this:

```
impl Connection for CustomTransporter {
    fn connected(&self) -> Connected {
        Connected::new()
    }
}

```

but then I get:

```
error[E0277]: the trait bound `std::io::Cursor<Vec<u8>>: hyper::client::connect::Connection` is not satisfied
  --> src/main.rs:42:36
   |
42 | let client = Client::builder().build(connector);
   | ^^^^^ the trait `hyper::client::connect::Connection` is not implemented for `std::io::Cursor<Vec<u8>>`

```

and I cannot implement `Connection` for `Cursor<Vec<u8>>`

---

<div class="post-metadata">

**Author:** ![alice](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alice/32/9777_2.png) [@alice](https://users.rust-lang.org/u/alice)\
**Post date:** [February 2, 2021, 8:43am UTC](https://users.rust-lang.org/t/how-to-use-a-custom-transporter-for-this-http-client/54758/7 "2021-02-02T08:43:21Z")

</div>

You can write your own wrapper struct around a `Cursor<Vec<u8>>`, the implement `Connection` for your custom struct.

---

<div class="post-metadata">

**Author:** ![guerlando](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@guerlando](https://users.rust-lang.org/u/guerlando)\
**Post date:** [February 2, 2021, 10:38pm UTC](https://users.rust-lang.org/t/how-to-use-a-custom-transporter-for-this-http-client/54758/8 "2021-02-02T22:38:50Z")

</div>

Nice. I just did that, take a look please:

```
use hyper::http::{Request, Response, StatusCode};
use hyper::service::Service;
use hyper::client::connect::Connect;
use core::task::{Context, Poll};
use core::future::Future;
use std::pin::Pin;
use std::io::Cursor;
use hyper::client::connect::{Connection, Connected};
use tokio::io::{AsyncRead, AsyncWrite};

#[derive(Clone)]
pub struct CustomTransporter;

unsafe impl Send for CustomTransporter {}

impl CustomTransporter {
    pub fn new() -> CustomTransporter {
        CustomTransporter{}
    }
}

impl Connection for CustomTransporter {
    fn connected(&self) -> Connected {
        Connected::new()
    }
}

struct CustomResponse {
    w: Cursor<Vec<u8>>
}

impl Connection for CustomResponse {
    fn connected(&self) -> Connected {
        Connected::new()
    }
}

impl AsyncRead for CustomResponse {
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut tokio::io::ReadBuf<'_>
    ) -> Poll<std::io::Result<()>> {
        self.w.poll_read(cx, buf)
    }
}

impl AsyncWrite for CustomResponse {
    fn poll_write(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &[u8]
    ) -> Poll<Result<usize, std::io::Error>>{
        let v = vec!();
        self.w.poll_write(cf, buf);
    }
    fn poll_flush(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>
    ) -> Poll<Result<(), std::io::Error>> {
        self.w.poll_flush(cx)
    }

    fn poll_shutdown(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>
    ) -> Poll<Result<(), std::io::Error>>
    {
        self.w.poll_shutdown(cx)
    }
}

impl Service<hyper::Uri> for CustomTransporter {
    type Response = CustomResponse;
    type Error = hyper::http::Error;
    type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: hyper::Uri) -> Self::Future {
        // create the body
        let body: Vec<u8> = "hello, world!\n"
            .as_bytes()
            .to_owned();
        // Create the HTTP response
        let resp = CustomResponse{
            w: Cursor::new(body.to_vec())
        };
         
        // create a response in a future.
        let fut = async {
            Ok(resp)
        };

        // Return the response as an immediate future
        Box::pin(fut)
    }
}

```

it asked me for `AsyncWrite` also. I don't understand why the `Response` must implement `AsyncWrite` since it's supposed to be read, not written to. Do you have an idea?

Anyways, I get problems on forwarding all these calls to `std::Cursor`:

```
error[E0599]: no method named `poll_flush` found for struct `std::io::Cursor<Vec<u8>>` in the current scope
  --> src/custom_req.rs:61:16
   |
61 | self.w.poll_flush(cx)
   | ^^^^^^^^^^ method not found in `std::io::Cursor<Vec<u8>>`

error[E0599]: no method named `poll_shutdown` found for struct `std::io::Cursor<Vec<u8>>` in the current scope
  --> src/custom_req.rs:69:16
   |
69 | self.w.poll_shutdown(cx)
   | ^^^^^^^^^^^^^ method not found in `std::io::Cursor<Vec<u8>>`

```

Why I cannot call these functions, if `std::Cursor` does implement them?

---

<div class="post-metadata">

**Author:** ![alice](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alice/32/9777_2.png) [@alice](https://users.rust-lang.org/u/alice)\
**Post date:** [February 3, 2021, 6:22am UTC](https://users.rust-lang.org/t/how-to-use-a-custom-transporter-for-this-http-client/54758/9 "2021-02-03T06:22:22Z")

</div>

To call them, you need to do `Pin::new(&mut self.w).poll_flush(cx)`

---

<div class="post-metadata">

**Author:** ![guerlando](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@guerlando](https://users.rust-lang.org/u/guerlando)\
**Post date:** [February 4, 2021, 2:19am UTC](https://users.rust-lang.org/t/how-to-use-a-custom-transporter-for-this-http-client/54758/10 "2021-02-04T02:19:55Z")

</div>

> [@alice](#):
>
> `Pin::new(&mut self.w)`

```
use hyper::http::{Request, Response, StatusCode};
use hyper::service::Service;
use hyper::client::connect::Connect;
use core::task::{Context, Poll};
use core::future::Future;
use std::pin::Pin;
use std::io::Cursor;
use hyper::client::connect::{Connection, Connected};
use tokio::io::{AsyncRead, AsyncWrite};

#[derive(Clone)]
pub struct CustomTransporter;

unsafe impl Send for CustomTransporter {}

impl CustomTransporter {
    pub fn new() -> CustomTransporter {
        CustomTransporter{}
    }
}

impl Connection for CustomTransporter {
    fn connected(&self) -> Connected {
        Connected::new()
    }
}

struct CustomResponse {
    w: Cursor<Vec<u8>>
}

unsafe impl Send for CustomResponse {
    
}

impl Connection for CustomResponse {
    fn connected(&self) -> Connected {
        Connected::new()
    }
}

impl AsyncRead for CustomResponse {
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut tokio::io::ReadBuf<'_>
    ) -> Poll<std::io::Result<()>> {
        Pin::new(&mut self.w).poll_read(cx, buf)
    }
}

impl AsyncWrite for CustomResponse {
    fn poll_write(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &[u8]
    ) -> Poll<Result<usize, std::io::Error>>{
        let v = vec!();
        Pin::new(&mut self.w).poll_write(cx, buf)
    }
    fn poll_flush(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>
    ) -> Poll<Result<(), std::io::Error>> {
        Pin::new(&mut self.w).poll_flush(cx)
    }

    fn poll_shutdown(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>
    ) -> Poll<Result<(), std::io::Error>>
    {
        Pin::new(&mut self.w).poll_shutdown(cx)
    }
}

impl Service<hyper::Uri> for CustomTransporter {
    type Response = CustomResponse;
    type Error = hyper::http::Error;
    type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: hyper::Uri) -> Self::Future {
        // create the body
        let body: Vec<u8> = "hello, world!\n"
            .as_bytes()
            .to_owned();
        // Create the HTTP response
        let resp = CustomResponse{
            w: Cursor::new(body.to_vec())
        };
         
        // create a response in a future.
        let fut = async {
            Ok(resp)
        };

        // Return the response as an immediate future
        Box::pin(fut)
    }
}

```

I get

```
error[E0277]: `(dyn Future<Output = std::result::Result<CustomResponse, hyper::http::Error>> + 'static)` cannot be sent between threads safely
  --> src/main.rs:42:36
   |
42 | let client = Client::builder().build(connector);
   | ^^^^^ `(dyn Future<Output = std::result::Result<CustomResponse, hyper::http::Error>> + 'static)` cannot be sent between threads safely
   |

```

I cannot simply implement `Send` to `Future`, and I cannot change `Future` by a wrapper.

---

<div class="post-metadata">

**Author:** ![guerlando](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@guerlando](https://users.rust-lang.org/u/guerlando)\
**Post date:** [February 4, 2021, 4:40am UTC](https://users.rust-lang.org/t/how-to-use-a-custom-transporter-for-this-http-client/54758/11 "2021-02-04T04:40:10Z")

</div>

Here's the project if anyone has interest in building to try [https://github.com/lzunsec/rust\_hyper\_custom\_transporter/tree/33e67c5db2db1462580a072893345027b634fe5a](https://github.com/lzunsec/rust_hyper_custom_transporter/tree/33e67c5db2db1462580a072893345027b634fe5a)

---

<div class="post-metadata">

**Author:** ![alice](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alice/32/9777_2.png) [@alice](https://users.rust-lang.org/u/alice)\
**Post date:** [February 4, 2021, 9:37am UTC](https://users.rust-lang.org/t/how-to-use-a-custom-transporter-for-this-http-client/54758/12 "2021-02-04T09:37:34Z")

</div>

Use an `dyn Future<...> + Send` instead, or the `BoxFuture` alias in the futures crate.

---

<div class="post-metadata">

**Author:** ![guerlando](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@guerlando](https://users.rust-lang.org/u/guerlando)\
**Post date:** [February 5, 2021, 4:44pm UTC](https://users.rust-lang.org/t/how-to-use-a-custom-transporter-for-this-http-client/54758/13 "2021-02-05T16:44:50Z")

</div>

thanks, after doing that and some other corrections, I got it to compile:

```
use hyper::service::Service;
use core::task::{Context, Poll};
use core::future::Future;
use std::pin::Pin;
use std::io::Cursor;
use hyper::client::connect::{Connection, Connected};
use tokio::io::{AsyncRead, AsyncWrite};

#[derive(Clone)]
pub struct CustomTransporter;

unsafe impl Send for CustomTransporter {}

impl CustomTransporter {
    pub fn new() -> CustomTransporter {
        CustomTransporter{}
    }
}

impl Connection for CustomTransporter {
    fn connected(&self) -> Connected {
        Connected::new()
    }
}

pub struct CustomResponse {
    w: Cursor<Vec<u8>>
}

unsafe impl Send for CustomResponse {
    
}

impl Connection for CustomResponse {
    fn connected(&self) -> Connected {
        println!("connected");
        Connected::new()
    }
}

impl AsyncRead for CustomResponse {
    fn poll_read(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut tokio::io::ReadBuf<'_>
    ) -> Poll<std::io::Result<()>> {
        println!("poll_read");
        Pin::new(&mut self.w).poll_read(cx, buf)
    }
}

impl AsyncWrite for CustomResponse {
    fn poll_write(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &[u8]
    ) -> Poll<Result<usize, std::io::Error>>{
        //let v = vec!();
        Pin::new(&mut self.w).poll_write(cx, buf)
    }
    fn poll_flush(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>
    ) -> Poll<Result<(), std::io::Error>> {
        Pin::new(&mut self.w).poll_flush(cx)
    }

    fn poll_shutdown(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>
    ) -> Poll<Result<(), std::io::Error>>
    {
        Pin::new(&mut self.w).poll_shutdown(cx)
    }
}

impl Service<hyper::Uri> for CustomTransporter {
    type Response = CustomResponse;
    type Error = hyper::http::Error;
    type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        println!("poll_ready");
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: hyper::Uri) -> Self::Future {
        println!("call");
        // create the body
        let body: Vec<u8> = "hello, world!\n"
            .as_bytes()
            .to_owned();
        // Create the HTTP response
        let resp = CustomResponse{
            w: Cursor::new(body)
        };
         
        // create a response in a future.
        let fut = async {
            Ok(resp)
        };
        println!("gonna return from call");
        // Return the response as an immediate future
        Box::pin(fut)
    }
}

```

If someone knows how to make the fake request work, it'd be awesome. I don't see anything I've done wrong

[https://github.com/lzunsec/rust\_hyper\_custom\_transporter/tree/334d0f160255082e40c76e7b96d11ef1d006bce1](https://github.com/lzunsec/rust_hyper_custom_transporter/tree/334d0f160255082e40c76e7b96d11ef1d006bce1)

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [May 6, 2021, 4:45pm UTC](https://users.rust-lang.org/t/how-to-use-a-custom-transporter-for-this-http-client/54758/14 "2021-05-06T16:45:12Z")

</div>

Closed on May 6, 2021, 4:45pm UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
