-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfsbGetHTTPResponseData.py
More file actions
67 lines (63 loc) · 2.76 KB
/
fsbGetHTTPResponseData.py
File metadata and controls
67 lines (63 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import ssl, urllib.request, urllib.parse, urllib.error;
try:
from mNotProvided import fAssertType as f0AssertType, fAssertTypes as f0AssertTypes;
except ModuleNotFoundError as oException:
if oException.args[0] != "No module named 'mNotProvided'":
raise;
f0AssertType = f0AssertTypes = None;
# The rest of the imports are at the end to prevent import loops.
def fsbGetHTTPResponseData(sbURL, sb0PostData = None, sURLNameInException = None):
if f0AssertTypes: f0AssertTypes({
"sbURL": (sbURL, bytes),
"sb0PostData": (sb0PostData, bytes, None),
"sURLNameInException": (sURLNameInException, str),
});
sURL = str(sbURL, "ascii", "strict");
try:
oURL = urllib.parse.urlparse(sURL);
except urllib.error.URLError as oException:
raise AssertionError(
"%s does not have a valid URL (URL: %s, error: %s)" % (sURLNameInException, repr(sbURL), repr(oException))
);
if oURL.scheme == "https":
s0CertificateFilePath = dsCertificateFilePath_by_sHostname.get(oURL.hostname);
if s0CertificateFilePath:
# For use with site-pinning.
o0SSLContext = ssl.create_default_context(cafile = s0CertificateFilePath);
else:
o0SSLContext = ssl.create_default_context();
o0SSLContext.load_default_certs();
else:
o0SSLContext = None;
try:
oHTTPRequest = urllib.request.urlopen(sURL, sb0PostData, context = o0SSLContext);
except urllib.error.HTTPError as oHTTPException:
raise cServerResponseException(
"%s could not be contacted (error: %s)" % (sURLNameInException, repr(oHTTPException)),
{"sbURL": sbURL, "o0HTTPException": oHTTPException, "u0StatusCode": None, "sb0Data": None},
);
uStatusCode = oHTTPRequest.getcode();
sbData = oHTTPRequest.read();
if uStatusCode == 404:
raise cServerResponseException(
"%s was not found (HTTP 404)." % sURLNameInException,
{"sbURL": sbURL, "o0HTTPException": None, "u0StatusCode": uStatusCode, "sb0Data": sbData},
);
if uStatusCode >= 500:
raise cServerResponseException(
"%s returned an internal error code %03d." % (sURLNameInException, uStatusCode),
{"sbURL": sbURL, "o0HTTPException": None, "u0StatusCode": uStatusCode, "sb0Data": sbData},
);
if uStatusCode != 200:
raise cServerResponseException(
"%s returned an unexpected response code %03d." % (sURLNameInException, uStatusCode),
{"sbURL": sbURL, "o0HTTPException": None, "u0StatusCode": uStatusCode, "sb0Data": sbData},
);
if not sbData:
raise cServerResponseException(
"%s did not return any data." % sURLNameInException,
{"sbURL": sbURL, "o0HTTPException": None, "u0StatusCode": uStatusCode, "sb0Data": sbData},
);
return sbData;
from .dsCertificateFilePath_by_sHostname import dsCertificateFilePath_by_sHostname;
from .mExceptions import *;