OpenRasta覆盖HEAD响应中的Content-Length头

时间:2012-03-26 19:15:07

标签: openrasta

我正在使用OpenRasta创建一个非常简单的HTTP服务。对于HEAD请求,HTTP 1.1规范声明HEAD请求应将Content-Length设置为“在请求为GET时已发送的实体主体的大小”(第14.13节)。

但是,OpenRasta显然看到响应正文为空并自动将Content-Length标头设置为“0”。

推荐的覆盖此行为的方法是什么?

谢谢 -

1 个答案:

答案 0 :(得分:0)

面对这个问题,我的解决方案是添加一个IPipelineContributor来处理HEAD请求。贡献者初始化如下:

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>();
}

在预处理步骤中,我将HTTP方法从HEAD交换到GET,以便正常处理请求。

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;
}    

在管道的末尾,响应标头按预期写入,但没有任何内容写入正文。

相关问题