1use std::collections::HashMap;
2use rustls::{ClientConnection, StreamOwned};
3use std::net::{IpAddr, TcpStream};
4use std::{fs, thread};
5use std::io::{Read};
6use std::path::Path;
7use std::str::FromStr;
8use std::sync::Arc;
9use std::time::Duration;
10use json::{JsonValue};
11use log::{debug};
12use rand::distr::Alphanumeric;
13use rand::Rng;
14use rustls::RootCertStore;
15use crate::{Method, Protocol};
16use crate::client_response::ClientResponse;
17use crate::stream::ClientStream;
18use crate::url::Url;
19
20pub struct Client {
21 debug: bool,
22 url: String,
23 protocol: Protocol,
24 method: Method,
25 query: HashMap<String, String>,
26 headers: HashMap<String, JsonValue>,
27 cookies: HashMap<String, String>,
28 body: Vec<u8>,
29 read_timeout: u64,
30 write_timeout: u64,
31 auto_location: bool,
32 boundary: String,
33 retry: usize,
35 ca_pem: String,
36 pkcs12: String,
37 pkcs12_password: String,
38}
39impl Default for Client {
40 fn default() -> Self {
41 Self::new()
42 }
43}
44
45impl Client {
46 pub fn new() -> Self {
47 Self { debug: false, url: "".to_string(), protocol: Protocol::HTTP1_1, method: Method::Other("".to_string()), query: HashMap::new(), headers: Default::default(), cookies: Default::default(), body: vec![], read_timeout: 10, write_timeout: 10, auto_location: false, boundary: "".to_string(), retry: 0, ca_pem: "".to_string(), pkcs12: "".to_string(), pkcs12_password: "".to_string() }
48 }
49 pub fn reset(&mut self) -> &mut Self {
51 self.debug = false;
52 self.url = "".to_string();
53 self.protocol = Protocol::HTTP1_1;
54 self.method = Method::Other("".to_string());
55 self.query = HashMap::new();
56 self.headers = Default::default();
57 self.cookies = Default::default();
58 self.body = vec![];
59 self.read_timeout = 0;
60 self.write_timeout = 0;
61 self.auto_location = false;
62 self.boundary = "".to_string();
63 self.retry = 0;
64 self
65 }
66 pub fn debug(&mut self) -> &mut Self {
67 self.debug = true;
68 self
69 }
70 pub fn ca_pem(&mut self, ca_pem: &str) -> &mut Self {
71 self.ca_pem = ca_pem.to_string();
72 self
73 }
74 pub fn pkcs12(&mut self, pkcs12: &str, password: &str) -> &mut Self {
75 self.pkcs12 = pkcs12.to_string();
76 self.pkcs12_password = password.to_string();
77 self
78 }
79 pub fn http10(&mut self) -> &mut Self {
80 self.protocol = Protocol::HTTP1_0;
81 self
82 }
83 pub fn http11(&mut self) -> &mut Self {
84 self.protocol = Protocol::HTTP1_1;
85 self
86 }
87 pub fn http2(&mut self) -> &mut Self {
88 self.protocol = Protocol::HTTP2;
89 self
90 }
91 pub fn retry(&mut self, count: usize) -> &mut Self {
93 self.retry = count;
94 self
95 }
96 pub fn auto_location(&mut self) -> &mut Self {
97 self.auto_location = true;
98 self
99 }
100 fn generate_boundary(&mut self) -> String {
101 let rand: String = rand::rng().sample_iter(&Alphanumeric).take(16).map(char::from).collect();
102 format!("----WebKitFormBoundary{}", rand)
103 }
104 pub fn get(&mut self, url: &str) -> &mut Self {
105 self.url = url.to_string();
106 self.method = Method::GET;
107 self
108 }
109 pub fn post(&mut self, url: &str) -> &mut Self {
110 self.url = url.to_string();
111 self.method = Method::POST;
112 self
113 }
114 pub fn head(&mut self, url: &str) -> &mut Self {
115 self.url = url.to_string();
116 self.method = Method::HEAD;
117 self
118 }
119 pub fn delete(&mut self, url: &str) -> &mut Self {
120 self.url = url.to_string();
121 self.method = Method::DELETE;
122 self
123 }
124 pub fn method(&mut self, method: &str, url: &str) -> &mut Self {
125 self.url = url.to_string();
126 self.method = Method::Other(method.to_string());
127 self
128 }
129 pub fn header(&mut self, key: &str, value: JsonValue) -> &mut Self {
130 self.headers.insert(key.to_string(), value);
131 self
132 }
133 pub fn query(&mut self, key: &str, value: JsonValue) -> &mut Self {
134 self.query.insert(key.to_string(), value.to_string());
135 self
136 }
137 pub fn raw_json(&mut self, data: JsonValue) -> &mut Self {
139 self.header("Content-Type", "application/json".into());
140 self.body = data.to_string().as_bytes().to_vec();
141 self.header("Content-Length", self.body.len().into());
142 self
143 }
144 pub fn form_data(&mut self, data: JsonValue) -> &mut Self {
146 self.boundary = self.generate_boundary();
147 self.header("Content-Type", format!("multipart/form-data; boundary={}", self.boundary).into());
148 let mut body = vec![];
149 for (key, value) in data.entries() {
150 let file = Path::new(value.as_str().unwrap());
151 if file.is_file() {
152 let mut t = vec![];
153 let r = fs::File::open(file).unwrap().read(&mut t).unwrap();
154 let filename = file.file_name().unwrap().to_str().unwrap().to_string();
155 let content_type = if let Some(kind) = infer::get(&t[..r]) {
156 kind.mime_type()
157 } else {
158 "application/octet-stream"
159 };
160 body.extend(format!("--{}\r\n", self.boundary).as_bytes().to_vec());
161 body.extend(format!(r#"Content-Disposition: form-data; name="{}"; filename="{}"{}"#, key, filename, "\r\n").as_bytes().to_vec());
162 body.extend(format!(r#"Content-Type: {}{}"#, content_type, "\r\n\r\n").as_bytes().to_vec());
163 body.extend(&t[..r]);
164 body.extend("\r\n".to_string().as_bytes().to_vec());
165 } else {
166 body.extend(format!("--{}\r\n", self.boundary).as_bytes().to_vec());
167 body.extend(format!(r#"Content-Disposition: form-data; name="{}"{}"#, key, "\r\n\r\n").as_bytes().to_vec());
168 body.extend(format!("{value}\r\n").as_bytes().to_vec());
169 }
170 }
171 body.extend(format!("--{}--\r\n", self.boundary).as_bytes().to_vec());
172 self.body = body.to_vec();
173 self.header("Content-Length", self.body.len().into());
174 self
175 }
176 pub fn x_www_form_urlencoded(&mut self, data: JsonValue) -> &mut Self {
178 self.header("Content-Type", "application/x-www-form-urlencoded".into());
179 let mut params = vec![];
180 for (key, value) in data.entries() {
181 params.push(format!("{}={}", key, value));
182 }
183 let params = params.join("&");
184 self.body = params.as_bytes().to_vec();
185 self.header("Content-Length", self.body.len().into());
186 self
187 }
188 pub fn send(&mut self) -> Result<ClientResponse, String> {
189 let mut url = Url::parse(&self.url)?;
190 let stream = match TcpStream::connect(format!("{}:{}", url.host, url.port)) {
191 Ok(stream) => stream,
192 Err(e) => {
193 if self.retry > 0 {
194 self.retry -= 1;
195 return self.send();
196 }
197 return Err(e.to_string());
198 }
199 };
200
201 if self.read_timeout > 0 {
202 let _ = stream.set_read_timeout(Option::from(Duration::from_secs(self.read_timeout)));
203 }
204 if self.write_timeout > 0 {
205 let _ = stream.set_write_timeout(Option::from(Duration::from_secs(self.write_timeout)));
206 }
207
208 let mut stream = if url.scheme == "https" {
209 let root_store = RootCertStore::from_iter(
210 webpki_roots::TLS_SERVER_ROOTS.iter().cloned()
211 );
212 let mut config = rustls::ClientConfig::builder().with_root_certificates(root_store).with_no_client_auth();
213 config.alpn_protocols = vec![self.protocol.str().to_lowercase().as_bytes().to_vec()];
215 let example_com = url.host.clone().try_into().unwrap();
216 let client = ClientConnection::new(Arc::new(config), example_com).unwrap();
217 ClientStream::Https(Box::new(StreamOwned::new(client, stream)))
218 } else {
219 ClientStream::Http(stream)
220 };
221
222
223 if !self.query.is_empty() {
224 url.set_query(self.query.clone());
225 }
226
227 let request_line = format!("{} {} {}", self.method.str(), url.path_query.as_str(), self.protocol.str());
228
229 let host = if IpAddr::from_str(url.host.as_str()).is_ok() {
230 format!("Host: {}:{}", url.host, url.port)
231 } else {
232 format!("Host: {}", url.host)
233 };
234
235
236 let mut header = vec![request_line, host];
237
238
239 for (key, value) in self.headers.iter() {
241 header.push(format!("{}: {}", key, value));
242 }
243
244 for (key, value) in self.cookies.iter() {
246 header.push(format!("Set-Cookie: {}={}", key, value));
247 }
248
249 header.push("\r\n".to_string());
251 match stream.write_all(header.join("\r\n").as_bytes()) {
252 Ok(()) => {
253 match stream.flush() {
254 Ok(e) => e,
255 Err(e) => return Err(e.to_string())
256 };
257 }
258 Err(e) => {
259 if self.retry > 0 {
260 self.retry -= 1;
261 return self.send();
262 }
263 return Err(e.to_string());
264 }
265 };
266
267 if self.debug {
268 debug!("\r\n==============请求头 {:?}============== \r\n{}\r\n============================",thread::current().id(), header.join("\r\n"));
269 }
270
271 if !self.body.is_empty() {
272 match stream.write_all(self.body.as_slice()) {
273 Ok(()) => {
274 match stream.flush() {
275 Ok(e) => e,
276 Err(e) => return Err(e.to_string())
277 };
278 }
279 Err(e) => {
280 if self.retry > 0 {
281 self.retry -= 1;
282 return self.send();
283 }
284 return Err(e.to_string());
285 }
286 }
287 }
288
289 if self.debug && self.boundary.is_empty() {
290 debug!("\r\n==============请求体 {:?}============== \r\n{}\r\n============================",thread::current().id(),String::from_utf8_lossy(self.body.as_slice()));
291 }
292
293
294 let mut res = ClientResponse::new(self.debug, stream).handle()?;
295 if [301, 302, 303].contains(&(res.status() as i32)) && !res.location().is_empty() {
296 self.url = res.location();
297 return self.send();
298 }
299 self.reset();
300 Ok(res)
301 }
302}
303
304
305