-
-
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
Serve LFS/attachment with http.ServeContent to Support Range-Request #18448
Conversation
This comment has been minimized.
This comment has been minimized.
I haven't read through all related code carefully, but I have a feeling that if we call Currectly, Gitea's We had better keep the behavior. Since we could get the |
According to my experience, the existing mime detection is problematic, some mp3/mp4 files can not be recognized accurately. |
Yes, there are related issues like:
Although I also think the If this PR need to change the existing behavior, it need more people @go-gitea/maintainers to take a look at it. My opinion: I prefer to use extension name to get mime types instead of |
I think the security headers should be kept. And another problem is |
|
The function served attachment/git blob/git lfs file, so I think the name |
|
|
It appears that there's also changes to the mime type detection? |
This comment was marked as off-topic.
This comment was marked as off-topic.
routers/common/repo.go
Outdated
// ServeBlob download a git.Blob | ||
func setCommonHeaders(ctx *context.Context, name string, data interface{}) error { | ||
// Google Chrome dislike commas in filenames, so let's change it to a space | ||
name = strings.ReplaceAll(name, ",", " ") |
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.
Ditto from the other PR:
I don't think this is needed actually, from what I remember about this weird bug, if you quote the filename(as being done in this function) chromium won't error about it.
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.
That's strange. Is it dependent on different chromium versions?
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.
It's a historical problem, I believe modern browsers (and modern frameworks) can handle it correctly, and we do not need hacky methods.
integrations/download_test.go
Outdated
@@ -73,19 +74,22 @@ func TestDownloadRawTextFileWithoutMimeTypeMapping(t *testing.T) { | |||
req := NewRequest(t, "GET", "/user2/repo2/raw/branch/master/test.xml") | |||
resp := session.MakeRequest(t, req, http.StatusOK) | |||
|
|||
assert.Equal(t, "text/plain; charset=utf-8", resp.HeaderMap.Get("Content-Type")) | |||
assert.Equal(t, "text/xml; charset=utf-8", resp.HeaderMap.Get("Content-Type")) |
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.
-> RawTextFileWithoutMimeTypeMapping this test is intended to not not find a specific mime type
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.
Yeah, but the type-detection is now taken care by mime.TypeByExtension
. And the default mime of XML file is text/xml
.
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.
The type-detection of text files has known logic confusion now. we may fix it after this PR.
There are too many modifications in this PR.
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.
To test a specific mime type not found, we can use any unknown extension, such as .foo
.xyz
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs during the next 2 months. Thank you for your contributions. |
routers/common/repo.go
Outdated
var cs string | ||
var err error | ||
if reader, ok := data.(io.ReadSeeker); ok { | ||
cs, err = charset.DetectEncodingFromReader(reader) |
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.
do we need a seeker reader explicit for http.ServeContent
- else change the detection to read in a buffer and use multi reader would also be a good option
@@ -78,15 +85,35 @@ func DetectContentType(data []byte) SniffedType { | |||
data = data[:sniffLen] | |||
} | |||
|
|||
if (strings.Contains(ct, "text/plain") || strings.Contains(ct, "text/html")) && svgTagRegex.Match(data) || | |||
strings.Contains(ct, "text/xml") && svgTagInXMLRegex.Match(data) { | |||
if (strings.HasPrefix(ct, "text/plain") || strings.HasPrefix(ct, "text/html")) && svgTagRegex.Match(data) || |
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.
Why not keep Contains
?
I need this feature badly. Can anyone merge it into the main branch? |
@@ -21,6 +26,10 @@ func newMimeTypeMap() { | |||
m := make(map[string]string, len(keys)) | |||
for _, key := range keys { | |||
m[strings.ToLower(key.Name())] = key.Value() | |||
err := mime.AddExtensionType(key.Name(), key.Value()) |
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.
please dont add unrelated changes in here
as soon as it's ready ... |
@marxangels whats going on? |
Maybe he lost interest, I think we should re-raise the PR in a cleaned up form. It has some valuable stuff in it. |
-> #20480 I put it on my list of WIPs ... |
... will work on it if i got a timeslot for it ... but feel free to also push to it ... |
The
ServeData
function now can't handle Range Request.Due to the complexity of this part of HTTP protocol, it is difficult to fully implement it and requires a lot of testing.
The official module
http.ServeContent
has this implementation, and we can replace with it directly.It can handle
If-Match, If-Unmodified-Since, If-None-Match, If-Modified-Since, If-Range
and more, perfectly!Thanks for @wxiaoguang 's tips.