File tree Expand file tree Collapse file tree 1 file changed +25
-7
lines changed
Expand file tree Collapse file tree 1 file changed +25
-7
lines changed Original file line number Diff line number Diff line change @@ -52,15 +52,33 @@ func (s *Server) Delete(path string, handler HandlerFunc) {
5252
5353// static file server
5454func (s * Server ) Static (routePath , dir string ) {
55- if ! strings .HasSuffix (routePath , "/" ) {
56- routePath += "/"
57- }
58-
59- s .router .Handle (http .MethodGet , routePath + "*" , func (ctx * Context ) {
60- http .ServeFile (ctx .Writer , ctx .Request , dir + strings .TrimPrefix (ctx .Request .URL .Path , routePath ))
61- })
55+ // routePath sonunda / yoksa ekle
56+ if ! strings .HasSuffix (routePath , "/" ) {
57+ routePath += "/"
58+ }
59+
60+ s .router .Handle (http .MethodGet , routePath + "*" , func (ctx * Context ) {
61+ // İstek yolu
62+ reqPath := ctx .Request .URL .Path
63+
64+ // routePath’i kırp
65+ relPath := strings .TrimPrefix (reqPath , routePath )
66+
67+ // Güvenli dosya yolu: dir + relPath
68+ fullPath := filepath .Join (dir , relPath )
69+
70+ // Dosya yoksa 404 dön
71+ if _ , err := os .Stat (fullPath ); os .IsNotExist (err ) {
72+ http .NotFound (ctx .Writer , ctx .Request )
73+ return
74+ }
75+
76+ // ServeFile ile gönder
77+ http .ServeFile (ctx .Writer , ctx .Request , fullPath )
78+ })
6279}
6380
81+
6482// NotFound override
6583func (s * Server ) SetNotFound (handler HandlerFunc ) {
6684 s .notFound = handler
You can’t perform that action at this time.
0 commit comments