http_pool/http2/
sender.rs

1use crate::body::VariantBody;
2use hyper::body::Incoming;
3use hyper::client::conn::{TrySendError, http2};
4use hyper::http::uri::InvalidUri;
5use hyper::{Method, Request, Response, Uri, Version, http};
6use std::fmt::Debug;
7use std::sync::Arc;
8
9pub type SendRequest = http2::SendRequest<VariantBody>;
10
11#[derive(Debug)]
12pub struct Sender {
13    sender: SendRequest,
14    base_url: String,
15    _ref_cnt: Arc<()>,
16}
17
18impl Sender {
19    pub(crate) fn new(sender: SendRequest, base_url: String, ref_cnt: Arc<()>) -> Self {
20        Sender {
21            sender,
22            base_url,
23            _ref_cnt: ref_cnt,
24        }
25    }
26
27    pub fn base_url(&self) -> &String {
28        &self.base_url
29    }
30
31    pub fn new_uri(&self, uri: &Uri) -> Result<Uri, InvalidUri> {
32        crate::utils::new_uri(self.base_url.clone(), uri)
33    }
34
35    pub fn is_ready(&self) -> bool {
36        self.sender.is_ready()
37    }
38
39    pub fn is_closed(&self) -> bool {
40        self.sender.is_closed()
41    }
42
43    pub fn send_request(
44        &mut self,
45        req: Request<VariantBody>,
46    ) -> impl Future<Output = hyper::Result<Response<Incoming>>> {
47        self.sender.send_request(req)
48    }
49
50    pub fn try_send_request(
51        &mut self,
52        req: Request<VariantBody>,
53    ) -> impl Future<Output = Result<Response<Incoming>, TrySendError<Request<VariantBody>>>> {
54        self.sender.try_send_request(req)
55    }
56}
57
58pub fn request_builder<T>(uri: T, method: Method) -> http::request::Builder
59where
60    T: TryInto<Uri>,
61    <T as TryInto<Uri>>::Error: Into<http::Error>,
62{
63    Request::builder()
64        .version(Version::HTTP_2)
65        .method(method)
66        .uri(uri)
67        .header(hyper::header::USER_AGENT, "proxy/0.1")
68}