📄 script.cpp
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft shared
// source or premium shared source license agreement under which you licensed
// this source code. If you did not accept the terms of the license agreement,
// you are not authorized to use this source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the SOURCE.RTF on your install media or the root of your tools installation.
// THE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
/*--
Module Name: script.cpp
Abstract: main interface between httpd and asp.dll
--*/
/*
#include "stdafx.h"
#include "Asp.h"
#include "script.h"
#include "dict.h"
#include "strlist.h"
*/
#include "aspmain.h"
const char* rgWkday[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", 0 };
const char* rgMonth[] = { "", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 0 };
const char cszDateOutputFmt[] = "%3s, %02d %3s %04d %02d:%02d:%02d GMT";
const char cszTextHTML[] = "text/html";
const char cszContentTextHTML[] = "Content-Type: text/html";
const char cszKeepAlive[] = "Connection: keep-alive\r\nContent-Length: %d\r\n"; // written directly to buf, include CRLF.
const char cszKeepAliveText[] = "keep-alive\r\nContent-Length: %d"; // called with AddHeader, don't include Connection or CRLF in this string
#define ADDCRLF(szHeader, i) { (szHeader)[(i++)] = '\r'; (szHeader)[(i++)] = '\n'; }
//
//------------- Debug data -----------------------
//
#ifdef UNDER_CE
#ifdef DEBUG
DBGPARAM dpCurSettings = {
TEXT("ASP"), {
TEXT("Error"),TEXT("Init"),TEXT("Script"),TEXT("Server"),
TEXT("Request"),TEXT("Response"),TEXT("RequestDict"),
TEXT("Mem"),TEXT(""),TEXT(""),TEXT(""),
TEXT(""),TEXT(""),TEXT(""),TEXT(""),TEXT("") },
0x0001
};
#endif
#endif
DWORD g_dwTlsSlot;
// #define TEST_PERFORMANCE
#ifdef TEST_PERFORMANCE
// NOTE: this is NOT thread safe, if TEST_PERFORMANCE is defined (never defined by default) make
// sure only one request comes at a time. For private dev tests only.
LARGE_INTEGER g_rgliCount[100];
int g_dwPerfCounter;
#define QUERY_COUNT() QueryPerformanceCounter(&g_rgliCount[g_dwPerfCounter++]);
#else
#define QUERY_COUNT()
#endif
// This function is only called by httpd when it's freeing the ISAPI cache
// for ASP.
void TerminateASP()
{
DEBUGMSG(ZONE_INIT,(L"ASP: Calling CoFreeUnusedLibraries()\r\n"));
CoInitializeEx(NULL,COINIT_MULTITHREADED);
CoFreeUnusedLibraries();
CoUninitialize();
}
// HTTPD calls this function to handle script processing.
DWORD ExecuteASP(PASP_CONTROL_BLOCK pASP)
{
DEBUG_CODE_INIT;
DWORD ret = HSE_STATUS_ERROR;
CASPState *pASPState = NULL;
QUERY_COUNT();
DEBUGMSG(ZONE_INIT,(L"ASP: ExecuteASP successfully called, ASP handler beginning\r\n"));
DEBUGCHK(pASP != NULL && pASP->wszFileName != NULL);
DEBUGCHK(pASP->cbSize == sizeof(ASP_CONTROL_BLOCK));
pASPState = new CASPState(pASP);
if (NULL == pASPState)
myleave(1);
// stores context so callbacks in this thread know where they're from
if ( ! TlsSetValue(g_dwTlsSlot,(LPVOID) pASPState))
myleave(2);
__try
{
__try
{
CoInitializeEx(NULL,COINIT_MULTITHREADED);
ret = pASPState->Main();
}
__finally
{
TlsSetValue(g_dwTlsSlot,0);
delete pASPState;
CoFreeUnusedLibraries(); // 1st call to this function
CoUninitialize();
}
}
__except(1)
{
RETAILMSG(1,(L"ASP: ASP Script caused an exception during exection\r\n"));
}
done:
DEBUGMSG_ERR(ZONE_ERROR,(L"ASP: ExecuteASP failed, err = %d\r\n",err));
DEBUGMSG(ZONE_INIT,(L"ASP: ExecuteASP completed processing, returning to httpd\r\n"));
return ret;
}
CASPState::CASPState(PASP_CONTROL_BLOCK pACB)
{
ZEROMEM(this);
m_pACB = pACB;
m_scriptLang = pACB->scriptLang;
m_aspErr = IDS_E_DEFAULT;
m_lcid = pACB->lcid;
m_lCodePage = pACB->lCodePage;
m_iExpires = 0;
m_pACB->ServerSupportFunction(m_pACB->ConnID,HSE_REQ_IS_KEEP_CONN,&m_fServerUsingKeepAlives,0,0);
}
CASPState::~CASPState()
{
DEBUGMSG(ZONE_SCRIPT,(L"ASP: In CASPState::~CASPState\r\n"));
int i = 0;
MyFree(m_pszFileData);
MyFree(m_pszScriptData);
if (m_bstrResponseStatus)
SysFreeString(m_bstrResponseStatus);
if (m_bstrScriptData)
SysFreeString(m_bstrScriptData);
if (m_bstrCharSet)
SysFreeString(m_bstrCharSet);
if (m_bstrContentType)
SysFreeString(m_bstrContentType);
if (m_pIncludeInfo)
{
for (; m_pIncludeInfo[i].m_pszFileName != NULL && i < MAX_INCLUDE_FILES; i++)
MyFree(m_pIncludeInfo[i].m_pszFileName);
MyFree(m_pIncludeInfo);
}
// Delete the objects used through the script
// Later --> move empty string to global?
if (m_pEmptyString)
m_pEmptyString->Release();
if (m_pResponseCookie)
m_pResponseCookie->Release();
// Note -- we don't delete the Server, Request, or Response objects.
// They are objects in the scripting engine's namespace, and they are deleted
// automatically when we closed the script site.
//if (m_pServer)
// m_pServer->Release();
//if (m_pRequest)
// m_pRequest->Release();
// if (m_pResponse)
// m_pResponse->Release();
if (m_piActiveScriptParse)
m_piActiveScriptParse->Release();
if (m_piActiveScript)
m_piActiveScript->Release();
#ifdef TEST_PERFORMANCE
LARGE_INTEGER liFrequency;
QUERY_COUNT();
if ( !QueryPerformanceFrequency(&liFrequency))
return; // no CPU Support for this
for (i = 0; i < g_dwPerfCounter; i++)
RETAILMSG(1,(L"ASP: Item %i - HighPart = %d, LowPart = 0x%08x\r\n",i,g_rgliCount[i].HighPart,g_rgliCount[i].LowPart));
#endif
}
DWORD CASPState::Main()
{
DEBUG_CODE_INIT;
DWORD ret = HSE_STATUS_SUCCESS; // we only return HSE_STATUS_ERROR if there's an Access Violation in ASP
DWORD dwFileSize = 0;
BOOL fOwnCritSect = FALSE;
if (! svsutil_OpenAndReadFile(m_pACB->wszFileName,(DWORD *) &m_cbFileData, &m_pszFileData))
myleave(11);
// Read include files and Preproc dirictives if extended parse component included
if (c_fUseExtendedParse)
{
if (! ParseIncludes() )
myleave(12);
if (! ParseProcDirectives() )
myleave(13);
}
// This is a plain old text file, no ASP commands
// Send it to client without further processing
// For ASP pages that have no script commands, this speeds up processing immensly.
if (NULL == MyStrStr(m_pszFileData,BEGIN_COMMAND))
{
CHAR szBuf[256];
strcpy(szBuf,cszContentTextHTML);
int i = SVSUTIL_CONSTSTRLEN(cszContentTextHTML);
ADDCRLF(szBuf,i);
if (m_fServerUsingKeepAlives)
{
i += sprintf(szBuf+i,cszKeepAlive,m_cbFileData);
m_fKeepAlive = TRUE;
}
ADDCRLF(szBuf,i);
szBuf[i] = 0;
m_fSentHeaders = TRUE;
m_pACB->ServerSupportFunction(m_pACB->ConnID,HSE_REQ_SEND_RESPONSE_HEADER, 0, 0, (DWORD*) szBuf);
m_pACB->WriteClient(m_pACB->ConnID,(LPVOID) m_pszFileData,(LPDWORD) &m_cbFileData, 0);
goto done; // we're done processing
}
if (! ConvertToScript())
myleave(14);
EnterCriticalSection(&g_scriptCS);
fOwnCritSect = TRUE;
if (c_fUseCollections)
{
m_pEmptyString = CreateCRequestStrList(this,NULL,EMPTY_TYPE);
}
// ATL is not thread safe, so protect access to it (which is invoked
// via the IActiveScript engine) with a crit section);
if (! InitScript())
myleave(15);
if (! RunScript())
myleave(16);
// If we haven't sent the headers yet, we can do a keep alive connection
if (FALSE == m_fSentHeaders)
{
m_fKeepAlive = m_fServerUsingKeepAlives;
SendHeaders();
}
m_pACB->Flush(m_pACB->ConnID);
done:
if (fOwnCritSect)
LeaveCriticalSection(&g_scriptCS);
DEBUGMSG_ERR(ZONE_ERROR,(L"ASP: CASPState::Main failed, err = %d\r\n",err));
if (m_fKeepAlive && m_fServerUsingKeepAlives)
ret = HSE_STATUS_SUCCESS_AND_KEEP_CONN;
return ret;
}
// Inits IActiveScript related libs/vars
BOOL CASPState::InitScript()
{
DEBUG_CODE_INIT;
HRESULT hr;
CLSID clsid;
CHAR szErrInfo[128];
m_aspErr = IDS_E_SCRIPT_INIT;
wsprintf(m_wszModName, L"%s-0x%08x", (m_scriptLang == VBSCRIPT) ? L"VBS" : L"JS ",
GetCurrentThreadId());
hr = CLSIDFromProgID((m_scriptLang == VBSCRIPT) ? L"VBScript" : L"JScript", &clsid);
if (FAILED(hr))
myleave(40);
hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
IID_IActiveScript, (void **)&m_piActiveScript);
if (FAILED(hr))
myleave(41);
hr = m_piActiveScript->SetScriptSite((IActiveScriptSite*) g_pScriptSite);
if (FAILED(hr))
myleave(42);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -