misc: switch http support to requests library (#19437)
This commit is contained in:
parent
e06b74f755
commit
218bc1063b
|
@ -16,6 +16,7 @@ Depends: ${misc:Depends}, ${python:Depends},
|
||||||
graphviz,
|
graphviz,
|
||||||
python-feedparser,
|
python-feedparser,
|
||||||
python-imaging,
|
python-imaging,
|
||||||
|
python-requests,
|
||||||
python-vobject,
|
python-vobject,
|
||||||
uwsgi,
|
uwsgi,
|
||||||
uwsgi-plugin-python
|
uwsgi-plugin-python
|
||||||
|
|
|
@ -272,9 +272,6 @@ class HttpRequestsMocking(object):
|
||||||
self.headers = headers or {}
|
self.headers = headers or {}
|
||||||
self.length = len(data or '')
|
self.length = len(data or '')
|
||||||
|
|
||||||
def getheader(self, header):
|
|
||||||
return self.headers.get(header, None)
|
|
||||||
|
|
||||||
if status is None:
|
if status is None:
|
||||||
raise ConnectionError('error')
|
raise ConnectionError('error')
|
||||||
return FakeResponse(status, data, headers), status, data, None
|
return FakeResponse(status, data, headers), status, data, None
|
||||||
|
|
|
@ -20,10 +20,7 @@ import calendar
|
||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
import httplib
|
|
||||||
import urllib
|
import urllib
|
||||||
import socket
|
|
||||||
import ssl
|
|
||||||
import base64
|
import base64
|
||||||
import json
|
import json
|
||||||
import subprocess
|
import subprocess
|
||||||
|
@ -31,6 +28,8 @@ import tempfile
|
||||||
import unicodedata
|
import unicodedata
|
||||||
import hashlib
|
import hashlib
|
||||||
|
|
||||||
|
import requests
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
@ -277,6 +276,7 @@ def _http_request(url, method='GET', body=None, headers={}, cert_file=None, time
|
||||||
else:
|
else:
|
||||||
hostname, query = urllib.splithost(url[6:])
|
hostname, query = urllib.splithost(url[6:])
|
||||||
|
|
||||||
|
auth = None
|
||||||
if '@' in hostname:
|
if '@' in hostname:
|
||||||
authenticator, hostname = hostname.split('@')
|
authenticator, hostname = hostname.split('@')
|
||||||
if ':' in authenticator:
|
if ':' in authenticator:
|
||||||
|
@ -284,39 +284,19 @@ def _http_request(url, method='GET', body=None, headers={}, cert_file=None, time
|
||||||
else:
|
else:
|
||||||
username = authenticator
|
username = authenticator
|
||||||
password = ''
|
password = ''
|
||||||
headers['Authorization'] = 'Basic %s' % base64.b64encode('%s:%s' % (username, password))
|
auth = (username, password)
|
||||||
|
|
||||||
connection_kwargs = {'host': hostname}
|
|
||||||
if timeout:
|
|
||||||
connection_kwargs['timeout'] = timeout
|
|
||||||
if cert_file:
|
|
||||||
connection_kwargs['cert_file'] = cert_file
|
|
||||||
if url.startswith('http://'):
|
|
||||||
conn = httplib.HTTPConnection(**connection_kwargs)
|
|
||||||
else: # https
|
|
||||||
conn = httplib.HTTPSConnection(**connection_kwargs)
|
|
||||||
query = query.replace('&', '&')
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
conn.request(method, query, body, headers)
|
response = requests.request(method, url, headers=headers, data=body,
|
||||||
response = conn.getresponse()
|
timeout=timeout, cert=cert_file)
|
||||||
except socket.gaierror, (err, msg):
|
except requests.Timeout:
|
||||||
conn.close()
|
|
||||||
raise ConnectionError('error while connecting to %s (%s)' % (hostname, msg))
|
|
||||||
except socket.timeout, err:
|
|
||||||
conn.close()
|
|
||||||
raise ConnectionError('connection timed out while fetching the page')
|
raise ConnectionError('connection timed out while fetching the page')
|
||||||
except socket.error, err:
|
except requests.RequestException as err:
|
||||||
conn.close()
|
raise ConnectionError('error in http request to to %s (%s)' % (hostname, err))
|
||||||
raise ConnectionError('error while fetching the page (%s)' % err)
|
|
||||||
except ssl.CertificateError, err:
|
|
||||||
conn.close()
|
|
||||||
raise ConnectionError('certificate error when connecting (%s)' % err)
|
|
||||||
else:
|
else:
|
||||||
data = response.read()
|
data = response.content
|
||||||
status = response.status
|
status = response.status_code
|
||||||
auth_header = response.getheader('WWW-Authenticate')
|
auth_header = response.headers.get('WWW-Authenticate')
|
||||||
conn.close()
|
|
||||||
|
|
||||||
return response, status, data, auth_header
|
return response, status, data, auth_header
|
||||||
|
|
||||||
|
|
|
@ -15,8 +15,6 @@
|
||||||
# along with this program; if not, see <http://www.gnu.org/licenses/>.
|
# along with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import httplib
|
|
||||||
import urllib
|
|
||||||
import urlparse
|
import urlparse
|
||||||
import time
|
import time
|
||||||
import sys
|
import sys
|
||||||
|
|
|
@ -275,7 +275,7 @@ class WebserviceCallStatusItem(WorkflowStatusItem):
|
||||||
return
|
return
|
||||||
|
|
||||||
app_error_code = 0
|
app_error_code = 0
|
||||||
app_error_code_header = response.getheader('x-error-code')
|
app_error_code_header = response.headers.get('x-error-code')
|
||||||
if app_error_code_header:
|
if app_error_code_header:
|
||||||
# result is good only if header value is '0'
|
# result is good only if header value is '0'
|
||||||
try:
|
try:
|
||||||
|
@ -337,7 +337,7 @@ class WebserviceCallStatusItem(WorkflowStatusItem):
|
||||||
elif d.get('display_id'):
|
elif d.get('display_id'):
|
||||||
formdata.id_display = d.get('display_id')
|
formdata.id_display = d.get('display_id')
|
||||||
else: # store result as attachment
|
else: # store result as attachment
|
||||||
content_type = response.getheader('content-type') or ''
|
content_type = response.headers.get('content-type') or ''
|
||||||
if content_type:
|
if content_type:
|
||||||
content_type = content_type.split(';')[0].strip().lower()
|
content_type = content_type.split(';')[0].strip().lower()
|
||||||
workflow_data['%s_content_type' % self.varname] = content_type
|
workflow_data['%s_content_type' % self.varname] = content_type
|
||||||
|
|
Loading…
Reference in New Issue