How can I convert XSLT to HttpModule?

I am trying to implement server side XSLT transformations as IIS HttpModule. My main approach is to install a new filter in BeginRequest, which translates the entries into a MemoryStream and then into PreSendRequestContent to convert the document using XSLT and write it to the original output stream. However, even without performing the conversion, I obviously am doing something wrong, since the HttpModule works to load the first page, and then I don’t get any response from the server at all until I restart the application pool. With the conversion to place, I get a blank page for the first time, and then no response. I'm obviously doing something stupid, but this is the first C # code I wrote in a few years (and my first attempt at an HttpModule), and I have no idea what the problem is.What mistakes am I making? (I commented out part of XSLT in the code below and uncommented the line that writes the contents of the cache in response.)

using System;
using System.IO;
using System.Text;
using System.Web;
using System.Xml;
using System.Xml.Xsl;

namespace Onyx {

    public class OnyxModule : IHttpModule {

        public String ModuleName {
            get { return "OnyxModule"; }
        }

        public void Dispose() {
        }

        public void Init(HttpApplication application) {

            application.BeginRequest += (sender, e) => {
                HttpResponse response = HttpContext.Current.Response;
                response.Filter = new CacheFilter(response.Filter);
                response.Buffer = true;
            };

            application.PreSendRequestContent += (sender, e) => {

                HttpResponse response = HttpContext.Current.Response;
                CacheFilter cache = (CacheFilter)response.Filter;

                response.Filter = cache.originalStream;
                response.Clear();

 /*               XmlReader xml = XmlReader.Create(new StreamReader(cache), new XmlReaderSettings() {
                    ProhibitDtd = false,
                    ConformanceLevel = ConformanceLevel.Auto
                });

                XmlWriter html = XmlWriter.Create(response.OutputStream, new XmlWriterSettings() {
                    ConformanceLevel = ConformanceLevel.Auto
                });

                XslCompiledTransform xslt = new XslCompiledTransform();
                xslt.Load("http://localhost/transformations/test_college.xsl", new XsltSettings() {
                    EnableDocumentFunction = true
                }, new XmlUrlResolver());
                xslt.Transform(xml, html); */

                response.Write(cache.ToString());

                response.Flush();

            };

        }


    }

    public class CacheFilter : MemoryStream {

        public Stream originalStream;
        private MemoryStream cacheStream;

        public CacheFilter(Stream stream) {
            originalStream = stream;
            cacheStream = new MemoryStream();
        }

        public override int Read(byte[] buffer, int offset, int count) {
            return cacheStream.Read(buffer, offset, count);
        }

        public override void Write(byte[] buffer, int offset, int count) {
            cacheStream.Write(buffer, offset, count);
        }

        public override bool CanRead {
            get { return cacheStream.CanRead; }
        }

        public override string ToString() {
            return Encoding.UTF8.GetString(cacheStream.ToArray());
        }

    }

}
+3
3

MemoryStream, . StreamReader/XmlReader reset 0.

stream.Position = 0;
/* or */
stream.Seek(0, SeekOrigin.Begin);
+3

, ( ). HttpApplication, , , , .

, PostReleaseRequestState - UpdateRequestCache PostUpdateRequestCache. , !

- MSDN HttpApplication PreSendRequestContent , Reflector HttpResponse.Flush.

, Response.Flush , , , :

if (contentLength > 0L) {
    byte[] bytes = Encoding.ASCII.GetBytes(Convert.ToString(contentLength, 0x10) + "\r\n");
    this._wr.SendResponseFromMemory(bytes, bytes.Length);
    this._httpWriter.Send(this._wr);
    this._wr.SendResponseFromMemory(s_chunkSuffix, s_chunkSuffix.Length);
}

, , , , . , , , Flush.

- - ( ), , , - , .

, IHttpHandler - . , .

+2

msdn example, HttpApplication.EndRequest:

context.EndRequest += (sender, e) => {
    HttpResponse response = HttpContext.Current.Response;
    response.Flush();
};

// ...

public void Init(HttpApplication application)
{
    // ...
    application.EndRequest += (new EventHandler(this.Application_EndRequest));
}

private void Application_EndRequest(object sender, EventArgs e)
{
    HttpApplication application = (HttpApplication)source;
    HttpContext context = application.Context;
    context.Current.Response.Flush();
}
+1

Source: https://habr.com/ru/post/1733197/


All Articles