-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcHTTPClient.py
268 lines (244 loc) · 12.8 KB
/
cHTTPClient.py
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
try: # mDebugOutput use is Optional
from mDebugOutput import *;
except: # Do nothing if not available.
ShowDebugOutput = lambda fxFunction: fxFunction;
fShowDebugOutput = lambda sMessage: None;
fEnableDebugOutputForModule = lambda mModule: None;
fEnableDebugOutputForClass = lambda cClass: None;
fEnableAllDebugOutput = lambda: None;
cCallStack = fTerminateWithException = fTerminateWithConsoleOutput = None;
from mHTTPConnections import cHTTPConnection, cHTTPConnectionsToServerPool, cURL;
from mMultiThreading import cLock, cWithCallbacks;
from mNotProvided import *;
try: # SSL support is optional.
from mSSL import cCertificateStore as c0CertificateStore;
except:
c0CertificateStore = None; # No SSL support
from .iHTTPClient import iHTTPClient;
# To turn access to data store in multiple variables into a single transaction, we will create locks.
# These locks should only ever be locked for a short time; if it is locked for too long, it is considered a "deadlock"
# bug, where "too long" is defined by the following value:
gnDeadlockTimeoutInSeconds = 1; # We're not doing anything time consuming, so this should suffice.
class cHTTPClient(iHTTPClient, cWithCallbacks):
u0zDefaultMaxNumberOfConnectionsToServer = 10;
n0zDefaultConnectTimeoutInSeconds = 10;
n0zDefaultSecureTimeoutInSeconds = 5;
n0zDefaultTransactionTimeoutInSeconds = 10;
@ShowDebugOutput
def __init__(oSelf,
o0zCertificateStore = zNotProvided,
u0zMaxNumberOfConnectionsToServer = zNotProvided,
n0zConnectTimeoutInSeconds = zNotProvided,
n0zSecureTimeoutInSeconds = zNotProvided,
n0zTransactionTimeoutInSeconds = zNotProvided,
bAllowUnverifiableCertificates = False,
bCheckHostname = True,
):
oSelf.__o0CertificateStore = (
o0zCertificateStore if fbIsProvided(o0zCertificateStore) else
c0CertificateStore() if c0CertificateStore else
None
);
oSelf.__u0zMaxNumberOfConnectionsToServer = fxGetFirstProvidedValue(u0zMaxNumberOfConnectionsToServer, oSelf.u0zDefaultMaxNumberOfConnectionsToServer);
# Timeouts can be provided through class default, instance defaults, or method call arguments.
oSelf.__n0zConnectTimeoutInSeconds = fxzGetFirstProvidedValueIfAny(n0zConnectTimeoutInSeconds, oSelf.n0zDefaultConnectTimeoutInSeconds);
oSelf.__n0zSecureTimeoutInSeconds = fxzGetFirstProvidedValueIfAny(n0zSecureTimeoutInSeconds, oSelf.n0zDefaultSecureTimeoutInSeconds);
oSelf.__n0zTransactionTimeoutInSeconds = fxzGetFirstProvidedValueIfAny(n0zTransactionTimeoutInSeconds, oSelf.n0zDefaultTransactionTimeoutInSeconds);
oSelf.__bAllowUnverifiableCertificates = bAllowUnverifiableCertificates;
oSelf.__bCheckHostname = bCheckHostname;
oSelf.__oPropertyAccessTransactionLock = cLock(
"%s.__oPropertyAccessTransactionLock" % oSelf.__class__.__name__,
n0DeadlockTimeoutInSeconds = gnDeadlockTimeoutInSeconds
);
oSelf.__doConnectionsToServerPool_by_sProtocolHostPort = {};
oSelf.__bStopping = False;
oSelf.__oTerminatedLock = cLock("%s.__oTerminatedLock" % oSelf.__class__.__name__, bLocked = True);
oSelf.fAddEvents(
"connect failed", "new connection",
"request sent", "response received", "request sent and response received",
"connection terminated",
"terminated",
);
@property
def bStopping(oSelf):
return oSelf.__bStopping;
@ShowDebugOutput
def fStop(oSelf):
oSelf.__oPropertyAccessTransactionLock.fAcquire();
try:
if oSelf.bTerminated:
return fShowDebugOutput("Already terminated");
if oSelf.__bStopping:
return fShowDebugOutput("Already stopping");
fShowDebugOutput("Stopping...");
# Prevent any new cHTTPConnectionsToServerPool instances from being created.
oSelf.__bStopping = True;
# Grab a list of active cHTTPConnectionsToServerPool instances that need to be stopped.
aoConnectionsToServerPools = oSelf.__doConnectionsToServerPool_by_sProtocolHostPort.values()
finally:
oSelf.__oPropertyAccessTransactionLock.fRelease();
if len(aoConnectionsToServerPools) == 0:
# We stopped when there were no connections: we are terminated.
fShowDebugOutput("Terminated.");
oSelf.__oTerminatedLock.fRelease();
oSelf.fFireEvent("terminated");
else:
fShowDebugOutput("Stopping connections to server pools...");
# Stop all cHTTPConnectionsToServerPool instances
for oConnectionsToServerPool in aoConnectionsToServerPools:
oConnectionsToServerPool.fStop();
@property
def bTerminated(oSelf):
return not oSelf.__oTerminatedLock.bLocked;
@ShowDebugOutput
def fTerminate(oSelf):
oSelf.__oPropertyAccessTransactionLock.fAcquire();
try:
if oSelf.bTerminated:
return fShowDebugOutput("Already terminated.");
fShowDebugOutput("Terminating...");
oSelf.__bStopping = True;
# Grab a list of active cHTTPConnectionsToServerPool instances that need to be terminated.
aoConnectionsToServerPools = oSelf.__doConnectionsToServerPool_by_sProtocolHostPort.values();
finally:
oSelf.__oPropertyAccessTransactionLock.fRelease();
# Terminate all cHTTPConnectionsToServerPool instances
if len(aoConnectionsToServerPools) == 0:
fShowDebugOutput("Terminated.");
oSelf.__oTerminatedLock.fRelease();
oSelf.fFireEvent("terminated");
else:
fShowDebugOutput("Terminating %d connections to server pools..." % len(aoConnectionsToServerPools));
for oConnectionsToServerPool in aoConnectionsToServerPools:
oConnectionsToServerPool.fTerminate();
@ShowDebugOutput
def fWait(oSelf):
return oSelf.__oTerminatedLock.fWait();
@ShowDebugOutput
def fbWait(oSelf, nTimeoutInSeconds):
return oSelf.__oTerminatedLock.fbWait(nTimeoutInSeconds);
def fo0GetProxyServerURLForURL(oSelf, oURL):
return None;
@ShowDebugOutput
def fo0GetResponseForRequestAndURL(oSelf,
oRequest, oURL,
u0zMaxStatusLineSize = zNotProvided,
u0zMaxHeaderNameSize = zNotProvided,
u0zMaxHeaderValueSize = zNotProvided,
u0zMaxNumberOfHeaders = zNotProvided,
u0zMaxBodySize = zNotProvided,
u0zMaxChunkSize = zNotProvided,
u0zMaxNumberOfChunks = zNotProvided,
u0MaxNumberOfChunksBeforeDisconnecting = None, # disconnect and return response once this many chunks are received.
):
if oSelf.__bStopping:
fShowDebugOutput("Stopping.");
return None;
oConnectionsToServerPool = oSelf.__foGetConnectionsToServerPoolForURL(oURL);
if oSelf.__bStopping:
fShowDebugOutput("Stopping.");
return None;
o0Response = oConnectionsToServerPool.fo0SendRequestAndReceiveResponse(
oRequest,
n0zConnectTimeoutInSeconds = oSelf.__n0zConnectTimeoutInSeconds,
n0zSecureTimeoutInSeconds = oSelf.__n0zSecureTimeoutInSeconds,
n0zTransactionTimeoutInSeconds = oSelf.__n0zTransactionTimeoutInSeconds,
u0zMaxStatusLineSize = u0zMaxStatusLineSize,
u0zMaxHeaderNameSize = u0zMaxHeaderNameSize,
u0zMaxHeaderValueSize = u0zMaxHeaderValueSize,
u0zMaxNumberOfHeaders = u0zMaxNumberOfHeaders,
u0zMaxBodySize = u0zMaxBodySize,
u0zMaxChunkSize = u0zMaxChunkSize,
u0zMaxNumberOfChunks = u0zMaxNumberOfChunks,
u0MaxNumberOfChunksBeforeDisconnecting = u0MaxNumberOfChunksBeforeDisconnecting,
);
if oSelf.__bStopping:
fShowDebugOutput("Stopping.");
return None;
assert o0Response, \
"Expected a response but got %s" % repr(o0Response);
return o0Response;
@ShowDebugOutput
def __foGetConnectionsToServerPoolForURL(oSelf, oURL):
# We will reuse connections to the same server if possible. Servers are identified by host name, port and whether
# or not the connection is secure. We may want to change this to identification by IP address rather than host name.
oSelf.__oPropertyAccessTransactionLock.fAcquire();
try:
oConnectionsToServerPool = oSelf.__doConnectionsToServerPool_by_sProtocolHostPort.get(oURL.sBase);
if oConnectionsToServerPool:
return oConnectionsToServerPool;
# No connections to the server have been made before: create a new Pool.
if oURL.bSecure:
assert oSelf.__o0CertificateStore, \
"Making secure connections requires a certificate store.";
if oSelf.__bAllowUnverifiableCertificates:
o0SSLContext = oSelf.__o0CertificateStore.foGetClientsideSSLContextWithoutVerification();
else:
o0SSLContext = oSelf.__o0CertificateStore.foGetClientsideSSLContextForHostname(
oURL.sHostname,
bCheckHostname = oSelf.__bCheckHostname
);
else:
o0SSLContext = None;
fShowDebugOutput("Creating new cConnectionsToServerPool for %s" % oURL.sBase);
oConnectionsToServerPool = cHTTPConnectionsToServerPool(
oServerBaseURL = oURL.oBase,
u0zMaxNumberOfConnectionsToServer = oSelf.__u0zMaxNumberOfConnectionsToServer,
o0SSLContext = o0SSLContext,
);
oConnectionsToServerPool.fAddCallback("new connection", oSelf.__fHandleNewConnectionCallbackFromConnectionsToServerPool);
oConnectionsToServerPool.fAddCallback("connect failed", oSelf.__fHandleConnectFailedCallbackFromConnectionsToServerPool);
oConnectionsToServerPool.fAddCallback("request sent", oSelf.__fHandleRequestSentCallbackFromConnectionsToServerPool);
oConnectionsToServerPool.fAddCallback("response received", oSelf.__fHandleResponseReceivedCallbackFromConnectionsToServerPool);
oConnectionsToServerPool.fAddCallback("request sent and response received", oSelf.__fHandleRequestSentAndResponseReceivedCallbackFromConnectionsToServerPool);
oConnectionsToServerPool.fAddCallback("connection terminated", oSelf.__fHandleConnectionTerminatedCallbackFromConnectionsToServerPool);
oConnectionsToServerPool.fAddCallback("terminated", oSelf.__fHandleTerminatedCallbackFromConnectionsToServerPool);
oSelf.__doConnectionsToServerPool_by_sProtocolHostPort[oURL.sBase] = oConnectionsToServerPool;
return oConnectionsToServerPool;
finally:
oSelf.__oPropertyAccessTransactionLock.fRelease();
def __fHandleConnectFailedCallbackFromConnectionsToServerPool(oSelf, oConnectionsToServerPool, sHostname, uPort, oException):
oSelf.fFireCallbacks("connect failed", sHostname, uPort, oException);
def __fHandleNewConnectionCallbackFromConnectionsToServerPool(oSelf, oConnectionsToServerPool, oConnection):
oSelf.fFireCallbacks("new connection", oConnection);
def __fHandleRequestSentCallbackFromConnectionsToServerPool(oSelf, oConnectionsToServerPool, oConnection, oRequest):
oSelf.fFireCallbacks("request sent", oConnection, oRequest);
def __fHandleResponseReceivedCallbackFromConnectionsToServerPool(oSelf, oConnectionsToServerPool, oConnection, oReponse):
oSelf.fFireCallbacks("response received", oConnection, oReponse);
def __fHandleRequestSentAndResponseReceivedCallbackFromConnectionsToServerPool(oSelf, oConnectionsToServerPool, oConnection, oRequest, oReponse):
oSelf.fFireCallbacks("request sent and response received", oConnection, oRequest, oReponse);
def __fHandleConnectionTerminatedCallbackFromConnectionsToServerPool(oSelf, oConnectionsToServerPool, oConnection):
oSelf.fFireCallbacks("connection terminated", oConnection);
@ShowDebugOutput
def __fHandleTerminatedCallbackFromConnectionsToServerPool(oSelf, oConnectionsToServerPool):
assert oSelf.__bStopping, \
"This is really unexpected!";
oSelf.__oPropertyAccessTransactionLock.fAcquire();
try:
for sProtocolHostPort in oSelf.__doConnectionsToServerPool_by_sProtocolHostPort:
if oSelf.__doConnectionsToServerPool_by_sProtocolHostPort[sProtocolHostPort] == oConnectionsToServerPool:
fShowDebugOutput("Removing cConnectionsToServerPool for %s" % sProtocolHostPort);
del oSelf.__doConnectionsToServerPool_by_sProtocolHostPort[sProtocolHostPort];
break;
else:
raise AssertionError("A cConnectionsToServerPool instance reported that it terminated, but we were not aware it existed");
# Return if we are not stopping or if there are other connections open:
if not oSelf.__bStopping:
return;
if len(oSelf.__doConnectionsToServerPool_by_sProtocolHostPort) > 0:
return;
finally:
oSelf.__oPropertyAccessTransactionLock.fRelease();
# We are stopping and the last connection just terminated: we are terminated.
fShowDebugOutput("Terminated.");
oSelf.__oTerminatedLock.fRelease();
oSelf.fFireCallbacks("terminated");
def fasGetDetails(oSelf):
# This is done without a property lock, so race-conditions exist and it
# approximates the real values.
if oSelf.bTerminated:
return ["terminated"];
return [s for s in [
"connected to %d servers" % len(oSelf.__doConnectionsToServerPool_by_sProtocolHostPort),
"stopping" if oSelf.__bStopping else None,
] if s];