This commit is contained in:
恍兮惚兮 2024-09-04 22:33:36 +08:00
parent ccbbd2a02b
commit a151564faa
3 changed files with 28 additions and 29 deletions

View File

@ -97,22 +97,28 @@ class Requester(Requester_common):
que.append(bs) que.append(bs)
return realsize return realsize
def _filter_header(self, headertext):
header = []
for line in headertext.split("\n"):
if line.startswith("HTTP/"):
header = []
header.append(line)
return "\n".join(header)
def __getrealheader(self, headerqueue): def __getrealheader(self, headerqueue):
if isinstance(headerqueue, queue.Queue): if isinstance(headerqueue, queue.Queue):
header = "" header = []
while True: while True:
_headerb = headerqueue.get() _headerb = headerqueue.get()
if _headerb == 0: if _headerb == 0:
break break
elif _headerb == 1: elif isinstance(_headerb, Exception):
raise CURLException() raise _headerb
_headerb = _headerb.decode("utf8") header.append(_headerb)
if _headerb.startswith("HTTP/"):
header = ""
header += _headerb
return header
elif isinstance(headerqueue, list): elif isinstance(headerqueue, list):
return b"".join(headerqueue).decode("utf8") header = headerqueue
return self._filter_header(b"".join(header).decode("utf8"))
def _setheaders(self, curl, headers, cookies): def _setheaders(self, curl, headers, cookies):
lheaders = Autoslist() lheaders = Autoslist()
@ -208,17 +214,11 @@ class Requester(Requester_common):
if stream: if stream:
def ___perform(): def ___perform():
error = False
try: try:
self._perform(curl) self._perform(curl)
except: except Exception as e:
print_exc() headerqueue.put(e)
headerqueue.put(1)
error = True
resp.queue.put(None) resp.queue.put(None)
if error:
print(url)
raise CURLException()
threading.Thread(target=___perform, daemon=True).start() threading.Thread(target=___perform, daemon=True).start()
@ -229,7 +229,7 @@ class Requester(Requester_common):
if not stream: if not stream:
resp.content = b"".join(resp.queue) resp.content = b"".join(resp.queue)
resp.headers, resp.cookies = self._parseheader2dict(header) resp.headers, resp.cookies, resp.status_text = self._parseheader2dict(header)
resp.status_code = self._getStatusCode(curl) resp.status_code = self._getStatusCode(curl)
resp.url = url resp.url = url

View File

@ -177,7 +177,9 @@ class Requester(Requester_common):
if succ == 0: if succ == 0:
MaybeRaiseException() MaybeRaiseException()
resp = Response(stream) resp = Response(stream)
resp.headers, resp.cookies = self._parseheader2dict(self._getheaders(hRequest)) resp.headers, resp.cookies, resp.status_text = self._parseheader2dict(
self._getheaders(hRequest)
)
resp.status_code = self._getStatusCode(hRequest) resp.status_code = self._getStatusCode(hRequest)
resp.url = url resp.url = url

View File

@ -72,6 +72,7 @@ class ResponseBase:
self.url = "" self.url = ""
self.cookies = {} self.cookies = {}
self.status_code = 0 self.status_code = 0
self.status_text = ""
self.__content = b"" self.__content = b""
self.__content_s = [] self.__content_s = []
self.content_prepared = threading.Event() self.content_prepared = threading.Event()
@ -156,17 +157,12 @@ class ResponseBase:
yield pending yield pending
def raise_for_status(self): def raise_for_status(self):
reason = ""
http_error_msg = "" http_error_msg = ""
if 400 <= self.status_code < 500: if 400 <= self.status_code < 500:
http_error_msg = ( http_error_msg = f"{self.status_code} Client Error: {self.status_text} for url: {self.url}"
f"{self.status_code} Client Error: {reason} for url: {self.url}"
)
elif 500 <= self.status_code < 600: elif 500 <= self.status_code < 600:
http_error_msg = ( http_error_msg = f"{self.status_code} Server Error: {self.status_text} for url: {self.url}"
f"{self.status_code} Server Error: {reason} for url: {self.url}"
)
if http_error_msg: if http_error_msg:
raise HTTPError(http_error_msg) raise HTTPError(http_error_msg)
@ -324,10 +320,11 @@ class Requester_common:
return cookie return cookie
def _parseheader2dict(self, headerstr: str): def _parseheader2dict(self, headerstr: str):
# print(headerstr)
header = CaseInsensitiveDict() header = CaseInsensitiveDict()
cookie = {} cookie = {}
for line in headerstr.split("\r\n")[1:]: lines = headerstr.split("\r\n")
status_text = " ".join(lines[0].split(" ")[2:])
for line in lines[1:]:
idx = line.find(": ") idx = line.find(": ")
if idx == -1: if idx == -1:
continue continue
@ -335,7 +332,7 @@ class Requester_common:
cookie.update(self._parsecookiestring(line[idx + 2 :])) cookie.update(self._parsecookiestring(line[idx + 2 :]))
else: else:
header[line[:idx]] = line[idx + 2 :] header[line[:idx]] = line[idx + 2 :]
return CaseInsensitiveDict(header), cookie return CaseInsensitiveDict(header), cookie, status_text
def _parsejson(self, _json): def _parsejson(self, _json):
databytes = json.dumps(_json).encode("utf8") databytes = json.dumps(_json).encode("utf8")