Skip to content

Commit 6ecfc0c

Browse files
makes the header names configurable
1 parent db0acdb commit 6ecfc0c

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

crates/common/build.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const TRUSTED_SERVER_OUTPUT_CONFIG_PATH: &str = "../../target/trusted-server-out
1414

1515
fn main() {
1616
merge_toml();
17+
generate_header_constants();
1718
rerun_if_changed();
1819
}
1920

@@ -57,6 +58,37 @@ fn merge_toml() {
5758
fs::write(dest_path, merged_toml).unwrap_or_else(|_| panic!("Failed to write {:?}", dest_path));
5859
}
5960

61+
fn generate_header_constants() {
62+
// Read and parse the config
63+
let init_config_path = Path::new(TRUSTED_SERVER_INIT_CONFIG_PATH);
64+
let toml_content = fs::read_to_string(init_config_path)
65+
.unwrap_or_else(|_| panic!("Failed to read {:?}", init_config_path));
66+
67+
let settings = settings::Settings::from_toml(&toml_content)
68+
.expect("Failed to parse settings at build time");
69+
70+
// Generate the header name based on psid
71+
let psid = &settings.publisher.psid;
72+
let header_name = format!("x-{}-ts", psid);
73+
74+
// Generate Rust code for the constant
75+
let generated_code = format!(
76+
r#"// This file is auto-generated by build.rs - DO NOT EDIT
77+
78+
pub const HEADER_SYNTHETIC_TRUSTED_SERVER: HeaderName = HeaderName::from_static("{}");
79+
"#,
80+
header_name
81+
);
82+
83+
// Write to OUT_DIR
84+
let out_dir = std::env::var("OUT_DIR").expect("OUT_DIR not set");
85+
let dest_path = Path::new(&out_dir).join("generated_header_constants.rs");
86+
fs::write(&dest_path, generated_code)
87+
.unwrap_or_else(|_| panic!("Failed to write {:?}", dest_path));
88+
89+
println!("cargo:warning=Generated header constant: {}", header_name);
90+
}
91+
6092
fn collect_env_vars(value: &Value, env_vars: &mut HashSet<String>, path: Vec<String>) {
6193
if let Value::Object(map) = value {
6294
for (key, val) in map {

crates/common/src/constants.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ use http::header::HeaderName;
33
pub const HEADER_SYNTHETIC_FRESH: HeaderName = HeaderName::from_static("x-synthetic-fresh");
44
pub const HEADER_SYNTHETIC_PUB_USER_ID: HeaderName = HeaderName::from_static("x-pub-user-id");
55
pub const HEADER_X_PUB_USER_ID: HeaderName = HeaderName::from_static("x-pub-user-id");
6-
pub const HEADER_SYNTHETIC_TRUSTED_SERVER: HeaderName =
7-
HeaderName::from_static("x-psid-ts");
6+
7+
// Generated at build time from publisher.id_prefix in trusted-server.toml
8+
include!(concat!(env!("OUT_DIR"), "/generated_header_constants.rs"));
89
pub const HEADER_X_CONSENT_ADVERTISING: HeaderName =
910
HeaderName::from_static("x-consent-advertising");
1011
pub const HEADER_X_FORWARDED_FOR: HeaderName = HeaderName::from_static("x-forwarded-for");

crates/common/src/settings.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ pub struct Publisher {
2323
/// Secret used to encrypt/decrypt proxied URLs in `/first-party/proxy`.
2424
/// Keep this secret stable to allow existing links to decode.
2525
pub proxy_secret: String,
26+
/// Prefix for the publisher-specific ID header (x-{psid}-ts).
27+
/// Defaults to "psid" if not specified.
28+
#[serde(default = "default_psid")]
29+
pub psid: String,
30+
}
31+
32+
fn default_psid() -> String {
33+
"psid".to_string()
2634
}
2735

2836
impl Publisher {
@@ -568,6 +576,7 @@ mod tests {
568576
cookie_domain: ".example.com".to_string(),
569577
origin_url: "https://origin.example.com:8080".to_string(),
570578
proxy_secret: "test-secret".to_string(),
579+
psid: "psid".to_string(),
571580
};
572581
assert_eq!(publisher.origin_host(), "origin.example.com:8080");
573582

@@ -577,6 +586,7 @@ mod tests {
577586
cookie_domain: ".example.com".to_string(),
578587
origin_url: "https://origin.example.com".to_string(),
579588
proxy_secret: "test-secret".to_string(),
589+
psid: "psid".to_string(),
580590
};
581591
assert_eq!(publisher.origin_host(), "origin.example.com");
582592

@@ -586,6 +596,7 @@ mod tests {
586596
cookie_domain: ".example.com".to_string(),
587597
origin_url: "http://localhost:9090".to_string(),
588598
proxy_secret: "test-secret".to_string(),
599+
psid: "psid".to_string(),
589600
};
590601
assert_eq!(publisher.origin_host(), "localhost:9090");
591602

@@ -595,6 +606,7 @@ mod tests {
595606
cookie_domain: ".example.com".to_string(),
596607
origin_url: "localhost:9090".to_string(),
597608
proxy_secret: "test-secret".to_string(),
609+
psid: "psid".to_string(),
598610
};
599611
assert_eq!(publisher.origin_host(), "localhost:9090");
600612

@@ -604,6 +616,7 @@ mod tests {
604616
cookie_domain: ".example.com".to_string(),
605617
origin_url: "http://192.168.1.1:8080".to_string(),
606618
proxy_secret: "test-secret".to_string(),
619+
psid: "psid".to_string(),
607620
};
608621
assert_eq!(publisher.origin_host(), "192.168.1.1:8080");
609622

@@ -613,6 +626,7 @@ mod tests {
613626
cookie_domain: ".example.com".to_string(),
614627
origin_url: "http://[::1]:8080".to_string(),
615628
proxy_secret: "test-secret".to_string(),
629+
psid: "psid".to_string(),
616630
};
617631
assert_eq!(publisher.origin_host(), "[::1]:8080");
618632
}

trusted-server.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ domain = "test-publisher.com"
88
cookie_domain = ".test-publisher.com"
99
origin_url = "https://origin.test-publisher.com"
1010
proxy_secret = "change-me-proxy-secret"
11+
psid = "psid"
1112

1213
[prebid]
1314
server_url = "http://68.183.113.79:8000"

0 commit comments

Comments
 (0)