aboutsummaryrefslogtreecommitdiff
path: root/lib/http/server.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2026-01-06 23:00:07 +0700
committerShulhan <ms@kilabit.info>2026-01-06 23:05:47 +0700
commite91954b5a65847970903e0d8294e54d918c8bc0b (patch)
tree95d971ce7c35fdc1d586e52ba53189ca2d88d93a /lib/http/server.go
parentf9567f0d4fc5cf6d0e1cea2d22289250c6b1cb2b (diff)
downloadpakakeh.go-e91954b5a65847970903e0d8294e54d918c8bc0b.tar.xz
lib/http: add second return value, statusCode, to FSHandler
Non-zero status code indicates that the function already response to the request, and the server will return immediately. Zero status code indicates that the function did not process the request, it is up to server to process the returned node `out`.
Diffstat (limited to 'lib/http/server.go')
-rw-r--r--lib/http/server.go14
1 files changed, 11 insertions, 3 deletions
diff --git a/lib/http/server.go b/lib/http/server.go
index 5801832f..4c89032b 100644
--- a/lib/http/server.go
+++ b/lib/http/server.go
@@ -449,11 +449,13 @@ func (srv *Server) handleDelete(res http.ResponseWriter, req *http.Request) {
// This method only works if the [ServerOptions.Memfs] is not nil.
//
// If the request Path exists and [ServerOptions.HandleFS] is set and
-// returning false, it will return immediately.
+// returning [*memfs.Node] with non-zero HTTP `statusCode`, server will return
+// immediately.
//
// If the request Path exists in file system, it will return 200 OK with the
// header Content-Type set accordingly to the detected file type and the
// 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 is not exist it will return 404 Not Found.
@@ -485,11 +487,17 @@ func (srv *Server) HandleFS(res http.ResponseWriter, req *http.Request) {
}
if srv.Options.HandleFS != nil {
- node = srv.Options.HandleFS(node, res, req)
- if node == nil {
+ var statusCode int
+ node, statusCode = srv.Options.HandleFS(node, res, req)
+ if statusCode != 0 {
+ // HandleFS already response to the request.
return
}
}
+ if node == nil {
+ res.WriteHeader(http.StatusNotFound)
+ return
+ }
res.Header().Set(HeaderContentType, node.ContentType)