add wrapper for HEAD #85

Former-commit-id: 8f4b2dbe70cc11d6143755070f9d1e72fecd02e5 [formerly af91dd4bd924866ea480538d21a7c72ff18695df] [formerly 30bb810facc8b076dde4903f0e98c0480d81da5e [formerly 27e2fbf12a]]
Former-commit-id: 595d02a70e4b27a1447ee131d3982a92deff6a33 [formerly 93521645cac3e9711c73e3ac48740a9d2ec2d61e]
Former-commit-id: fafa7cbc884baa16196442bb83a7125e6cd614e1
pull/726/head
Henrique Dias 2017-04-16 14:29:35 +01:00
parent fa1c6b7d83
commit 625797393b
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)
}