1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
pub mod abi;
pub mod backend;
pub mod body;
pub mod geo;
pub mod log;
pub mod request;
pub mod response;
pub mod error {
pub use anyhow::{anyhow, bail, ensure, Error};
}
pub use crate::body::Body;
pub use crate::error::Error;
pub use crate::request::{
downstream_client_ip_addr, downstream_original_header_count, downstream_original_header_names,
downstream_request, downstream_tls_cipher_openssl_name, downstream_tls_client_hello,
downstream_tls_protocol, select, PendingRequest, RequestBuilderExt, RequestExt,
};
pub use crate::response::ResponseExt;
pub use fastly_macros::main;
pub use http::{self, Request, Response};
pub(crate) const HEADER_NAME_MAX_LEN: usize = 4096;
pub(crate) const HEADER_VALUE_MAX_LEN: usize = 4096;
pub(crate) const METHOD_MAX_LEN: usize = 1024;
pub(crate) const URI_MAX_LEN: usize = 4096;
pub fn uap_parse(
user_agent: &str,
) -> Result<(String, Option<String>, Option<String>, Option<String>), Error> {
let user_agent: &[u8] = user_agent.as_ref();
let max_length = 255;
let mut family = Vec::with_capacity(max_length);
let mut major = Vec::with_capacity(max_length);
let mut minor = Vec::with_capacity(max_length);
let mut patch = Vec::with_capacity(max_length);
let mut family_nwritten = 0;
let mut major_nwritten = 0;
let mut minor_nwritten = 0;
let mut patch_nwritten = 0;
let status = unsafe {
abi::fastly_uap::parse(
user_agent.as_ptr(),
user_agent.len(),
family.as_mut_ptr(),
family.capacity(),
&mut family_nwritten,
major.as_mut_ptr(),
major.capacity(),
&mut major_nwritten,
minor.as_mut_ptr(),
minor.capacity(),
&mut minor_nwritten,
patch.as_mut_ptr(),
patch.capacity(),
&mut patch_nwritten,
)
};
if status.is_err() {
return Err(Error::msg("fastly_uap::parse failed"));
}
assert!(
family_nwritten <= family.capacity(),
"fastly_uap::parse wrote too many bytes for family"
);
unsafe {
family.set_len(family_nwritten);
}
assert!(
major_nwritten <= major.capacity(),
"fastly_uap::parse wrote too many bytes for major"
);
unsafe {
major.set_len(major_nwritten);
}
assert!(
minor_nwritten <= minor.capacity(),
"fastly_uap::parse wrote too many bytes for minor"
);
unsafe {
minor.set_len(minor_nwritten);
}
assert!(
patch_nwritten <= patch.capacity(),
"fastly_uap::parse wrote too many bytes for patch"
);
unsafe {
patch.set_len(patch_nwritten);
}
Ok((
String::from_utf8_lossy(&family).to_string(),
Some(String::from_utf8_lossy(&major).to_string()),
Some(String::from_utf8_lossy(&minor).to_string()),
Some(String::from_utf8_lossy(&patch).to_string()),
))
}
pub fn init() {
use abi::{fastly::init, XQD_ABI_VERSION};
unsafe { init(XQD_ABI_VERSION) };
}