Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hhhrrrttt222111
GitHub Repository: hhhrrrttt222111/Dorkify
Path: blob/master/venv/Lib/site-packages/urllib3/util/response.py
811 views
1
from __future__ import absolute_import
2
from ..packages.six.moves import http_client as httplib
3
4
from ..exceptions import HeaderParsingError
5
6
7
def is_fp_closed(obj):
8
"""
9
Checks whether a given file-like object is closed.
10
11
:param obj:
12
The file-like object to check.
13
"""
14
15
try:
16
# Check `isclosed()` first, in case Python3 doesn't set `closed`.
17
# GH Issue #928
18
return obj.isclosed()
19
except AttributeError:
20
pass
21
22
try:
23
# Check via the official file-like-object way.
24
return obj.closed
25
except AttributeError:
26
pass
27
28
try:
29
# Check if the object is a container for another file-like object that
30
# gets released on exhaustion (e.g. HTTPResponse).
31
return obj.fp is None
32
except AttributeError:
33
pass
34
35
raise ValueError("Unable to determine whether fp is closed.")
36
37
38
def assert_header_parsing(headers):
39
"""
40
Asserts whether all headers have been successfully parsed.
41
Extracts encountered errors from the result of parsing headers.
42
43
Only works on Python 3.
44
45
:param headers: Headers to verify.
46
:type headers: `httplib.HTTPMessage`.
47
48
:raises urllib3.exceptions.HeaderParsingError:
49
If parsing errors are found.
50
"""
51
52
# This will fail silently if we pass in the wrong kind of parameter.
53
# To make debugging easier add an explicit check.
54
if not isinstance(headers, httplib.HTTPMessage):
55
raise TypeError("expected httplib.Message, got {0}.".format(type(headers)))
56
57
defects = getattr(headers, "defects", None)
58
get_payload = getattr(headers, "get_payload", None)
59
60
unparsed_data = None
61
if get_payload:
62
# get_payload is actually email.message.Message.get_payload;
63
# we're only interested in the result if it's not a multipart message
64
if not headers.is_multipart():
65
payload = get_payload()
66
67
if isinstance(payload, (bytes, str)):
68
unparsed_data = payload
69
70
if defects or unparsed_data:
71
raise HeaderParsingError(defects=defects, unparsed_data=unparsed_data)
72
73
74
def is_response_to_head(response):
75
"""
76
Checks whether the request of a response has been a HEAD-request.
77
Handles the quirks of AppEngine.
78
79
:param conn:
80
:type conn: :class:`httplib.HTTPResponse`
81
"""
82
# FIXME: Can we do this somehow without accessing private httplib _method?
83
method = response._method
84
if isinstance(method, int): # Platform-specific: Appengine
85
return method == 3
86
return method.upper() == "HEAD"
87
88