EricLaw
unread,Mar 31, 2009, 7:37:07 PM3/31/09Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Fiddler
Someone asked how to inject HTML tags into the HEAD section of a
document using a Fiddler extension. Here's a code sample:
private bool insertIntoHTMLHead(Session oSession, string
sToInsert)
{
if ((null == oSession.responseBodyBytes) || (null ==
oSession.oResponse) || (null == oSession.oResponse.headers)){
return false;
}
// If HTML
if (oSession.oResponse.headers.ExistsAndContains("Content-
Type", "html"))
{
// Remove any content-encoding so we can properly scan
the response
oSession.utilDecodeResponse();
Encoding oEnc = Utilities.getResponseBodyEncoding
(oSession);
string sBody = Utilities.GetStringFromArrayRemovingBOM
(oSession.responseBodyBytes, oEnc);
int iStart = 0;
int iHeadPtr = 0;
do
{
iHeadPtr = sBody.IndexOf("<head", iStart, Math.Min
(16384, sBody.Length-iStart), StringComparison.OrdinalIgnoreCase); //
Find <HEAD within first 16k
if (0 > iHeadPtr) return false;
if ((sBody.Length > (iHeadPtr + 5) &&
(sBody[iHeadPtr + 5] != '>') &&
((int)(sBody[iHeadPtr + 5]) > 0x21) && (int)
(sBody[iHeadPtr + 5]) < 0x7a))
{
// Someone tried to trick us with a "<headNOT"
or similar tag.
iStart = iHeadPtr + 5;
iHeadPtr = -1;
}
} while (0 > iHeadPtr);
iStart = iHeadPtr + 1;
int iInsertPtr = sBody.IndexOf(">", iStart, Math.Min
(2048, sBody.Length - iStart),
StringComparison.OrdinalIgnoreCase); // Find > within next 2k
if (0 > iHeadPtr) { return false; }
oSession.responseBodyBytes = oEnc.GetBytes(sBody.Insert
(iInsertPtr + 1, sToInsert));
oSession.oResponse.headers["Content-Length"] =
oSession.responseBodyBytes.LongLength.ToString();
return true;
}
return false;
}
Call it like so:
insertIntoHTMLHead(oSession, "\r\n<!-- kilroy was here -->\r\n");