forked from tjanczuk/iisnode
-
Notifications
You must be signed in to change notification settings - Fork 100
/
cnodehttpstoredcontext.cpp
461 lines (371 loc) · 11.4 KB
/
cnodehttpstoredcontext.cpp
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#include "precomp.h"
CNodeHttpStoredContext::CNodeHttpStoredContext(CNodeApplication* nodeApplication, CNodeEventProvider* eventProvider, IHttpContext* context)
: nodeApplication(nodeApplication), context(context), process(NULL), buffer(NULL), bufferSize(0), dataSize(0), parsingOffset(0),
chunkLength(0), chunkTransmitted(0), isChunked(FALSE), pipe(INVALID_HANDLE_VALUE), result(S_OK), isLastChunk(FALSE),
requestNotificationStatus(RQ_NOTIFICATION_PENDING), connectionRetryCount(0), pendingAsyncOperationCount(1),
targetUrl(NULL), targetUrlLength(0), childContext(NULL), isConnectionFromPool(FALSE), expectResponseBody(TRUE),
closeConnection(FALSE), isUpgrade(FALSE), upgradeContext(NULL), opaqueFlagSet(FALSE), requestPumpStarted(FALSE),
responseChunkBufferSize(0), m_cRefs(1)
{
IHttpTraceContext* tctx;
LPCGUID pguid;
this->responseChunk.DataChunkType = HttpDataChunkFromMemory;
this->responseChunk.FromMemory.pBuffer = NULL;
RtlZeroMemory(&this->asyncContext, sizeof(ASYNC_CONTEXT));
CoCreateGuid(&this->activityId);
this->asyncContext.data = this;
this->eventProvider = eventProvider;
}
CNodeHttpStoredContext::~CNodeHttpStoredContext()
{
if (NULL != this->upgradeContext)
{
delete this->upgradeContext;
this->upgradeContext = NULL;
}
else if (INVALID_HANDLE_VALUE != this->pipe)
{
CloseHandle(this->pipe);
this->pipe = INVALID_HANDLE_VALUE;
}
if (this->responseChunk.FromMemory.pBuffer) {
free(this->responseChunk.FromMemory.pBuffer);
this->responseChunk.FromMemory.pBuffer = NULL;
responseChunkBufferSize = 0;
}
}
HRESULT CNodeHttpStoredContext::EnsureResponseChunk(DWORD size, HTTP_DATA_CHUNK** chunk)
{
HRESULT hr;
if (size > this->responseChunkBufferSize)
{
if (this->responseChunk.FromMemory.pBuffer)
{
free(this->responseChunk.FromMemory.pBuffer);
this->responseChunk.FromMemory.pBuffer = NULL;
this->responseChunkBufferSize = 0;
}
ErrorIf(NULL == (this->responseChunk.FromMemory.pBuffer = malloc(size)), ERROR_NOT_ENOUGH_MEMORY);
this->responseChunkBufferSize = size;
}
*chunk = &this->responseChunk;
return S_OK;
Error:
return hr;
}
IHttpContext* CNodeHttpStoredContext::GetHttpContext()
{
return this->context;
}
CNodeApplication* CNodeHttpStoredContext::GetNodeApplication()
{
return this->nodeApplication;
}
void CNodeHttpStoredContext::SetNextProcessor(LPOVERLAPPED_COMPLETION_ROUTINE_IISNODE processor)
{
this->asyncContext.completionProcessor = processor;
this->SetContinueSynchronously(FALSE);
}
LPOVERLAPPED CNodeHttpStoredContext::GetOverlapped()
{
return &this->asyncContext.overlapped;
}
LPOVERLAPPED CNodeHttpStoredContext::InitializeOverlapped()
{
RtlZeroMemory(&this->asyncContext.overlapped, sizeof(OVERLAPPED));
return &this->asyncContext.overlapped;
}
void CNodeHttpStoredContext::CleanupStoredContext()
{
DereferenceNodeHttpStoredContext();
}
CNodeProcess* CNodeHttpStoredContext::GetNodeProcess()
{
return this->process;
}
void CNodeHttpStoredContext::SetNodeProcess(CNodeProcess* process)
{
this->process = process;
}
ASYNC_CONTEXT* CNodeHttpStoredContext::GetAsyncContext()
{
return &this->asyncContext;
}
CNodeHttpStoredContext* CNodeHttpStoredContext::Get(LPOVERLAPPED overlapped)
{
return overlapped == NULL ? NULL : (CNodeHttpStoredContext*)((ASYNC_CONTEXT*)overlapped)->data;
}
HANDLE CNodeHttpStoredContext::GetPipe()
{
return this->pipe;
}
void CNodeHttpStoredContext::SetPipe(HANDLE pipe)
{
this->pipe = pipe;
if (NULL != this->upgradeContext)
{
this->upgradeContext->SetPipe(pipe);
}
}
DWORD CNodeHttpStoredContext::GetConnectionRetryCount()
{
return this->connectionRetryCount;
}
void CNodeHttpStoredContext::SetConnectionRetryCount(DWORD count)
{
this->connectionRetryCount = count;
}
void* CNodeHttpStoredContext::GetBuffer()
{
return this->buffer;
}
DWORD CNodeHttpStoredContext::GetBufferSize()
{
return this->bufferSize;
}
void* CNodeHttpStoredContext::GetChunkBuffer()
{
// leave room in the allocated memory buffer for a chunk transfer encoding header that
// will be calculated only after the entity body chunk had been read
return (void*)((char*)this->GetBuffer() + this->GetChunkHeaderMaxSize());
}
DWORD CNodeHttpStoredContext::GetChunkBufferSize()
{
// leave room in the buffer for the chunk header and the CRLF following a chunk
return this->GetBufferSize() - this->GetChunkHeaderMaxSize() - 2;
}
DWORD CNodeHttpStoredContext::GetChunkHeaderMaxSize()
{
// the maximum size of the chunk header
return 64;
}
void** CNodeHttpStoredContext::GetBufferRef()
{
return &this->buffer;
}
DWORD* CNodeHttpStoredContext::GetBufferSizeRef()
{
return &this->bufferSize;
}
void CNodeHttpStoredContext::SetBuffer(void* buffer)
{
this->buffer = buffer;
}
void CNodeHttpStoredContext::SetBufferSize(DWORD bufferSize)
{
this->bufferSize = bufferSize;
}
DWORD CNodeHttpStoredContext::GetDataSize()
{
return this->dataSize;
}
DWORD CNodeHttpStoredContext::GetParsingOffset()
{
return this->parsingOffset;
}
void CNodeHttpStoredContext::SetDataSize(DWORD dataSize)
{
this->dataSize = dataSize;
}
void CNodeHttpStoredContext::SetParsingOffset(DWORD parsingOffset)
{
this->parsingOffset = parsingOffset;
}
LONGLONG CNodeHttpStoredContext::GetChunkTransmitted()
{
return this->chunkTransmitted;
}
LONGLONG CNodeHttpStoredContext::GetChunkLength()
{
return this->chunkLength;
}
void CNodeHttpStoredContext::SetChunkTransmitted(LONGLONG length)
{
this->chunkTransmitted = length;
}
void CNodeHttpStoredContext::SetChunkLength(LONGLONG length)
{
this->chunkLength = length;
}
BOOL CNodeHttpStoredContext::GetIsChunked()
{
return this->isChunked;
}
void CNodeHttpStoredContext::SetIsChunked(BOOL chunked)
{
this->isChunked = chunked;
}
void CNodeHttpStoredContext::SetIsLastChunk(BOOL lastChunk)
{
this->isLastChunk = lastChunk;
}
BOOL CNodeHttpStoredContext::GetIsLastChunk()
{
return this->isLastChunk;
}
HRESULT CNodeHttpStoredContext::GetHresult()
{
return this->result;
}
void CNodeHttpStoredContext::SetHresult(HRESULT result)
{
this->result = result;
}
REQUEST_NOTIFICATION_STATUS CNodeHttpStoredContext::GetRequestNotificationStatus()
{
return this->requestNotificationStatus;
}
void CNodeHttpStoredContext::SetRequestNotificationStatus(REQUEST_NOTIFICATION_STATUS status)
{
this->requestNotificationStatus = status;
}
GUID* CNodeHttpStoredContext::GetActivityId()
{
return &this->activityId;
}
long CNodeHttpStoredContext::IncreasePendingAsyncOperationCount()
{
this->eventProvider->Log(this->context,
L"iisnode increases pending async operation count",
WINEVENT_LEVEL_VERBOSE,
this->GetActivityId());
if (this->requestPumpStarted)
{
return InterlockedIncrement(&this->upgradeContext->pendingAsyncOperationCount);
}
else
{
return InterlockedIncrement(&this->pendingAsyncOperationCount);
}
}
long CNodeHttpStoredContext::DecreasePendingAsyncOperationCount()
{
this->eventProvider->Log(this->context,
L"iisnode decreases pending async operation count",
WINEVENT_LEVEL_VERBOSE,
this->GetActivityId());
if (this->requestPumpStarted)
{
return InterlockedDecrement(&this->upgradeContext->pendingAsyncOperationCount);
}
else
{
return InterlockedDecrement(&this->pendingAsyncOperationCount);
}
}
PCSTR CNodeHttpStoredContext::GetTargetUrl()
{
return this->targetUrl;
}
DWORD CNodeHttpStoredContext::GetTargetUrlLength()
{
return this->targetUrlLength;
}
void CNodeHttpStoredContext::SetTargetUrl(PCSTR targetUrl, DWORD targetUrlLength)
{
this->targetUrl = targetUrl;
this->targetUrlLength = targetUrlLength;
}
void CNodeHttpStoredContext::SetChildContext(IHttpContext* context)
{
this->childContext = context;
}
IHttpContext* CNodeHttpStoredContext::GetChildContext()
{
return this->childContext;
}
BOOL CNodeHttpStoredContext::GetIsConnectionFromPool()
{
return this->isConnectionFromPool;
}
void CNodeHttpStoredContext::SetIsConnectionFromPool(BOOL fromPool)
{
this->isConnectionFromPool = fromPool;
}
void CNodeHttpStoredContext::SetExpectResponseBody(BOOL expect)
{
this->expectResponseBody = expect;
}
BOOL CNodeHttpStoredContext::GetExpectResponseBody()
{
return this->expectResponseBody;
}
void CNodeHttpStoredContext::SetCloseConnection(BOOL close)
{
this->closeConnection = close;
}
BOOL CNodeHttpStoredContext::GetCloseConnection()
{
return this->closeConnection;
}
HRESULT CNodeHttpStoredContext::SetupUpgrade()
{
HRESULT hr;
ErrorIf(this->isUpgrade, E_FAIL);
// The upgradeContext is used to pump incoming bytes to the node.js application.
// The 'this' context is used to pump outgoing bytes to IIS. Once the response headers are flushed,
// both contexts are used concurrently in a full duplex, asynchronous fashion.
// The last context to complete pumping closes the IIS request.
ErrorIf(NULL == (this->upgradeContext = new CNodeHttpStoredContext(this->GetNodeApplication(), this->eventProvider, this->GetHttpContext())),
ERROR_NOT_ENOUGH_MEMORY);
this->upgradeContext->bufferSize = CModuleConfiguration::GetInitialRequestBufferSize(this->context);
ErrorIf(NULL == (this->upgradeContext->buffer = this->context->AllocateRequestMemory(this->upgradeContext->bufferSize)),
ERROR_NOT_ENOUGH_MEMORY);
// Enable duplex read/write of data
IHttpContext3* ctx3 = (IHttpContext3*)this->GetHttpContext();
ctx3->EnableFullDuplex();
// Disable caching and buffering
ctx3->GetResponse()->DisableBuffering();
ctx3->GetResponse()->DisableKernelCache();
this->upgradeContext->SetPipe(this->GetPipe());
this->upgradeContext->SetNodeProcess(this->GetNodeProcess());
this->upgradeContext->isUpgrade = TRUE;
this->isUpgrade = TRUE;
return S_OK;
Error:
return hr;
}
BOOL CNodeHttpStoredContext::GetIsUpgrade()
{
return this->isUpgrade;
}
CNodeHttpStoredContext* CNodeHttpStoredContext::GetUpgradeContext()
{
return this->upgradeContext;
}
void CNodeHttpStoredContext::SetOpaqueFlag()
{
this->opaqueFlagSet = TRUE;
}
BOOL CNodeHttpStoredContext::GetOpaqueFlagSet()
{
return this->opaqueFlagSet;
}
void CNodeHttpStoredContext::SetRequestPumpStarted()
{
// The pending async operation count for the pair of CNodeHttpStoredContexts will be maintained in the upgradeContext instance from now on.
// The +1 represents the creation of the upgradeContext and the corresponding decrease happens when the pumping of incoming bytes completes.
this->upgradeContext->pendingAsyncOperationCount = this->pendingAsyncOperationCount + 1;
this->pendingAsyncOperationCount = 0;
this->requestPumpStarted = TRUE;
}
BOOL CNodeHttpStoredContext::GetRequestPumpStarted()
{
return this->requestPumpStarted;
}
FILETIME* CNodeHttpStoredContext::GetStartTime()
{
return &this->startTime;
}
DWORD CNodeHttpStoredContext::GetBytesCompleted()
{
return this->asyncContext.bytesCompleteted;
}
void CNodeHttpStoredContext::SetBytesCompleted(DWORD bytesCompleted)
{
this->asyncContext.bytesCompleteted = bytesCompleted;
}
void CNodeHttpStoredContext::SetContinueSynchronously(BOOL continueSynchronously)
{
this->asyncContext.continueSynchronously = continueSynchronously;
}