Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
HIllya51 committed Jul 8, 2024
1 parent 0657cfc commit 220b8be
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 54 deletions.
2 changes: 2 additions & 0 deletions LunaTranslator/LunaTranslator/network/libcurl/libcurl.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,10 @@ class CURLoption(c_int):
CURLOPT_COOKIEFILE = CURLOPTTYPE_STRINGPOINT + 31
CURLOPT_CUSTOMREQUEST = CURLOPTTYPE_STRINGPOINT + 36
CURLOPT_POST = CURLOPTTYPE_LONG + 47
CURLOPT_FOLLOWLOCATION = CURLOPTTYPE_LONG + 52
CURLOPT_POSTFIELDSIZE = CURLOPTTYPE_LONG + 60
CURLOPT_SSL_VERIFYPEER = CURLOPTTYPE_LONG + 64
CURLOPT_MAXREDIRS = CURLOPTTYPE_LONG + 68
CURLOPT_HEADERFUNCTION = CURLOPTTYPE_FUNCTIONPOINT + 79
CURLOPT_HTTPGET = CURLOPTTYPE_LONG + 80
CURLOPT_SSL_VERIFYHOST = CURLOPTTYPE_LONG + 81
Expand Down
42 changes: 21 additions & 21 deletions LunaTranslator/LunaTranslator/network/libcurl/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ def _perform(self, curl):
self.last_error = curl_easy_perform(curl)
self.raise_for_status()

def _set_allow_redirects(self, curl, allow_redirects):

curl_easy_setopt(curl, CURLoption.CURLOPT_FOLLOWLOCATION, int(allow_redirects))
# curl_easy_setopt(curl, CURLoption.CURLOPT_MAXREDIRS, 100) #默认50够了

@ExceptionFilter
def request_impl(
self,
Expand All @@ -103,6 +108,7 @@ def request_impl(
stream,
verify,
timeout,
allow_redirects,
):
curl = AutoCURLHandle(curl_easy_init())
curl_easy_setopt(curl, CURLoption.CURLOPT_COOKIEJAR, "")
Expand Down Expand Up @@ -146,6 +152,7 @@ def request_impl(

self._set_verify(curl, verify)
self._set_proxy(curl, proxy)
self._set_allow_redirects(curl, allow_redirects)
if datalen:
curl_easy_setopt(curl, CURLoption.CURLOPT_POSTFIELDS, dataptr)
curl_easy_setopt(curl, CURLoption.CURLOPT_POSTFIELDSIZE, datalen)
Expand All @@ -154,17 +161,21 @@ def request_impl(
resp.keeprefs.append(curl)
if stream:

def WriteMemoryCallback(queue, contents, size, nmemb, userp):
def WriteMemoryCallback(headerqueue, queue, contents, size, nmemb, userp):
if headerqueue:
headerqueue.put(0)
realsize = size * nmemb
queue.put(cast(contents, POINTER(c_char))[:realsize])
return realsize

_content = []
_headers = []
headerqueue = queue.Queue()
keepref1 = WRITEFUNCTION(functools.partial(WriteMemoryCallback, resp.queue))
keepref1 = WRITEFUNCTION(
functools.partial(WriteMemoryCallback, headerqueue, resp.queue)
)
keepref2 = WRITEFUNCTION(
functools.partial(WriteMemoryCallback, headerqueue)
functools.partial(WriteMemoryCallback, None, headerqueue)
)
curl_easy_setopt(
curl,
Expand All @@ -185,7 +196,7 @@ def ___perform():
self._perform(curl)
except:
print_exc()
headerqueue.put(None)
headerqueue.put(1)
error = True
resp.queue.put(None)
if error:
Expand All @@ -195,27 +206,16 @@ def ___perform():
threading.Thread(target=___perform, daemon=True).start()

headerb = ""
cnt = 1
while True:
_headerb = headerqueue.get()
if _headerb is None:
if _headerb == 0:
break
elif _headerb == 1:
self.raise_for_status()
_headerb = _headerb.decode("utf8")

if _headerb.endswith(
"200 Connection established\r\n"
): # HTTP/1.1 200 Connection established\r\n
cnt += 1
elif _headerb == "\r\n":
cnt -= 1
if cnt == 0:
break
else:
# 有proxy时,proxy也有可能有header.
headerb = ""
else:
headerb += _headerb

if _headerb.startswith("HTTP/"):
headerb = ""
headerb += _headerb
resp.headers = self._update_header_cookie(headerb)
resp.status_code = self._getStatusCode(curl)
else:
Expand Down
31 changes: 1 addition & 30 deletions LunaTranslator/LunaTranslator/network/requests_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,38 +313,9 @@ def request(
stream,
verify,
timeout,
allow_redirects,
)

while allow_redirects and (
_.status_code == 301 or _.status_code == 302 or _.status_code == 307
):
location = _.headers["Location"]
if location.startswith("/"): # vndb
url = url = scheme + "://" + server + location
param = location
elif location.startswith(
"http"
): # https://api.github.com/repos/XXX/XXX/zipball
scheme, server, port, param, url = self._parseurl(location, None)
else:
raise Exception("redirect {}: {}".format(_.status_code, location))
_ = self.request_impl(
method,
scheme,
server,
port,
param,
url,
headers,
cookies,
dataptr,
datalen,
proxy,
stream,
verify,
timeout,
)

return _

def get(self, url, **kwargs):
Expand Down
19 changes: 16 additions & 3 deletions LunaTranslator/LunaTranslator/network/winhttp/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,25 @@ def _set_proxy(self, hsess, proxy):
if proxy:
winhttpsetproxy(hsess, proxy)

def _set_verify(self, curl, verify):
def _set_verify(self, hRequest, verify):
if verify == False:
dwFlags = DWORD(SECURITY_FLAG_IGNORE_ALL_CERT_ERRORS)
WinHttpSetOption(
curl, WINHTTP_OPTION_SECURITY_FLAGS, pointer(dwFlags), sizeof(dwFlags)
hRequest,
WINHTTP_OPTION_SECURITY_FLAGS,
pointer(dwFlags),
sizeof(dwFlags),
)

def _set_allow_redirects(self, hRequest, allow_redirects):
if allow_redirects:
dwFlags = DWORD(WINHTTP_OPTION_REDIRECT_POLICY_ALWAYS)
else:
dwFlags = DWORD(WINHTTP_OPTION_REDIRECT_POLICY_NEVER)
WinHttpSetOption(
hRequest, WINHTTP_OPTION_REDIRECT_POLICY, pointer(dwFlags), sizeof(dwFlags)
)

@ExceptionFilter
def request_impl(
self,
Expand All @@ -142,6 +154,7 @@ def request_impl(
stream,
verify,
timeout,
allow_redirects,
):
headers = self._parseheader(headers, cookies)
flag = WINHTTP_FLAG_SECURE if scheme == "https" else 0
Expand All @@ -168,7 +181,7 @@ def request_impl(
raise WinhttpException(GetLastError())
self._set_verify(hRequest, verify)
self._set_proxy(hRequest, proxy)

self._set_allow_redirects(hRequest, allow_redirects)
succ = WinHttpSendRequest(
hRequest, headers, -1, dataptr, datalen, datalen, None
)
Expand Down
6 changes: 6 additions & 0 deletions LunaTranslator/LunaTranslator/network/winhttp/winhttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from ctypes.wintypes import LPCWSTR, DWORD, LPVOID, WORD, BOOL, LPCVOID, LPWSTR, USHORT
from network.requests_common import NetWorkException


class WinhttpException(NetWorkException):
ERROR_INVALID_PARAMETER = 87
ERROR_INVALID_OPERATION = 4317
Expand Down Expand Up @@ -225,3 +226,8 @@ def _undefined(*args):
WINHTTP_WEB_SOCKET_CLOSE_BUFFER_TYPE = 4
ERROR_SUCCESS = 0
WINHTTP_WEB_SOCKET_SUCCESS_CLOSE_STATUS = 1000


WINHTTP_OPTION_REDIRECT_POLICY = 88
WINHTTP_OPTION_REDIRECT_POLICY_ALWAYS = 2
WINHTTP_OPTION_REDIRECT_POLICY_NEVER = 0

0 comments on commit 220b8be

Please sign in to comment.