filebrowser/http.go

60 lines
1.4 KiB
Go
Raw Normal View History

2017-06-24 11:12:15 +00:00
package filemanager
import (
"net/http"
"os"
"strings"
)
2017-06-27 08:28:29 +00:00
// 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
2017-06-25 08:11:25 +00:00
}
2017-06-27 08:28:29 +00:00
// newResponseWriterNoBody creates a new responseWriterNoBody.
func newResponseWriterNoBody(w http.ResponseWriter) *responseWriterNoBody {
return &responseWriterNoBody{w}
2017-06-24 11:12:15 +00:00
}
2017-06-25 14:19:23 +00:00
2017-06-27 08:28:29 +00:00
// Header executes the Header method from the http.ResponseWriter.
func (w responseWriterNoBody) Header() http.Header {
return w.ResponseWriter.Header()
}
2017-06-25 14:19:23 +00:00
2017-06-27 08:28:29 +00:00
// Write suprresses the body.
func (w responseWriterNoBody) Write(data []byte) (int, error) {
return 0, nil
}
2017-06-25 14:19:23 +00:00
2017-06-27 08:28:29 +00:00
// WriteHeader writes the header to the http.ResponseWriter.
func (w responseWriterNoBody) WriteHeader(statusCode int) {
w.ResponseWriter.WriteHeader(statusCode)
}
2017-06-25 14:19:23 +00:00
2017-06-27 08:28:29 +00:00
// matchURL checks if the first URL matches the second.
func matchURL(first, second string) bool {
first = strings.ToLower(first)
second = strings.ToLower(second)
2017-06-25 14:19:23 +00:00
2017-06-27 08:28:29 +00:00
return strings.HasPrefix(first, second)
}
2017-06-25 14:19:23 +00:00
2017-06-27 08:28:29 +00:00
// errorToHTTP converts errors to HTTP Status Code.
func errorToHTTP(err error, gone bool) int {
switch {
case os.IsPermission(err):
return http.StatusForbidden
case os.IsNotExist(err):
if !gone {
return http.StatusNotFound
2017-06-25 14:19:23 +00:00
}
2017-06-27 08:28:29 +00:00
return http.StatusGone
case os.IsExist(err):
return http.StatusGone
default:
return http.StatusInternalServerError
2017-06-25 14:19:23 +00:00
}
}