Recently I talked with my boss, Jerome, on ways to improve a website not develloped. NET but would host in IIS .
could begin to learn Python ... but here we are a bit lazy:) One solution that I suggested were to take advantage of the extensibility mechanism of the IIS7 pipeline to intervene directly in the HTML returned ; the browser.
To illustrate this we will simply add a div to bottom of page, the div containing the system time.
We start by creating our HTTPmodule and it responds to the event BeginRequest:
using System; using System.IO; using System.Web; InjectorModule namespace {public class Injector: IHttpModule {private HttpApplication _Application; public void Init (HttpApplication context) {+ = context.BeginRequest OnBeginRequest; _Application = context; } Private void OnBeginRequest (object sender, EventArgs e) {filter = FilterFactory.GetFilter Stream (_Application) if (filter == null) return; _application.Response.Filter = filter;} public void Dispose () {}}}
are delegated to a factory to create the role or not a filter based on rules that are not known by the module.
using System.IO; using System.Web; namespace InjectorModule {internal static class Stream {public static FilterFactory getFilter (HttpApplication application) {if (application == null du Filter qui tranforme la réponse envoyée aux navigateurs :
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace InjectorModule
{
internal class AppendDateTimeFilter : Stream
{
private readonly Stream _inputStream;
private readonly Encoding _encoding;
private readonly StringBuilder _responseHtml;
public AppendDateTimeFilter(Stream input, Encoding contentEncoding)
{
_inputStream = input;
_encoding = contentEncoding;
_responseHtml = new StringBuilder();
}
#region Filter overrides
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek
{
get { return true; }
}
public override bool CanWrite
{
get { return true; }
}
public override void Close()
{
_inputStream.Close();
}
public override void Flush()
{
_inputStream.Flush();
}
public override long Length
{
get { return 0; }
}
public override long Position { get; set; }
public override long Seek(long offset, SeekOrigin origin)
{
return _inputStream.Seek(offset, origin);
}
public override void SetLength(long length)
{
_inputStream.SetLength(length);
}
public override int Read(byte[] buffer, int offset, int count)
{
return _inputStream.Read(buffer, offset, count);
}
#endregion
public override void Write(byte[] buffer, int offset, int count)
{
try
{
string bufferContent = _encoding.GetString(buffer);
// Wait for the closing tag
Regex eof = new Regex("", RegexOptions.IgnoreCase);
_responseHtml.Append (bufferContent) if (! eof.IsMatch (bufferContent)) return / / Transform the response and write it back out string = finalHtml _responseHtml. Replace ("", String.Format (@ "{0}
", DateTime.Now)). ToString () / / Send. byte [] data = _encoding.GetBytes (finalHtml) _inputStream.Write (data, 0, data.Length);} catch (Exception) {}}}}
To test is simple, we compile the assembly, it is placed in the bin folder of the website that you want traffic.
In our case, we also create the bin for not existing in the Python web app.
Placing the assembly in a bin, is a technical constraint imposed by IIS, I have found no alternative and a priori there is no configuration that could affect this constraint . Finally
Opening the window for adding modules, your assembly will now present; Select it, and point your browser latter.
Tadam! time appears at the bottom:) I hope this
solutions opens up new possibilities.
A final word to advise you the excellent bookstore
htmlagilitypackgiving you easy ways to parse an HTML document, even malformed.
0 comments:
Post a Comment