-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Fix mime-type detection for HTTP server #18370
Merged
Merged
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 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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2022 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package public | ||
|
||
import "strings" | ||
|
||
// wellKnownMimeTypesLower comes from Golang's builtin mime package: `builtinTypesLower`, see the comment of detectWellKnownMimeType | ||
var wellKnownMimeTypesLower = map[string]string{ | ||
".avif": "image/avif", | ||
".css": "text/css; charset=utf-8", | ||
".gif": "image/gif", | ||
".htm": "text/html; charset=utf-8", | ||
".html": "text/html; charset=utf-8", | ||
".jpeg": "image/jpeg", | ||
".jpg": "image/jpeg", | ||
".js": "text/javascript; charset=utf-8", | ||
".json": "application/json", | ||
".mjs": "text/javascript; charset=utf-8", | ||
".pdf": "application/pdf", | ||
".png": "image/png", | ||
".svg": "image/svg+xml", | ||
".wasm": "application/wasm", | ||
".webp": "image/webp", | ||
".xml": "text/xml; charset=utf-8", | ||
|
||
// well, there are some types missing from the builtin list | ||
".txt": "text/plain; charset=utf-8", | ||
} | ||
|
||
// detectWellKnownMimeType will return the mime-type for a well-known file ext name | ||
// The purpose of this function is to bypass the unstable behavior of Golang's mime.TypeByExtension | ||
// mime.TypeByExtension would use OS's mime-type config to overwrite the well-known types (see its document). | ||
// If the user's OS has incorrect mime-type config, it would make Gitea can not respond a correct Content-Type to browsers. | ||
// For example, if Gitea returns `text/plain` for a `.js` file, the browser couldn't run the JS due to security reasons. | ||
// detectWellKnownMimeType makes the Content-Type for well-known files stable. | ||
func detectWellKnownMimeType(ext string) string { | ||
ext = strings.ToLower(ext) | ||
return wellKnownMimeTypesLower[ext] | ||
} |
This file contains 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
File renamed without changes.
This file contains 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
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.
Hmm, thought:
Isn't it risky to set charset utf-8 here? It could UTF-16 or alike...
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.
No problem here. Our mime types are only used inside
modules/public
package, which only serves files in/assets/
, they are either from Gitea source code or custom files inGITEA_CUSTOM/public
, they can be guaranteed to beutf-8
encoding.