Read CDN properties
CDN send several information to EdgeCompute application that can be retrieve.
List of properties available
| Property | Description |
|---|---|
| x_real_ip | IP of the real user |
| asn | asn from x_real_ip |
| continent | continent from x_real_ip |
| country | LU / PL / etc. (geoip2_country_code) from x_real_ip |
| country.name | country from x_real_ip |
| region | region from x_real_ip (if in GeoIP database) |
| city | city from x_real_ip (if in GeoIP database) |
| geo.lat | geo latitude from x_real_ip |
| geo.long | geo longitude from x_real_ip |
| url | The complete requested url with host |
| scheme | The url scheme |
| host | Hostname of the resource (prefixed by shield_ on shield) |
| path | The path of the requested url |
| extension | The file extension |
| query | The query portion of the URL in the format of “name1=value1&name2=value2” |
Localisation information are retrieve from MaxMind GeoIP database from IP adress.
Compute
Use this solution in your CDB Edge Compute service:
use proxy_wasm::traits::*;
use proxy_wasm::types::*;
proxy_wasm::main! {{
proxy_wasm::set_log_level(LogLevel::Trace);
proxy_wasm::set_root_context(|_| -> Box<dyn RootContext> { Box::new(HttpHeadersRoot) });
}}
struct HttpHeadersRoot;
impl Context for HttpHeadersRoot {}
impl RootContext for HttpHeadersRoot {
fn create_http_context(&self, _context_id: u32) -> Option<Box<dyn HttpContext>> {
Some(Box::new(HttpHeaders {}))
}
fn get_type(&self) -> Option<ContextType> {
Some(ContextType::HttpContext)
}
}
struct HttpHeaders {}
impl Context for HttpHeaders {}
const INTERNAL_SERVER_ERROR: u32 = 500;
const FOUND: u32 = 302;
impl HttpContext for HttpHeaders {
fn on_http_request_headers(&mut self, _: usize, _: bool) -> Action {
let Some(country_code) = self.get_property(vec!["request.country"]) else {
self.send_http_response(
INTERNAL_SERVER_ERROR,
vec![],
Some(b"Malformed request - no country field"),
);
println!("Malformed request - no country field");
return Action::Pause;
};
let Ok(country_code) = std::str::from_utf8(&country_code) else {
self.send_http_response(
INTERNAL_SERVER_ERROR,
vec![],
Some(b"Malformed request - country not utf8 string"),
);
println!("Malformed request - country not utf8 string");
return Action::Pause;
};
println!("country: {}|", country_code);
return Action::Continue;
}
}