I am trying to alter an HTML page before it renders in a browser using an HTTP module. I tried to implement the agility HTML parser, but it only seems to read from files.
How can I have it read from a buffer/stream?
public override void Write(byte[] buffer, int offset, int count)
{
byte[] data = new byte[count];
Buffer.BlockCopy(buffer, offset, data, 0, count);
string html = System.Text.Encoding.Default.GetString(buffer);
HtmlDocument doc = new HtmlDocument();
doc.Load(html);
foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href"])
{
HtmlAttribute att = link["href"];
att.Value = FixLink(att);
}
}
You should be able to use a MemoryStream
to read in the data:
public override void Write(byte[] buffer, int offset, int count)
{
var stream = new MemoryStream(buffer, offset, count);
HtmlDocument doc = new HtmlDocument();
doc.Load(stream);
foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href"])
{
HtmlAttribute att = link["href"];
att.Value = FixLink(att);
}
}