mirror of
https://github.com/hansjone/netx.git
synced 2026-10-09 00:50:46 +08:00
fix(UME): 按现场实测调整请求头并强制HTTP/1.1
- 请求头仅保留 content-type 与 accessToken,不再发送 accept/application - 统一通过 httpx.Client(http2=False) 调用,避免 UME 在 HTTP/2 下超时 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
7f2e4ec393
commit
3396111f2d
1 changed files with 11 additions and 6 deletions
|
|
@ -164,7 +164,6 @@ class UMEClient:
|
|||
|
||||
def _headers(self, *, include_token: bool = True) -> dict[str, str]:
|
||||
headers = {
|
||||
"accept": "application/yang-data+json",
|
||||
"content-type": self.content_type,
|
||||
}
|
||||
if include_token:
|
||||
|
|
@ -173,6 +172,12 @@ class UMEClient:
|
|||
headers[self.auth_header] = token
|
||||
return headers
|
||||
|
||||
def _client(self) -> httpx.Client:
|
||||
# Force HTTP/1.1 (disable HTTP/2) due to UME server compatibility issues.
|
||||
# httpx defaults to HTTP/1.1 unless http2=True, but some environments still
|
||||
# negotiate/behave unexpectedly; be explicit here.
|
||||
return httpx.Client(verify=self.verify_tls, timeout=self.timeout_s, http2=False)
|
||||
|
||||
def _extract_token_and_ttl(self, payload: dict[str, Any]) -> tuple[str, int | None]:
|
||||
token = ""
|
||||
ttl: int | None = None
|
||||
|
|
@ -229,7 +234,7 @@ class UMEClient:
|
|||
url = self._build_url(self.token_path)
|
||||
payload = {"login-info": {"user-name": self.username, "value": self.password}}
|
||||
try:
|
||||
with httpx.Client(verify=self.verify_tls, timeout=self.timeout_s) as client:
|
||||
with self._client() as client:
|
||||
resp = client.post(url, json=payload, headers=self._headers(include_token=False))
|
||||
if not resp.is_success:
|
||||
raise RuntimeError(f"ume_login_failed:{resp.status_code}:{resp.text[:240]}")
|
||||
|
|
@ -273,7 +278,7 @@ class UMEClient:
|
|||
return self.login(force=True)
|
||||
url = self._build_url(self.token_handshake_path)
|
||||
try:
|
||||
with httpx.Client(verify=self.verify_tls, timeout=self.timeout_s) as client:
|
||||
with self._client() as client:
|
||||
resp = client.post(url, headers=self._headers(include_token=True))
|
||||
if not resp.is_success:
|
||||
# handshake may fail if token expired; fallback to full login
|
||||
|
|
@ -307,7 +312,7 @@ class UMEClient:
|
|||
return True
|
||||
url = self._build_url(self.token_logout_path)
|
||||
try:
|
||||
with httpx.Client(verify=self.verify_tls, timeout=self.timeout_s) as client:
|
||||
with self._client() as client:
|
||||
resp = client.delete(url, headers=self._headers(include_token=True))
|
||||
if resp.status_code in (401, 403):
|
||||
# Token already invalid/expired on server side can be treated as logged out.
|
||||
|
|
@ -349,12 +354,12 @@ class UMEClient:
|
|||
retry_count = 0
|
||||
t0 = time()
|
||||
try:
|
||||
with httpx.Client(verify=self.verify_tls, timeout=self.timeout_s) as client:
|
||||
with self._client() as client:
|
||||
resp = client.request(m, url, params=params, json=body, headers=self._headers(include_token=True))
|
||||
if resp.status_code in (401, 403):
|
||||
retry_count = 1
|
||||
self.login(force=True)
|
||||
with httpx.Client(verify=self.verify_tls, timeout=self.timeout_s) as client:
|
||||
with self._client() as client:
|
||||
resp = client.request(m, url, params=params, json=body, headers=self._headers(include_token=True))
|
||||
if not resp.is_success:
|
||||
diag = RequestDiagnostics(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue