How to remove the ETag header from the response served by the FileServer

97 views Asked by At

How Can i remove the "ETag" response header which is set in gobuffalo fileServer ?

I have tried creating a middleware function :

func RemoveETagHeader(next buffalo.Handler) buffalo.Handler {
    return func(c buffalo.Context) error {
        // Run the next handler in the chain
        err := next(c)

        // Remove the ETag header from the response
        c.Response().Header().Del("ETag")

        return err
    }
}

and then using it like this:

func App() *buffalo.App {

app = buffalo.New(buffalo.Options{
// ....
}
app.Use(middleware.RemoveETagHeader)
    return app
}

but the ETag has not been removed from the response

1

There are 1 answers

1
oren On

You can't do it like this, once you write into the Writer, the header is set.

What you can do, is wrap the context and delete the header when Response is called:

type deleteHeaderContext struct {
    buffalo.Context
}

func (c deleteHeaderContext) Response() http.ResponseWriter {
    res := c.Context.Response()
    res.Header().Del("ETag")
    return c.Context.Response()
}

func RemoveETagHeader(next buffalo.Handler) buffalo.Handler {
    return func(c buffalo.Context) error {
        c = deleteHeaderContext{c}
        err := next(c)
        
        return err
    }
}