Skip to content

Commit b29e691

Browse files
committed
Wrappers (does not yet compile)
1 parent 72ff26f commit b29e691

File tree

11 files changed

+116
-32
lines changed

11 files changed

+116
-32
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/common/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ futures = "0.3"
1919
handlebars = "6.3.2"
2020
hex = "0.4.3"
2121
hmac = "0.12.1"
22+
http = "1.3.1"
2223
log = "0.4.20"
2324
log-fastly = "0.10.0"
2425
serde = { version = "1.0", features = ["derive"] }

crates/common/src/constants.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
pub const SYNTHETIC_HEADER_FRESH: &str = "X-Synthetic-Fresh";
2-
pub const SYNTHETIC_HEADER_TRUSTED_SERVER: &str = "X-Synthetic-Trusted-Server";
3-
pub const SYNTHETIC_HEADER_PUB_USER_ID: &str = "X-Pub-User-ID";
1+
use http::header::HeaderName;
2+
3+
pub const HEADER_SYNTHETIC_FRESH: HeaderName = HeaderName::from_static("x-synthetic-fresh");
4+
pub const HEADER_SYNTHETIC_PUB_USER_ID: HeaderName = HeaderName::from_static("x-pub-user-id");
5+
pub const HEADER_SYNTHETIC_TRUSTED_SERVER: HeaderName = HeaderName::from_static("x-synthetic-trusted-server");
6+
pub const HEADER_X_FORWARDED_FOR: HeaderName = HeaderName::from_static("x-forwarded-for");

crates/common/src/cookies.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use cookie::{Cookie, CookieJar};
22
use fastly::http::header;
3-
use fastly::Request;
3+
4+
use crate::http_wrapper::RequestWrapper;
45

56
const COOKIE_MAX_AGE: i32 = 365 * 24 * 60 * 60; // 1 year
67

@@ -17,7 +18,7 @@ pub fn parse_cookies_to_jar(s: &str) -> CookieJar {
1718
jar
1819
}
1920

20-
pub fn handle_request_cookies(req: &Request) -> Option<CookieJar> {
21+
pub fn handle_request_cookies<T: RequestWrapper>(req: &T) -> Option<CookieJar> {
2122
match req.get_header(header::COOKIE) {
2223
Some(header_value) => {
2324
let header_value_str: &str = header_value.to_str().unwrap_or("");

crates/common/src/http_wrapper.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use http::Method;
2+
use http::header::{HeaderName, HeaderValue};
3+
use std::net::IpAddr;
4+
5+
pub trait RequestWrapper {
6+
fn get_client_ip_addr(&self) -> Option<IpAddr>;
7+
8+
fn get_header(
9+
&self,
10+
name: HeaderName,
11+
) -> Option<&HeaderValue>;
12+
13+
fn get_headers(&self) -> impl Iterator<Item = (&HeaderName, &HeaderValue)>;
14+
15+
fn get_method(&self) -> &Method;
16+
17+
fn get_path(&self) -> &str;
18+
19+
fn set_header(&mut self, name: HeaderName, value: HeaderValue);
20+
}

crates/common/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod constants;
22
pub mod cookies;
3+
pub mod http_wrapper;
34
pub mod models;
45
pub mod prebid;
56
pub mod settings;

crates/common/src/prebid.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use fastly::http::{header, Method};
22
use fastly::{Error, Request, Response};
33
use serde_json::json;
44

5-
use crate::constants::{SYNTHETIC_HEADER_FRESH, SYNTHETIC_HEADER_TRUSTED_SERVER};
5+
use crate::constants::{HEADER_X_FORWARDED_FOR, HEADER_SYNTHETIC_FRESH, HEADER_SYNTHETIC_TRUSTED_SERVER};
6+
use crate::http_wrapper::RequestWrapper;
67
use crate::settings::Settings;
78
use crate::synthetic::generate_synthetic_id;
89

@@ -28,10 +29,10 @@ impl PrebidRequest {
2829
///
2930
/// # Returns
3031
/// * `Result<Self, Error>` - New PrebidRequest or error
31-
pub fn new(settings: &Settings, req: &Request) -> Result<Self, Error> {
32+
pub fn new<T: RequestWrapper>(settings: &Settings, req: &T) -> Result<Self, Error> {
3233
// Get the POTSI ID from header (which we just set in handle_prebid_test)
3334
let synthetic_id = req
34-
.get_header(SYNTHETIC_HEADER_TRUSTED_SERVER)
35+
.get_header(HEADER_SYNTHETIC_TRUSTED_SERVER)
3536
.and_then(|h| h.to_str().ok())
3637
.map(|s| s.to_string())
3738
.unwrap_or_else(|| generate_synthetic_id(settings, req));
@@ -41,7 +42,7 @@ impl PrebidRequest {
4142
.get_client_ip_addr()
4243
.map(|ip| ip.to_string())
4344
.unwrap_or_else(|| {
44-
req.get_header("X-Forwarded-For")
45+
req.get_header(HEADER_X_FORWARDED_FOR)
4546
.and_then(|h| h.to_str().ok())
4647
.unwrap_or("")
4748
.split(',') // X-Forwarded-For can be a comma-separated list
@@ -89,16 +90,16 @@ impl PrebidRequest {
8990
///
9091
/// # Returns
9192
/// * `Result<Response, Error>` - Prebid Server response or error
92-
pub async fn send_bid_request(
93+
pub async fn send_bid_request<T: RequestWrapper>(
9394
&self,
9495
settings: &Settings,
95-
incoming_req: &Request,
96+
incoming_req: &T,
9697
) -> Result<Response, Error> {
9798
let mut req = Request::new(Method::POST, settings.prebid.server_url.to_owned());
9899

99100
// Get and store the POTSI ID value from the incoming request
100101
let potsi_id = incoming_req
101-
.get_header(SYNTHETIC_HEADER_TRUSTED_SERVER)
102+
.get_header(HEADER_SYNTHETIC_TRUSTED_SERVER)
102103
.and_then(|h| h.to_str().ok())
103104
.map(|s| s.to_string())
104105
.unwrap_or_else(|| self.synthetic_id.clone());
@@ -169,8 +170,8 @@ impl PrebidRequest {
169170
req.set_header(header::CONTENT_TYPE, "application/json");
170171
req.set_header("X-Forwarded-For", &self.client_ip);
171172
req.set_header(header::ORIGIN, &self.origin);
172-
req.set_header(SYNTHETIC_HEADER_FRESH, &self.synthetic_id);
173-
req.set_header(SYNTHETIC_HEADER_TRUSTED_SERVER, &potsi_id);
173+
req.set_header(HEADER_SYNTHETIC_FRESH, &self.synthetic_id);
174+
req.set_header(HEADER_SYNTHETIC_TRUSTED_SERVER, &potsi_id);
174175

175176
println!(
176177
"Sending prebid request with Fresh ID: {} and POTSI ID: {}",

crates/common/src/synthetic.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
use fastly::http::header;
2-
use fastly::Request;
32
use handlebars::Handlebars;
43
use hmac::{Hmac, Mac};
54
use serde_json::json;
65
use sha2::Sha256;
76

8-
use crate::constants::{SYNTHETIC_HEADER_PUB_USER_ID, SYNTHETIC_HEADER_TRUSTED_SERVER};
7+
use crate::constants::{HEADER_SYNTHETIC_PUB_USER_ID, HEADER_SYNTHETIC_TRUSTED_SERVER};
98
use crate::cookies::handle_request_cookies;
9+
use crate::http_wrapper::RequestWrapper;
1010
use crate::settings::Settings;
1111

1212
type HmacSha256 = Hmac<Sha256>;
1313

1414
/// Generates a fresh synthetic_id based on request parameters
15-
pub fn generate_synthetic_id(settings: &Settings, req: &Request) -> String {
15+
pub fn generate_synthetic_id<T: RequestWrapper>(settings: &Settings, req: &T) -> String {
1616
let user_agent = req
1717
.get_header(header::USER_AGENT)
1818
.map(|h| h.to_str().unwrap_or("unknown"));
@@ -21,7 +21,7 @@ pub fn generate_synthetic_id(settings: &Settings, req: &Request) -> String {
2121
.map(|cookie| cookie.value().to_string())
2222
});
2323
let auth_user_id = req
24-
.get_header(SYNTHETIC_HEADER_PUB_USER_ID)
24+
.get_header(HEADER_SYNTHETIC_PUB_USER_ID)
2525
.map(|h| h.to_str().unwrap_or("anonymous"));
2626
let publisher_domain = req
2727
.get_header(header::HOST)
@@ -58,10 +58,10 @@ pub fn generate_synthetic_id(settings: &Settings, req: &Request) -> String {
5858
}
5959

6060
/// Gets or creates a synthetic_id from the request
61-
pub fn get_or_generate_synthetic_id(settings: &Settings, req: &Request) -> String {
61+
pub fn get_or_generate_synthetic_id<T: RequestWrapper>(settings: &Settings, req: &T) -> String {
6262
// First try to get existing POTSI ID from header
6363
if let Some(synthetic_id) = req
64-
.get_header(SYNTHETIC_HEADER_TRUSTED_SERVER)
64+
.get_header(HEADER_SYNTHETIC_TRUSTED_SERVER)
6565
.and_then(|h| h.to_str().ok())
6666
.map(|s| s.to_string())
6767
{
@@ -148,7 +148,7 @@ mod tests {
148148
fn test_get_or_generate_synthetic_id_with_header() {
149149
let settings = create_settings();
150150
let req = create_test_request(vec![(
151-
SYNTHETIC_HEADER_TRUSTED_SERVER,
151+
HEADER_SYNTHETIC_TRUSTED_SERVER,
152152
"existing_synthetic_id",
153153
)]);
154154

crates/fastly/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2021"
66
[dependencies]
77
fastly = "0.11.2"
88
futures = "0.3"
9+
http = "1.3.1"
910
log = "0.4.20"
1011
log-fastly = "0.10.0"
1112
serde = { version = "1.0", features = ["derive"] }

crates/fastly/src/http_wrapper.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use fastly::Request as FastlyRequest;
2+
use http::header::{HeaderName, HeaderValue};
3+
use http::Method;
4+
use std::net::IpAddr;
5+
6+
use trusted_server_common::http_wrapper::RequestWrapper;
7+
8+
#[derive(Debug)]
9+
pub struct FastlyRequestWrapper {
10+
request: FastlyRequest,
11+
}
12+
13+
impl FastlyRequestWrapper {
14+
pub fn new(request: FastlyRequest) -> Self {
15+
FastlyRequestWrapper { request }
16+
}
17+
}
18+
19+
impl RequestWrapper for FastlyRequestWrapper {
20+
#[inline(always)]
21+
fn get_client_ip_addr(&self) -> Option<IpAddr> {
22+
self.request.get_client_ip_addr()
23+
}
24+
25+
#[inline(always)]
26+
fn get_header(&self, name: HeaderName) -> Option<&HeaderValue> {
27+
self.request.get_header(name)
28+
}
29+
30+
#[inline(always)]
31+
fn get_headers(&self) -> impl Iterator<Item = (&HeaderName, &HeaderValue)> {
32+
self.request.get_headers()
33+
}
34+
35+
#[inline(always)]
36+
fn get_method(&self) -> &Method {
37+
self.request.get_method()
38+
}
39+
40+
#[inline(always)]
41+
fn get_path(&self) -> &str {
42+
self.request.get_path()
43+
}
44+
45+
#[inline(always)]
46+
fn set_header(&mut self, name: HeaderName, value: HeaderValue) {
47+
self.request.set_header(name, value)
48+
}
49+
}

0 commit comments

Comments
 (0)