Outline af løsning:
using System;
using System.IO;
using System.Web;
public class ScriptReplaceModule : IHttpModule
{
public void Init(HttpApplication app)
{
app.BeginRequest += OnBeginRequest;
}
protected void OnBeginRequest(object sender, EventArgs e)
{
HttpResponse resp = ((HttpApplication)sender).Response;
resp.Filter = new ScriptReplace(resp.Filter);
}
public void Dispose()
{
}
}
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
public class ScriptReplace : Stream
{
private Stream real;
private Stream temp;
public ScriptReplace(Stream real)
{
this.real = real;
this.temp = new MemoryStream();
}
public override bool CanRead
{
get
{
return false;
}
}
public override bool CanSeek
{
get
{
return false;
}
}
public override bool CanWrite
{
get
{
return true;
}
}
public override long Length
{
get
{
return temp.Length;
}
}
public override long Position
{
get
{
return temp.Position;
}
set
{
throw new NotSupportedException();
}
}
public override int Read(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
public override long Seek(long offset, SeekOrigin direction)
{
throw new NotSupportedException();
}
public override void SetLength(long length)
{
throw new NotSupportedException();
}
public override void Close()
{
temp.Close();
StreamReader sr = new StreamReader(new MemoryStream(((MemoryStream)temp).ToArray()), Encoding.UTF8);
StreamWriter sw = new StreamWriter(real, Encoding.UTF8);
string line;
while((line = sr.ReadLine()) != null)
{
line = Regex.Replace(line, @"(<script src="")([^""]+)("">)", "$1/js/$2$3");
sw.WriteLine(line);
}
sr.Close();
sw.Close();
}
public override void Flush()
{
temp.Flush();
}
public override void Write(byte[] buffer, int offset, int count)
{
temp.Write(buffer, offset, count);
}
}