Skip to main content

Read CDN properties

CDN send several information to EdgeCompute application that can be retrieve.

List of properties available

PropertyDescription
x_real_ipIP of the real user
asnasn from x_real_ip
continentcontinent from x_real_ip
countryLU / PL / etc. (geoip2_country_code) from x_real_ip
country.namecountry from x_real_ip
regionregion from x_real_ip (if in GeoIP database)
citycity from x_real_ip (if in GeoIP database)
geo.latgeo latitude from x_real_ip
geo.longgeo longitude from x_real_ip
urlThe complete requested url with host
schemeThe url scheme
hostHostname of the resource (prefixed by shield_ on shield)
pathThe path of the requested url
extensionThe file extension
queryThe 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;
    }
}
[package]
name = "cdn-read-property"
version = "1.0.0"
edition = "2021"
description = "Proxy-Wasm example: Retrieve cdn information"


[lib]
crate-type = ["cdylib"]
path = "src/cdn-read-property.rs"

[dependencies]
fastedge = "0.2.0"
proxy-wasm = "0.2.1"