home *** CD-ROM | disk | FTP | other *** search
- // LST15_01.CPP - Implementation file for your Internet Server
- // lst15_01 Filter
-
- #include <afx.h>
- #include <afxwin.h>
- #include <afxisapi.h>
- #include "resource.h"
- #include "lst17_01.h"
-
-
- ///////////////////////////////////////////////////////////////////////
- // The one and only CLst15_01Filter object
-
- CLst15_01Filter theFilter;
- CWinApp BugFix;
-
- ///////////////////////////////////////////////////////////////////////
- // CLst15_01Filter implementation
-
- CLst15_01Filter::CLst15_01Filter()
- {
- m_lpcsFlag=new CRITICAL_SECTION;
- InitializeCriticalSection(m_lpcsFlag);
-
- // Hard Coded Datasource, login, and password
- m_csDSN="TrafficLog";
- m_csUser="sa";
- m_csPassword="";
-
- Connect();
- }
-
- CLst15_01Filter::~CLst15_01Filter()
- {
- Disconnect();
-
- DeleteCriticalSection(m_lpcsFlag);
- delete m_lpcsFlag;
- }
-
- BOOL CLst15_01Filter::GetFilterVersion(PHTTP_FILTER_VERSION pVer)
- {
- // Call default implementation for initialization
- CHttpFilter::GetFilterVersion(pVer);
-
- // Clear the flags set by base class
- pVer->dwFlags &= ~SF_NOTIFY_ORDER_MASK;
-
- // Set the flags we are interested in
- pVer->dwFlags |= SF_NOTIFY_ORDER_LOW | SF_NOTIFY_SECURE_PORT | SF_NOTIFY_NONSECURE_PORT
- | SF_NOTIFY_SEND_RAW_DATA | SF_NOTIFY_END_OF_NET_SESSION;
-
- // Load description string
- TCHAR sz[SF_MAX_FILTER_DESC_LEN+1];
- ISAPIVERIFY(::LoadString(AfxGetResourceHandle(),
- IDS_FILTER, sz, SF_MAX_FILTER_DESC_LEN));
- _tcscpy(pVer->lpszFilterDesc, sz);
- return TRUE;
- }
-
- DWORD CLst15_01Filter::OnSendRawData(CHttpFilterContext* pCtxt,
- PHTTP_FILTER_RAW_DATA pRawData)
- {
- DWORD cbInData=pRawData->cbInData;
- LPTSTR lpszURL;
-
- if (lpszURL=GetServerVariable(pCtxt,"URL"))
- {
- Log(lpszURL,cbInData);
- delete lpszURL;
- }
-
- // TODO: React to this notification accordingly and
- // return the appropriate status code
- return SF_STATUS_REQ_NEXT_NOTIFICATION;
- }
-
- DWORD CLst15_01Filter::OnEndOfNetSession(CHttpFilterContext* pCtxt)
- {
- // TODO: React to this notification accordingly and
- // return the appropriate status code
- return SF_STATUS_REQ_NEXT_NOTIFICATION;
- }
-
- ///////////////////////////////////////////////////////////////////////
- // If your extension will not use MFC, you'll need this code to make
- // sure the extension objects can find the resource handle for the
- // module. If you convert your extension to not be dependent on MFC,
- // remove the comments arounn the following AfxGetResourceHandle()
- // and DllMain() functions, as well as the g_hInstance global.
-
- /****
-
- static HINSTANCE g_hInstance;
-
- HINSTANCE AFXISAPI AfxGetResourceHandle()
- {
- return g_hInstance;
- }
-
- BOOL WINAPI DllMain(HINSTANCE hInst, ULONG ulReason,
- LPVOID lpReserved)
- {
- if (ulReason == DLL_PROCESS_ATTACH)
- {
- g_hInstance = hInst;
- }
-
- return TRUE;
- }
-
- ****/
-
- void CLst15_01Filter::Connect()
- {
- RETCODE rc;
-
- rc=SQLAllocEnv(&m_henv);
- if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
- ODBCError(SQL_NULL_HSTMT);
-
- rc=SQLAllocConnect(m_henv, &m_hdbc);
- if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
- ODBCError(SQL_NULL_HSTMT);
-
- rc=SQLConnect(m_hdbc, (UCHAR FAR *)(LPCTSTR) m_csDSN, SQL_NTS, (UCHAR FAR *)(LPCTSTR) m_csUser , m_csUser.GetLength(), (UCHAR FAR *)(LPCTSTR) m_csPassword, m_csPassword.GetLength());
- if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
- ODBCError(SQL_NULL_HSTMT);
- };
-
- void CLst15_01Filter::Disconnect()
- {
- SQLDisconnect(m_hdbc);
- SQLFreeConnect(m_hdbc);
- SQLFreeEnv(m_henv);
- }
-
- void CLst15_01Filter::ODBCError (HSTMT hstmt)
- {
- UCHAR FAR szSqlState[5];
- UCHAR FAR szErrorMsg[80];
- SWORD cbErrorMsgMax=80;
- SWORD FAR *pcbErrorMsg= new SWORD;
- SDWORD FAR *pfNativeError= new SDWORD;
- RETCODE rc;
-
- rc=SQLError(m_henv, m_hdbc, hstmt, szSqlState, pfNativeError, szErrorMsg, cbErrorMsgMax, pcbErrorMsg);
-
- if (!_tcscmp((char*)szSqlState,"08S01"))
- {
- // Bad Connection Let's try to recover!
- ISAPITRACE("Bad Connection\n");
- EnterCriticalSection(m_lpcsFlag);
- Disconnect();
- Connect();
- LeaveCriticalSection(m_lpcsFlag);
- }
-
- ISAPITRACE1("SQL Error: %s\n",szErrorMsg);
-
- delete pfNativeError;
- delete pcbErrorMsg;
- };
-
- HSTMT CLst15_01Filter::GetStatement()
- {
- // The only reason that we are entering a critical section here
- // is because the connection could be bad and we are trying to reconnect
- // otherwise this isn't needed since ODBC is thread proof.
- EnterCriticalSection(m_lpcsFlag);
- HSTMT hstmt;
- RETCODE rc;
-
- rc=SQLAllocStmt(m_hdbc, &hstmt);
- if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
- {
- // Bad Connection Let's try to recover!
- // The CriticalSection Prevents other threads
- // From trying to get a statement with a bad connection
- ISAPITRACE("Bad Connection\n");
- EnterCriticalSection(m_lpcsFlag);
- Disconnect();
- Connect();
- LeaveCriticalSection(m_lpcsFlag);
-
- //Try Again
- rc=SQLAllocStmt(m_hdbc, &hstmt);
- if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
- {
- // Only try to reconnect once
- // Otherwise it is beyond our control
- return(NULL);
- }
- }
-
- LeaveCriticalSection(m_lpcsFlag);
-
- return(hstmt);
- }
-
- void CLst15_01Filter::FreeStatement(HSTMT hstmt)
- {
- SQLFreeStmt(hstmt, SQL_DROP);
- };
-
- // The Caller must delete the memory, unless error in which case returns NULL
- LPTSTR CLst15_01Filter::GetServerVariable(CHttpFilterContext* pCtxt, LPCTSTR pszVariableName)
- {
- LPVOID lpvBuffer=NULL;
- DWORD dwSize=0;
-
- pCtxt->GetServerVariable((LPTSTR)pszVariableName,NULL,(LPDWORD)&dwSize);
-
- // Check to see if variable exists
- if(dwSize==0)
- return(NULL);
-
- lpvBuffer=(LPVOID)new TCHAR[dwSize+1];
-
- if (!(pCtxt->GetServerVariable((LPTSTR)pszVariableName,lpvBuffer,(LPDWORD)&dwSize)))
- {
- delete lpvBuffer;
- return(NULL);
- }
-
- if(dwSize==0)
- {
- delete lpvBuffer;
- return(NULL);
- }
-
- return((LPTSTR)lpvBuffer);
- };
-
- void CLst15_01Filter::Log(LPCTSTR lpszURL, DWORD dwSize)
- {
- HSTMT hstmt;
- RETCODE rc;
- CString SQLScript;
-
- if((hstmt=GetStatement())==NULL)
- {
- return;
- }
-
- SQLScript.Format("INSERT TrafficLog (TrafficLog_Size,TrafficLog_URL) VALUES (%d,'%s')",dwSize,lpszURL);
-
- rc=SQLExecDirect(hstmt, (UCHAR FAR *) (LPCTSTR) SQLScript, SQL_NTS);
- if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
- {
- ODBCError(hstmt);
- goto End;
- }
-
- End:
- FreeStatement(hstmt);
- return;
- }