/* ====================================================================== Copyright 2002-2003 by Blackdog Software Pty Ltd. All Rights Reserved Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation, and that the name of Blackdog Software not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. BLACKDOG SOFTWARE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL BLACKDOG SOFTWARE BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ====================================================================== */ #include "stdafx.h" #include "Utils.h" extern HINSTANCE g_hInstance; static bool g_bRegisteredEventSource = false; static WCHAR *source_name = L"ISAPI Filter or Extension"; // some py3k-friendly type conversions. const char *PyISAPIString_AsBytes(PyObject *ob, DWORD *psize /* = NULL */) { PyObject *obNew = NULL; #if (PY_VERSION_HEX >= 0x03000000) // py3k - check for unicode object and use utf-8 encoding. if (PyUnicode_Check(ob)) { obNew = ob = PyUnicode_AsUTF8String(ob); if (ob == NULL) return NULL; } #endif // These 'PyString_' calls are all mapped to the bytes API in py3k... if (!PyString_Check(ob)) { PyErr_Format(PyExc_ValueError, "Expected a string object (got %s)", ob->ob_type->tp_name); return NULL; } if (psize) *psize = PyString_Size(ob); const char *result = PyString_AsString(ob); Py_XDECREF(obNew); return result; } // returns the pathname of this module TCHAR *GetModulePath(void) { // directory values TCHAR szFilePath[_MAX_PATH]; TCHAR szDrive[_MAX_DRIVE]; TCHAR szDir[_MAX_DIR]; // find out where the exe lives // NOTE: the long file name does not get returned (don't know why) ::GetModuleFileName(g_hInstance, szFilePath, sizeof(szFilePath)); ::_tsplitpath( szFilePath, szDrive, szDir, NULL, NULL ); int dir_len = _tcslen(szDir); if (dir_len && szDir[dir_len-1] == _T('\\')) szDir[dir_len-1] = _T('\0'); TCHAR *result = (TCHAR *)malloc((_tcslen(szDrive)+_tcslen(szDir)+1)*sizeof(TCHAR)); if (result) { _tcscpy(result, szDrive); _tcscat(result, szDir); } return result; } // Formats a system error code char *FormatSysError(const DWORD nErrNo) { // This should never happen, so we can be a little brutal. char *result = (char *)malloc(1024); if (!result) return NULL; result[0] = '\0'; int nLen =FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, nErrNo, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), result, 1024, NULL); if (nLen > 2) { if ( result[nLen - 1] == '\n') result[nLen - 1] = 0; if (result[nLen - 2] == '\r') result[nLen - 2] = 0; } return result; } // format an error char *HTMLErrorResp(const char *msg) { const char *htmlBody = "