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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
//! Device detection based on the User-Agent header.
use serde::{Deserialize, Serialize};
use crate::abi::{self, FastlyStatus};
use crate::limits;
/// Look up the data associated with a particular User-Agent string.
pub fn lookup(user_agent: &str) -> Option<Device> {
lookup_impl(user_agent, limits::INITIAL_DEVICE_DETECTION_BUF_SIZE)
}
fn lookup_impl(user_agent: &str, max_length: usize) -> Option<Device> {
let mut buf = Vec::with_capacity(max_length);
let mut nwritten: usize = 0;
let status = unsafe {
abi::fastly_device_detection::lookup(
user_agent.as_ptr(),
user_agent.len(),
buf.as_mut_ptr(),
buf.capacity(),
&mut nwritten,
)
};
let status = match status {
// If the buffer length wasn't enough, try again with a larger buffer.
// This failure mode should not repeat.
FastlyStatus::BUFLEN if nwritten != 0 => {
buf.resize(nwritten, 0);
nwritten = 0;
unsafe {
abi::fastly_device_detection::lookup(
user_agent.as_ptr(),
user_agent.len(),
buf.as_mut_ptr(),
buf.capacity(),
&mut nwritten,
)
}
}
s => s,
};
match status.result() {
Ok(_) => {
assert!(
nwritten <= buf.capacity(),
"fastly_device_detection::lookup wrote too many bytes"
);
unsafe {
buf.set_len(nwritten);
}
serde_json::from_slice::<'_, Device>(&buf).ok()
}
Err(_) => None,
}
}
/// The device data associated with a particular User-Agent string.
#[derive(Debug, Deserialize, Serialize)]
pub struct Device {
user_agent: Option<RawUserAgent>,
os: Option<RawOS>,
device: Option<RawDevice>,
}
impl Device {
/// The name of the client device.
pub fn device_name(&self) -> Option<&str> {
match &self.device {
None => None,
Some(d) => d.name.as_deref(),
}
}
/// The brand of the client device, possibly different from the
/// manufacturer of that device.
pub fn brand(&self) -> Option<&str> {
match &self.device {
None => None,
Some(d) => d.brand.as_deref(),
}
}
/// The model of the client device.
pub fn model(&self) -> Option<&str> {
match &self.device {
None => None,
Some(d) => d.model.as_deref(),
}
}
/// A string representation of the primary client platform hardware.
/// The most commonly used device types are also identified via
/// boolean variables. Because a device may have multiple device
/// types and this variable only has the primary type, we recommend
/// using the boolean variables for logic and using this string
/// representation for logging.
pub fn hwtype(&self) -> Option<&str> {
match &self.device {
None => None,
Some(d) => d.hwtype.as_deref(),
}
}
/// The client device is a reading device (like a Kindle).
pub fn is_ereader(&self) -> Option<bool> {
match &self.device {
None => None,
Some(d) => d.is_ereader,
}
}
/// The client device is a video game console (like a PlayStation or Xbox).
pub fn is_gameconsole(&self) -> Option<bool> {
match &self.device {
None => None,
Some(d) => d.is_gameconsole,
}
}
/// The client device is a media player (like Blu-ray players, iPod
/// devices, and smart speakers such as Amazon Echo).
pub fn is_mediaplayer(&self) -> Option<bool> {
match &self.device {
None => None,
Some(d) => d.is_mediaplayer,
}
}
/// The client device is a mobile phone.
pub fn is_mobile(&self) -> Option<bool> {
match &self.device {
None => None,
Some(d) => d.is_mobile,
}
}
/// The client device is a smart TV.
pub fn is_smarttv(&self) -> Option<bool> {
match &self.device {
None => None,
Some(d) => d.is_smarttv,
}
}
/// The client device is a tablet (like an iPad).
pub fn is_tablet(&self) -> Option<bool> {
match &self.device {
None => None,
Some(d) => d.is_tablet,
}
}
/// The client device is a set-top box or other TV player (like a Roku or Apple TV).
pub fn is_tvplayer(&self) -> Option<bool> {
match &self.device {
None => None,
Some(d) => d.is_tvplayer,
}
}
/// The client is a desktop web browser.
pub fn is_desktop(&self) -> Option<bool> {
match &self.device {
None => None,
Some(d) => d.is_desktop,
}
}
/// The client device's screen is touch sensitive.
pub fn is_touchscreen(&self) -> Option<bool> {
match &self.device {
None => None,
Some(d) => d.is_touchscreen,
}
}
}
#[derive(Debug, Deserialize, Serialize)]
struct RawUserAgent {
name: Option<String>,
major: Option<String>,
minor: Option<String>,
patch: Option<String>,
bot_name: Option<String>,
is_bot: Option<bool>,
is_downloader: Option<bool>,
is_feedreader: Option<bool>,
identified: Option<bool>,
}
#[derive(Debug, Deserialize, Serialize)]
struct RawOS {
name: Option<String>,
major: Option<String>,
minor: Option<String>,
patch: Option<String>,
patch_minor: Option<String>,
}
#[derive(Debug, Deserialize, Serialize)]
struct RawDevice {
name: Option<String>,
brand: Option<String>,
model: Option<String>,
hwtype: Option<String>,
is_ereader: Option<bool>,
is_gameconsole: Option<bool>,
is_mediaplayer: Option<bool>,
is_mobile: Option<bool>,
is_smarttv: Option<bool>,
is_tablet: Option<bool>,
is_tvplayer: Option<bool>,
is_desktop: Option<bool>,
is_touchscreen: Option<bool>,
}