-
Notifications
You must be signed in to change notification settings - Fork 4
server: serve static files from memory (macOS sendfile fix) #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ZukwiZ
wants to merge
1
commit into
master
Choose a base branch
from
feat/static-serve-inmemory
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,12 @@ | ||
| package server | ||
|
|
||
| import ( | ||
| "mime" | ||
| "net/http" | ||
| "os" | ||
| "path/filepath" | ||
| "regexp" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "reverse-watch/api" | ||
|
|
@@ -60,9 +64,13 @@ func New(cfg config.Config, factory repository.Factory) (*Server, error) { | |
|
|
||
| r.Use(rwmiddleware.FactoryMiddleware(factory)) | ||
|
|
||
| r.Get("/", func(w http.ResponseWriter, r *http.Request) { | ||
| http.ServeFile(w, r, "static/index.html") | ||
| }) | ||
| // Static files are read into memory and written in a single response | ||
| // body. We avoid http.ServeFile / http.FileServer because the sendfile | ||
| // fast path on some local setups truncates large responses at the | ||
| // first TCP segment. Files served from here are tiny (HTML + a handful | ||
| // of logos/icons), so the read-once cost is negligible. | ||
| r.Get("/", serveStaticFile("static/index.html", "text/html; charset=utf-8")) | ||
| r.Get("/static/*", staticDirHandler("static")) | ||
|
|
||
| r.Mount("/api", api.Router()) | ||
|
|
||
|
|
@@ -74,3 +82,43 @@ func New(cfg config.Config, factory repository.Factory) (*Server, error) { | |
| func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
| s.r.ServeHTTP(w, r) | ||
| } | ||
|
|
||
| // serveStaticFile returns a handler that reads the file fresh on every | ||
| // request and writes its bytes with the given content-type. | ||
| func serveStaticFile(path, contentType string) http.HandlerFunc { | ||
| return func(w http.ResponseWriter, r *http.Request) { | ||
| b, err := os.ReadFile(path) | ||
| if err != nil { | ||
| http.Error(w, "not found", http.StatusNotFound) | ||
| return | ||
| } | ||
| w.Header().Set("Content-Type", contentType) | ||
| w.Header().Set("Content-Length", strconv.Itoa(len(b))) | ||
| _, _ = w.Write(b) | ||
| } | ||
| } | ||
|
|
||
| // staticDirHandler serves files from baseDir under a chi wildcard | ||
| // /<prefix>/*. Path traversal is rejected. | ||
| func staticDirHandler(baseDir string) http.HandlerFunc { | ||
| return func(w http.ResponseWriter, r *http.Request) { | ||
| rest := strings.TrimPrefix(r.URL.Path, "/static/") | ||
| if rest == "" || strings.Contains(rest, "..") { | ||
| http.NotFound(w, r) | ||
| return | ||
| } | ||
| full := filepath.Join(baseDir, filepath.Clean("/"+rest)) | ||
| b, err := os.ReadFile(full) | ||
| if err != nil { | ||
| http.NotFound(w, r) | ||
| return | ||
| } | ||
| ctype := mime.TypeByExtension(filepath.Ext(full)) | ||
| if ctype == "" { | ||
| ctype = http.DetectContentType(b) | ||
| } | ||
| w.Header().Set("Content-Type", ctype) | ||
| w.Header().Set("Content-Length", strconv.Itoa(len(b))) | ||
| _, _ = w.Write(b) | ||
| } | ||
| } | ||
|
Comment on lines
+101
to
+124
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is needed, and it poses a security risk. We will likely embed the icons directly in the binary in the future. |
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hardcoded URL prefix instead of chi wildcard param
Low Severity
staticDirHandlerextracts the relative path viastrings.TrimPrefix(r.URL.Path, "/static/"), hardcoding the/static/URL prefix rather than usingchi.URLParam(r, "*"). The rest of the codebase consistently useschi.URLParamfor route parameter extraction. This couples the handler's internals to the route registration string and will silently break if the route is ever moved to a sub-router (where chi rewritesr.URL.Path).Reviewed by Cursor Bugbot for commit a701758. Configure here.