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 Reimplementation of HTTP/HTTPSConnection to improve usability and enable
33 certificate validation
34 '''
35
36 from httplib import HTTPConnection
37 import socket
38
40 '''
41 Subclass of HTTPSxConnection that will use an existing connected sock
42 '''
43
44 - def __init__(self, sock, host, port=None, strict=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
45 '''
46 Constructor. It delegates construction to the base class
47 HTTPConnection and initializes the new member variables
48 with the default values from the Python documentation
49
50 @param sock: connected sock to use
51 @type sock: socket
52 @param host: host to connect to
53 @type host: str
54 @param port: port to connect to. use None for the default HTTP port
55 @type port: int|None
56 @param strict: n/a
57 @type strict: Unknown
58 @param timeout: default time for network operations
59 @type timeout: float
60 '''
61 HTTPConnection.__init__(self, host, port, strict, timeout)
62 self.sock = sock
63
64
66 '''
67 Overrriding is needed to avoid establishing a new connection ... this goes over
68 an existing socket
69 '''
70 pass
71
72
73 try:
74 import ssl
75 except ImportError:
77 '''
78 Stub class to raise an exception on instantiation and let the user know that SSL
79 failed
80 '''
82 '''
83 Constructor. It simply raises an exception
84 @raise NotImplemented
85 '''
86 raise NotImplemented('SSL support is missing. Https connections cannot be opened')
87
88
90 '''
91 Stub class to raise an exception on instantiation and let the user know that SSL
92 failed
93 '''
95 '''
96 Constructor. It simply raises an exception
97 @raise NotImplemented
98 '''
99 raise NotImplemented('SSL support is missing. Https connections cannot be opened')
100
101 else:
102 from httplib import HTTPSConnection
103
105 '''
106 Reimplementation of HTTPSConnection but sublcassing from L{HTTPSConnection} to allow
107 certificate validation if wished
108
109 HTTPxConnection omitted this possibilitiy which is supported by the
110 Python ssl library and the wrap_socket functionality
111
112 HTTPxConnection is subclassed, receives two new member variables
113 and overrides connect to better call ssl.wrap_socket
114
115 Please read the Python 2.6 documentation on SSL and certificate validation
116
117 @ivar cert_reqs: It dictates if certificate validation will be done.
118 It uses the ssl: CERT_NONE, CERT_OPTIONAL, CERT_REQUIRED
119 values from the ssl module
120 @type cert_reqs: enumeration
121 @ivar ca_certs: Path to the file containing the root (chain of)
122 certificate(s)
123 @type ca_certs: str
124 '''
125
126 - def __init__(self, host, port=None, key_file=None, cert_file=None,
127 strict=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
128 '''
129 Constructor. It delegates construction to the base class
130 HTTPConnection and initializes the new member variables
131 with the default values from the Python documentation
132
133 @param host: host to connect to
134 @type host: str
135 @param port: port to connect to. use None for the default HTTP port
136 @type port: int|None
137 @param key_file: path to private key_file or None if stored in cert_file
138 @type key_file: str|None
139 @param cert_file: path to certificate to be used for validation or None
140 @type cert_file: str|None
141 @param strict: n/a
142 @type strict: Unknown
143 @param timeout: default time for network operations
144 @type timeout: float
145 '''
146 HTTPSConnection.__init__(self, host, port, key_file, cert_file, strict, timeout)
147 self.cert_reqs = ssl.CERT_NONE
148 self.ca_certs = None
149
150
152 '''
153 Opens the connection and wraps it in a ssl socket
154
155 Overrriding is needed to enable certificate validation
156 '''
157 self.sock = socket.create_connection((self.host, self.port),
158 self.timeout, self.source_address)
159 self.sslwrap()
160
161
163 '''
164 Wraps the connection into a ssl socket
165 '''
166 self.sock = ssl.wrap_socket(self.sock,
167 self.key_file, self.cert_file,
168 cert_reqs=self.cert_reqs, ca_certs=self.ca_certs)
169
170
172 '''
173 Subclass of HTTPSxConnection that will use an existing connected sock
174 '''
175
176 - def __init__(self, sock, host, port=None, key_file=None, cert_file=None,
177 strict=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
178 '''
179 Constructor. It delegates construction to the base class
180 HTTPConnection and initializes the new member variables
181 with the default values from the Python documentation
182
183 HTTPConnection and initializes the new member variables
184 with the default values from the Python documentation
185
186 @param sock: connected sock to use
187 @type sock: socket
188 @param host: host to connect to
189 @type host: str
190 @param port: port to connect to. use None for the default HTTP port
191 @type port: int|None
192 @param key_file: path to private key_file or None if stored in cert_file
193 @type key_file: str|None
194 @param cert_file: path to certificate to be used for validation or None
195 @type cert_file: str|None
196 @param strict: n/a
197 @type strict: Unknown
198 @param timeout: default time for network operations
199 @type timeout: float
200 '''
201 HTTPSxConnection.__init__(self, host, port, key_file, cert_file, strict, timeout)
202 self.sock = sock
203
204
206 '''
207 Wraps the existing socket into SSL
208 '''
209 self.sslwrap()
210