From f6a0707fe6de789d57042f9b8c18a03931c39a53 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Mon, 22 Jul 2024 22:03:13 +0200 Subject: [PATCH] refactor: shorten response writer code --- lib/handler.go | 10 +++++++++- lib/response_writer.go | 27 --------------------------- 2 files changed, 9 insertions(+), 28 deletions(-) delete mode 100644 lib/response_writer.go diff --git a/lib/handler.go b/lib/handler.go index 7e1924b..5529123 100644 --- a/lib/handler.go +++ b/lib/handler.go @@ -106,7 +106,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } if r.Method == "HEAD" { - w = newResponseWriterNoBody(w) + w = responseWriterNoBody{w} } // Excerpt from RFC4918, section 9.4: @@ -130,3 +130,11 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // Runs the WebDAV. user.ServeHTTP(w, r) } + +type responseWriterNoBody struct { + http.ResponseWriter +} + +func (w responseWriterNoBody) Write(data []byte) (int, error) { + return 0, nil +} diff --git a/lib/response_writer.go b/lib/response_writer.go deleted file mode 100644 index 2090871..0000000 --- a/lib/response_writer.go +++ /dev/null @@ -1,27 +0,0 @@ -package lib - -import "net/http" - -var _ http.ResponseWriter = responseWriterNoBody{} - -// responseWriterNoBody is a wrapper used to suppress 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} -} - -// Write suppress 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.Header().Del("Content-Length") - w.ResponseWriter.WriteHeader(statusCode) -}