2016-11-30 06:21:53 +01:00
|
|
|
"""
|
|
|
|
Primitive replacement for requests to avoid extra dependency.
|
|
|
|
Avoids use of urllib2 due to lack of SNI support.
|
|
|
|
"""
|
2019-07-12 08:46:20 +02:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
import json
|
2017-09-13 21:23:48 +02:00
|
|
|
import time
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
from urllib import urlencode
|
|
|
|
except ImportError:
|
2017-05-03 17:19:44 +02:00
|
|
|
# noinspection PyCompatibility, PyUnresolvedReferences
|
2016-11-30 06:21:53 +01:00
|
|
|
from urllib.parse import urlencode # pylint: disable=locally-disabled, import-error, no-name-in-module
|
|
|
|
|
2017-05-05 10:23:00 +02:00
|
|
|
try:
|
|
|
|
# noinspection PyCompatibility
|
2017-08-12 02:33:39 +02:00
|
|
|
from urlparse import urlparse, urlunparse, parse_qs
|
2017-05-05 10:23:00 +02:00
|
|
|
except ImportError:
|
|
|
|
# noinspection PyCompatibility, PyUnresolvedReferences
|
2017-08-12 02:33:39 +02:00
|
|
|
from urllib.parse import urlparse, urlunparse, parse_qs # pylint: disable=locally-disabled, ungrouped-imports
|
2017-05-05 10:23:00 +02:00
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .util import (
|
2016-11-30 06:21:53 +01:00
|
|
|
ApplicationError,
|
2017-09-13 21:23:48 +02:00
|
|
|
SubprocessError,
|
|
|
|
display,
|
2016-11-30 06:21:53 +01:00
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .util_common import (
|
2019-07-11 07:00:34 +02:00
|
|
|
CommonConfig,
|
|
|
|
run_command,
|
|
|
|
)
|
|
|
|
|
2016-11-30 06:21:53 +01:00
|
|
|
|
2019-07-12 22:17:20 +02:00
|
|
|
class HttpClient:
|
2016-11-30 06:21:53 +01:00
|
|
|
"""Make HTTP requests via curl."""
|
2019-06-19 15:12:14 +02:00
|
|
|
def __init__(self, args, always=False, insecure=False, proxy=None):
|
2016-11-30 06:21:53 +01:00
|
|
|
"""
|
|
|
|
:type args: CommonConfig
|
|
|
|
:type always: bool
|
2018-02-14 20:15:01 +01:00
|
|
|
:type insecure: bool
|
2016-11-30 06:21:53 +01:00
|
|
|
"""
|
|
|
|
self.args = args
|
|
|
|
self.always = always
|
2018-02-14 20:15:01 +01:00
|
|
|
self.insecure = insecure
|
2019-06-19 15:12:14 +02:00
|
|
|
self.proxy = proxy
|
2016-11-30 06:21:53 +01:00
|
|
|
|
2018-03-07 23:02:31 +01:00
|
|
|
self.username = None
|
|
|
|
self.password = None
|
|
|
|
|
2016-11-30 06:21:53 +01:00
|
|
|
def get(self, url):
|
|
|
|
"""
|
|
|
|
:type url: str
|
|
|
|
:rtype: HttpResponse
|
|
|
|
"""
|
|
|
|
return self.request('GET', url)
|
|
|
|
|
|
|
|
def delete(self, url):
|
|
|
|
"""
|
|
|
|
:type url: str
|
|
|
|
:rtype: HttpResponse
|
|
|
|
"""
|
|
|
|
return self.request('DELETE', url)
|
|
|
|
|
|
|
|
def put(self, url, data=None, headers=None):
|
|
|
|
"""
|
|
|
|
:type url: str
|
|
|
|
:type data: str | None
|
|
|
|
:type headers: dict[str, str] | None
|
|
|
|
:rtype: HttpResponse
|
|
|
|
"""
|
|
|
|
return self.request('PUT', url, data, headers)
|
|
|
|
|
|
|
|
def request(self, method, url, data=None, headers=None):
|
|
|
|
"""
|
|
|
|
:type method: str
|
|
|
|
:type url: str
|
|
|
|
:type data: str | None
|
|
|
|
:type headers: dict[str, str] | None
|
|
|
|
:rtype: HttpResponse
|
|
|
|
"""
|
|
|
|
cmd = ['curl', '-s', '-S', '-i', '-X', method]
|
|
|
|
|
2018-02-14 20:15:01 +01:00
|
|
|
if self.insecure:
|
|
|
|
cmd += ['--insecure']
|
|
|
|
|
2016-11-30 06:21:53 +01:00
|
|
|
if headers is None:
|
|
|
|
headers = {}
|
|
|
|
|
|
|
|
headers['Expect'] = '' # don't send expect continue header
|
|
|
|
|
2018-03-07 23:02:31 +01:00
|
|
|
if self.username:
|
|
|
|
if self.password:
|
|
|
|
display.sensitive.add(self.password)
|
|
|
|
cmd += ['-u', '%s:%s' % (self.username, self.password)]
|
|
|
|
else:
|
|
|
|
cmd += ['-u', self.username]
|
|
|
|
|
2016-11-30 06:21:53 +01:00
|
|
|
for header in headers.keys():
|
|
|
|
cmd += ['-H', '%s: %s' % (header, headers[header])]
|
|
|
|
|
|
|
|
if data is not None:
|
|
|
|
cmd += ['-d', data]
|
|
|
|
|
2019-06-19 15:12:14 +02:00
|
|
|
if self.proxy:
|
|
|
|
cmd += ['-x', self.proxy]
|
|
|
|
|
2016-11-30 06:21:53 +01:00
|
|
|
cmd += [url]
|
|
|
|
|
2017-09-13 21:23:48 +02:00
|
|
|
attempts = 0
|
|
|
|
max_attempts = 3
|
|
|
|
sleep_seconds = 3
|
|
|
|
|
|
|
|
# curl error codes which are safe to retry (request never sent to server)
|
|
|
|
retry_on_status = (
|
|
|
|
6, # CURLE_COULDNT_RESOLVE_HOST
|
|
|
|
)
|
|
|
|
|
2019-07-11 22:03:49 +02:00
|
|
|
stdout = ''
|
|
|
|
|
2017-09-13 21:23:48 +02:00
|
|
|
while True:
|
|
|
|
attempts += 1
|
|
|
|
|
|
|
|
try:
|
2019-07-15 22:47:16 +02:00
|
|
|
stdout = run_command(self.args, cmd, capture=True, always=self.always, cmd_verbosity=2)[0]
|
2017-09-13 21:23:48 +02:00
|
|
|
break
|
|
|
|
except SubprocessError as ex:
|
|
|
|
if ex.status in retry_on_status and attempts < max_attempts:
|
|
|
|
display.warning(u'%s' % ex)
|
|
|
|
time.sleep(sleep_seconds)
|
|
|
|
continue
|
|
|
|
|
|
|
|
raise
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
if self.args.explain and not self.always:
|
2017-08-09 07:55:17 +02:00
|
|
|
return HttpResponse(method, url, 200, '')
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
header, body = stdout.split('\r\n\r\n', 1)
|
|
|
|
|
|
|
|
response_headers = header.split('\r\n')
|
|
|
|
first_line = response_headers[0]
|
|
|
|
http_response = first_line.split(' ')
|
|
|
|
status_code = int(http_response[1])
|
|
|
|
|
2017-08-09 07:55:17 +02:00
|
|
|
return HttpResponse(method, url, status_code, body)
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
|
2019-07-12 22:17:20 +02:00
|
|
|
class HttpResponse:
|
2016-11-30 06:21:53 +01:00
|
|
|
"""HTTP response from curl."""
|
2017-08-09 07:55:17 +02:00
|
|
|
def __init__(self, method, url, status_code, response):
|
2016-11-30 06:21:53 +01:00
|
|
|
"""
|
2017-08-09 07:55:17 +02:00
|
|
|
:type method: str
|
|
|
|
:type url: str
|
2016-11-30 06:21:53 +01:00
|
|
|
:type status_code: int
|
|
|
|
:type response: str
|
|
|
|
"""
|
2017-08-09 07:55:17 +02:00
|
|
|
self.method = method
|
|
|
|
self.url = url
|
2016-11-30 06:21:53 +01:00
|
|
|
self.status_code = status_code
|
|
|
|
self.response = response
|
|
|
|
|
|
|
|
def json(self):
|
|
|
|
"""
|
|
|
|
:rtype: any
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return json.loads(self.response)
|
|
|
|
except ValueError:
|
2017-08-09 07:55:17 +02:00
|
|
|
raise HttpError(self.status_code, 'Cannot parse response to %s %s as JSON:\n%s' % (self.method, self.url, self.response))
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
class HttpError(ApplicationError):
|
|
|
|
"""HTTP response as an error."""
|
|
|
|
def __init__(self, status, message):
|
|
|
|
"""
|
|
|
|
:type status: int
|
|
|
|
:type message: str
|
|
|
|
"""
|
|
|
|
super(HttpError, self).__init__('%s: %s' % (status, message))
|
|
|
|
self.status = status
|