-
Notifications
You must be signed in to change notification settings - Fork 3
feature: add gzip support for static files #143
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
Draft
siamskoi
wants to merge
4
commits into
roadrunner-server:master
Choose a base branch
from
siamskoi:master
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.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3297bdf
Add gzip support for static files
siamskoi 86baaaf
Enable configurable gzip compression with file size limits
siamskoi c9999dc
Enable configurable gzip compression with file size limits
siamskoi 37d9eb0
Refactor gzip handling to use klauspost/compress library
siamskoi 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
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
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
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 |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ package static | |
|
|
||
| import ( | ||
| "fmt" | ||
| "github.com/klauspost/compress/gzhttp" | ||
| "io" | ||
| "net/http" | ||
| "path" | ||
| "strings" | ||
|
|
@@ -48,6 +50,9 @@ type Plugin struct { | |
| forbiddenExtensions map[string]struct{} | ||
| // opentelemetry | ||
| prop propagation.TextMapPropagator | ||
|
|
||
| // gzipEnabled indicates if gzip compression is enabled for serving static files. | ||
| gzipEnabled bool | ||
| } | ||
|
|
||
| // Init must return configure service and return true if the service hasStatus enabled. Must return error in case of | ||
|
|
@@ -79,6 +84,7 @@ func (s *Plugin) Init(cfg Configurer, log Logger) error { | |
|
|
||
| s.log = log.NamedLogger(PluginName) | ||
| s.root = http.Dir(s.cfg.Dir) | ||
| s.gzipEnabled = s.cfg.GzipEnabled | ||
|
|
||
| // init forbidden | ||
| for i := 0; i < len(s.cfg.Forbid); i++ { | ||
|
|
@@ -225,6 +231,26 @@ func (s *Plugin) Middleware(next http.Handler) http.Handler { //nolint:gocognit, | |
| } | ||
| } | ||
|
|
||
| if s.gzipEnabled && strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") { | ||
| s.log.Debug("gzip compression requested") | ||
| // Skip compression for already compressed formats | ||
| contentType := http.DetectContentType(make([]byte, 512)) | ||
|
Member
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. Are you detecting the content-type for the empty slice of bytes? |
||
| if strings.Contains(contentType, "image/") || | ||
| strings.Contains(contentType, "video/") || | ||
| strings.Contains(contentType, "audio/") { | ||
| s.log.Debug("skipping compression for already compressed content", | ||
| zap.String("type", contentType)) | ||
| } else { | ||
| gzhttp.GzipHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| _, err := io.Copy(w, f) | ||
| if err != nil { | ||
| s.log.Error("failed to copy file to response: ", zap.Error(err)) | ||
| } | ||
| })).ServeHTTP(w, r) | ||
| return | ||
| } | ||
| } | ||
|
|
||
| // we passed all checks - serve the file | ||
| http.ServeContent(w, r, finfo.Name(), finfo.ModTime(), f) | ||
| }) | ||
|
|
||
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.
I'm not sure this is a good name for the option. From my POV option name should be self-explanatory.
I think that we may remove that option at all and check the
gzipheader only.