cut down on exposed platform information

This commit is contained in:
Jakob Pinterits
2024-12-28 17:01:09 +01:00
parent 83dbf23c23
commit 5aedd3886e
3 changed files with 0 additions and 506 deletions
-76
View File
@@ -117,67 +117,6 @@ class Session(unicall.Unicall):
good practice to add a fallback for unknown strings.
`os_name`: The name of the operating system the client is using. Possible
values are:
- `"windows"`
- `"macos"`
- `"linux"`
- `"android"`
- `"ios"`
- `"unknown"`
Warning: This literal shouldn't be seen as complete. Future versions of
Rio may add additional values as new operating systems get popular.
Extending this literal is not considered a compatibility break and
may happen even in minor versions. If you rely on this value, it's
good practice to add a fallback for unknown strings.
`browser_name`: A human readable name for the browser the client is using.
Given the vast number of browsers available, there is no exhaustive list
of possible values. Because of this, the value is most suitable to
display to humans. If you need to make decisions based on the browser
type, `browser_engine` is the more reliable choice.
*Some* typical values are:
- `"Chrome"`
- `"Firefox"`
- `"Safari"`
- `"unknown"`
`browser_engine`: The engine the client's browser is based on. Despite the
huge number of available browsers, many of them are based on just a few
common engines. Possible values are:
- `"chrome"`: e.g Chrome, Edge, Opera
- `"firefox"`: e.g. Firefox, Waterfox
- `"webkit"`: e.g. Safari, Brave
- `"unknown"`
Warning: This literal shouldn't be seen as complete. Future versions of
Rio may add additional values as new engines establish themselves.
Extending this literal is not considered a compatibility break and
may happen even in minor versions. If you rely on this value, it's
good practice to add a fallback for unknown strings.
`device_type`: The class of device the client is using. This is a rough
categorization of the device based on its capabilities. Possible values
are:
- `"desktop"`: A traditional desktop or laptop computer
- `"tablet"`: Oversized phones
- `"phone"`: Phones
- `"crawler"`: Search engines and other automated systems
- `"unknown"`: None of the above, or the device is not recognized
Warning: This literal shouldn't be seen as complete. Future versions of
Rio may add additional values as new types of devices get popular.
Extending this literal is not considered a compatibility break and
may happen even in minor versions. If you rely on this value, it's
good practice to add a fallback for unknown strings.
`base_url`: This is the URL the app's home page is hosted at, as seen from
the client. So if the user needs to type `https://example.com/my-app/`
to see the app, this will be `https://example.com/my-app/`. Note that
@@ -224,11 +163,6 @@ class Session(unicall.Unicall):
primary_pointer_type: t.Literal["mouse", "touch"]
os_name: t.Literal["windows", "macos", "linux", "android", "ios", "unknown"]
browser_name: str
browser_engine: t.Literal["chrome", "firefox", "webkit", "unknown"]
device_type: t.Literal["desktop", "tablet", "phone", "unknown"]
theme: rio.Theme
http_headers: t.Mapping[str, str]
@@ -294,16 +228,6 @@ class Session(unicall.Unicall):
self.primary_pointer_type = primary_pointer_type
# Some information needs to be extracted from the user agent
(
self.os_name, # type: ignore (literal vs. string)
self.browser_name,
self.browser_engine, # type: ignore (literal vs. string)
self.device_type, # type: ignore (literal vs. string)
) = self._parse_system_information_from_user_agent(
http_headers.get("user-agent", "")
)
self._is_maximized = False
self._is_fullscreen = False
-91
View File
@@ -15,7 +15,6 @@ from pathlib import Path
import imy.assets
import typing_extensions as te
import ua_parser
from PIL.Image import Image
from yarl import URL
@@ -764,93 +763,3 @@ def soft_sort(
for element, _, _ in keyed_elements:
elements.append(element)
def parse_system_information_from_user_agent(
user_agent_str: str,
) -> tuple[
str, # OS name
str, # Browser name
str, # Browser engine
str, # Device type
]:
"""
Extracts information about the client's system from the user agent
string.
The return types match what is expected by `rio.Session`, but the exact
literals aren't repeated here for brevity.
"""
# Handle empty user agents
if not user_agent_str:
return "unknown", "unknown", "unknown", "unknown"
# Let a specialized library do the parsing
user_agent = ua_parser.parse(user_agent_str)
import revel
revel.debug(user_agent)
# Get the operating system
if user_agent.os is None:
os_name = "unknown"
else:
os_name = user_agent.os.family
match os_name:
case "Windows":
device_type = "desktop"
case "Mac OS X":
device_type = "desktop"
case "iOS":
device_type = (
"phone" # How to distinguish between phone and tablet?
)
case "Android":
device_type = (
"phone" # How to distinguish between phone and tablet?
)
case _:
device_type = "unknown"
# Get the browser name
if user_agent.user_agent is None:
browser_name = "unknown"
else:
browser_name = user_agent.user_agent.family.removeprefix(" Mobile")
# Get the browser engine
match browser_name:
case "Chrome":
browser_engine = "chrome"
case "Firefox":
browser_engine = "firefox"
case "Safari":
browser_engine = "webkit"
case "Edge":
browser_engine = "chrome"
case _:
browser_engine = "unknown"
# Get the device type
if user_agent.device is not None:
a = 1
a = 2
# Done
return (
os_name,
browser_name,
browser_engine,
device_type,
)
-339
View File
@@ -1,339 +0,0 @@
"""
Parses various user agents, making sure the parsed information is correct.
"""
import pytest
import rio.utils
@pytest.mark.parametrize(
"user_agent_string, os_name, browser_name, browser_engine, device_type",
[
# Invalid user agent strings
#
# These are incredible feats of my own imagination.
[
"",
"unknown",
"unknown",
"unknown",
"unknown",
],
[
"totally/made up user///agent",
"unknown",
"unknown",
"unknown",
"unknown",
],
[
"Mozilla/5.0 (MyOsIsBetterThanUrs) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Mobile Safari/537.3",
"unknown",
"Chrome",
"chrome",
"desktop",
],
[
"Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/27.0 Foobar Mobile Safari/537.3",
"unknown",
"Foobar",
"unknown",
"desktop",
],
# Valid user agent strings
#
# These were taken from https://useragents.me/ on 2024-12-28.
(
"Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Mobile Safari/537.3",
"Android",
"Chrome",
"chrome",
"phone",
),
(
"Mozilla/5.0 (iPhone; CPU iPhone OS 18_1_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1.1 Mobile/15E148 Safari/604.",
"iOS",
"Chrome",
"chrome",
"phone",
),
(
"Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/27.0 Chrome/125.0.0.0 Mobile Safari/537.3",
"Android",
"Chrome",
"chrome",
"phone",
),
(
"Mozilla/5.0 (iPhone; CPU iPhone OS 18_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) GSA/346.1.704810410 Mobile/15E148 Safari/604.",
"iOS",
"Chrome",
"chrome",
"phone",
),
(
"Mozilla/5.0 (iPhone; CPU iPhone OS 17_6_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.6 Mobile/15E148 Safari/604.",
"iOS",
"Chrome",
"chrome",
"phone",
),
(
"Mozilla/5.0 (iPhone; CPU iPhone OS 18_1_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/131.0.6778.134 Mobile/15E148 Safari/604.",
"iOS",
"Chrome",
"chrome",
"phone",
),
(
"Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Mobile Safari/537.36 OPR/86.0.0.",
"Android",
"Chrome",
"chrome",
"phone",
),
(
"Mozilla/5.0 (Linux; Android 11; moto e20 Build/RONS31.267-94-14) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.6778.135 Mobile Safari/537.3",
"Android",
"Chrome",
"chrome",
"phone",
),
(
"Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Mobile Safari/537.3",
"Android",
"Chrome",
"chrome",
"phone",
),
(
"Mozilla/5.0 (Android 13; Mobile; rv:133.0) Gecko/133.0 Firefox/133.",
"Android",
"Firefox",
"firefox",
"phone",
),
(
"Mozilla/5.0 (iPhone; CPU iPhone OS 17_0_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0.1 Mobile/15E148 Safari/604.",
"iOS",
"Chrome",
"chrome",
"phone",
),
(
"Mozilla/5.0 (iPhone; CPU iPhone OS 17_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Mobile/15E148 Safari/604.",
"iOS",
"Chrome",
"chrome",
"phone",
),
(
"Mozilla/5.0 (iPhone; CPU iPhone OS 18_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.2 Mobile/15E148 Safari/604.",
"iOS",
"Chrome",
"chrome",
"phone",
),
(
"Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Mobile Safari/537.3",
"Android",
"Chrome",
"chrome",
"phone",
),
(
"Mozilla/5.0 (Linux; arm_64; Android 15; 24030PN60G) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.98 YaBrowser/24.12.1.98.00 SA/3 Mobile Safari/537.3",
"Android",
"Chrome",
"chrome",
"phone",
),
(
"Mozilla/5.0 (Linux; Android 7.0; SM-G930V Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.125 Mobile Safari/537.36 (compatible; Google-Read-Aloud; +https://support.google.com/webmasters/answer/1061943",
"Android",
"Chrome",
"chrome",
"phone",
),
(
"Mozilla/5.0 (Linux; Android 13; M2103K19G) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Mobile Safari/537.3",
"Android",
"Chrome",
"chrome",
"phone",
),
(
"Mozilla/5.0 (Linux; Android 12; 220733SG Build/SP1A.210812.016) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.6778.135 Mobile Safari/537.3",
"Android",
"Chrome",
"chrome",
"phone",
),
(
"Mozilla/5.0 (Linux; Android 11; moto e20 Build/RONS31.267-94-14) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.6778.105 Mobile Safari/537.3",
"Android",
"Chrome",
"chrome",
"phone",
),
(
"Mozilla/5.0 (Linux; Android 10; SAMSUNG SM-G980F) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/22.0 Chrome/111.0.5563.116 Mobile Safari/537.3",
"Android",
"Chrome",
"chrome",
"phone",
),
(
"Mozilla/5.0 (Linux; Android 10; NEN-LX1 Build/HUAWEINEN-LX1; wv) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.105 Mobile Safari/537.36 HuaweiBrowser/15.0.4.312 HMSCore/6.14.0.32",
"Android",
"Chrome",
"chrome",
"phone",
),
(
"Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/25.0 Chrome/121.0.0.0 Mobile Safari/537.3",
"Android",
"Chrome",
"chrome",
"phone",
),
(
"Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Mobile Safari/537.3",
"Android",
"Chrome",
"chrome",
"phone",
),
(
"Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.3",
"Android",
"Chrome",
"chrome",
"phone",
),
(
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.6 Safari/605.1.1",
"macOS",
"Chrome",
"chrome",
"desktop",
),
(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.3",
"Windows",
"Chrome",
"chrome",
"desktop",
),
(
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.3",
"macOS",
"Chrome",
"chrome",
"desktop",
),
(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:133.0) Gecko/20100101 Firefox/133.",
"Windows",
"Firefox",
"firefox",
"desktop",
),
(
"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.",
"Windows",
"Firefox",
"firefox",
"desktop",
),
(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.",
"Windows",
"Chrome",
"chrome",
"desktop",
),
(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Herring/97.1.8280.8",
"Windows",
"Chrome",
"chrome",
"desktop",
),
(
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.3",
"Linux",
"Chrome",
"chrome",
"desktop",
),
(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 OPR/115.0.0.",
"Windows",
"Chrome",
"chrome",
"desktop",
),
(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 AtContent/95.5.5462.5",
"Windows",
"Chrome",
"chrome",
"desktop",
),
(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.1958",
"Windows",
"Chrome",
"chrome",
"desktop",
),
(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.3",
"Windows",
"Chrome",
"chrome",
"desktop",
),
(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 OPR/114.0.0.",
"Windows",
"Chrome",
"chrome",
"desktop",
),
(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.3",
"Windows",
"Chrome",
"chrome",
"desktop",
),
],
)
def test_parse_user_agent(
user_agent_string: str,
os_name: str,
browser_name: str,
browser_engine: str,
device_type: str,
) -> None:
# Get information from the user agent string
(
parsed_os_name,
parsed_browser_name,
parsed_browser_engine,
parsed_device_type,
) = rio.utils.parse_system_information_from_user_agent(user_agent_string)
# Verify the parsed information is correct
assert parsed_os_name == os_name
assert parsed_browser_name == browser_name
assert parsed_browser_engine == browser_engine
assert parsed_device_type == device_type
# rio.utils.parse_system_information_from_user_agent(
# "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.3",
# )