2

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-

Dave Nichol
  • 181
  • 3
  • 8
  • For now, I'm handling this by not using a codec at all for the HEAD requests (which leads to some code duplication since I actually was setting headers in the codec). Instead I just set the headers in the handler for HEAD requests, and don't return an entity in the response. – Dave Nichol Mar 27 '12 at 17:26

1 Answers1

0

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.

Joe Steele
  • 691
  • 5
  • 12