mirror of
https://github.com/imgproxy/imgproxy.git
synced 2025-10-11 04:32:29 +02:00
Set proper ContentLength for 404 responses in FS transport
This commit is contained in:
@@ -27,19 +27,7 @@ func (t transport) RoundTrip(req *http.Request) (resp *http.Response, err error)
|
|||||||
f, err := t.fs.Open(req.URL.Path)
|
f, err := t.fs.Open(req.URL.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
return &http.Response{
|
return respNotFound(req, fmt.Sprintf("%s doesn't exist", req.URL.Path)), nil
|
||||||
StatusCode: http.StatusNotFound,
|
|
||||||
Proto: "HTTP/1.0",
|
|
||||||
ProtoMajor: 1,
|
|
||||||
ProtoMinor: 0,
|
|
||||||
Header: header,
|
|
||||||
ContentLength: 0,
|
|
||||||
Body: io.NopCloser(strings.NewReader(
|
|
||||||
fmt.Sprintf("%s doesn't exist", req.URL.Path),
|
|
||||||
)),
|
|
||||||
Close: false,
|
|
||||||
Request: req,
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -50,19 +38,7 @@ func (t transport) RoundTrip(req *http.Request) (resp *http.Response, err error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if fi.IsDir() {
|
if fi.IsDir() {
|
||||||
return &http.Response{
|
return respNotFound(req, fmt.Sprintf("%s is directory", req.URL.Path)), nil
|
||||||
StatusCode: http.StatusNotFound,
|
|
||||||
Proto: "HTTP/1.0",
|
|
||||||
ProtoMajor: 1,
|
|
||||||
ProtoMinor: 0,
|
|
||||||
Header: header,
|
|
||||||
ContentLength: 0,
|
|
||||||
Body: io.NopCloser(strings.NewReader(
|
|
||||||
fmt.Sprintf("%s is directory", req.URL.Path),
|
|
||||||
)),
|
|
||||||
Close: false,
|
|
||||||
Request: req,
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.ETagEnabled {
|
if config.ETagEnabled {
|
||||||
@@ -105,3 +81,17 @@ func BuildEtag(path string, fi fs.FileInfo) string {
|
|||||||
hash := md5.Sum([]byte(tag))
|
hash := md5.Sum([]byte(tag))
|
||||||
return `"` + string(base64.RawURLEncoding.EncodeToString(hash[:])) + `"`
|
return `"` + string(base64.RawURLEncoding.EncodeToString(hash[:])) + `"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func respNotFound(req *http.Request, msg string) *http.Response {
|
||||||
|
return &http.Response{
|
||||||
|
StatusCode: http.StatusNotFound,
|
||||||
|
Proto: "HTTP/1.0",
|
||||||
|
ProtoMajor: 1,
|
||||||
|
ProtoMinor: 0,
|
||||||
|
Header: make(http.Header),
|
||||||
|
ContentLength: int64(len(msg)),
|
||||||
|
Body: io.NopCloser(strings.NewReader(msg)),
|
||||||
|
Close: false,
|
||||||
|
Request: req,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user