Skip to content

Commit

Permalink
Fix http.FileServer short write
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Feb 18, 2025
1 parent ec1a977 commit 63d73e9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion experimental/clashapi/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func NewServer(ctx context.Context, logFactory log.ObservableFactory, options op
s.externalUI = filemanager.BasePath(ctx, os.ExpandEnv(options.ExternalUI))
chiRouter.Group(func(r chi.Router) {
r.Get("/ui", http.RedirectHandler("/ui/", http.StatusMovedPermanently).ServeHTTP)
r.Handle("/ui/*", http.StripPrefix("/ui/", http.FileServer(http.Dir(s.externalUI))))
r.Handle("/ui/*", http.StripPrefix("/ui/", http.FileServer(Dir(s.externalUI))))
})
}
return s, nil
Expand Down
18 changes: 18 additions & 0 deletions experimental/clashapi/server_fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package clashapi

import "net/http"

type Dir http.Dir

func (d Dir) Open(name string) (http.File, error) {
file, err := http.Dir(d).Open(name)
if err != nil {
return nil, err
}
return &fileWrapper{file}, nil
}

// workaround for #2345 #2596
type fileWrapper struct {
http.File
}
2 changes: 1 addition & 1 deletion experimental/clashapi/server_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func (s *Server) downloadExternalUI() error {
} else {
downloadURL = "https://github.com/MetaCubeX/Yacd-meta/archive/gh-pages.zip"
}
s.logger.Info("downloading external ui")
var detour adapter.Outbound
if s.externalUIDownloadDetour != "" {
outbound, loaded := s.outbound.Outbound(s.externalUIDownloadDetour)
Expand All @@ -55,6 +54,7 @@ func (s *Server) downloadExternalUI() error {
outbound := s.outbound.Default()
detour = outbound
}
s.logger.Info("downloading external ui using outbound/", detour.Type(), "[", detour.Tag(), "]")
httpClient := &http.Client{
Transport: &http.Transport{
ForceAttemptHTTP2: true,
Expand Down

0 comments on commit 63d73e9

Please sign in to comment.