init
This commit is contained in:
29
app/services/headers.py
Normal file
29
app/services/headers.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from typing import Mapping
|
||||
|
||||
from app.models import SubscriptionUserInfo
|
||||
|
||||
_SUBSCRIPTION_FIELDS = re.compile(r"(upload|download|total|expire)=(\d+)")
|
||||
|
||||
|
||||
def get_header_case_insensitive(headers: Mapping[str, str], name: str) -> str | None:
|
||||
target = name.lower()
|
||||
for key, value in headers.items():
|
||||
if key.lower() == target:
|
||||
return value
|
||||
return None
|
||||
|
||||
|
||||
def parse_subscription_userinfo(headers: Mapping[str, str]) -> SubscriptionUserInfo | None:
|
||||
raw = get_header_case_insensitive(headers, "Subscription-Userinfo")
|
||||
if not raw:
|
||||
return None
|
||||
|
||||
values: dict[str, int] = {}
|
||||
for key, value in _SUBSCRIPTION_FIELDS.findall(raw):
|
||||
values[key] = int(value)
|
||||
|
||||
info = SubscriptionUserInfo(**values)
|
||||
return None if info.is_empty() else info
|
||||
Reference in New Issue
Block a user