| 12345678910111213141516171819202122232425262728293031323334353637 |
- from dataclasses import dataclass
- from http.cookies import SimpleCookie
- @dataclass
- class RequestContext:
- gateway_session_id: str = ''
- source: str = ''
- def has_session(self):
- return bool(self.gateway_session_id)
- class RequestContextParser:
- def parse(self, headers):
- normalized = {str(k).lower(): str(v).strip() for k, v in (headers or {}).items()}
- header_value = normalized.get('x-gateway-session', '')
- if header_value.startswith('GWS_'):
- return RequestContext(header_value, 'header')
- authorization = normalized.get('authorization', '')
- prefix = 'bearer '
- if authorization.lower().startswith(prefix):
- token = authorization[len(prefix):].strip()
- if token.startswith('GWS_'):
- return RequestContext(token, 'authorization')
- cookie_header = normalized.get('cookie', '')
- if cookie_header:
- cookie = SimpleCookie()
- cookie.load(cookie_header)
- morsel = cookie.get('gateway_session_id')
- if morsel and morsel.value.startswith('GWS_'):
- return RequestContext(morsel.value, 'cookie')
- return RequestContext()
|