📄 script.cpp
字号:
hr = m_piActiveScript->QueryInterface(IID_IActiveScriptParse, (void **)&m_piActiveScriptParse);
if (FAILED(hr))
myleave(43);
hr = m_piActiveScriptParse->InitNew();
if (FAILED(hr))
myleave(44);
hr = m_piActiveScript->AddNamedItem(m_wszModName, SCRIPTITEM_CODEONLY);
if (FAILED(hr))
myleave(45);
// Make the Response, Request, and Server ASP objects visible
hr = m_piActiveScript->AddNamedItem(L"Response", SCRIPTITEM_ISVISIBLE);
if (FAILED(hr))
myleave(46);
// Make the Response, Request, and Server ASP objects visible
hr = m_piActiveScript->AddNamedItem(L"Request", SCRIPTITEM_ISVISIBLE);
if (FAILED(hr))
myleave(47);
// Make the Response, Request, and Server ASP objects visible
hr = m_piActiveScript->AddNamedItem(L"Server", SCRIPTITEM_ISVISIBLE);
if (FAILED(hr))
myleave(48);
done:
DEBUGMSG_ERR(ZONE_ERROR,(L"ASP: InitScript failed, err = %d, hr = 0x%X, GLE = 0x%X\r\n", err, hr, GetLastError() ));
if (FAILED(hr))
{
WCHAR wszFormat[256];
CHAR szFormat[256];
MyLoadStringW2A(m_pACB->hInst,IDS_ERROR_CODE,wszFormat,szFormat);
sprintf(szErrInfo,szFormat,"",hr);
ServerError(szErrInfo);
}
else
{
m_aspErr = IDS_E_DEFAULT; // set error back to default
}
return SUCCEEDED(hr);
}
// After library initilization, this runs
BOOL CASPState::RunScript()
{
DEBUG_CODE_INIT;
HRESULT hr = S_OK;
EXCEPINFO excep;
CHAR szErrInfo[128];
szErrInfo[0] = '\0';
hr = m_piActiveScriptParse->ParseScriptText(
m_bstrScriptData, // script text
m_wszModName, // module name
NULL, // pstrItemName
NULL, // punkContext
0, // End script delemiter
1, // starting line #
0,
NULL, // pvar Result
&excep // error info if there's a failure, must not = NULL
);
// CScriptSite::OnScriptError provides detailed run-time error info, so
// don't bother reporting it again if it was called.
if (SCRIPT_E_REPORTED == hr)
hr = S_OK;
// Is we didn't report an error already, give them the error code and
// a brief description of the problem.
if (FAILED(hr))
{
CHAR szFormat[256];
WCHAR wszFormat[256];
MyLoadStringW2A(m_pACB->hInst,IDS_PARSE_ERROR_FROM_IACTIVE,wszFormat,szFormat);
StringCchPrintfA(szErrInfo,SVSUTIL_ARRLEN(szErrInfo),szFormat, hr);
myleave(51);
}
hr = m_piActiveScript->SetScriptState(SCRIPTSTATE_STARTED);
if (FAILED(hr))
myleave(49);
done:
m_piActiveScript->Close();
if (FAILED(hr))
ServerError(szErrInfo);
DEBUGMSG_ERR(ZONE_ERROR,(L"ASP: RunScript failed, hr = %X, GLE = %X",hr, GetLastError() ));
return SUCCEEDED(hr);
}
// This (and ServerError) are the only functions that are allowed to directly
// access the SetHeaders. Changes ASP script could've made that we handle
// here and not in httpd are:
// * Custom response status
// * Cookies
// * Expires field
BOOL CASPState::SendHeaders(BOOL fSendResponseHeaders)
{
BOOL ret = FALSE;
DEBUG_CODE_INIT;
PSTR pszResponseStatus = NULL;
SYSTEMTIME st;
CHAR szExtraHeaders[4096];
int i = 0;
// The user has set a custom m_bstrResponseStatus string.
// Codepage note: Convert to ANSI string since it's an http header, like IIS.
if (m_bstrResponseStatus)
{
pszResponseStatus = MySzDupWtoA(m_bstrResponseStatus);
if (NULL == pszResponseStatus)
{
m_aspErr = IDS_E_NOMEM;
myleave(300);
}
}
// Expires header. We don't convert the value until now.
if (m_iExpires != 0)
{
FileTimeToSystemTime( (FILETIME*) &m_iExpires, &st);
sprintf(szExtraHeaders, cszDateOutputFmt, rgWkday[st.wDayOfWeek], st.wDay,
rgMonth[st.wMonth], st.wYear, st.wHour, st.wMinute, st.wSecond);
if (! m_pACB->AddHeader(m_pACB->ConnID,"Expires",szExtraHeaders))
{
m_aspErr = IDS_E_NOMEM; // only possible reason AddHeader() can fail.
myleave(304);
}
}
if (m_bstrContentType)
{
i = MyW2A(m_bstrContentType,szExtraHeaders,sizeof(szExtraHeaders)) -1;
}
else
{
strcpy(szExtraHeaders,cszTextHTML);
i = sizeof(cszTextHTML) - 1;
}
// char set follows immediatly content-type in same HTTP line, don't add CRLF here.
if (m_bstrCharSet)
{
szExtraHeaders[i++] = ';';
szExtraHeaders[i++] = ' ';
strcpy(szExtraHeaders + i,"Charset=");
i += sizeof("Charset=") - 1;
i += MyW2A(m_bstrCharSet,szExtraHeaders + i,sizeof(szExtraHeaders) - i) - 1;
}
if (! m_pACB->AddHeader(m_pACB->ConnID,"Content-Type",szExtraHeaders))
{
m_aspErr = IDS_E_NOMEM; // only possible reason AddHeader() can fail.
myleave(306);
}
if (m_fKeepAlive)
{
sprintf(szExtraHeaders,cszKeepAliveText,m_cbBody);
if (! m_pACB->AddHeader(m_pACB->ConnID,"Connection",szExtraHeaders))
{
m_aspErr = IDS_E_NOMEM; // only possible reason AddHeader() can fail.
myleave(307);
}
}
// WriteCookies takes care of setting error code.
if (m_pResponseCookie && FALSE == m_pResponseCookie->WriteCookies())
myleave(302);
// fSendResponseHeader = FALSE if calling function has additional work to perform before sending headers.
if (fSendResponseHeaders)
{
if (FALSE == m_pACB->ServerSupportFunction(m_pACB->ConnID,
HSE_REQ_SEND_RESPONSE_HEADER,(LPVOID) pszResponseStatus,NULL,NULL))
{
m_aspErr = IDS_E_HTTPD;
myleave(303);
}
}
ret = TRUE;
done:
DEBUGMSG_ERR(ZONE_ERROR,(L"ASP: CASPState::SendHeaders died, err = %d\r\n",err));
MyFree(pszResponseStatus);
m_fSentHeaders = TRUE;
return ret;
}
// NOTE: IE5 does not display the body information on a status code "500 - Internal
// Error" but instead a local page describing server errs in general, so we
// send a 200 back no matter what (like IIS ASP).
const char cszInternalErrorHeader[] = "200 - OK";
// To create the body of error message, we nicely format it,
// then we put data in pszBody if any, then we look up code based on
// m_aspErr's status
void CASPState::ServerError(PSTR pszBody, int iErrorLine)
{
CHAR szErrorString[512];
CHAR szBodyFormatted[4000];
CHAR *szTrav = szBodyFormatted;
size_t ccBufferRemaining = sizeof(szBodyFormatted);
WCHAR wszErrString[512];
DWORD cbBodyFormatted;
// Should always have a valid error code set. If not, then reset to unknown err.
DEBUGCHK(m_aspErr <= MAX_ERROR_CODE);
if (m_aspErr >= MAX_ERROR_CODE)
m_aspErr = IDS_E_UNKNOWN;
if (! m_pACB->fASPVerboseErrorMessages)
{
// web server has requesteted that only vague errors be return on script failure.
pszBody = NULL;
iErrorLine = 0;
m_aspErr = IDS_E_PARSER;
}
MyLoadStringW2A(m_pACB->hInst,m_aspErr,wszErrString,szErrorString);
StringCchPrintfExA(szTrav, ccBufferRemaining,&szTrav,&ccBufferRemaining,0,
"<font face=\"Arial\" size=2><p>%s\r\n",szErrorString);
if (pszBody)
{
StringCchPrintfExA(szTrav, ccBufferRemaining,&szTrav,&ccBufferRemaining,0,
"<p>%s\r\n",pszBody);
}
// if iErrorLine = -1 or 0, then we don't have an error line number for it.
// This happens in cases where server dies on mem failure or initilization and
// a few other strange cases.
if (iErrorLine > 0)
{
PSTR pszIncFileName = NULL;
int iFileErrorLine = 0;
if ( c_fUseExtendedParse )
{
GetIncErrorInfo(iErrorLine, &iFileErrorLine, &pszIncFileName);
}
else
iFileErrorLine = iErrorLine;
// GetIncErrorInfo sets pszIncFile name =NULL if error occured in main asp page
if (NULL == pszIncFileName)
pszIncFileName = m_pACB->pszVirtualFileName;
WCHAR wszErrFormat[256];
CHAR szErrFormat[256];
if (0 != LoadString(m_pACB->hInst,IDS_LINE_FILE_ERROR,wszErrFormat,ARRAYSIZEOF(wszErrFormat)))
{
MyW2A(wszErrFormat,szErrFormat,sizeof(szErrFormat));
StringCchPrintfExA(szTrav, ccBufferRemaining,&szTrav,&ccBufferRemaining,0,
szErrFormat,pszIncFileName, iFileErrorLine);
}
}
StringCchPrintfExA(szTrav, ccBufferRemaining,&szTrav,&ccBufferRemaining,0,"</font>\r\n");
cbBodyFormatted = strlen(szBodyFormatted);
// If we haven't send the headers yet, do so now. Don't send the custom
// headers / cookies / etc in error case.
// However, we can do a keep-alive in this case.
if (m_fSentHeaders == FALSE)
{
CHAR szExtraHeaders[1024];
CHAR *szHeaderTrav = szExtraHeaders;
size_t ccHeaderRemaining = sizeof(szExtraHeaders);
StringCchPrintfExA(szHeaderTrav,ccHeaderRemaining,&szHeaderTrav,&ccHeaderRemaining,0,
"%s\r\n",cszContentTextHTML);
if (m_fServerUsingKeepAlives)
{
StringCchPrintfExA(szHeaderTrav,ccHeaderRemaining,&szHeaderTrav,&ccHeaderRemaining,0,
cszKeepAlive,m_cbBody+cbBodyFormatted);
// This could be called from IActiveLibs, in which case we have no way
// to tell CASPState::Main() that we're keeping the session alive.
m_fKeepAlive = TRUE;
}
StringCchPrintfExA(szHeaderTrav,ccHeaderRemaining,&szHeaderTrav,&ccHeaderRemaining,0,
"\r\n");
m_pACB->ServerSupportFunction(m_pACB->ConnID,HSE_REQ_SEND_RESPONSE_HEADER,
(void *) cszInternalErrorHeader,NULL,(LPDWORD) szExtraHeaders);
m_fSentHeaders = TRUE;
}
// dump out any buffer before error message, like IIS.
m_pACB->Flush(m_pACB->ConnID);
m_pACB->WriteClient(m_pACB->ConnID, (LPVOID) szBodyFormatted, &cbBodyFormatted, 0);
m_pACB->Flush(m_pACB->ConnID);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -