openrasta

OpenRasta overwriting Content-Length header in HEAD responses


I'm creating a pretty simple HTTP service using OpenRasta. For HEAD requests, the HTTP 1.1 spec states that HEAD requests should have the Content-Length set to "the size of the entity-body that would have been sent had the request been a GET" (section 14.13).

However, OpenRasta apparently sees that the response body is empty and automatically sets the Content-Length header to "0".

What is the recommended way to override this behavior?

Thanks-


Solution

  • Faced with exactly this problem my solution was to add an IPipelineContributor to deal with HEAD requests. The contributor was initialized as follows:

    public void Initialize(IPipeline pipelineRunner)
    {
      // We're going to modify the HTTP method, so allow Rasta to have a go first
      pipelineRunner.Notify(PreProcessRequest).After<HttpMethodOverriderContributor>();
    }
    

    In the pre-process step I swapped the HTTP method from HEAD to GET in order to allow the request to be processed as normal.

    static PipelineContinuation PreProcessRequest(ICommunicationContext arg)
    {
      if (arg.Request.HttpMethod == "HEAD")
      {
        // Change the method to GET to allow normal processing
        arg.Request.HttpMethod = HttpMethod.GET.ToString();
      }
    
      return PipelineContinuation.Continue;
    }    
    

    At the end of the pipeline the response headers get written as expected, but nothing is written to the body.