From d7ebef036a4bc57304cc682b27c7aaa57bee7819 Mon Sep 17 00:00:00 2001 From: HIllya51 <1871535768@qq.com> Date: Wed, 10 Jan 2024 11:23:34 +0800 Subject: [PATCH] fix --- .../network/libcurl/requests.py | 11 ++++++++-- .../LunaTranslator/network/requests_common.py | 20 +++++++++---------- .../network/winhttp/requests.py | 4 ++-- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/LunaTranslator/LunaTranslator/network/libcurl/requests.py b/LunaTranslator/LunaTranslator/network/libcurl/requests.py index 551504cb..a3cf71dd 100644 --- a/LunaTranslator/LunaTranslator/network/libcurl/requests.py +++ b/LunaTranslator/LunaTranslator/network/libcurl/requests.py @@ -49,15 +49,22 @@ class Session(Sessionbase): def _getmembyte(self,mem): return cast(mem.memory,POINTER(c_char))[:mem.size] def request_impl(self, - method,scheme,server,port,param,url,headers,dataptr,datalen,proxy,stream,verify ): + method,scheme,server,port,param,url,headers,cookies,dataptr,datalen,proxy,stream,verify): - + headers=self._parseheader(headers,None)#curl对于headers中有cookie且session中也有cookie的情况下,不会自动合并而是会有两个cookie键,所以不能在header里放cookie if self._status==0: curl=self.curl __=autostatus(self) else: #不能多线程同时复用同一个curl对象 curl=AutoCURLHandle(curl_easy_duphandle(self.curl)) + if cookies: + cookies.update(self.cookies) + else: + cookies=self.cookies + if cookies: + cookie=self._parsecookie(cookies) + curl_easy_setopt(curl, CURLoption.CURLOPT_COOKIE, cookie.encode('utf8')); curl_easy_setopt(curl,CURLoption.CURLOPT_CUSTOMREQUEST,method.upper().encode('utf8')) self.last_error=curl_easy_setopt(curl,CURLoption.CURLOPT_URL,url.encode('utf8')) diff --git a/LunaTranslator/LunaTranslator/network/requests_common.py b/LunaTranslator/LunaTranslator/network/requests_common.py index 0a653b51..55a02476 100644 --- a/LunaTranslator/LunaTranslator/network/requests_common.py +++ b/LunaTranslator/LunaTranslator/network/requests_common.py @@ -140,16 +140,16 @@ class Sessionbase: _x=[] if cookies: - - self.cookies.update(cookies) - _c=[] - for k ,v in self.cookies.items(): - _c.append('{}={}'.format(k,v)) - cookie='; '.join(_c) - headers.update({'Cookie':cookie}) + cookie=self._parsecookie(cookies) + headers.update({'Cookie':cookie}) for k in sorted(headers.keys()): _x.append('{}: {}'.format(k,headers[k])) return _x + def _parsecookie(self,cookie): + _c=[] + for k ,v in cookie.items(): + _c.append('{}={}'.format(k,v)) + return '; '.join(_c) def _update_header_cookie(self,headerstr): self.headers,cookies=self._parseheader2dict(headerstr) self.cookies.update(cookies) @@ -162,7 +162,7 @@ class Sessionbase: if line[:idx].lower()=='set-cookie': _c=line[idx+2:].split('; ')[0] _idx=_c.find('=') - cookie[_c[:_idx]]=_c[idx+1:] + cookie[_c[:_idx]]=_c[_idx+1:] else: header[line[:idx]]=line[idx+2:] return CaseInsensitiveDict(header),cookie @@ -199,9 +199,9 @@ class Sessionbase: scheme,server,port,param,url=self._parseurl(url,params) headers,dataptr,datalen=self._parsedata(data,headers,json) - headers=self._parseheader(headers,cookies) proxy= proxies.get(scheme,None) if proxies else None - _= self.request_impl(method,scheme,server,port,param,url,headers,dataptr,datalen,proxy,stream,verify) + + _= self.request_impl(method,scheme,server,port,param,url,headers,cookies,dataptr,datalen,proxy,stream,verify) return _ Sessionimpl=[Sessionbase] diff --git a/LunaTranslator/LunaTranslator/network/winhttp/requests.py b/LunaTranslator/LunaTranslator/network/winhttp/requests.py index 461bc189..c28033af 100644 --- a/LunaTranslator/LunaTranslator/network/winhttp/requests.py +++ b/LunaTranslator/LunaTranslator/network/winhttp/requests.py @@ -39,8 +39,8 @@ class Session(Sessionbase): winhttpsetproxy(hsess,proxy) def request_impl(self, - method,scheme,server,port,param,url,headers,dataptr,datalen,proxy,stream,verify): - + method,scheme,server,port,param,url,headers,cookies,dataptr,datalen,proxy,stream,verify): + headers=self._parseheader(headers,cookies) flag=WINHTTP_FLAG_SECURE if scheme=='https' else 0 #print(server,port,param,dataptr) headers='\r\n'.join(headers)