Skip to content

Commit 24c267d

Browse files
makes the header names configurable
1 parent f79594d commit 24c267d

File tree

5 files changed

+53
-3
lines changed

5 files changed

+53
-3
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/gam.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1078,7 +1078,9 @@ mod tests {
10781078
use super::*;
10791079
use serde_json::json;
10801080

1081-
use crate::{constants::HEADER_SYNTHETIC_TRUSTED_SERVER, test_support::tests::create_test_settings};
1081+
use crate::{
1082+
constants::HEADER_SYNTHETIC_TRUSTED_SERVER, test_support::tests::create_test_settings,
1083+
};
10821084

10831085
fn create_test_request() -> Request {
10841086
let mut req = Request::new(Method::GET, "https://example.com/test");

crates/common/src/settings.rs

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

3038
impl Publisher {
@@ -530,6 +538,7 @@ mod tests {
530538
cookie_domain: ".example.com".to_string(),
531539
origin_url: "https://origin.example.com:8080".to_string(),
532540
proxy_secret: "test-secret".to_string(),
541+
psid: "psid".to_string(),
533542
};
534543
assert_eq!(publisher.origin_host(), "origin.example.com:8080");
535544

@@ -539,6 +548,7 @@ mod tests {
539548
cookie_domain: ".example.com".to_string(),
540549
origin_url: "https://origin.example.com".to_string(),
541550
proxy_secret: "test-secret".to_string(),
551+
psid: "psid".to_string(),
542552
};
543553
assert_eq!(publisher.origin_host(), "origin.example.com");
544554

@@ -548,6 +558,7 @@ mod tests {
548558
cookie_domain: ".example.com".to_string(),
549559
origin_url: "http://localhost:9090".to_string(),
550560
proxy_secret: "test-secret".to_string(),
561+
psid: "psid".to_string(),
551562
};
552563
assert_eq!(publisher.origin_host(), "localhost:9090");
553564

@@ -557,6 +568,7 @@ mod tests {
557568
cookie_domain: ".example.com".to_string(),
558569
origin_url: "localhost:9090".to_string(),
559570
proxy_secret: "test-secret".to_string(),
571+
psid: "psid".to_string(),
560572
};
561573
assert_eq!(publisher.origin_host(), "localhost:9090");
562574

@@ -566,6 +578,7 @@ mod tests {
566578
cookie_domain: ".example.com".to_string(),
567579
origin_url: "http://192.168.1.1:8080".to_string(),
568580
proxy_secret: "test-secret".to_string(),
581+
psid: "psid".to_string(),
569582
};
570583
assert_eq!(publisher.origin_host(), "192.168.1.1:8080");
571584

@@ -575,6 +588,7 @@ mod tests {
575588
cookie_domain: ".example.com".to_string(),
576589
origin_url: "http://[::1]:8080".to_string(),
577590
proxy_secret: "test-secret".to_string(),
591+
psid: "psid".to_string(),
578592
};
579593
assert_eq!(publisher.origin_host(), "[::1]:8080");
580594
}

trusted-server.toml

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

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

0 commit comments

Comments
 (0)