File size: 586 Bytes
d411bd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
"""The visitor's address behind the Hugging Face proxy."""

from __future__ import annotations

from fastapi import Request


def client_ip(request: Request) -> str:
    """The address rate limits are counted against.

    uvicorn sees the proxy, so request.client alone would put every visitor in
    one bucket; the proxy lists the original client first in X-Forwarded-For.
    """
    forwarded = request.headers.get("x-forwarded-for", "")
    first = forwarded.split(",")[0].strip()
    if first:
        return first
    return request.client.host if request.client else "unknown"