L
lstanczyk
I am using visual studio 2003 and trying to write an ISAPI filter that
onReadRawData will modify html by adding some javascript before </head
tag.
Below is my code what is that i do wrong? Appreciate your answers.
DWORD CpURLRedirectFilter::OnSendRawData(CHttpFilterContext*
pCtxt,PHTTP_FILTER_RAW_DATA pRawData)
{
TCHAR lpszURL[255];
char lpszBuffer[10000];
char lpszBuffer2[10000];
DWORD dwURLSize;
CHAR* pszBuffer;
CHAR* pszBuffer2;
size_t st404Length;
CString bufor;
st404Length=_tcslen("HTTP/1.1 404");
dwURLSize=255;
if(!strstr((LPTSTR)pRawData->pvInData,"HTTP/1.1 404") & !
strstr((LPTSTR)pRawData->pvInData,"HTTP/1.1 400") &
!strstr((LPTSTR)pRawData->pvInData,"HTTP/1.1 101") & !
strstr((LPTSTR)pRawData->pvInData,"HTTP/1.1 401") &
!(!strstr((LPTSTR)pRawData->pvInData,"<html"))){
if(pCtxt->GetServerVariable("URL",lpszURL,&dwURLSize))
{if (strstr (lpszURL, MAGIC_WORD))
{
pszBuffer=strstr((char *)pRawData->pvInData,"<");
//add javascript
char * pos=strstr(pszBuffer,"</head>");
strncpy(lpszBuffer,pszBuffer, (size_t)(pos-pszBuffer));
//bufor=(CStringA)pszBuffer;
bufor +="<script type=\"text/javascript\">\r\n";
bufor +="function getURLParam(strParamName){\r\n";
bufor +=" var strHref = window.location.href;\r\n";
bufor +=" if ( strHref.indexOf(\"#\") > -1 ){\r\n";
bufor +=" var strQueryString = strHref.substr(strHref.indexOf(\"#
\")).toLowerCase();\r\n";
bufor +=" var aQueryString = strQueryString.split(\"^\");\r\n";
bufor +=" for ( var iParam = 0; iParam < aQueryString.length;
iParam++ ){\r\n";
bufor +=" if ( \r\n";
bufor +="aQueryString[iParam].indexOf(strParamName.toLowerCase() +
\"=\") > -1 ){\r\n";
bufor +=" var aParam = aQueryString[iParam].split(\"=\");\r
\n";
bufor +=" strReturn = aParam[1];\r\n";
bufor +=" break;\r\n";
bufor +=" }}} return unescape(strReturn);}\r\n";
bufor +="function populate(){\r\n";
bufor +="var x=frames[0].document.forms[0].length;\r\n";
bufor +="var tmp_txt='';\r\n";
bufor +="for (var i=0;i<x;i++){\r\n";
bufor +=" if (frames[0].document.forms[0].elements.type!
='submit'){\r\n";
bufor +="
tmp_txt=getURLParam(frames[0].document.forms[0].elements.name);\r
\n";
bufor +=" if (tmp_txt!=''){\r\n";
bufor +=" frames[0].document.forms[0].elements.value=tmp_txt;
\r\n";
bufor +=" }}}}\r\n";
bufor +="</script>\r\n";
int datasize=strlen(pszBuffer)+strlen(bufor);
sprintf(lpszBuffer2,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r
\nContent-Length: %d\r\n\r\n%s%s%s",datasize,lpszBuffer,bufor,pos);
pRawData->pvInData=(char*)(char)lpszBuffer2;
pRawData->cbInData=(DWORD) strlen((char*) pRawData->pvInData);
pRawData->cbInBuffer=(DWORD) strlen(lpszBuffer2);
}
}
}
return SF_STATUS_REQ_NEXT_NOTIFICATION;
}
onReadRawData will modify html by adding some javascript before </head
tag.
Below is my code what is that i do wrong? Appreciate your answers.
DWORD CpURLRedirectFilter::OnSendRawData(CHttpFilterContext*
pCtxt,PHTTP_FILTER_RAW_DATA pRawData)
{
TCHAR lpszURL[255];
char lpszBuffer[10000];
char lpszBuffer2[10000];
DWORD dwURLSize;
CHAR* pszBuffer;
CHAR* pszBuffer2;
size_t st404Length;
CString bufor;
st404Length=_tcslen("HTTP/1.1 404");
dwURLSize=255;
if(!strstr((LPTSTR)pRawData->pvInData,"HTTP/1.1 404") & !
strstr((LPTSTR)pRawData->pvInData,"HTTP/1.1 400") &
!strstr((LPTSTR)pRawData->pvInData,"HTTP/1.1 101") & !
strstr((LPTSTR)pRawData->pvInData,"HTTP/1.1 401") &
!(!strstr((LPTSTR)pRawData->pvInData,"<html"))){
if(pCtxt->GetServerVariable("URL",lpszURL,&dwURLSize))
{if (strstr (lpszURL, MAGIC_WORD))
{
pszBuffer=strstr((char *)pRawData->pvInData,"<");
//add javascript
char * pos=strstr(pszBuffer,"</head>");
strncpy(lpszBuffer,pszBuffer, (size_t)(pos-pszBuffer));
//bufor=(CStringA)pszBuffer;
bufor +="<script type=\"text/javascript\">\r\n";
bufor +="function getURLParam(strParamName){\r\n";
bufor +=" var strHref = window.location.href;\r\n";
bufor +=" if ( strHref.indexOf(\"#\") > -1 ){\r\n";
bufor +=" var strQueryString = strHref.substr(strHref.indexOf(\"#
\")).toLowerCase();\r\n";
bufor +=" var aQueryString = strQueryString.split(\"^\");\r\n";
bufor +=" for ( var iParam = 0; iParam < aQueryString.length;
iParam++ ){\r\n";
bufor +=" if ( \r\n";
bufor +="aQueryString[iParam].indexOf(strParamName.toLowerCase() +
\"=\") > -1 ){\r\n";
bufor +=" var aParam = aQueryString[iParam].split(\"=\");\r
\n";
bufor +=" strReturn = aParam[1];\r\n";
bufor +=" break;\r\n";
bufor +=" }}} return unescape(strReturn);}\r\n";
bufor +="function populate(){\r\n";
bufor +="var x=frames[0].document.forms[0].length;\r\n";
bufor +="var tmp_txt='';\r\n";
bufor +="for (var i=0;i<x;i++){\r\n";
bufor +=" if (frames[0].document.forms[0].elements.type!
='submit'){\r\n";
bufor +="
tmp_txt=getURLParam(frames[0].document.forms[0].elements.name);\r
\n";
bufor +=" if (tmp_txt!=''){\r\n";
bufor +=" frames[0].document.forms[0].elements.value=tmp_txt;
\r\n";
bufor +=" }}}}\r\n";
bufor +="</script>\r\n";
int datasize=strlen(pszBuffer)+strlen(bufor);
sprintf(lpszBuffer2,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r
\nContent-Length: %d\r\n\r\n%s%s%s",datasize,lpszBuffer,bufor,pos);
pRawData->pvInData=(char*)(char)lpszBuffer2;
pRawData->cbInData=(DWORD) strlen((char*) pRawData->pvInData);
pRawData->cbInBuffer=(DWORD) strlen(lpszBuffer2);
}
}
}
return SF_STATUS_REQ_NEXT_NOTIFICATION;
}