[r737]: trunk / py2exe / source / run_isapi.c  Maximize  Restore  History

Download this file

275 lines (248 with data), 9.9 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
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
/*
* Copyright (c) 2000, 2001 Thomas Heller
* Copyright (c) 2003 Mark Hammond
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <windows.h>
#include "httpext.h"
#include <httpfilt.h>
//#include "Python.h"
#include <stdio.h>
#include "Python-dynload.h"
typedef BOOL (__stdcall *__PROC__GetExtensionVersion)(HSE_VERSION_INFO *pVer);
typedef BOOL (__stdcall *__PROC__HttpExtensionProc)(EXTENSION_CONTROL_BLOCK *pECB);
typedef BOOL (__stdcall *__PROC__TerminateExtension)(DWORD dwFlags);
typedef BOOL (__stdcall *__PROC__GetFilterVersion)(HTTP_FILTER_VERSION *pVer);
typedef DWORD (__stdcall *__PROC__HttpFilterProc)(HTTP_FILTER_CONTEXT *phfc, DWORD NotificationType, VOID *pvData);
typedef BOOL (__stdcall *__PROC__TerminateFilter)(DWORD status);
typedef void (__stdcall *__PROC__PyISAPISetOptions)(const char *modname, BOOL is_frozen);
typedef BOOL (__stdcall *__PROC__WriteEventLogMessage)(WORD eventType, DWORD eventID, WORD num_inserts, const char **inserts);
CRITICAL_SECTION csInit; // protecting our init code
HMODULE gInstance = 0;
HMODULE hmodPyISAPI = 0;
BOOL have_init = FALSE;
__PROC__GetExtensionVersion pGetExtensionVersion = NULL;
__PROC__HttpExtensionProc pHttpExtensionProc = NULL;
__PROC__TerminateExtension pTerminateExtension = NULL;
__PROC__GetFilterVersion pGetFilterVersion = NULL;
__PROC__HttpFilterProc pHttpFilterProc = NULL;
__PROC__TerminateFilter pTerminateFilter = NULL;
__PROC__PyISAPISetOptions pPyISAPISetOptions = NULL;
__PROC__WriteEventLogMessage pWriteEventLogMessage = NULL;
extern int init_with_instance(HMODULE, char *);
extern BOOL _LoadPythonDLL(HMODULE);
extern void fini();
extern int run_script(void);
extern void init_memimporter(void);
void SystemError(int error, char *msg)
{
// From pywin32's pyisapi_messages.h:
// #define E_PYISAPI_STARTUP_FAILED ((DWORD)0xC0001100L)
// 0x1100 == E_PYISAPI_STARTUP_FAILED
// We use this even when we can't use pyisapi to log the record.
const DWORD evt_id = (DWORD)0xC0001100L;
char Buffer[1024];
int n;
HANDLE hEventSource;
if (error) {
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&lpMsgBuf,
0,
NULL
);
strncpy(Buffer, lpMsgBuf, sizeof(Buffer));
LocalFree(lpMsgBuf);
} else
Buffer[0] = '\0';
// Can't display messages in a service. Write to the event log.
// Later pywin32 versions export a function for writing to the
// event log - useful as it also has the message resources necessary
// to get reasonable looking messages.
// If that exists, use it - otherwise write an "ugly" message.
if (pWriteEventLogMessage) {
// Can use the one exported!
TCHAR * inserts[] = {msg, Buffer};
pWriteEventLogMessage(EVENTLOG_ERROR_TYPE, evt_id,
2, inserts);
return; // all done!
}
n = lstrlen(Buffer);
_snprintf(Buffer+n, sizeof(Buffer)-n, msg);
// We have no message resources, so the message will be somewhat
// ugly - but so long as the info is there, that's ok.
hEventSource = RegisterEventSource(NULL, "ISAPI Filter or Extension");
if (hEventSource) {
// a big sucky - windows error already in string - so we
// send "see above"
TCHAR * inserts[] = {Buffer, "see above"};
ReportEvent(hEventSource, // handle of event source
EVENTLOG_ERROR_TYPE, // event type
0, // event category
evt_id, // event ID
NULL, // current user's SID
2, // strings in lpszStrings
0, // no bytes of raw data
inserts, // array of error strings
NULL); // no raw data
DeregisterEventSource(hEventSource);
}
}
BOOL check_init()
{
BOOL ok = TRUE; // if already initialized all is good.
if (!have_init) {
EnterCriticalSection(&csInit);
// Check the flag again - another thread may have beat us to it!
if (!have_init) {
PyGILState_STATE restore_state = PyGILState_UNLOCKED;
PyObject *frozen;
char dll_path[1024];
char *slash;
ok = FALSE; // must be reset after good init.
// We must ensure Python is loaded, and therefore the
// function pointers are non-NULL, before we can check
// if Python is initialized! Further, as our
// ISAPI dll depends on python, we must load Python
// before loading our module.
if (!_LoadPythonDLL(gInstance))
goto done;
// Find and load the pyisapi DLL.
GetModuleFileName(gInstance, dll_path, sizeof(dll_path)/sizeof(dll_path[0]));
slash = strrchr(dll_path, '\\');
if (slash) {
// insert an underscore.
char *pos_move = dll_path + strlen(dll_path);
while (pos_move > slash) {
*(pos_move+1) = *pos_move;
pos_move --;
}
*(slash+1) = '_';
hmodPyISAPI = LoadLibrary(dll_path);
if (hmodPyISAPI) {
pGetExtensionVersion = (__PROC__GetExtensionVersion)GetProcAddress(hmodPyISAPI, "GetExtensionVersion");
pHttpExtensionProc = (__PROC__HttpExtensionProc)GetProcAddress(hmodPyISAPI, "HttpExtensionProc");
pTerminateExtension = (__PROC__TerminateExtension)GetProcAddress(hmodPyISAPI, "TerminateExtension");
pGetFilterVersion = (__PROC__GetFilterVersion)GetProcAddress(hmodPyISAPI, "GetFilterVersion");
pHttpFilterProc = (__PROC__HttpFilterProc)GetProcAddress(hmodPyISAPI, "HttpFilterProc");
pTerminateFilter = (__PROC__TerminateFilter)GetProcAddress(hmodPyISAPI, "TerminateFilter");
pPyISAPISetOptions = (__PROC__PyISAPISetOptions)GetProcAddress(hmodPyISAPI, "PyISAPISetOptions");
pWriteEventLogMessage = (__PROC__WriteEventLogMessage)GetProcAddress(hmodPyISAPI, "WriteEventLogMessage");
} else {
SystemError(GetLastError(), "Failed to load the extension DLL");
}
} else {
SystemError(GetLastError(), "Failed to locate my own DLL");
}
if (Py_IsInitialized && Py_IsInitialized())
restore_state = PyGILState_Ensure();
// a little DLL magic. Set sys.frozen='dll'
if (init_with_instance(gInstance, "dll") != 0) {
PyGILState_Release(restore_state);
goto done;
}
init_memimporter();
frozen = PyLong_FromVoidPtr(gInstance);
if (frozen) {
PySys_SetObject("frozendllhandle", frozen);
Py_DECREF(frozen);
}
// Now run the generic script - this always returns in a DLL.
run_script();
// Let the ISAPI extension know about the frozen environment.
// (the ISAPI boot code currently has no way of talking directly
// to the pyISAPI dll, so we fetch it directly from the py2exe
// 'variable' which is already in module '__main__'.
if (pPyISAPISetOptions) {
PyObject *main = PyImport_ImportModule("__main__");
if (main) {
PyObject *name = PyObject_GetAttrString(main, "isapi_module_name");
char *str;
if (name && (str = PyString_AsString(name)))
(*pPyISAPISetOptions)(str, TRUE);
else
PyErr_Clear(); // In case PyString_AsString fails
Py_XDECREF(name);
}
Py_DECREF(main);
}
have_init = TRUE;
PyGILState_Release(restore_state);
ok = TRUE;
}
done:
LeaveCriticalSection(&csInit);
}
return ok;
}
// *****************************************************************
// All the public entry points needed for COM, Windows, and anyone
// else who wants their piece of the action.
// *****************************************************************
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
if ( dwReason == DLL_PROCESS_ATTACH) {
gInstance = hInstance;
InitializeCriticalSection(&csInit);
}
else if ( dwReason == DLL_PROCESS_DETACH ) {
gInstance = 0;
if (hmodPyISAPI)
FreeLibrary(hmodPyISAPI);
DeleteCriticalSection(&csInit);
}
return TRUE;
}
BOOL WINAPI GetExtensionVersion(HSE_VERSION_INFO *pVer)
{
if (!check_init() || !pGetExtensionVersion) return FALSE;
return (*pGetExtensionVersion)(pVer);
}
DWORD WINAPI HttpExtensionProc(EXTENSION_CONTROL_BLOCK *pECB)
{
if (!check_init() || !pHttpExtensionProc) return HSE_STATUS_ERROR;
return (*pHttpExtensionProc)(pECB);
}
BOOL WINAPI TerminateExtension(DWORD dwFlags)
{
if (!check_init() || !pTerminateExtension) return FALSE;
return (*pTerminateExtension)(dwFlags);
}
BOOL WINAPI GetFilterVersion(HTTP_FILTER_VERSION *pVer)
{
if (!check_init() || !pGetFilterVersion) return FALSE;
return (*pGetFilterVersion)(pVer);
}
DWORD WINAPI HttpFilterProc(HTTP_FILTER_CONTEXT *phfc, DWORD NotificationType, VOID *pvData)
{
if (!check_init() || !pHttpFilterProc) return SF_STATUS_REQ_NEXT_NOTIFICATION;
return (*pHttpFilterProc)(phfc, NotificationType, pvData);
}
BOOL WINAPI TerminateFilter(DWORD status)
{
if (!check_init() || !pTerminateFilter) return FALSE;
return (*pTerminateFilter)(status);
}