rust_tls_client/
request.rs1use std::collections::HashMap;
2use reqwest::header::HeaderMap;
3use serde::{Deserialize, Serialize};
4use crate::cffi::{SHARED_METHODS};
5use crate::client::{CustomClient};
6use crate::error::TlsClientError;
7use crate::response::RequestResponse;
8use crate::types::ClientIdentifier;
9
10#[derive(Debug, Default, Deserialize, Serialize)]
11#[serde(rename_all = "camelCase")]
12pub struct RequestPayload {
13 catch_panics: Option<bool>,
14 certificate_pinning_hosts: Option<HashMap<String, String>>,
15 custom_tls_client: Option<CustomClient>,
16 transport_options: Option<TransportOptions>,
17 follow_redirects: Option<bool>,
18 force_http_1: Option<bool>,
19 header_order: Option<Vec<String>>,
20 headers: Option<HashMap<String, String>>,
21 insecure_skip_verify: Option<bool>,
22 is_byte_request: Option<bool>,
23 is_byte_response: Option<bool>,
24 is_rotating_proxy: Option<bool>,
25 proxy_url: Option<String>,
26 request_body: Option<String>,
27 request_cookies: Option<Vec<String>>,
28 default_headers: Option<HashMap<String, Vec<String>>>,
29 request_method: Option<String>,
30 request_url: Option<String>,
31 #[serde(rename = "disableIPV6")]
32 disable_ipv6: Option<bool>,
33 local_address: Option<String>,
34 session_id: Option<String>,
35 server_name_overwrite: Option<String>,
36 stream_output_block_size: Option<u32>, stream_output_e_o_f_symbol: Option<String>, stream_output_path: Option<String>, timeout_milliseconds: Option<u32>,
40 timeout_seconds: Option<u32>,
41 tls_client_identifier: Option<ClientIdentifier>,
42 with_debug: Option<bool>,
43 with_default_cookie_jar: Option<bool>,
44 without_cookie_jar: Option<bool>,
45 #[serde(rename = "withRandomTLSExtensionOrder")]
46 with_random_tls_extension_order: Option<bool>,
47}
48
49
50impl RequestPayload {
51 fn add_header(&mut self, key: &str, value: &str) {
52 if let Some(headers) = &mut self.headers {
53 headers.insert(key.to_string(), value.to_string());
54 } else {
55 let mut h = HashMap::new();
56 h.insert(key.to_string(), value.to_string());
57 self.headers = Some(h);
58 }
59 }
60
61 pub fn byte_response(&mut self) -> &mut Self {
62 self.is_byte_response = Some(true);
63 self
64 }
65
66 pub fn byte_request(&mut self) -> &mut Self {
67 self.is_byte_request = Some(true);
68 self
69 }
70
71 pub fn headers(&mut self, header_map: HeaderMap) -> &mut Self {
72 let mut h = HashMap::new();
73 for (key, value) in header_map.iter() {
74 h.insert(key.as_str().to_string(), value.to_str().unwrap().to_string());
75 }
76 self.headers = Some(h);
77 self
78 }
79
80 pub fn body<B: Into<String>>(&mut self, body: B) -> &mut Self {
81 self.request_body = Some(body.into());
82 self
83 }
84
85 pub fn json(&mut self, json: serde_json::Value) -> &mut Self {
86 self.add_header("Content-Type", "application/json");
87 self.request_body = Some(serde_json::to_string(&json).unwrap());
88 self
89 }
90
91 pub fn send(&self) -> Result<RequestResponse, TlsClientError> {
92 Ok(SHARED_METHODS.request(self))
93 }
94}
95
96#[derive(Debug, Default, Deserialize, Serialize)]
97pub struct TransportOptions {
98 disable_keep_alives: bool,
99 disable_compression: bool,
100 max_idle_conns: u32,
101 max_idle_conns_per_host: u32,
102 max_conns_per_host: u32,
103 max_response_header_bytes: u32,
104 write_buffer_size: u32,
105 read_buffer_size: u32,
106 idle_conn_timeout: u32,
107}
108
109
110