Content-Length missing in HTTPModule event handler

111 Views Asked by At

I have created an HttpModule for my MVC application that will intercept the request/responses to my MVC application.

The httpModule is aded in the webconfig as below

<system.webServer>
        <modules>
            <!--Below HTTP MOdule is  required to log req./res. traffic. -->
            <add name="MyLoggerModule" type="MyProject.HttpModule.MyModule" />      
        </modules>
        <urlCompression doStaticCompression="false" doDynamicCompression="false"/>
        <httpProtocol>
            <customHeaders>
                <add name="Access-Control-Allow-Origin" value="*"/>
                <add name="Access-Control-Allow-Credentials" value="true"/>
                <remove name="X-Powered-By" />
                <remove name="Vary" />
            </customHeaders>
        </httpProtocol>
        <handlers>
            <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
            <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*."
                 verb="GET,HEAD,POST,PATCH,DEBUG,PUT,DELETE"
                 type="System.Web.Handlers.TransferRequestHandler"
                 preCondition="integratedMode,runtimeVersionv4.0" />
        </handlers>
        <httpErrors existingResponse="PassThrough" />
    </system.webServer>

I am trying to get the headers in the HTTPModule PreSendRequestContent eventhandler as below:-

namespace MyProject.HttpModule
{
    public class MyModule : IHttpModule
    {
           
        public void Dispose()
        {
            //throw new NotImplementedException();
        }   
        private HttpApplication applicationContext;
        public void Init(HttpApplication context)
        {
            applicationContext = context;
            context.PreSendRequestContent += new EventHandler(context_PreSendRequestContent);
        }

        void context_PreSendRequestContent(object sender, EventArgs e)
        
        {
           HttpApplication requestApplicationContext = (HttpApplication)sender;
            var headers = applicationContext.Response.Headers;
            foreach (string header in headers.AllKeys)
            {
                string[] values = headers.GetValues(header);
                string encodedHeader = Util.Base64Encode(string.Concat(header, ":", string.Join(",", values)));
                
            }
        }
        
    }


}

when i call a curl to the api in my MVC web app viz.,

        [HttpDelete]
        [AcceptVerbs(HttpVerbs.Delete)]
        public void status(int statusCode)
        {
           HttpContext.Response.StatusCode = statusCode;
           HttpContext.Response.Headers.Add("ContentType", "text/html; charset=utf-8");

        }

i get the following response

DELETE http://localhost:59867/home/status/200
200
102 ms
DELETE /home/status/200 HTTP/1.1
x-test-id: delete-200
User-Agent: PostmanRuntime/7.29.0
Accept: */*
Postman-Token: c191829f-9bc5-4d0f-9103-f95cd0db7cfa
Host: localhost:59867
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
 
HTTP/1.1 200 OK
Server: Microsoft-IIS/10.0
ContentType: text/html; charset=utf-8
X-SourceFiles: =?UTF-8?B?RTpcR2l0bWFyY2hQT0Ncc2FsdC1paXMtcGx1Z2luXElJU1BsdWdpblxTYWx0U2VjdXJpdHkuSHR0cE1vZHVsZUNvbnN1bWVyLlNhbXBsZVdlYkFwcFxob21lXHN0YXR1c1wyMDA=?=
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Date: Tue, 08 Mar 2022 05:57:18 GMT
Content-Length: 0

However the Content-Length header seen in the response above is missing in the HTTPContext.Response.Headers.AllKeys in the code below.

 void context_PreSendRequestContent(object sender, EventArgs e)       
    {
       
        HttpApplication applicationContext = (HttpApplication)sender;

        var headers = applicationContext.Context.Request.Headers;
        
        foreach (string header in headers.AllKeys)
        {
            string[] values = headers.GetValues(header);
            string encodedHeader = Util.Base64Encode(string.Concat(header, ":", string.Join(",", values)));
          
        }
        
    }

Debugging shows that the only headers present in the context are

"Server:Microsoft-IIS/10.0","Cache-Control:private","Content-Type:text/html; charset=utf-8"

I need the content-Length header to be used in the application.Is there any way to get the content-length header that is being returned in the response?

0

There are 0 best solutions below