add wrapper for HEAD #85

This commit is contained in:
Henrique Dias 2017-04-16 14:29:35 +01:00
parent efdcb9fc58
commit 27e2fbf12a
No known key found for this signature in database
GPG Key ID: 936F5EB68D786730
2 changed files with 34 additions and 1 deletions

View File

@ -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 {

View File

@ -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)
}