From d5b3f73cefdb2a07cb4165097b4cef224c70c025 Mon Sep 17 00:00:00 2001 From: HIllya51 <1871535768@qq.com> Date: Thu, 11 Jan 2024 00:45:06 +0800 Subject: [PATCH] fix --- .../LunaTranslator/network/libcurl/libcurl.py | 2 ++ .../LunaTranslator/network/libcurl/requests.py | 12 +++++++++++- .../LunaTranslator/network/requests_common.py | 4 +--- .../LunaTranslator/network/winhttp/requests.py | 17 +++++++++-------- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/LunaTranslator/LunaTranslator/network/libcurl/libcurl.py b/LunaTranslator/LunaTranslator/network/libcurl/libcurl.py index d2c597de..ded44836 100644 --- a/LunaTranslator/LunaTranslator/network/libcurl/libcurl.py +++ b/LunaTranslator/LunaTranslator/network/libcurl/libcurl.py @@ -262,6 +262,8 @@ curl_ws_send.restype=CURLcode curl_easy_duphandle=libcurl.curl_easy_duphandle curl_easy_duphandle.argtypes=CURL, curl_easy_duphandle.restype=CURL +curl_easy_reset=libcurl.curl_easy_reset +curl_easy_reset.argtypes=CURL, CURLWS_TEXT=1<<0 CURLWS_BINARY=1<<1 CURLWS_CLOSE=1<<3 diff --git a/LunaTranslator/LunaTranslator/network/libcurl/requests.py b/LunaTranslator/LunaTranslator/network/libcurl/requests.py index e21cdc67..4f3ed4d5 100644 --- a/LunaTranslator/LunaTranslator/network/libcurl/requests.py +++ b/LunaTranslator/LunaTranslator/network/libcurl/requests.py @@ -10,6 +10,15 @@ class autostatus: def __del__(self): self.ref._status=0 +class Response(ResponseBase): + def __init__(self): + super().__init__() + self.last_error=0 + def iter_content(self,chunk_size=1024): + yield self.content + def raise_for_status(self): + if self.last_error: + raise CURLException(self.last_error) class Session(Sessionbase): def __init__(self) -> None: @@ -95,9 +104,10 @@ class Session(Sessionbase): resp=Response() resp.content=self._getmembyte(_content) resp.status_code=self._getStatusCode(curl) - + resp.last_error=self.last_error resp.headers=self._update_header_cookie(self._getmembyte(_headers).decode('utf8')) resp.cookies=self.cookies + curl_easy_reset(curl) return resp Sessionimpl[0]=Session diff --git a/LunaTranslator/LunaTranslator/network/requests_common.py b/LunaTranslator/LunaTranslator/network/requests_common.py index e3ca959c..81888798 100644 --- a/LunaTranslator/LunaTranslator/network/requests_common.py +++ b/LunaTranslator/LunaTranslator/network/requests_common.py @@ -46,7 +46,7 @@ class CaseInsensitiveDict(MutableMapping): def __repr__(self): return str(dict(self.items())) -class Response: +class ResponseBase: def __init__(self): self.headers=CaseInsensitiveDict() self.cookies={} @@ -67,8 +67,6 @@ class Response: return charset def json(self): return json.loads(self.text) - def iter_content(self,chunk_size=1024): - yield self.content class Sessionbase: def __init__(self) -> None: self.UA='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36' diff --git a/LunaTranslator/LunaTranslator/network/winhttp/requests.py b/LunaTranslator/LunaTranslator/network/winhttp/requests.py index 053912a9..2d73de4f 100644 --- a/LunaTranslator/LunaTranslator/network/winhttp/requests.py +++ b/LunaTranslator/LunaTranslator/network/winhttp/requests.py @@ -7,7 +7,7 @@ try: from brotli_dec import decompress except: pass -class winhttp_resp(Response): +class Response(ResponseBase): def iter_content(self,chunk_size=1024): downloadedSize=DWORD() buff=create_string_buffer(chunk_size) @@ -19,17 +19,16 @@ class winhttp_resp(Response): del self.hconn break yield buff[:downloadedSize.value] + def raise_for_status(self): + error=GetLastError() + if error: + raise WinhttpException(error) class Session(Sessionbase): def __init__(self) -> None: super().__init__() self.hSession=AutoWinHttpHandle(WinHttpOpen(self.UA,WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,WINHTTP_NO_PROXY_NAME,WINHTTP_NO_PROXY_BYPASS,0)) if self.hSession==0: raise WinhttpException(GetLastError()) - - def raise_for_status(self): - error=GetLastError() - if error: - raise WinhttpException(error) def _getheaders(self,hreq): @@ -49,7 +48,9 @@ class Session(Sessionbase): pointer(dwSize), None ) if bResults==0: - self.raise_for_status() + error=GetLastError() + if error: + raise WinhttpException(error) return dwStatusCode.value def _set_proxy(self,hsess,proxy): @@ -81,7 +82,7 @@ class Session(Sessionbase): raise WinhttpException(GetLastError()) headers=self._update_header_cookie(self._getheaders(hRequest)) - resp=winhttp_resp() + resp=Response() resp.status_code=self._getStatusCode(hRequest) resp.headers=headers resp.cookies=self.cookies