| 1 |
use crate::{Requester, RequesterBuilder}; |
| 2 |
use async_trait::async_trait; |
| 3 |
use hyper::client::HttpConnector; |
| 4 |
use hyper::{Body, Method, Request}; |
| 5 |
use hyper_rustls::builderstates::WantsProtocols2; |
| 6 |
use hyper_rustls::{HttpsConnector, HttpsConnectorBuilder}; |
| 7 |
use std::collections::HashMap; |
| 8 |
|
| 9 |
#[derive(Clone)] |
| 10 |
#[deprecated(note = "Use ReqwestRequester instead")] |
| 11 |
pub struct HyperRustlsRequester(hyper::Client<HttpsConnector<HttpConnector>, Body>); |
| 12 |
|
| 13 |
#[cfg_attr(feature = "rt-multi-thread", async_trait)] |
| 14 |
#[cfg_attr(not(feature = "rt-multi-thread"), async_trait(?Send))] |
| 15 |
impl Requester for HyperRustlsRequester { |
| 16 |
type Error = HyperRustlsRequesterError; |
| 17 |
|
| 18 |
async fn get( |
| 19 |
&self, |
| 20 |
url: &url::Url, |
| 21 |
body: &[u8], |
| 22 |
headers: HashMap<&str, &str>, |
| 23 |
) -> Result<Vec<u8>, Self::Error> { |
| 24 |
self.request(Method::GET, url, body, headers).await |
| 25 |
} |
| 26 |
|
| 27 |
async fn post( |
| 28 |
&self, |
| 29 |
url: &url::Url, |
| 30 |
body: &[u8], |
| 31 |
headers: HashMap<&str, &str>, |
| 32 |
) -> Result<Vec<u8>, Self::Error> { |
| 33 |
self.request(Method::POST, url, body, headers).await |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
#[deprecated(note = "Use ReqwestRequesterBuilder instead")] |
| 38 |
pub struct HyperRustlsRequesterBuilder( |
| 39 |
hyper::client::Builder, |
| 40 |
HttpsConnectorBuilder<WantsProtocols2>, |
| 41 |
); |
| 42 |
|
| 43 |
impl RequesterBuilder for HyperRustlsRequesterBuilder { |
| 44 |
type Requester = HyperRustlsRequester; |
| 45 |
|
| 46 |
fn with_pem_bundle(mut self, bytes: &[u8]) -> Self { |
| 47 |
let mut bytes = &bytes[..]; |
| 48 |
|
| 49 |
self.1 = HttpsConnectorBuilder::new() |
| 50 |
.with_tls_config({ |
| 51 |
let mut store = rustls::RootCertStore::empty(); |
| 52 |
let certs = rustls_pemfile::certs(&mut bytes); |
| 53 |
for cert in certs.into_iter() { |
| 54 |
if let Ok(cert) = cert { |
| 55 |
if let Err(e) = store.add(&rustls::Certificate(cert.to_vec())) { |
| 56 |
log::error!("Failed to add certificate: {}", e); |
| 57 |
} |
| 58 |
} else { |
| 59 |
log::error!("Failed to read certificate"); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
rustls::ClientConfig::builder() |
| 64 |
.with_safe_defaults() |
| 65 |
.with_root_certificates(store) |
| 66 |
.with_no_client_auth() |
| 67 |
}) |
| 68 |
.https_or_http() |
| 69 |
.enable_http1(); |
| 70 |
self |
| 71 |
} |
| 72 |
|
| 73 |
fn build(self) -> Self::Requester { |
| 74 |
let https = self.1.build(); |
| 75 |
let client = self.0.build(https); |
| 76 |
HyperRustlsRequester(client) |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
impl Default for HyperRustlsRequesterBuilder { |
| 81 |
fn default() -> Self { |
| 82 |
Self( |
| 83 |
hyper::Client::builder(), |
| 84 |
HttpsConnectorBuilder::new() |
| 85 |
.with_native_roots() |
| 86 |
.https_or_http() |
| 87 |
.enable_http1(), |
| 88 |
) |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
impl HyperRustlsRequester { |
| 93 |
pub fn new() -> Self { |
| 94 |
let https = HttpsConnectorBuilder::new() |
| 95 |
.with_native_roots() |
| 96 |
.https_or_http() |
| 97 |
.enable_http1() |
| 98 |
.build(); |
| 99 |
let client = hyper::Client::builder().build(https); |
| 100 |
Self(client) |
| 101 |
} |
| 102 |
|
| 103 |
async fn request( |
| 104 |
&self, |
| 105 |
method: hyper::Method, |
| 106 |
url: &url::Url, |
| 107 |
body: &[u8], |
| 108 |
headers: HashMap<&str, &str>, |
| 109 |
) -> Result<Vec<u8>, HyperRustlsRequesterError> { |
| 110 |
log::trace!( |
| 111 |
"{}: URL: {}, Body: {}, Headers: {:?}", |
| 112 |
method, |
| 113 |
url, |
| 114 |
String::from_utf8_lossy(body), |
| 115 |
headers |
| 116 |
); |
| 117 |
let body = body.to_vec(); |
| 118 |
let mut req = Request::builder().method(method).uri(url.as_str()); |
| 119 |
|
| 120 |
for (k, v) in headers { |
| 121 |
req = req.header(k, v); |
| 122 |
} |
| 123 |
|
| 124 |
let req = req.body(Body::from(body)).unwrap(); |
| 125 |
|
| 126 |
let (parts, resp_body) = self.0.request(req).await?.into_parts(); |
| 127 |
let bytes = hyper::body::to_bytes(resp_body).await?; |
| 128 |
|
| 129 |
if parts.status.is_success() { |
| 130 |
Ok(bytes.to_vec()) |
| 131 |
} else { |
| 132 |
Err(HyperRustlsRequesterError::NoSuccessStatusCode( |
| 133 |
parts.status.as_u16(), |
| 134 |
parts.status.canonical_reason(), |
| 135 |
bytes.to_vec(), |
| 136 |
)) |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
impl Default for HyperRustlsRequester { |
| 142 |
fn default() -> Self { |
| 143 |
Self::new() |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
#[derive(Debug)] |
| 148 |
pub enum HyperRustlsRequesterError { |
| 149 |
|
| 150 |
Hyper(hyper::Error), |
| 151 |
|
| 152 |
|
| 153 |
NoSuccessStatusCode(u16, Option<&'static str>, Vec<u8>), |
| 154 |
} |
| 155 |
|
| 156 |
impl std::fmt::Display for HyperRustlsRequesterError { |
| 157 |
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { |
| 158 |
match self { |
| 159 |
Self::Hyper(e) => write!(fmt, "hyper error: {}", e), |
| 160 |
Self::NoSuccessStatusCode(code, Some(reason), _) => { |
| 161 |
write!(fmt, "unsuccessful status code {} ({})", code, reason) |
| 162 |
} |
| 163 |
Self::NoSuccessStatusCode(code, None, _) => { |
| 164 |
write!(fmt, "unsuccessful status code {}", code) |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
impl std::error::Error for HyperRustlsRequesterError {} |
| 171 |
|
| 172 |
impl From<hyper::Error> for HyperRustlsRequesterError { |
| 173 |
fn from(e: hyper::Error) -> HyperRustlsRequesterError { |
| 174 |
Self::Hyper(e) |
| 175 |
} |
| 176 |
} |