pub fn decode_request_data(
domain: &str,
) -> Result<DecodedClientRoutingLabel, DecodeLengthError>
Expand description
Returns a result containing either a DecodedClientRoutingLabel
or a
DecodeLengthError
.
The decode function takes in a &str param: domain
. This domain can be a FQDN
or just the dns label generated by the encode_request_data
function. It
decodes the string and formats it into a DecodedClientRoutingLabel
. If the
client routing label is not the first DNS label or is not included in domain
a DecodeLengthError
will be returned.
ยงExamples:
use amazon_cloudfront_client_routing_lib::decode_request_data;
// valid client routing label
let decoded_label = decode_request_data("abacaqdaaaaaaaamnjg3oubcyvrgm");
match decoded_label {
Ok(data) => {
assert_eq!([1, 2, 3, 0, 0, 0, 0, 0], data.client_subnet);
assert_eq!(24, data.subnet_mask);
assert_eq!(false, data.is_ipv6);
assert_eq!(15319960192071419084, data.cgid);
},
Err(e) => panic!("Decoding error when there shouldn't be: {}", e)
};
// fqdn with valid client routing label
let decoded_label = decode_request_data("abacaqdaaaaaaaamnjg3oubcyvrgm.example.com");
match decoded_label {
Ok(data) => {
assert_eq!([1, 2, 3, 0, 0, 0, 0, 0], data.client_subnet);
assert_eq!(24, data.subnet_mask);
assert_eq!(false, data.is_ipv6);
assert_eq!(15319960192071419084, data.cgid);
},
Err(e) => panic!("Decoding error when there shouldn't be: {}", e)
};
// fqdn with subdomain and valid client routing label
let decoded_label = decode_request_data("abacaqdaaaaaaaamnjg3oubcyvrgm.vod1.example.com");
match decoded_label {
Ok(data) => {
assert_eq!([1, 2, 3, 0, 0, 0, 0, 0], data.client_subnet);
assert_eq!(24, data.subnet_mask);
assert_eq!(false, data.is_ipv6);
assert_eq!(15319960192071419084, data.cgid);
},
Err(e) => panic!("Decoding error when there shouldn't be: {}", e)
};
// fqdn without valid client routing label
let decoded_label = decode_request_data("example.com");
match decoded_label {
Ok(data) => panic!("Should have thrown a DecodeLengthError"),
Err(e) => {
assert_eq!(format!("{}", e), "Passed 7 - expected 29 characters");
}
};
// client routing label needs to be the first DNS label
let decoded_label = decode_request_data("vod1.abacaqdaaaaaaaamnjg3oubcyvrgm.example.com");
match decoded_label {
Ok(data) => panic!("Should have thrown a DecodeLengthError"),
Err(e) => {
assert_eq!(format!("{}", e), "Passed 4 - expected 29 characters");
}
};
// invalid
let decoded_label = decode_request_data("abacaqdaaaaaaaamnjg3oubcy"); // invalid length
match decoded_label {
Ok(data) => panic!("Should have thrown a DecodeLengthError"),
Err(e) => {
assert_eq!(format!("{}", e), "Passed 25 - expected 29 characters");
}
};