forked from forgejo/forgejo
Use standard HTTP library to serve files (#24693)
`http.ServeFile/ServeContent` handles `If-xxx`, `Content-Length`, `Range` and `Etag` correctly After this PR, storage files (eg: avatar) could be responded with correct Content-Length.
This commit is contained in:
parent
f745016092
commit
a94a8d0ab1
8 changed files with 22 additions and 157 deletions
|
@ -1,35 +0,0 @@
|
|||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package httplib
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type MockResponseWriter struct {
|
||||
header http.Header
|
||||
|
||||
StatusCode int
|
||||
BodyBuffer bytes.Buffer
|
||||
}
|
||||
|
||||
func (m *MockResponseWriter) Header() http.Header {
|
||||
return m.header
|
||||
}
|
||||
|
||||
func (m *MockResponseWriter) Write(bytes []byte) (int, error) {
|
||||
if m.StatusCode == 0 {
|
||||
m.StatusCode = http.StatusOK
|
||||
}
|
||||
return m.BodyBuffer.Write(bytes)
|
||||
}
|
||||
|
||||
func (m *MockResponseWriter) WriteHeader(statusCode int) {
|
||||
m.StatusCode = statusCode
|
||||
}
|
||||
|
||||
func NewMockResponseWriter() *MockResponseWriter {
|
||||
return &MockResponseWriter{header: http.Header{}}
|
||||
}
|
|
@ -6,6 +6,7 @@ package httplib
|
|||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
|
@ -25,12 +26,12 @@ func TestServeContentByReader(t *testing.T) {
|
|||
r.Header.Set("Range", fmt.Sprintf("bytes=%s", rangeStr))
|
||||
}
|
||||
reader := strings.NewReader(data)
|
||||
w := NewMockResponseWriter()
|
||||
w := httptest.NewRecorder()
|
||||
ServeContentByReader(r, w, "test", int64(len(data)), reader)
|
||||
assert.Equal(t, expectedStatusCode, w.StatusCode)
|
||||
assert.Equal(t, expectedStatusCode, w.Code)
|
||||
if expectedStatusCode == http.StatusPartialContent || expectedStatusCode == http.StatusOK {
|
||||
assert.Equal(t, fmt.Sprint(len(expectedContent)), w.Header().Get("Content-Length"))
|
||||
assert.Equal(t, expectedContent, w.BodyBuffer.String())
|
||||
assert.Equal(t, expectedContent, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -76,12 +77,12 @@ func TestServeContentByReadSeeker(t *testing.T) {
|
|||
}
|
||||
defer seekReader.Close()
|
||||
|
||||
w := NewMockResponseWriter()
|
||||
w := httptest.NewRecorder()
|
||||
ServeContentByReadSeeker(r, w, "test", time.Time{}, seekReader)
|
||||
assert.Equal(t, expectedStatusCode, w.StatusCode)
|
||||
assert.Equal(t, expectedStatusCode, w.Code)
|
||||
if expectedStatusCode == http.StatusPartialContent || expectedStatusCode == http.StatusOK {
|
||||
assert.Equal(t, fmt.Sprint(len(expectedContent)), w.Header().Get("Content-Length"))
|
||||
assert.Equal(t, expectedContent, w.BodyBuffer.String())
|
||||
assert.Equal(t, expectedContent, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue