aboutsummaryrefslogtreecommitdiff
path: root/lib/http/server.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2022-03-30 01:49:14 +0700
committerShulhan <ms@kilabit.info>2022-03-30 01:49:14 +0700
commit8a8de54d66fd14d1fbdb7e8598c597f871230980 (patch)
tree1104e23c761563502a2f05be2ef1cda3e0fe12f6 /lib/http/server.go
parent0c288a5880fbc0fceb792dcfd0747d95163a90dc (diff)
downloadpakakeh.go-8a8de54d66fd14d1fbdb7e8598c597f871230980.tar.xz
lib/http: implement handler to authorized request to Server Memfs
The FSAuthHandler in the ServerOptions define a function that will be called to each GET request to Server Memfs instance using the value from the HTTP Request instance. If the request is not authorized it must return false and the HTTP response will be set to 401 Unauthorized with empty body.
Diffstat (limited to 'lib/http/server.go')
-rw-r--r--lib/http/server.go13
1 files changed, 13 insertions, 0 deletions
diff --git a/lib/http/server.go b/lib/http/server.go
index 4a9f4aa7..e6ef66b1 100644
--- a/lib/http/server.go
+++ b/lib/http/server.go
@@ -489,6 +489,9 @@ func (srv *Server) handleDelete(res http.ResponseWriter, req *http.Request) {
// response body set to the content of file.
// If the request Method is HEAD, only the header will be sent back to client.
//
+// If the request Path exists and Server Options FSAuthHandler is set and
+// returning false, it will return 401 Unauthorized.
+//
// If the request Path is not exist it will return 404 Not Found.
//
func (srv *Server) HandleFS(res http.ResponseWriter, req *http.Request) {
@@ -501,6 +504,7 @@ func (srv *Server) HandleFS(res http.ResponseWriter, req *http.Request) {
body []byte
size int64
err error
+ isAuthorized bool
)
node = srv.getFSNode(req.URL.Path)
@@ -509,6 +513,15 @@ func (srv *Server) HandleFS(res http.ResponseWriter, req *http.Request) {
return
}
+ if srv.Options.HandleFSAuth != nil {
+ req.URL.Path = node.Path
+ isAuthorized = srv.Options.HandleFSAuth(req)
+ if !isAuthorized {
+ res.WriteHeader(http.StatusUnauthorized)
+ return
+ }
+ }
+
res.Header().Set(HeaderContentType, node.ContentType)
responseETag = strconv.FormatInt(node.ModTime().Unix(), 10)