-
Notifications
You must be signed in to change notification settings - Fork 318
Expand file tree
/
Copy pathIHttpServerAssociateWithThreadPool.cpp
More file actions
203 lines (180 loc) · 5.72 KB
/
Copy pathIHttpServerAssociateWithThreadPool.cpp
File metadata and controls
203 lines (180 loc) · 5.72 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
// <Snippet1>
#define _WINSOCKAPI_
#include <windows.h>
#include <sal.h>
#include <httpserv.h>
// Create a pointer for the global server interface.
IHttpServer * g_pGlobalInfo = NULL;
// Create a global file handle.
HANDLE g_hFile = NULL;
// Create a utility method for asynchronous completion.
VOID
__stdcall
MyCompletionRoutine(
DWORD dwErrorCode,
DWORD dwNumberOfBytesTransfered,
LPOVERLAPPED lpOverlapped)
{
if ((g_hFile != NULL) && (g_hFile != INVALID_HANDLE_VALUE))
{
CloseHandle(g_hFile);
}
return;
}
// Create the module class.
class MyHttpModule : public CHttpModule
{
public:
REQUEST_NOTIFICATION_STATUS
OnSendResponse(
IN IHttpContext * pHttpContext,
IN ISendResponseProvider * pProvider
)
{
UNREFERENCED_PARAMETER( pHttpContext );
UNREFERENCED_PARAMETER( pProvider );
BOOL fAuthenticated = FALSE;
// Retrieve an IHttpUser interface.
IHttpUser * pHttpUser = pHttpContext->GetUser();
char * pszUserName = NULL;
PCWSTR pwszUserName = NULL;
// Test for an error.
if (pHttpUser != NULL)
{
// Retrieve the user name.
pwszUserName = pHttpUser->GetUserName();
// Test for an error.
if (pwszUserName!=NULL)
{
// Test for anonymous user.
if (wcslen(pwszUserName)>0)
{
// Set the flag to indicate an authenticated user.
fAuthenticated = TRUE;
}
}
}
// Test for an authenticated user.
if (fAuthenticated == FALSE)
{
// Clear the existing response.
pHttpContext->GetResponse()->Clear();
// Return an access denied message.
pHttpContext->GetResponse()->SetStatus(401,"Access denied.",0,0);
// End additional processing.
return RQ_NOTIFICATION_FINISH_REQUEST;
}
g_hFile = CreateFile(TEXT("d:\\inetpub\\wwwroot\\myfile.txt"),
GENERIC_WRITE,0,NULL,CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
if (g_hFile == INVALID_HANDLE_VALUE)
{
char szBuffer[256] = "";
sprintf_s(szBuffer,256,"Could not open file (error %d)\n", GetLastError());
WriteResponseMessage(pHttpContext,szBuffer);
return RQ_NOTIFICATION_FINISH_REQUEST;
}
else
{
OVERLAPPED oOverlapped;
oOverlapped.hEvent = NULL;
oOverlapped.Offset = 0;
oOverlapped.OffsetHigh = 0;
DWORD dwBytesWritten = 0;
g_pGlobalInfo->AssociateWithThreadPool(g_hFile,&MyCompletionRoutine);
//pszUserName = (char*) pHttpContext->AllocateRequestMemory( (DWORD) wcslen(pwszUserName)+1 );
//wcstombs(pszUserName,pwszUserName,wcslen(pwszUserName));
//WriteFile(g_hFile, pszUserName, strlen(pszUserName), &dwBytesWritten, &oOverlapped);
WriteFile(g_hFile, "Hello", strlen("Hello"), &dwBytesWritten, oOverlapped);
return RQ_NOTIFICATION_PENDING;
}
}
private:
// Create a utility method that inserts a string value into the response.
HRESULT WriteResponseMessage(
IHttpContext * pHttpContext,
PCSTR pszBuffer
)
{
// Create an HRESULT to receive return values from methods.
HRESULT hr;
// Create a data chunk. (Defined in the Http.h file.)
HTTP_DATA_CHUNK dataChunk;
// Set the chunk to a chunk in memory.
dataChunk.DataChunkType = HttpDataChunkFromMemory;
// Buffer for bytes written of data chunk.
DWORD cbSent;
// Set the chunk to the buffer.
dataChunk.FromMemory.pBuffer =
(PVOID) pszBuffer;
// Set the chunk size to the buffer size.
dataChunk.FromMemory.BufferLength =
(USHORT) strlen(pszBuffer);
// Insert the data chunk into the response.
hr = pHttpContext->GetResponse()->WriteEntityChunks(
&dataChunk,1,FALSE,TRUE,&cbSent);
// Test for an error.
if (FAILED(hr))
{
// Return the error status.
return hr;
}
// Return a success status.
return S_OK;
}
};
// 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 the factory 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 );
// Store the pointer for the global server interface.
g_pGlobalInfo = pGlobalInfo;
// Set the request notifications and exit.
return pModuleInfo->SetRequestNotifications(
new MyHttpModuleFactory,
RQ_SEND_RESPONSE,
0
);
}
// </Snippet1>