Skip to content

Commit

Permalink
fix: screen size persistency
Browse files Browse the repository at this point in the history
  • Loading branch information
SachaMorard committed Oct 22, 2024
1 parent 68ffe62 commit ce8feae
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 88 deletions.
13 changes: 7 additions & 6 deletions src/proxy/compute/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::config::config;
use crate::proxy::proxy::set_edgee_header;
use crate::tools::{
self,
edgee_cookie::{self, EdgeeCookie},
edgee_cookie::{self},
};

pub async fn html_handler(
Expand Down Expand Up @@ -57,17 +57,16 @@ pub async fn html_handler(

match do_process_payload(path, request_headers, response_parts) {
Ok(_) => {
let cookie = edgee_cookie::get(request_headers, response_parts, host);
if cookie.is_none() {
if !edgee_cookie::has_cookie(request_headers) {
set_edgee_header(response_parts, "compute-aborted(no-cookie)");
} else {
let data_collection_events = data_collection::process_from_html(
&document,
&cookie.unwrap(),
proto,
host,
path,
request_headers,
response_parts,
client_ip,
)
.await;
Expand All @@ -86,12 +85,14 @@ pub async fn html_handler(

pub async fn json_handler(
body: &Bytes,
cookie: &EdgeeCookie,
path: &PathAndQuery,
host: &str,
request_headers: &HeaderMap,
client_ip: &String,
response_parts: &mut Parts,
) -> Option<String> {
data_collection::process_from_json(body, cookie, path, request_headers, client_ip).await
data_collection::process_from_json(body, path, host, request_headers, client_ip, response_parts)
.await
}

/// Processes the payload of a request under certain conditions.
Expand Down
132 changes: 81 additions & 51 deletions src/proxy/compute/data_collection/data_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ use crate::config::config;
use crate::proxy::compute::data_collection::payload::{EventData, EventType, Payload};
use crate::proxy::compute::data_collection::{components, payload};
use crate::proxy::compute::html::Document;
use crate::tools::edgee_cookie::EdgeeCookie;
use crate::tools::edgee_cookie;
use base64::alphabet::STANDARD;
use base64::engine::general_purpose::PAD;
use base64::engine::GeneralPurpose;
use base64::Engine;
use bytes::Bytes;
use html_escape;
use http::response::Parts;
use http::uri::PathAndQuery;
use http::{header, HeaderMap};
use json_comments::StripComments;
Expand All @@ -19,15 +20,15 @@ use tracing::{info, warn, Instrument};

#[tracing::instrument(
name = "data_collection",
skip(document, edgee_cookie, proto, host, path, request_headers, client_ip)
skip(document, proto, host, path, request_headers, client_ip)
)]
pub async fn process_from_html(
document: &Document,
edgee_cookie: &EdgeeCookie,
proto: &str,
host: &str,
path: &PathAndQuery,
request_headers: &HeaderMap,
response_parts: &mut Parts,
client_ip: &String,
) -> Option<String> {
let json_data_layer = document.data_layer.clone();
Expand All @@ -48,7 +49,7 @@ pub async fn process_from_html(
payload = prepare_data_collection_payload(payload);

// add session info
payload = add_session(payload, edgee_cookie);
payload = add_session(payload, request_headers, response_parts, host);

// add more info from the html or request
payload = add_more_info_from_html_or_request(document, payload, proto, host, path);
Expand Down Expand Up @@ -143,16 +144,14 @@ pub async fn process_from_html(
Option::from(events_json)
}

#[tracing::instrument(
name = "data_collection",
skip(body, edgee_cookie, path, request_headers, client_ip)
)]
#[tracing::instrument(name = "data_collection", skip(body, path, request_headers, client_ip))]
pub async fn process_from_json(
body: &Bytes,
edgee_cookie: &EdgeeCookie,
path: &PathAndQuery,
host: &str,
request_headers: &HeaderMap,
client_ip: &String,
response_parts: &mut Parts,
) -> Option<String> {
// populate the edgee payload from the json
let payload_result = parse_payload(body.as_ref());
Expand All @@ -166,7 +165,7 @@ pub async fn process_from_json(
payload = prepare_data_collection_payload(payload);

// add session info
payload = add_session(payload, edgee_cookie);
payload = add_session(payload, request_headers, response_parts, host);

// add more info from the request
payload = add_more_info_from_request(request_headers, payload, path, client_ip);
Expand Down Expand Up @@ -250,56 +249,32 @@ pub async fn process_from_json(
/// # Returns
/// - `Payload`: The updated `Payload` object with initialized fields.
fn prepare_data_collection_payload(mut payload: Payload) -> Payload {
if payload.data_collection.is_none() {
payload.data_collection = Some(Default::default());
}

// Initialize the context field within data_collection if it is None
if payload.data_collection.as_ref().unwrap().context.is_none() {
payload.data_collection.as_mut().unwrap().context = Some(Default::default());
}

// Initialize the page field within context if it is None
if payload
// Ensure data_collection and its nested fields are initialized
payload
.data_collection
.as_ref()
.get_or_insert_with(Default::default)
.context
.get_or_insert_with(Default::default)
.client
.get_or_insert_with(Default::default);
payload
.data_collection
.as_mut()
.unwrap()
.context
.as_ref()
.as_mut()
.unwrap()
.page
.is_none()
{
payload
.data_collection
.as_mut()
.unwrap()
.context
.as_mut()
.unwrap()
.page = Some(Default::default());
}

// Initialize the user field within context if it is None
if payload
.get_or_insert_with(Default::default);
payload
.data_collection
.as_ref()
.as_mut()
.unwrap()
.context
.as_ref()
.as_mut()
.unwrap()
.user
.is_none()
{
payload
.data_collection
.as_mut()
.unwrap()
.context
.as_mut()
.unwrap()
.user = Some(Default::default());
}
.get_or_insert_with(Default::default);

payload
}
Expand All @@ -312,7 +287,14 @@ fn prepare_data_collection_payload(mut payload: Payload) -> Payload {
///
/// # Returns
/// - `Payload`: The updated `Payload` object with session information.
fn add_session(mut payload: Payload, edgee_cookie: &EdgeeCookie) -> Payload {
fn add_session(
mut payload: Payload,
request_headers: &HeaderMap,
response_parts: &mut Parts,
host: &str,
) -> Payload {
let edgee_cookie = edgee_cookie::get_or_set(&request_headers, response_parts, &host, &payload);

// edgee_id
let user_id = edgee_cookie.id.to_string();
payload
Expand Down Expand Up @@ -354,6 +336,54 @@ fn add_session(mut payload: Payload, edgee_cookie: &EdgeeCookie) -> Payload {
.as_mut()
.unwrap()
.session = Some(user_session);

// if edgee_cookie.sz is not None, we add it to the payload
if edgee_cookie.sz.is_some() {
if let Some(sz) = &edgee_cookie.sz {
let size_vec: Vec<&str> = sz.split('x').collect();
if size_vec.len() == 3 {
if let (Ok(width), Ok(height), Ok(density)) = (
size_vec[0].parse::<i32>(),
size_vec[1].parse::<i32>(),
size_vec[2].parse::<i32>(),
) {
payload
.data_collection
.as_mut()
.unwrap()
.context
.as_mut()
.unwrap()
.client
.as_mut()
.unwrap()
.screen_width = Some(width);
payload
.data_collection
.as_mut()
.unwrap()
.context
.as_mut()
.unwrap()
.client
.as_mut()
.unwrap()
.screen_height = Some(height);
payload
.data_collection
.as_mut()
.unwrap()
.context
.as_mut()
.unwrap()
.client
.as_mut()
.unwrap()
.screen_density = Some(density);
}
}
}
}
payload
}

Expand Down
56 changes: 36 additions & 20 deletions src/proxy/controller/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Response = http::Response<BoxBody<Bytes, Infallible>>;

pub async fn edgee_client_event(
incoming_ctx: IncomingContext,
host: &String,
host: &str,
path: &PathAndQuery,
request_headers: &HeaderMap,
client_ip: &String,
Expand All @@ -29,13 +29,18 @@ pub async fn edgee_client_event(
.body(empty())?;

let (mut response_parts, _incoming) = res.into_parts();
let cookie = edgee_cookie::get_or_set(&request_headers, &mut response_parts, &host);

let body = incoming_ctx.incoming_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, &cookie, path, request_headers, client_ip).await;
let data_collection_events_res = compute::json_handler(
&body,
path,
host,
request_headers,
client_ip,
&mut response_parts,
)
.await;
if data_collection_events_res.is_some() {
data_collection_events = data_collection_events_res.unwrap();
}
Expand All @@ -54,11 +59,20 @@ pub async fn edgee_client_event(

pub async fn edgee_client_event_from_third_party_sdk(
incoming_ctx: IncomingContext,
host: &str,
path: &PathAndQuery,
request_headers: &HeaderMap,
client_ip: &String,
) -> anyhow::Result<Response> {
let body = incoming_ctx.incoming_body.collect().await?.to_bytes();
let res = http::Response::builder()
.status(StatusCode::OK)
.header(header::ACCESS_CONTROL_ALLOW_ORIGIN, "*")
.header(header::CONTENT_TYPE, "application/json")
.header(header::CACHE_CONTROL, "private, no-store")
.body(empty())?;

let (mut response_parts, _incoming) = res.into_parts();
let resp_body = incoming_ctx.incoming_body.collect().await?.to_bytes();

let cookie: EdgeeCookie;

Expand All @@ -84,30 +98,32 @@ pub async fn edgee_client_event_from_third_party_sdk(
let cookie_encrypted = encrypt(&cookie_str).unwrap();

let mut data_collection_events: String = String::new();
if body.len() > 0 {
let data_collection_events_res =
compute::json_handler(&body, &cookie, path, request_headers, client_ip).await;
if resp_body.len() > 0 {
let data_collection_events_res = compute::json_handler(
&resp_body,
path,
host,
request_headers,
client_ip,
&mut response_parts,
)
.await;
if data_collection_events_res.is_some() {
data_collection_events = data_collection_events_res.unwrap();
}
}

let mut body = Full::from(Bytes::from(format!(r#"{{"e":"{}"}}"#, cookie_encrypted))).boxed();
let body: Bytes;
if incoming_ctx.is_debug_mode && data_collection_events.len() > 0 {
body = Full::from(Bytes::from(format!(
body = Bytes::from(format!(
r#"{{"e":"{}", "events":{}}}"#,
cookie_encrypted, data_collection_events
)))
.boxed()
));
} else {
body = Bytes::from(format!(r#"{{"e":"{}"}}"#, cookie_encrypted));
}

Ok(http::Response::builder()
.status(StatusCode::OK)
.header(header::ACCESS_CONTROL_ALLOW_ORIGIN, "*")
.header(header::CONTENT_TYPE, "application/json")
.header(header::CACHE_CONTROL, "private, no-store")
.body(body)
.expect("serving sdk should never fail"))
Ok(build_response(response_parts, body))
}

pub fn options(allow_methods: &str) -> anyhow::Result<Response> {
Expand Down
3 changes: 2 additions & 1 deletion src/proxy/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub async fn handle_request(
);
return controller::edgee_client_event(
incoming_ctx,
&incoming_host,
incoming_host.as_str(),
&incoming_path,
&incoming_headers,
&client_ip,
Expand Down Expand Up @@ -130,6 +130,7 @@ pub async fn handle_request(
);
return controller::edgee_client_event_from_third_party_sdk(
incoming_ctx,
incoming_host.as_str(),
&incoming_path,
&incoming_headers,
&client_ip,
Expand Down
Loading

0 comments on commit ce8feae

Please sign in to comment.