Skip to content

Commit 29b8d5a

Browse files
committed
add support for HTTPS
Modify HTTP to use delegation instead of inheritance. The _connection_class attribute of the class defines what class to delegate to. The HTTPS class is a subclass of HTTP that redefines _connection_class.
1 parent 1c2b178 commit 29b8d5a

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

Lib/httplib.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
#
2-
# HTTP/1.1 client library
3-
#
4-
5-
# ### this may as well go into a doc string...
61
"""HTTP/1.1 client library
72
83
<intro stuff goes here>
@@ -71,7 +66,6 @@
7166
Req-sent-unread-response _CS_REQ_SENT <response_class>
7267
"""
7368

74-
7569
import socket
7670
import string
7771
import mimetools
@@ -599,14 +593,16 @@ def connect(self):
599593
self.sock = FakeSocket(sock, ssl)
600594

601595

602-
class HTTP(HTTPConnection):
596+
class HTTP:
603597
"Compatibility class with httplib.py from 1.5."
604598

605599
_http_vsn = 10
606600
_http_vsn_str = 'HTTP/1.0'
607601

608602
debuglevel = 0
609603

604+
_connection_class = HTTPConnection
605+
610606
def __init__(self, host='', port=None, **x509):
611607
"Provide a default host, since the superclass requires one."
612608

@@ -617,7 +613,11 @@ def __init__(self, host='', port=None, **x509):
617613
# Note that we may pass an empty string as the host; this will throw
618614
# an error when we attempt to connect. Presumably, the client code
619615
# will call connect before then, with a proper host.
620-
HTTPConnection.__init__(self, host, port)
616+
self._conn = self._connection_class(host, port)
617+
# set up delegation to flesh out interface
618+
self.send = self._conn.send
619+
self.putrequest = self._conn.putrequest
620+
self.endheaders = self._conn.endheaders
621621

622622
# we never actually use these for anything, but we keep them here for
623623
# compatibility with post-1.5.2 CVS.
@@ -630,8 +630,8 @@ def connect(self, host=None, port=None):
630630
"Accept arguments to set the host/port, since the superclass doesn't."
631631

632632
if host is not None:
633-
self._set_hostport(host, port)
634-
HTTPConnection.connect(self)
633+
self._conn._set_hostport(host, port)
634+
self._conn.connect()
635635

636636
def set_debuglevel(self, debuglevel):
637637
"The class no longer supports the debuglevel."
@@ -643,8 +643,8 @@ def getfile(self):
643643

644644
def putheader(self, header, *values):
645645
"The superclass allows only one value argument."
646-
HTTPConnection.putheader(self, header,
647-
string.joinfields(values, '\r\n\t'))
646+
self._conn.putheader(header,
647+
string.joinfields(values, '\r\n\t'))
648648

649649
def getreply(self):
650650
"""Compat definition since superclass does not define it.
@@ -655,14 +655,14 @@ def getreply(self):
655655
- any RFC822 headers in the response from the server
656656
"""
657657
try:
658-
response = self.getresponse()
658+
response = self._conn.getresponse()
659659
except BadStatusLine, e:
660660
### hmm. if getresponse() ever closes the socket on a bad request,
661661
### then we are going to have problems with self.sock
662662

663663
### should we keep this behavior? do people use it?
664664
# keep the socket open (as a file), and return it
665-
self.file = self.sock.makefile('rb', 0)
665+
self.file = self._conn.sock.makefile('rb', 0)
666666

667667
# close our socket -- we want to restart after any protocol error
668668
self.close()
@@ -675,7 +675,7 @@ def getreply(self):
675675
return response.status, response.reason, response.msg
676676

677677
def close(self):
678-
HTTPConnection.close(self)
678+
self._conn.close()
679679

680680
# note that self.file == response.fp, which gets closed by the
681681
# superclass. just clear the object ref here.
@@ -684,6 +684,17 @@ def close(self):
684684
### do it
685685
self.file = None
686686

687+
if hasattr(socket, 'ssl'):
688+
class HTTPS(HTTP):
689+
"""Compatibility with 1.5 httplib interface
690+
691+
Python 1.5.2 did not have an HTTPS class, but it defined an
692+
interface for sending http requests that is also useful for
693+
https.
694+
"""
695+
696+
_connection_class = HTTPSConnection
697+
687698

688699
class HTTPException(Exception):
689700
pass
@@ -764,7 +775,7 @@ def test():
764775
print h.getfile().read()
765776

766777
if hasattr(socket, 'ssl'):
767-
host = 'www.c2.net'
778+
host = 'sourceforge.net'
768779
hs = HTTPS()
769780
hs.connect(host)
770781
hs.putrequest('GET', selector)

0 commit comments

Comments
 (0)