Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent unwanted headers when returning 304 Not Modified with ServiceStack

Using ServiceStack, I just want to return 304 Not Modified as such:

HTTP/1.1 304 Not Modified

But ServiceStack adds many other unwanted (returning HttpResult with 304 code) headers as such:

HTTP/1.1 304 Not Modified
Content-Length: 0
Content-Type: application/json
Server: Microsoft-HTTPAPI/2.0
X-Powered-By: ServiceStack/3.94 Win32NT/.NET
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Headers: Content-Type
Date: Tue, 07 Aug 2012 13:39:19 GMT

How can I prevent the other headers from being outputted? I've tried various approaches with HttpResult, registering a dummy content type filter, but as its name implies only controls content, not the headers, or others listed here. I've also tried implementing my own IHttpResult derivative with IStreamWriter and IHasOptions with the same results: ServiceStack adds unwanted headers.

Thanks

Update

Was able to remove content-type by using the following, but some headers are still present i.e. content-length, server, and date.

    public override object OnGet(FaultTypes request)
    {
      var result = new HttpResult
      {
       StatusCode = HttpStatusCode.NotModified,
       StatusDescription = "Not Modified", // Otherwise NotModified written!
      };

      // The following are hacks to remove as much HTTP headers as possible
      result.ResponseFilter = new NotModifiedContentTypeWriter();
      // Removes the content-type header
      base.Request.ResponseContentType = string.Empty;

      return result;
    }

class NotModifiedContentTypeWriter : ServiceStack.ServiceHost.IContentTypeWriter
{
  ServiceStack.ServiceHost.ResponseSerializerDelegate ServiceStack.ServiceHost.IContentTypeWriter.GetResponseSerializer(string contentType)
  {
    return ResponseSerializerDelegate;
  }

  void ServiceStack.ServiceHost.IContentTypeWriter.SerializeToResponse(ServiceStack.ServiceHost.IRequestContext requestContext, object response, ServiceStack.ServiceHost.IHttpResponse httpRes)
  {
  }

  void ServiceStack.ServiceHost.IContentTypeWriter.SerializeToStream(ServiceStack.ServiceHost.IRequestContext requestContext, object response, System.IO.Stream toStream)
  {
  }

  string ServiceStack.ServiceHost.IContentTypeWriter.SerializeToString(ServiceStack.ServiceHost.IRequestContext requestContext, object response)
  {
    return string.Empty;
  }

  public void ResponseSerializerDelegate(ServiceStack.ServiceHost.IRequestContext requestContext, object dto, ServiceStack.ServiceHost.IHttpResponse httpRes)
  {
  }
}
like image 489
Martin Tapp Avatar asked Aug 07 '12 13:08

Martin Tapp


1 Answers

The only headers emitted by ServiceStack are the ones registered in the EndpointHostConfig.GlobalResponseHeaders.

Remove them if you don't want them emitted, e.g:

SetConfig(new EndpointHostConfig { 
    GlobalResponseHeaders = new Dictionary<string,string>()
});

You can add them on an adhoc-basis using a HttpResult, e.g:

return new HttpResult(dto) {
    Headers = {
       { "Access-Control-Allow-Origin", "*" },
       { "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS" } 
       { "Access-Control-Allow-Headers", "Content-Type" }, }
};

Both options explained in more detail at: servicestack REST API and CORS

like image 110
mythz Avatar answered Sep 19 '22 13:09

mythz