Skip to content

Commit

Permalink
chore: change response_parts into response
Browse files Browse the repository at this point in the history
  • Loading branch information
SachaMorard committed Oct 23, 2024
1 parent 57f8748 commit 8da63e2
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 58 deletions.
22 changes: 11 additions & 11 deletions src/proxy/compute/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::tools::{
pub async fn html_handler(
body: &str,
request: &RequestHandle,
response_parts: &mut Parts,
response: &mut Parts,
) -> Result<Document, &'static str> {
// if the decompressed body is too large, abort the computation
if body.len() > config::get().compute.max_decompressed_body_size {
Expand All @@ -45,26 +45,26 @@ pub async fn html_handler(

// enforce_no_store_policy is used to enforce no-store cache-control header in the response for requests that can be computed
if config::get().compute.enforce_no_store_policy {
response_parts.headers.insert(
response.headers.insert(
HeaderName::from_str(CACHE_CONTROL.as_ref()).unwrap(),
HeaderValue::from_str("no-store").unwrap(),
);
}

match do_process_payload(request, response_parts) {
match do_process_payload(request, response) {
Ok(_) => {
if !edgee_cookie::has_cookie(request) {
set_edgee_header(response_parts, "compute-aborted(no-cookie)");
set_edgee_header(response, "compute-aborted(no-cookie)");
} else {
let data_collection_events =
data_collection::process_from_html(&document, request, response_parts).await;
data_collection::process_from_html(&document, request, response).await;
if data_collection_events.is_some() {
document.data_collection_events = data_collection_events.unwrap();
}
}
}
Err(reason) => {
set_edgee_header(response_parts, reason);
set_edgee_header(response, reason);
}
}

Expand All @@ -74,9 +74,9 @@ pub async fn html_handler(
pub async fn json_handler(
body: &Bytes,
request: &RequestHandle,
response_parts: &mut Parts,
response: &mut Parts,
) -> Option<String> {
data_collection::process_from_json(body, request, response_parts).await
data_collection::process_from_json(body, request, response).await
}

/// Processes the payload of a request under certain conditions.
Expand All @@ -87,7 +87,7 @@ pub async fn json_handler(
/// # Arguments
///
/// * `path` - A reference to the path
/// * `response_parts` - A mutable reference to the response parts
/// * `response` - A mutable reference to the response parts
///
/// # Returns
///
Expand All @@ -103,7 +103,7 @@ pub async fn json_handler(
/// * The request is for prefetch (indicated by the `Purpose` or `Sec-Purpose` headers).
fn do_process_payload(
request: &RequestHandle,
response_parts: &mut Parts,
response: &mut Parts,
) -> Result<bool, &'static str> {
// do not process the payload if disableEdgeDataCollection query param is present in the URL
let query = request.get_query().as_str();
Expand All @@ -114,7 +114,7 @@ fn do_process_payload(
if !config::get().compute.enforce_no_store_policy {
// process the payload, only if response is not cacheable
// transform response_headers to HashMap<String, String>
let res_headers = response_parts
let res_headers = response
.headers
.iter()
.map(|(k, v)| (k.as_str().to_string(), v.to_str().unwrap().to_string()))
Expand Down
16 changes: 8 additions & 8 deletions src/proxy/controller/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,28 @@ pub async fn edgee_client_event(ctx: IncomingContext) -> anyhow::Result<Response
.header(header::CACHE_CONTROL, "private, no-store")
.body(empty())?;

let (mut response_parts, _incoming) = res.into_parts();
let (mut response, _incoming) = res.into_parts();
let request = &ctx.get_request().clone();
let body = ctx.body.collect().await?.to_bytes();

let mut data_collection_events: String = String::new();
if body.len() > 0 {
let data_collection_events_res =
compute::json_handler(&body, request, &mut response_parts).await;
compute::json_handler(&body, request, &mut response).await;
if data_collection_events_res.is_some() {
data_collection_events = data_collection_events_res.unwrap();
}
}

if request.is_debug_mode() {
response_parts.status = StatusCode::OK;
response.status = StatusCode::OK;
return Ok(build_response(
response_parts,
response,
Bytes::from(data_collection_events),
));
}

Ok(build_response(response_parts, Bytes::new()))
Ok(build_response(response, Bytes::new()))
}

pub async fn edgee_client_event_from_third_party_sdk(
Expand All @@ -56,7 +56,7 @@ pub async fn edgee_client_event_from_third_party_sdk(
.header(header::CONTENT_TYPE, "application/json")
.header(header::CACHE_CONTROL, "private, no-store")
.body(empty())?;
let (mut response_parts, _incoming) = res.into_parts();
let (mut response, _incoming) = res.into_parts();

let request = &ctx.get_request().clone();
let body = ctx.body.collect().await?.to_bytes();
Expand Down Expand Up @@ -86,7 +86,7 @@ pub async fn edgee_client_event_from_third_party_sdk(
let mut data_collection_events: String = String::new();
if body.len() > 0 {
let data_collection_events_res =
compute::json_handler(&body, &request, &mut response_parts).await;
compute::json_handler(&body, &request, &mut response).await;
if data_collection_events_res.is_some() {
data_collection_events = data_collection_events_res.unwrap();
}
Expand All @@ -102,7 +102,7 @@ pub async fn edgee_client_event_from_third_party_sdk(
resp_body = Bytes::from(format!(r#"{{"e":"{}"}}"#, cookie_encrypted));
}

Ok(build_response(response_parts, resp_body))
Ok(build_response(response, resp_body))
}

pub fn options(allow_methods: &str) -> anyhow::Result<Response> {
Expand Down
50 changes: 25 additions & 25 deletions src/proxy/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,35 +131,35 @@ pub async fn handle_request(
controller::bad_gateway_error(request, timer_start)
}
Ok(upstream) => {
let (mut response_parts, incoming) = upstream.into_parts();
let (mut response, incoming) = upstream.into_parts();
let response_body = incoming.collect().await?.to_bytes();
info!(
"{} - {} {}{} - {}ms",
response_parts.status.as_str(),
response.status.as_str(),
request.get_method(),
request.get_host(),
request.get_path(),
timer_start.elapsed().as_millis()
);

// Only proxy in some cases
match do_only_proxy(request.get_method(), &response_body, &response_parts) {
match do_only_proxy(request.get_method(), &response_body, &response) {
Ok(_) => {}
Err(reason) => {
set_edgee_header(&mut response_parts, reason);
set_edgee_header(&mut response, reason);
set_duration_headers(
&mut response_parts,
&mut response,
request.is_debug_mode(),
timer_start.elapsed().as_millis(),
None,
);
return Ok(controller::build_response(response_parts, response_body));
return Ok(controller::build_response(response, response_body));
}
}

set_edgee_header(&mut response_parts, "compute");
set_edgee_header(&mut response, "compute");
let proxy_duration = timer_start.elapsed().as_millis();
let response_headers = response_parts.headers.clone();
let response_headers = response.headers.clone();
let encoding = response_headers
.get(header::CONTENT_ENCODING)
.and_then(|h| h.to_str().ok());
Expand Down Expand Up @@ -189,7 +189,7 @@ pub async fn handle_request(
};

// interpret what's in the body
let _ = match compute::html_handler(&mut body_str, request, &mut response_parts).await {
let _ = match compute::html_handler(&mut body_str, request, &mut response).await {
Ok(document) => {
let mut client_side_param = r#" data-client-side="true""#;
let event_path_param = format!(
Expand Down Expand Up @@ -239,7 +239,7 @@ pub async fn handle_request(
}
}
Err(reason) => {
set_edgee_header(&mut response_parts, reason);
set_edgee_header(&mut response, reason);
}
};

Expand Down Expand Up @@ -268,14 +268,14 @@ pub async fn handle_request(
let full_duration = timer_start.elapsed().as_millis();
let compute_duration = full_duration - proxy_duration;
set_duration_headers(
&mut response_parts,
&mut response,
request.is_debug_mode(),
full_duration,
Some(compute_duration),
);

Ok(controller::build_response(
response_parts,
response,
Bytes::from(data),
))
}
Expand All @@ -286,7 +286,7 @@ pub async fn handle_request(
///
/// # Arguments
///
/// * `response_parts` - A mutable reference to the response parts.
/// * `response` - A mutable reference to the response parts.
/// * `is_debug_mode` - A boolean indicating whether debug mode is enabled.
/// * `full_duration` - The full duration of the request in milliseconds.
/// * `compute_duration` - An optional duration of the compute phase in milliseconds.
Expand All @@ -297,25 +297,25 @@ pub async fn handle_request(
/// If a compute duration is provided, it is inserted into the response headers.
/// Additionally, if debug mode is enabled, the function calculates the proxy duration and inserts it into the response headers.
fn set_duration_headers(
response_parts: &mut Parts,
response: &mut Parts,
is_debug_mode: bool,
full_duration: u128,
compute_duration: Option<u128>,
) {
if is_debug_mode {
response_parts.headers.insert(
response.headers.insert(
HeaderName::from_str(EDGEE_FULL_DURATION_HEADER).unwrap(),
HeaderValue::from_str(format!("{}ms", full_duration).as_str()).unwrap(),
);
}
if let Some(duration) = compute_duration {
response_parts.headers.insert(
response.headers.insert(
HeaderName::from_str(EDGEE_COMPUTE_DURATION_HEADER).unwrap(),
HeaderValue::from_str(format!("{}ms", duration).as_str()).unwrap(),
);
if is_debug_mode {
let proxy_duration = full_duration - duration;
response_parts.headers.insert(
response.headers.insert(
HeaderName::from_str(EDGEE_PROXY_DURATION_HEADER).unwrap(),
HeaderValue::from_str(format!("{}ms", proxy_duration).as_str()).unwrap(),
);
Expand All @@ -327,14 +327,14 @@ fn set_duration_headers(
///
/// # Arguments
///
/// * `response_parts` - A mutable reference to the response parts.
/// * `response` - A mutable reference to the response parts.
/// * `process` - A string slice representing the process to be set in the header.
///
/// # Logic
///
/// The function inserts the process information into the response headers.
pub fn set_edgee_header(response_parts: &mut Parts, process: &str) {
response_parts.headers.insert(
pub fn set_edgee_header(response: &mut Parts, process: &str) {
response.headers.insert(
HeaderName::from_str(EDGEE_HEADER).unwrap(),
HeaderValue::from_str(process).unwrap(),
);
Expand All @@ -346,7 +346,7 @@ pub fn set_edgee_header(response_parts: &mut Parts, process: &str) {
///
/// * `method` - The HTTP method of the request.
/// * `response_body` - The body of the response.
/// * `response_parts` - The parts of the response.
/// * `response` - The parts of the response.
///
/// # Returns
///
Expand All @@ -368,9 +368,9 @@ pub fn set_edgee_header(response_parts: &mut Parts, process: &str) {
fn do_only_proxy(
method: &Method,
response_body: &Bytes,
response_parts: &Parts,
response: &Parts,
) -> Result<bool, &'static str> {
let response_headers = response_parts.headers.clone();
let response_headers = response.headers.clone();
let encoding = response_headers
.get(header::CONTENT_ENCODING)
.and_then(|h| h.to_str().ok());
Expand All @@ -393,12 +393,12 @@ fn do_only_proxy(
}

// if response is redirection
if response_parts.status.is_redirection() {
if response.status.is_redirection() {
Err("proxy-only(3xx)")?;
}

// if response is informational
if response_parts.status.is_informational() {
if response.status.is_informational() {
Err("proxy-only(1xx)")?;
}

Expand Down
Loading

0 comments on commit 8da63e2

Please sign in to comment.