|
|
@@ -18,10 +18,8 @@ class HTTPClient(object): |
|
|
|
reparsed = urlparse.urlparse("http://" + baseurl).geturl() |
|
|
|
self.baseurl = reparsed.rstrip('/') + '/' |
|
|
|
|
|
|
|
# Build Requests session object, enable SSL verification |
|
|
|
# Note whether we want SSL verification |
|
|
|
self.verify_ssl = verify_ssl |
|
|
|
self.session = requests.Session() |
|
|
|
self.session.verify = True |
|
|
|
|
|
|
|
# Saved response, so that tests can verify a few things. |
|
|
|
self._last_response = {} |
|
|
@@ -59,17 +57,34 @@ class HTTPClient(object): |
|
|
|
raise Error(**args) |
|
|
|
|
|
|
|
def close(self): |
|
|
|
self.session.close() |
|
|
|
pass |
|
|
|
|
|
|
|
def _do_req(self, method, url, query_data, body_data, stream, headers): |
|
|
|
url = urlparse.urljoin(self.baseurl, url) |
|
|
|
try: |
|
|
|
response = self.session.request(method, url, |
|
|
|
params = query_data, |
|
|
|
data = body_data, |
|
|
|
stream = stream, |
|
|
|
headers = headers, |
|
|
|
verify = self.verify_ssl) |
|
|
|
# Create a new session, ensure we send "Connection: close", |
|
|
|
# and explicitly close connection after the transfer. |
|
|
|
# This is to avoid HTTP/1.1 persistent connections |
|
|
|
# (keepalive), because they have fundamental race |
|
|
|
# conditions when there are delays between requests: |
|
|
|
# a new request may be sent at the same instant that the |
|
|
|
# server decides to timeout the connection. |
|
|
|
session = requests.Session() |
|
|
|
if headers is None: |
|
|
|
headers = {} |
|
|
|
headers["Connection"] = "close" |
|
|
|
response = session.request(method, url, |
|
|
|
params = query_data, |
|
|
|
data = body_data, |
|
|
|
stream = stream, |
|
|
|
headers = headers, |
|
|
|
verify = self.verify_ssl) |
|
|
|
|
|
|
|
# Close the connection. If it's a generator (stream = |
|
|
|
# True), the requests library shouldn't actually close the |
|
|
|
# HTTP connection until all data has been read from the |
|
|
|
# response. |
|
|
|
session.close() |
|
|
|
except requests.RequestException as e: |
|
|
|
raise ServerError(status = "502 Error", url = url, |
|
|
|
message = str(e.message)) |
|
|
|