-
Notifications
You must be signed in to change notification settings - Fork 21
Abstract reader #39
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
base: master
Are you sure you want to change the base?
Abstract reader #39
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,14 +34,11 @@ func (z *ZipStream) StreamAllFiles() error { | |
| success := 0 | ||
|
|
||
| for _, entry := range z.entries { | ||
| resp, err := http.Get(entry.Url().String()) | ||
| r, err := entry.reader() | ||
| if err != nil { | ||
| continue | ||
| } | ||
| defer resp.Body.Close() | ||
| if resp.StatusCode != http.StatusOK { | ||
| continue | ||
| } | ||
| defer r.Close() | ||
|
Comment on lines
+37
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: defer in loop causes resource leak. The Apply this diff to close readers immediately after use: for _, entry := range z.entries {
r, err := entry.reader()
if err != nil {
continue
}
- defer r.Close()
header := &zip.FileHeader{
Name: entry.ZipPath(),
Method: z.CompressionMethod,
Modified: time.Now(),
}
entryWriter, err := zipWriter.CreateHeader(header)
if err != nil {
+ r.Close()
return err
}
_, err = io.Copy(entryWriter, r)
+ r.Close()
if err != nil {
return err
}Alternatively, extract the processing into a separate function where 🤖 Prompt for AI Agents |
||
|
|
||
| header := &zip.FileHeader{ | ||
| Name: entry.ZipPath(), | ||
|
|
@@ -53,7 +50,7 @@ func (z *ZipStream) StreamAllFiles() error { | |
| return err | ||
| } | ||
|
|
||
| _, err = io.Copy(entryWriter, resp.Body) | ||
| _, err = io.Copy(entryWriter, r) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
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.
Critical: HTTP status code not validated.
SimpleGetreturns the response body without checkingresp.StatusCode. A 404, 500, or any non-2xx response will return an open body that may contain error HTML/JSON instead of the expected content, causing corrupt zip entries.Apply this diff to validate the status code:
func SimpleGet(url string) ReaderFunc { return func() (io.ReadCloser, error) { resp, err := http.Get(url) if err != nil { return nil, err } + if resp.StatusCode != http.StatusOK { + resp.Body.Close() + return nil, errors.New("HTTP request failed with status: " + resp.Status) + } return resp.Body, nil } }🤖 Prompt for AI Agents