#
# Copyright (c) 2010 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import sys
import base64
import mimetypes
import os
import socket
import time
import urllib
import urlparse
import logging
import threading
from httplib import HTTPConnection, BadStatusLine, HTTPSConnection
# from M2Crypto import SSL
import ssl
# from M2Crypto.httpslib import HTTPSConnection
# from redhat_support_lib.web.proxyhttpsconnection import RSLProxyHTTPSConnection
from redhat_support_lib.infrastructure.errors import RequestError
from redhat_support_lib.utils.timerhelper import TimerThread
logger = logging.getLogger("redhat_support_lib.web.connection")
class Connection(object):
'''
The strata api connection proxy
'''
def __init__(self,
url,
username,
password,
manager,
key_file=None,
cert_file=None,
strict=None,
# timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
timeout=None,
proxy_url=None,
proxy_user=None,
proxy_pass=None,
debug=False,
noverify=False,
ssl_ca=None,
auth=None):
self.url = url
self.username = username
self.password = password
self.key_file = key_file
self.cert_file = cert_file
self.strict = strict
self.timeout = timeout
self.proxy_url = proxy_url
self.proxy_user = proxy_user
self.proxy_pass = proxy_pass
self.debug = debug
self.noverify = noverify
self.ssl_ca = ssl_ca
self.read_size = 4096
self.auth = auth
u = self.__parse_url(url)
self.url_schema = u[0]
self.url_host = u[1]
self.url_port = u[2]
self.handlerprefix = ''
if proxy_url:
purl = self.__parse_url(proxy_url)
self.proxy_host = purl[1]
self.proxy_port = purl[2]
self.__connection = self.__createConnection()
self.__connection.set_debuglevel(int(debug))
self.__manager = manager
self.__id = id(self)
def get_id(self):
return self.__id
def getConnection(self):
return self.__connection
def getDefaultHeaders(self):
return self.__headers.copy()
def doRequest(self, method, url, body=urllib.urlencode({}), headers={}):
attempts = 0
if self.auth:
headers["Authorization"] = "Bearer %s" % self.auth.get_access_token()
while True:
try:
self.__connection.request(method,
self.handlerprefix + url,
body,
self.getHeaders(headers))
return self.__connection.getresponse()
except BadStatusLine:
if attempts == 0:
# Try to reset the connection, HTTPS proxying on RHEL5
# will trigger this a lot as M2Crypto doesn't support
# SSL/TLS Proxy Keep-Alive.
# Transient issues may also trigger this on other setups.
self.resetConnection()
attempts += 1
logging.debug(_('Connection socket closed, reconnecting.'))
else:
raise
def doUpload(self, url, fileName, fileChunk=None, description=None, user_agent=None):
'''Wrapper for _doUpload to handle connection retries'''
attempts = 0
while True:
try:
self._doUpload(url, fileName, fileChunk, description, user_agent)
return self.__connection.getresponse()
except BadStatusLine:
if attempts == 0:
# Like doRequest, we may need to reset the connection.
self.resetConnection()
attempts += 1
logging.debug(_('Connection socket closed, reconnecting.'))
else:
raise
def _doUpload(self, url, fileName, fileChunk=None, description=None, user_agent=None):
'''
Do an upload of a single file to the given URL.
Keyword arguments:
url -- The URL to upload to.
fileName -- An open file handle whose content needs to be sent.
fileChunk -- Dict describing the chunk of fileName to be sent.
description -- Optional description of the file.
'''
def upload_progress(fh, file_size):
fh_tell = fh.tell()
percent = str(int(fh_tell * 100 / file_size))
logger.debug('upload_progress: fh.tell() = %d, file_size = %d, percent = %s'
% (fh_tell, file_size, percent))
sys.stdout.write("%s%3s%%" % ("\b\b\b\b", percent))
sys.stdout.flush()
boundary = '----------%x' % time.time()
fh = None
timer = None
try:
try:
fh = open(fileName, 'rb')
fileSize = os.path.getsize(fileName)
# Setup thread to display upload progress
logger.debug('Starting TimerThread ...')
timer = TimerThread(1, upload_progress, fh, fileSize)
# Loop used for uploading all the chunks of split attachments
while True:
# Compose the head of the form.
headFormAry = []
if description:
headFormAry.append('--' + boundary)
headFormAry.append(\
'Content-Disposition: form-data; name="description"')
headFormAry.append('')
headFormAry.append(str(description))
headFormAry.append('--' + boundary)
if fileChunk:
chunkName = ("%s.%03d" % (os.path.basename(fileName),
fileChunk['num']))
fileChunk['names'].append(chunkName)
header_filename = chunkName
chunk_size = fileChunk['size']
if fileSize - fh.tell() < chunk_size:
chunk_size = fileSize - fh.tell()
chunk_end = fh.tell() + chunk_size
else:
header_filename = os.path.basename(fileName)
headFormAry.append(\
'Content-Disposition: form-data; name="%s"; filename="%s"'\
% ('file', header_filename))
headFormAry.append('Content-Type: %s' % \
(mimetypes.guess_type(fileName)[0] or \
'application/octet-stream'))
# headFormAry.append('User-Agent: %s' %user_agent['User-Agent'])
headFormAry.append('')
headFormAry.append('')
headForm = '\r\n'.join(headFormAry)
# Compose the tail
tailFormAry = []
tailFormAry.append('')
tailFormAry.append('--' + boundary + '--')
tailFormAry.append('')
tailForm = '\r\n'.join(tailFormAry)
totalLength = len(headForm) + len(tailForm)
if fileChunk:
totalLength += chunk_size
else:
totalLength += fileSize
# Start sending.
if self.proxy_url:
self.__connection.putrequest(method='POST',
url=self.url + url,
skip_host=1,
skip_accept_encoding=1)
else:
self.__connection.putrequest(method='POST',
url=url,
skip_host=0,
skip_accept_encoding=1)
hdrDict = self.getHeaders({'Content-Length': str(totalLength),
'Content-Type': 'multipart/form-data; boundary=%s' % boundary,
'Accept': 'text/plain'})
hdrDict.update(user_agent)
if self.auth:
hdrDict["Authorization"] = "Bearer %s" % self.auth.get_access_token()
for key, value in hdrDict.items():
self.__connection.putheader(key, value)
self.__connection.endheaders()
self.__connection.send(headForm)
if fileChunk:
while fh.tell() < chunk_end:
self.__connection.send(fh.read(self.read_size))
self.__connection.send(tailForm)
if fh.tell() >= fileSize:
break
else:
response = self.__connection.getresponse()
response.read()
if response.status >= 400:
logger.debug("HTTP status(%s) HTTP reason(%s) "
"HTTP response(%s)" %
(response.status, response.reason,
response.read()))
raise RequestError(response.status,
response.reason,
response.read())
fileChunk['num'] += 1
else:
while fh.tell() < fileSize:
self.__connection.send(fh.read(self.read_size))
self.__connection.send(tailForm)
break
except:
raise
finally:
timer.stop()
if fh:
fh.close()
def getHeaders(self, headers):
extended_headers = self.getDefaultHeaders()
for k in headers.keys():
if (headers[k] is None and
extended_headers.has_key(k)):
extended_headers.pop(k)
else:
extended_headers[k] = headers[k]
return extended_headers
def setDebugLevel(self, level):
self.__connection.set_debuglevel(level)
def setTunnel(self, host, port=None, headers=None):
self.__connection.set_tunnel(host, port, headers)
def close(self):
self.__connection.close()
# FIXME: create connection watchdog to close it on
# idle-ttl expiration, rather than after the call
if (self.__manager is not None):
self.__manager._freeResource(self)
def state(self):
return self.__connection.__state
def __parse_url(self, url):
if not url.startswith('http'):
url = "https://" + url
parse = urlparse.urlparse(url)
hostport = parse[1].rsplit(':')
ret = [parse[0]]
if len(hostport) == 2:
ret.append(hostport[0])
try:
ret.append(int(hostport[1]))
except ValueError:
ret.append(None)
else:
ret.append(parse[1])
ret.append(None)
return ret
def __createConnection(self):
def makeHTTPSConnection():
hdr = {}
conn = None
context = ssl.create_default_context()
if self.noverify:
# User has asked to ignore the results of certificate validity
# checks, needed for people that use transparent SSL proxies.
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
else:
context.verify_mode = ssl.CERT_REQUIRED
if os.access('/etc/pki/tls/certs/ca-bundle.crt', os.R_OK):
context.load_verify_locations(
'/etc/pki/tls/certs/ca-bundle.crt')
if self.ssl_ca and os.access(self.ssl_ca, os.R_OK):
context.load_verify_locations(self.ssl_ca)
if self.proxy_url:
# conn = RSLProxyHTTPSConnection(self.proxy_host,
# self.proxy_port, self.strict,
# self.proxy_user,
# self.proxy_pass,
# ssl_context=context)
conn = HTTPSConnection(self.proxy_host, self.proxy_port, context=context)
hdr['Proxy-Connection'] = 'Keep-Alive'
if self.proxy_user and self.proxy_pass:
auth = base64.b64encode("%s:%s" % (self.proxy_user,
self.proxy_pass)).strip()
hdr['Proxy-authorization'] = "Basic %s" % auth
conn.set_tunnel(self.url_host, self.url_port, hdr)
else:
# conn = HTTPSConnection(self.url_host, self.url_port,
# self.strict, ssl_context=context)
conn = HTTPSConnection(self.url_host, self.url_port, context=context)
return (conn, hdr)
def makeHTTPConnection():
hdr = {}
conn = None
if self.proxy_url:
conn = HTTPConnection(self.proxy_host, self.proxy_port)
hdr['Proxy-Connection'] = 'Keep-Alive'
if self.proxy_user and self.proxy_pass:
auth = base64.b64encode("%s:%s" % (self.proxy_user,
self.proxy_pass)).strip()
hdr['Proxy-authorization'] = "Basic %s" % auth
conn.set_tunnel(self.url_host, self.url_port, hdr)
else:
conn = HTTPConnection(self.url_host, self.url_port)
return (conn, hdr)
conn = None
hdr = {}
if self.url_schema == 'http':
(conn, hdr) = makeHTTPConnection()
else:
(conn, hdr) = makeHTTPSConnection()
# Create the default set of headers
self.__headers = self.__createHeaders(hdr)
return conn
def __createHeaders(self, defaultheaders):
hdrs = {"Accept": "application/json",
"Content-Type": "application/json"
}
hdrs["Host"] = self.url_host
if self.auth:
hdrs["Authorization"] = "Bearer %s" % self.auth.get_access_token()
else:
if isinstance(self.password, str) and isinstance(self.username, str):
# BZ 1881343
string_auth = ("%s:%s" % (self.username, self.password))
auth = base64.b64encode(string_auth.encode()).strip()
hdrs["Authorization"] = "Basic %s" % auth.decode()
hdrs.update(defaultheaders)
return hdrs
def resetConnection(self):
self.close()
self.__connection == self.__createConnection()
id = property(get_id, None, None, None)