@@ -3,17 +3,20 @@ package httpd
33import (
44 "bytes"
55 _ "embed"
6- mfest "github.com/DSpeichert/netbootd/manifest"
7- "github.com/DSpeichert/netbootd/static"
8- "github.com/Masterminds/sprig"
96 "io"
107 "net"
118 "net/http"
129 "net/http/httputil"
1310 "net/url"
11+ "os"
12+ "path/filepath"
1413 "strings"
1514 "text/template"
1615 "time"
16+
17+ mfest "github.com/DSpeichert/netbootd/manifest"
18+ "github.com/DSpeichert/netbootd/static"
19+ "github.com/Masterminds/sprig"
1720)
1821
1922type Handler struct {
@@ -129,8 +132,41 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
129132 }
130133 rp .ServeHTTP (w , r )
131134 return
135+ } else if mount .LocalDir != "" {
136+ path := filepath .Join (mount .LocalDir , mount .Path )
137+
138+ if mount .AppendSuffix {
139+ path = filepath .Join (mount .LocalDir , strings .TrimPrefix (r .URL .Path , mount .Path ))
140+ }
141+
142+ if ! strings .HasPrefix (path , mount .LocalDir ) {
143+ h .server .logger .Error ().
144+ Err (err ).
145+ Msgf ("Requested path is invalid: %q" , path )
146+ http .Error (w , err .Error (), http .StatusBadRequest )
147+ return
148+ }
149+
150+ f , err := os .Open (path )
151+ if err != nil {
152+ h .server .logger .Error ().
153+ Err (err ).
154+ Msgf ("Could not get file from local dir: %q" , path )
155+ http .Error (w , err .Error (), http .StatusInternalServerError )
156+ return
157+ }
158+ stat , err := f .Stat ()
159+ if err != nil {
160+ h .server .logger .Error ().
161+ Err (err ).
162+ Msgf ("could not stat file: %q" , path )
163+ http .Error (w , err .Error (), http .StatusInternalServerError )
164+ return
165+ }
166+ http .ServeContent (w , r , r .URL .Path , stat .ModTime (), f )
167+ return
132168 } else {
133- // mount has neither .Path nor .Proxy defined
169+ // mount has neither .Path, .Proxy nor .LocalDir defined
134170 h .server .logger .Error ().
135171 Str ("path" , r .RequestURI ).
136172 Str ("client" , raddr .String ()).
0 commit comments