-
Notifications
You must be signed in to change notification settings - Fork 324
Expand file tree
/
Copy pathIHttpCompletionInfoIHttpCompletionInfo.cpp
More file actions
234 lines (205 loc) · 6.61 KB
/
IHttpCompletionInfoIHttpCompletionInfo.cpp
File metadata and controls
234 lines (205 loc) · 6.61 KB
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
// <Snippet1>
#define _WINSOCKAPI_
#include <windows.h>
#include <sal.h>
#include <httpserv.h>
#include <wchar.h>
// Create the module class.
class MyHttpModule : public CHttpModule
{
public:
REQUEST_NOTIFICATION_STATUS
OnBeginRequest(
IN IHttpContext * pHttpContext,
IN IHttpEventProvider * pProvider
)
{
UNREFERENCED_PARAMETER( pProvider );
// Create an HRESULT to receive return values from methods.
HRESULT hr;
// Buffer to store the byte count.
DWORD cbSent = 0;
// Buffer to store if asyncronous completion is pending.
BOOL fCompletionExpected = false;
// Create an example string to return to the Web client.
char szBuffer[] = "Hello World!";
// Clear the existing response.
pHttpContext->GetResponse()->Clear();
// Set the MIME type to plain text.
pHttpContext->GetResponse()->SetHeader(
HttpHeaderContentType,"text/plain",
(USHORT)strlen("text/plain"),TRUE);
// Create a data chunk.
HTTP_DATA_CHUNK dataChunk;
// Set the chunk to a chunk in memory.
dataChunk.DataChunkType = HttpDataChunkFromMemory;
// Set the chunk to the buffer.
dataChunk.FromMemory.pBuffer =
(PVOID) szBuffer;
// Set the chunk size to the buffer size.
dataChunk.FromMemory.BufferLength =
(USHORT) strlen(szBuffer);
// Insert the data chunk into the response.
hr = pHttpContext->GetResponse()->WriteEntityChunks(
&dataChunk,1,TRUE,TRUE,&cbSent,&fCompletionExpected);
// Test for a failure.
if (FAILED(hr))
{
// Set the HTTP status.
pHttpContext->GetResponse()->SetStatus(
500,"Server Error",0,hr);
// End additional processing.
return RQ_NOTIFICATION_FINISH_REQUEST;
}
// Test for pending asynchronous operations.
if (fCompletionExpected)
{
return RQ_NOTIFICATION_PENDING;
}
// Return processing to the pipeline.
return RQ_NOTIFICATION_CONTINUE;
}
REQUEST_NOTIFICATION_STATUS
OnMapRequestHandler(
IN IHttpContext * pHttpContext,
IN IMapHandlerProvider * pProvider
)
{
// Create an HRESULT to receive return values from methods.
HRESULT hr;
// Buffer to store the byte count.
DWORD cbSent = 0;
// Buffer to store if asyncronous completion is pending.
BOOL fCompletionExpected = false;
// Flush the response to the client.
hr = pHttpContext->GetResponse()->Flush(
TRUE,FALSE,&cbSent,&fCompletionExpected);
// Test for a failure.
if (FAILED(hr))
{
// Set the HTTP status.
pHttpContext->GetResponse()->SetStatus(
500,"Server Error",0,hr);
}
// Test for pending asynchronous operations.
if (fCompletionExpected)
{
return RQ_NOTIFICATION_PENDING;
}
// End additional processing.
return RQ_NOTIFICATION_CONTINUE;
}
REQUEST_NOTIFICATION_STATUS
OnAsyncCompletion(
IN IHttpContext * pHttpContext,
IN DWORD dwNotification,
IN BOOL fPostNotification,
IN IHttpEventProvider * pProvider,
IN IHttpCompletionInfo * pCompletionInfo
)
{
if ( NULL != pCompletionInfo )
{
// Create strings for completion information.
char szNotification[256] = "";
char szBytes[256] = "";
char szStatus[256] = "";
// Retrieve and format the completion information.
sprintf_s(szNotification,255,"Notification: %u",
dwNotification);
sprintf_s(szBytes,255,"Completion Bytes: %u",
pCompletionInfo->GetCompletionBytes());
sprintf_s(szStatus,255,"Completion Status: 0x%08x",
pCompletionInfo->GetCompletionStatus());
// Create an array of strings.
LPCSTR szBuffer[3] = {szNotification,szBytes,szStatus};
// Write the strings to the Event Viewer.
WriteEventViewerLog(szBuffer,3);
}
// Return processing to the pipeline.
return RQ_NOTIFICATION_CONTINUE;
}
MyHttpModule(void)
{
// Open a handle to the Event Viewer.
m_hEventLog = RegisterEventSource( NULL,"IISADMIN" );
}
~MyHttpModule(void)
{
// Test if the handle for the Event Viewer is open.
if (NULL != m_hEventLog)
{
// Close the handle to the Event Viewer.
DeregisterEventSource( m_hEventLog );
m_hEventLog = NULL;
}
}
private:
// Handle for the Event Viewer.
HANDLE m_hEventLog;
// Define a method that writes to the Event Viewer.
BOOL WriteEventViewerLog(LPCSTR * lpStrings, WORD wNumStrings)
{
// Test whether the handle for the Event Viewer is open.
if (NULL != m_hEventLog)
{
// Write any strings to the Event Viewer and return.
return ReportEvent(
m_hEventLog, EVENTLOG_INFORMATION_TYPE,
0, 0, NULL, wNumStrings, 0, lpStrings, NULL );
}
return FALSE;
}
};
// Create the module's class factory.
class MyHttpModuleFactory : public IHttpModuleFactory
{
public:
HRESULT
GetHttpModule(
OUT CHttpModule ** ppModule,
IN IModuleAllocator * pAllocator
)
{
UNREFERENCED_PARAMETER( pAllocator );
// Create a new instance.
MyHttpModule * pModule = new MyHttpModule;
// Test for an error.
if (!pModule)
{
// Return an error if we cannot create the instance.
return HRESULT_FROM_WIN32( ERROR_NOT_ENOUGH_MEMORY );
}
else
{
// Return a pointer to the module.
*ppModule = pModule;
pModule = NULL;
// Return a success status.
return S_OK;
}
}
void Terminate()
{
// Remove the class from memory.
delete this;
}
};
// Create the module's exported registration function.
HRESULT
__stdcall
RegisterModule(
DWORD dwServerVersion,
IHttpModuleRegistrationInfo * pModuleInfo,
IHttpServer * pGlobalInfo
)
{
UNREFERENCED_PARAMETER( dwServerVersion );
UNREFERENCED_PARAMETER( pGlobalInfo );
return pModuleInfo->SetRequestNotifications(
new MyHttpModuleFactory,
RQ_BEGIN_REQUEST | RQ_MAP_REQUEST_HANDLER,
0
);
}
// </Snippet1>