fix latin-1 errors
This commit is contained in:
18
app/api.py
18
app/api.py
@@ -3,10 +3,13 @@ from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
|||||||
from .deps import get_geo
|
from .deps import get_geo
|
||||||
from .config import settings
|
from .config import settings
|
||||||
from .geo import reload_provider
|
from .geo import reload_provider
|
||||||
|
from urllib.parse import quote
|
||||||
import secrets
|
import secrets
|
||||||
import ipaddress
|
import ipaddress
|
||||||
import re
|
import re
|
||||||
import json
|
import json
|
||||||
|
import unicodedata
|
||||||
|
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
security = HTTPBasic()
|
security = HTTPBasic()
|
||||||
@@ -37,6 +40,14 @@ def _check_admin(creds: HTTPBasicCredentials):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def _safe_hdr(v: str) -> str:
|
||||||
|
try:
|
||||||
|
v.encode("latin-1")
|
||||||
|
return v
|
||||||
|
except UnicodeEncodeError:
|
||||||
|
return unicodedata.normalize("NFKD", v).encode("ascii", "ignore").decode("ascii") or "?"
|
||||||
|
|
||||||
|
|
||||||
def _normalize_ip_str(ip_raw: str) -> str | None:
|
def _normalize_ip_str(ip_raw: str) -> str | None:
|
||||||
"""Usuń port, whitespace i ewentualne cudzysłowy"""
|
"""Usuń port, whitespace i ewentualne cudzysłowy"""
|
||||||
if not ip_raw:
|
if not ip_raw:
|
||||||
@@ -82,13 +93,12 @@ def geo_headers(data: dict) -> dict:
|
|||||||
city = data.get("city")
|
city = data.get("city")
|
||||||
ip_val = data.get("ip")
|
ip_val = data.get("ip")
|
||||||
if ip_val and country:
|
if ip_val and country:
|
||||||
h["X-IP-ADDRESS"] = ip_val
|
h["X-IP-ADDRESS"] = _safe_hdr(str(ip_val))
|
||||||
h["X-COUNTRY"] = country
|
h["X-COUNTRY"] = _safe_hdr(str(country))
|
||||||
if city:
|
if city:
|
||||||
h["X-CITY"] = city
|
h["X-CITY"] = _safe_hdr(str(city))
|
||||||
return h
|
return h
|
||||||
|
|
||||||
|
|
||||||
def get_client_ip(request: Request) -> str:
|
def get_client_ip(request: Request) -> str:
|
||||||
"""
|
"""
|
||||||
Zwraca IP klienta biorąc pod uwagę:
|
Zwraca IP klienta biorąc pod uwagę:
|
||||||
|
|||||||
14
app/main.py
14
app/main.py
@@ -2,7 +2,7 @@ from fastapi import FastAPI, Request, Response
|
|||||||
from fastapi.responses import JSONResponse, PlainTextResponse
|
from fastapi.responses import JSONResponse, PlainTextResponse
|
||||||
from starlette.middleware.base import BaseHTTPMiddleware
|
from starlette.middleware.base import BaseHTTPMiddleware
|
||||||
from .deps import get_geo
|
from .deps import get_geo
|
||||||
from .api import get_client_ip, router
|
from .api import get_client_ip, router, geo_headers
|
||||||
from .config import settings
|
from .config import settings
|
||||||
import uvicorn
|
import uvicorn
|
||||||
|
|
||||||
@@ -17,16 +17,8 @@ async def add_geo_headers(request, call_next):
|
|||||||
|
|
||||||
response: Response = await call_next(request)
|
response: Response = await call_next(request)
|
||||||
|
|
||||||
country = data.get("country", {}).get("name") if data.get("country") else None
|
for k, v in geo_headers(data).items():
|
||||||
city = data.get("city")
|
response.headers[k] = v
|
||||||
ip_val = data.get("ip")
|
|
||||||
|
|
||||||
if ip_val and country:
|
|
||||||
response.headers["X-IP-ADDRESS"] = ip_val
|
|
||||||
response.headers["X-COUNTRY"] = country
|
|
||||||
if city:
|
|
||||||
response.headers["X-CITY"] = city
|
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user