Modify variable name to be more specific (response_headers vs headers, request_body vs data, response_body vs body)

This commit is contained in:
jcamiel 2022-07-04 16:31:16 +02:00 committed by Fabrice Reix
parent 568b83266e
commit 750e7e0e18

View File

@ -113,10 +113,8 @@ impl Client {
Ok(calls) Ok(calls)
} }
///
/// Execute an http request /// Execute an http request
/// pub fn execute(&mut self, request_spec: &RequestSpec) -> Result<(Request, Response), HttpError> {
pub fn execute(&mut self, request: &RequestSpec) -> Result<(Request, Response), HttpError> {
// set handle attributes // set handle attributes
// that have not been set or reset // that have not been set or reset
@ -142,32 +140,28 @@ impl Client {
.connect_timeout(self.options.connect_timeout) .connect_timeout(self.options.connect_timeout)
.unwrap(); .unwrap();
let url = self.generate_url(&request.url, &request.querystring); let url = self.generate_url(&request_spec.url, &request_spec.querystring);
self.handle.url(url.as_str()).unwrap(); self.handle.url(url.as_str()).unwrap();
self.set_method(&request.method); self.set_method(&request_spec.method);
self.set_cookies(&request_spec.cookies);
self.set_cookies(&request.cookies); self.set_form(&request_spec.form);
self.set_form(&request.form); self.set_multipart(&request_spec.multipart);
self.set_multipart(&request.multipart); let mut request_body: &[u8] = &request_spec.body.bytes();
self.set_body(request_body);
let bytes = request.body.bytes(); self.set_headers(request_spec);
let mut data: &[u8] = bytes.as_ref();
self.set_body(data);
self.set_headers(request);
let verbose = self.options.verbosity != None;
let mut request_headers: Vec<Header> = vec![];
let start = Instant::now(); let start = Instant::now();
let verbose = self.options.verbosity != None;
let very_verbose = self.options.verbosity == Some(Verbosity::VeryVerbose);
let mut request_headers: Vec<Header> = vec![];
let mut status_lines = vec![]; let mut status_lines = vec![];
let mut headers = vec![]; let mut response_headers = vec![];
let mut body = Vec::<u8>::new(); let mut response_body = Vec::<u8>::new();
{ {
let mut transfer = self.handle.transfer(); let mut transfer = self.handle.transfer();
if !data.is_empty() { if !request_body.is_empty() {
transfer transfer
.read_function(|buf| Ok(data.read(buf).unwrap_or(0))) .read_function(|buf| Ok(request_body.read(buf).unwrap_or(0)))
.unwrap(); .unwrap();
} }
transfer transfer
@ -215,7 +209,7 @@ impl Client {
if s.starts_with("HTTP/") { if s.starts_with("HTTP/") {
status_lines.push(s); status_lines.push(s);
} else { } else {
headers.push(s) response_headers.push(s)
} }
} }
true true
@ -224,7 +218,7 @@ impl Client {
transfer transfer
.write_function(|data| { .write_function(|data| {
body.extend(data); response_body.extend(data);
Ok(data.len()) Ok(data.len())
}) })
.unwrap(); .unwrap();
@ -248,13 +242,13 @@ impl Client {
None => return Err(HttpError::StatuslineIsMissing { url }), None => return Err(HttpError::StatuslineIsMissing { url }),
Some(status_line) => self.parse_response_version(status_line.clone())?, Some(status_line) => self.parse_response_version(status_line.clone())?,
}; };
let headers = self.parse_response_headers(&headers); let headers = self.parse_response_headers(&response_headers);
let duration = start.elapsed(); let duration = start.elapsed();
self.handle.reset(); self.handle.reset();
let request = Request { let request = Request {
url, url,
method: (&request.method).to_string(), method: (&request_spec.method).to_string(),
headers: request_headers, headers: request_headers,
body: vec![], // TODO: we don't record the request body for the moment. body: vec![], // TODO: we don't record the request body for the moment.
}; };
@ -262,11 +256,11 @@ impl Client {
version, version,
status, status,
headers, headers,
body, body: response_body,
duration, duration,
}; };
if self.options.verbosity == Some(Verbosity::VeryVerbose) { if very_verbose {
response.log_body(); response.log_body();
} }