Skip to content

Commit 6642b4d

Browse files
committed
Don't read entire file into memory buffer
Signed-off-by: Anders F Björklund <[email protected]>
1 parent 3e55360 commit 6642b4d

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

Diff for: pkg/hostagent/proxy/proxy.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ package proxy
44

55
import (
66
"bufio"
7-
"bytes"
87
"context"
9-
"io"
108
"net"
119
"net/http"
1210
"os"
@@ -61,19 +59,23 @@ func sendFile(req *http.Request, path string, lastModified time.Time, contentTyp
6159
status := http.StatusOK
6260
resp.StatusCode = status
6361
resp.Status = http.StatusText(status)
64-
b, err := os.ReadFile(path)
62+
st, err := os.Stat(path)
6563
if err != nil {
6664
return nil, err
6765
}
68-
resp.Body = io.NopCloser(bytes.NewBuffer(b))
66+
f, err := os.Open(path)
67+
if err != nil {
68+
return nil, err
69+
}
70+
resp.Body = f
6971
if contentType == "" {
7072
contentType = "application/octet-stream"
7173
}
7274
resp.Header.Set("Content-Type", contentType)
7375
if !lastModified.IsZero() {
7476
resp.Header.Set("Last-Modified", lastModified.Format(http.TimeFormat))
7577
}
76-
resp.ContentLength = int64(len(b))
78+
resp.ContentLength = st.Size()
7779
return resp, nil
7880
}
7981

0 commit comments

Comments
 (0)