ISAPI driving me nuts

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;
}
 
L

lstanczyk

Well acctually I know that the problem is in sprintf... line. - only
datasize and lpszBuffer are printed to lpszBuffer2. bufor and pos are
not.
I can't figure out why.
 
V

Victor Bazarov

lstanczyk said:
Well acctually I know that the problem is in sprintf... line. - only
datasize and lpszBuffer are printed to lpszBuffer2. bufor and pos are
not.
I can't figure out why.

Are you sure the buffer is big enough? It would be better not to use
an array with the hard-coded size but dynamicall allocate it (or use
'std::string' and/or 'std::eek:stringstream').

V
 
L

lstanczyk

Are you sure the buffer is big enough?  It would be better not to use
an array with the hard-coded size but dynamicall allocate it (or use
'std::string' and/or 'std::eek:stringstream').

V

I am sure my buffer is big enough.
size of my pszBuffer is about 1005 bytes, pos is about 2500 bytes.

I did some code rewriting and here's how it looks now:
DWORD CpURLRedirectFilter::OnSendRawData(CHttpFilterContext*
pCtxt,PHTTP_FILTER_RAW_DATA pRawData)
{
TCHAR lpszURL[255];
char wholepage[10000];
DWORD dwURLSize;
CHAR* pszBuffer;
CHAR* pszBuffer2="";
size_t st404Length;

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>");
wholepage[0]='\0';
strncpy(wholepage,pszBuffer, (size_t)(pos-pszBuffer-2));
strcat(wholepage,"<script type=\"text/javascript\">\r\n");
strcat(wholepage,"function getURLParam(strParamName){\r\n");
strcat(wholepage," var strHref = window.location.href;\r\n");
strcat(wholepage," if ( strHref.indexOf(\"#\") > -1 ){\r\n");
strcat(wholepage," var strQueryString =
strHref.substr(strHref.indexOf(\"#\")).toLowerCase();\r\n");
strcat(wholepage," var aQueryString = strQueryString.split(\"^
\");\r\n");
strcat(wholepage," for ( var iParam = 0; iParam <
aQueryString.length; iParam++ ){\r\n");
strcat(wholepage," if ( \r\n");

strcat(wholepage,"aQueryString[iParam].indexOf(strParamName.toLowerCase()
+ \"=\") > -1 ){\r\n");
strcat(wholepage," var aParam =
aQueryString[iParam].split(\"=\");\r\n");
strcat(wholepage," strReturn = aParam[1];\r\n");
strcat(wholepage," break;\r\n");
strcat(wholepage," }}} return unescape(strReturn);}\r\n");
strcat(wholepage,"function populate(){\r\n");
strcat(wholepage,"var x=frames[0].document.forms[0].length;\r\n");
strcat(wholepage,"var tmp_txt='';\r\n");
strcat(wholepage,"for (var i=0;i<x;i++){\r\n");
strcat(wholepage," if
(frames[0].document.forms[0].elements.type!='submit'){\r\n");
strcat(wholepage,"
tmp_txt=getURLParam(frames[0].document.forms[0].elements.name);\r
\n");
strcat(wholepage," if (tmp_txt!=''){\r\n");
strcat(wholepage,"
frames[0].document.forms[0].elements.value=tmp_txt;\r\n");
strcat(wholepage," }}}}\r\n");
strcat(wholepage,"</script>\r\n");
strcat(wholepage,pos);

pRawData->pvInData=(char*)wholepage;
pRawData->cbInData=(DWORD) strlen((char*) pRawData->pvInData);
pRawData->cbInBuffer=(DWORD) strlen(wholepage);
}
}

}

return SF_STATUS_REQ_NEXT_NOTIFICATION;
}
 
L

lstanczyk

Are you sure the buffer is big enough?  It would be better not to use
an array with the hard-coded size but dynamicall allocate it (or use
'std::string' and/or 'std::eek:stringstream').

I am sure my buffer is big enough.
size of my pszBuffer is about 1005 bytes, pos is about 2500 bytes.

I did some code rewriting and here's how it looks now:
DWORD CpURLRedirectFilter::OnSendRawData(CHttpFilterContext*
pCtxt,PHTTP_FILTER_RAW_DATA pRawData)
{
        TCHAR   lpszURL[255];
        char    wholepage[10000];
        DWORD   dwURLSize;
        CHAR*   pszBuffer;
        CHAR*   pszBuffer2="";
        size_t  st404Length;

        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>");
                                wholepage[0]='\0';
                                strncpy(wholepage,pszBuffer, (size_t)(pos-pszBuffer-2));
                                strcat(wholepage,"<script type=\"text/javascript\">\r\n");
                                strcat(wholepage,"function getURLParam(strParamName){\r\n");
                                strcat(wholepage," var strHref = window.location.href;\r\n");
                                strcat(wholepage," if ( strHref.indexOf(\"#\") > -1 ){\r\n");
                                strcat(wholepage," var strQueryString =
strHref.substr(strHref.indexOf(\"#\")).toLowerCase();\r\n");
                                strcat(wholepage,"   var aQueryString = strQueryString.split(\"^
\");\r\n");
                                strcat(wholepage,"    for ( var iParam = 0; iParam <
aQueryString.length; iParam++ ){\r\n");
                                strcat(wholepage,"      if ( \r\n");

strcat(wholepage,"aQueryString[iParam].indexOf(strParamName.toLowerCase()
+ \"=\") > -1 ){\r\n");
                                strcat(wholepage,"        var aParam =
aQueryString[iParam].split(\"=\");\r\n");
                                strcat(wholepage,"        strReturn = aParam[1];\r\n");
                                strcat(wholepage,"        break;\r\n");
                                strcat(wholepage,"      }}}  return unescape(strReturn);}\r\n");
                                strcat(wholepage,"function populate(){\r\n");
                                strcat(wholepage,"var x=frames[0].document.forms[0].length;\r\n");
                                strcat(wholepage,"var tmp_txt='';\r\n");
                                strcat(wholepage,"for (var i=0;i<x;i++){\r\n");
                                strcat(wholepage," if
(frames[0].document.forms[0].elements.type!='submit'){\r\n");
                                strcat(wholepage,"
tmp_txt=getURLParam(frames[0].document.forms[0].elements.name);\r
\n");
                                strcat(wholepage,"         if (tmp_txt!=''){\r\n");
                                strcat(wholepage,"
frames[0].document.forms[0].elements.value=tmp_txt;\r\n");
                                strcat(wholepage,"         }}}}\r\n");
                                strcat(wholepage,"</script>\r\n");
                                strcat(wholepage,pos);

                                pRawData->pvInData=(char*)wholepage;
                                pRawData->cbInData=(DWORD) strlen((char*) pRawData->pvInData);
                                pRawData->cbInBuffer=(DWORD) strlen(wholepage);
                        }
                }

        }

        return SF_STATUS_REQ_NEXT_NOTIFICATION;



}- Hide quoted text -

- Show quoted text -


Ok I figured it out - it was about null termination of the string:
here's code:

pszBuffer=strstr((char *)pRawData->pvInData,"<");
//add javascript
char * pos=strstr(pszBuffer,"</head>");
wholepage[0]='\0';
strncpy(wholepage,pszBuffer, (size_t)(pos-pszBuffer-1));
wholepage[pos-pszBuffer-1]='\0';
strcat(wholepage,"<script type=\"text/javascript\">\r\n");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,969
Members
47,524
Latest member
ecomwebdesign

Latest Threads

Top