http_pool/http1/
sender.rs1use crate::body::VariantBody;
2use hyper::client::conn::http1;
3use hyper::http::uri::InvalidUri;
4use hyper::{Method, Request, Uri, Version, http};
5use std::fmt::Debug;
6use std::ops::{Deref, DerefMut};
7use std::sync::Arc;
8use crate::utils::RefCount;
9
10pub type SendRequest = http1::SendRequest<VariantBody>;
11
12impl RefCount for Arc<SendRequest> {
13 fn ref_count(&self) -> usize {
14 Arc::strong_count(&self)
15 }
16}
17
18#[derive(Debug)]
19pub struct Sender {
20 sender: Arc<SendRequest>,
21 base_url: String,
22}
23
24impl Sender {
25 pub fn new(sender: Arc<SendRequest>, base_url: String) -> Self {
26 Sender { sender, base_url }
27 }
28
29 pub fn base_url(&self) -> &String {
30 &self.base_url
31 }
32
33 pub fn new_uri(&self, uri: &Uri) -> Result<Uri, InvalidUri> {
34 crate::utils::new_uri(self.base_url.clone(), uri)
35 }
36
37 fn sender(&self) -> &SendRequest {
38 &self.sender
39 }
40
41 fn sender_mut(&mut self) -> &mut SendRequest {
42 unsafe { &mut *(Arc::as_ptr(&self.sender) as *mut _) }
44 }
45}
46
47impl Deref for Sender {
48 type Target = SendRequest;
49
50 fn deref(&self) -> &Self::Target {
51 self.sender()
52 }
53}
54
55impl DerefMut for Sender {
56 fn deref_mut(&mut self) -> &mut Self::Target {
57 self.sender_mut()
58 }
59}
60
61pub fn request_builder<T>(uri: T, method: Method) -> http::request::Builder
63where
64 T: TryInto<Uri>,
65 <T as TryInto<Uri>>::Error: Into<http::Error>,
66{
67 let uri = TryInto::<Uri>::try_into(uri).unwrap_or(Uri::from_static("/"));
68 let host = uri.host().unwrap_or("").to_string();
69
70 Request::builder()
71 .version(Version::HTTP_11)
72 .method(method)
73 .uri(uri)
74 .header(hyper::header::USER_AGENT, "proxy/0.1")
75 .header(hyper::header::HOST, host)
76}