/usr/lib/python2.7/dist-packages/rhn/connections.py is in python-rhn 2.5.55-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | #
# Connection objects
#
# Copyright (c) 2002--2011 Red Hat, Inc.
#
# Author: Mihai Ibanescu <misa@redhat.com>
# $Id$
import base64
import SSL
import nonblocking
import httplib
import xmlrpclib
import encodings.idna
import socket
from platform import python_version
# Import into the local namespace some httplib-related names
from httplib import _CS_REQ_SENT, _CS_IDLE, ResponseNotReady
class HTTPResponse(httplib.HTTPResponse):
def set_callback(self, rs, ws, ex, user_data, callback):
if not isinstance(self.fp, nonblocking.NonBlockingFile):
self.fp = nonblocking.NonBlockingFile(self.fp)
self.fp.set_callback(rs, ws, ex, user_data, callback)
class HTTPConnection(httplib.HTTPConnection):
response_class = HTTPResponse
def __init__(self, host, port=None):
if python_version() >= '2.6.1':
httplib.HTTPConnection.__init__(self, host, port, timeout=SSL.DEFAULT_TIMEOUT)
else:
httplib.HTTPConnection.__init__(self, host, port)
self._cb_rs = []
self._cb_ws = []
self._cb_ex = []
self._cb_user_data = None
self._cb_callback = None
self._user_agent = "rhn.connections $Revision$ (python)"
def set_callback(self, rs, ws, ex, user_data, callback):
# XXX check the params
self._cb_rs = rs
self._cb_ws = ws
self._cb_ex = ex
self._cb_user_data = user_data
self._cb_callback = callback
def set_user_agent(self, user_agent):
self._user_agent = user_agent
# XXX Had to copy the function from httplib.py, because the nonblocking
# framework had to be initialized
def getresponse(self):
"Get the response from the server."
# check if a prior response has been completed
if self.__response and self.__response.isclosed():
self.__response = None
#
# if a prior response exists, then it must be completed (otherwise, we
# cannot read this response's header to determine the connection-close
# behavior)
#
# note: if a prior response existed, but was connection-close, then the
# socket and response were made independent of this HTTPConnection
# object since a new request requires that we open a whole new
# connection
#
# this means the prior response had one of two states:
# 1) will_close: this connection was reset and the prior socket and
# response operate independently
# 2) persistent: the response was retained and we await its
# isclosed() status to become true.
#
if self.__state != _CS_REQ_SENT or self.__response:
raise ResponseNotReady()
if self.debuglevel > 0:
response = self.response_class(self.sock, self.debuglevel)
else:
response = self.response_class(self.sock)
# The only modification compared to the stock HTTPConnection
if self._cb_callback:
response.set_callback(self._cb_rs, self._cb_ws, self._cb_ex,
self._cb_user_data, self._cb_callback)
response.begin()
assert response.will_close != httplib._UNKNOWN
self.__state = _CS_IDLE
if response.will_close:
# this effectively passes the connection to the response
self.close()
else:
# remember this, so we can tell when it is complete
self.__response = response
return response
class HTTPProxyConnection(HTTPConnection):
def __init__(self, proxy, host, port=None, username=None, password=None):
# The connection goes through the proxy
HTTPConnection.__init__(self, proxy)
# save the proxy values
self.__proxy, self.__proxy_port = self.host, self.port
# self.host and self.port will point to the real host
self._set_hostport(host, port)
# save the host and port
self._host, self._port = self.host, self.port
# Authenticated proxies support
self.__username = username
self.__password = password
def connect(self):
# We are actually connecting to the proxy
self._set_hostport(self.__proxy, self.__proxy_port)
HTTPConnection.connect(self)
# Restore the real host and port
self._set_hostport(self._host, self._port)
def putrequest(self, method, url, skip_host=0):
# The URL has to include the real host
hostname = self._host
if self._port != self.default_port:
hostname = hostname + ':' + str(self._port)
newurl = "http://%s%s" % (hostname, url)
# Piggyback on the parent class
HTTPConnection.putrequest(self, method, newurl, skip_host=skip_host)
# Add proxy-specific headers
self._add_proxy_headers()
def _add_proxy_headers(self):
if not self.__username:
return
# Authenticated proxy
userpass = "%s:%s" % (self.__username, self.__password)
enc_userpass = base64.encodestring(userpass).replace("\n", "")
self.putheader("Proxy-Authorization", "Basic %s" % enc_userpass)
class HTTPSConnection(HTTPConnection):
response_class = HTTPResponse
default_port = httplib.HTTPSConnection.default_port
def __init__(self, host, port=None, trusted_certs=None):
HTTPConnection.__init__(self, host, port)
trusted_certs = trusted_certs or []
self.trusted_certs = trusted_certs
def connect(self):
"Connect to a host on a given (SSL) port"
results = socket.getaddrinfo(self.host, self.port,
socket.AF_UNSPEC, socket.SOCK_STREAM)
for r in results:
af, socktype, proto, canonname, sa = r
try:
sock = socket.socket(af, socktype, proto)
except socket.error, msg:
sock = None
continue
sock.settimeout(SSL.DEFAULT_TIMEOUT)
try:
sock.connect((self.host, self.port))
except socket.error, e:
sock.close()
sock = None
continue
break
if sock is None:
raise socket.error("Unable to connect to the host and port specified")
self.sock = SSL.SSLSocket(sock, self.trusted_certs)
self.sock.init_ssl()
class HTTPSProxyResponse(HTTPResponse):
def begin(self):
HTTPResponse.begin(self)
self.will_close = 0
class HTTPSProxyConnection(HTTPProxyConnection):
default_port = HTTPSConnection.default_port
def __init__(self, proxy, host, port=None, username=None, password=None,
trusted_certs=None):
HTTPProxyConnection.__init__(self, proxy, host, port, username, password)
trusted_certs = trusted_certs or []
self.trusted_certs = trusted_certs
def connect(self):
# Set the connection with the proxy
HTTPProxyConnection.connect(self)
# Use the stock HTTPConnection putrequest
host = "%s:%s" % (self._host, self._port)
HTTPConnection.putrequest(self, "CONNECT", host)
# Add proxy-specific stuff
self._add_proxy_headers()
# And send the request
HTTPConnection.endheaders(self)
# Save the response class
response_class = self.response_class
# And replace the response class with our own one, which does not
# close the connection after
self.response_class = HTTPSProxyResponse
response = HTTPConnection.getresponse(self)
# Restore the response class
self.response_class = response_class
# Close the response object manually
response.close()
if response.status != 200:
# Close the connection manually
self.close()
raise xmlrpclib.ProtocolError(host,
response.status, response.reason, response.msg)
self.sock.settimeout(SSL.DEFAULT_TIMEOUT)
self.sock = SSL.SSLSocket(self.sock, self.trusted_certs)
self.sock.init_ssl()
def putrequest(self, method, url, skip_host=0):
return HTTPConnection.putrequest(self, method, url, skip_host=skip_host)
def _add_proxy_headers(self):
HTTPProxyConnection._add_proxy_headers(self)
# Add a User-Agent header
self.putheader("User-Agent", self._user_agent)
def idn_pune_to_unicode(hostname):
""" Convert Internationalized domain name from Pune encoding to Unicode """
if hostname is None:
return None
else:
return hostname.decode('idna')
def idn_ascii_to_pune(hostname):
""" Convert domain name to Pune encoding. Hostname can be instance of string or Unicode """
if hostname is None:
return None
else:
if not isinstance(hostname, unicode):
hostname = unicode(hostname, 'utf-8')
return hostname.encode('idna')
|