From 27e2fbf12a7a941506b097567b1092d6eafb2335 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sun, 16 Apr 2017 14:29:35 +0100 Subject: [PATCH] add wrapper for HEAD #85 --- filemanager.go | 6 +++++- wrapper/response_writer.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 wrapper/response_writer.go diff --git a/filemanager.go b/filemanager.go index 2536cacb..bf833282 100644 --- a/filemanager.go +++ b/filemanager.go @@ -15,6 +15,7 @@ import ( "github.com/hacdias/caddy-filemanager/file" "github.com/hacdias/caddy-filemanager/handlers" "github.com/hacdias/caddy-filemanager/page" + "github.com/hacdias/caddy-filemanager/wrapper" "github.com/mholt/caddy/caddyhttp/httpserver" ) @@ -89,9 +90,12 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err break } - // TODO: since HEAD shouldn't return any body, we should make a wrapper here... if i.IsDir() { r.Method = "PROPFIND" + + if r.Method == "HEAD" { + w = wrapper.NewResponseWriterNoBody(w) + } } case "PROPPATCH", "MOVE", "PATCH", "PUT", "DELETE": if !user.AllowEdit { diff --git a/wrapper/response_writer.go b/wrapper/response_writer.go new file mode 100644 index 00000000..0af79376 --- /dev/null +++ b/wrapper/response_writer.go @@ -0,0 +1,29 @@ +package wrapper + +import "net/http" + +// ResponseWriterNoBody is a wrapper used to suprress the body of the response +// to a request. Mainly used for HEAD requests. +type ResponseWriterNoBody struct { + http.ResponseWriter +} + +// NewResponseWriterNoBody creates a new ResponseWriterNoBody. +func NewResponseWriterNoBody(w http.ResponseWriter) *ResponseWriterNoBody { + return &ResponseWriterNoBody{w} +} + +// Header executes the Header method from the http.ResponseWriter. +func (w ResponseWriterNoBody) Header() http.Header { + return w.ResponseWriter.Header() +} + +// Write suprresses the body. +func (w ResponseWriterNoBody) Write(data []byte) (int, error) { + return 0, nil +} + +// WriteHeader writes the header to the http.ResponseWriter. +func (w ResponseWriterNoBody) WriteHeader(statusCode int) { + w.ResponseWriter.WriteHeader(statusCode) +}