Skip to content

Commit

Permalink
Simplify forward method
Browse files Browse the repository at this point in the history
  • Loading branch information
s-l-teichmann committed Sep 27, 2023
1 parent 7a8cdb6 commit ff3ae16
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions cmd/csaf_downloader/forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,19 @@ func (f *forwarder) storeFailed(filename, doc, sha256, sha512 string) {
}
}

// limitedString reads max bytes from reader and returns it as a string.
// Longer strings are indicated by "..." as a suffix.
func limitedString(r io.Reader, max int) (string, error) {
var msg strings.Builder
if _, err := io.Copy(&msg, io.LimitReader(r, int64(max))); err != nil {
return "", err
}
if msg.Len() >= max {
msg.WriteString("...")
}
return msg.String(), nil
}

// forward sends a given document with filename, status and
// checksums to the forwarder. This is async to the degree
// till the configured queue size is filled.
Expand All @@ -252,16 +265,15 @@ func (f *forwarder) forward(
}
if res.StatusCode != http.StatusCreated {
defer res.Body.Close()
var msg strings.Builder
io.Copy(&msg, io.LimitReader(res.Body, 512))
var dots string
if msg.Len() >= 512 {
dots = "..."
if msg, err := limitedString(res.Body, 512); err != nil {
slog.Error("reading forward result failed",
"error", err)
} else {
slog.Error("forwarding failed",
"filename", filename,
"body", msg,
"status_code", res.StatusCode)
}
slog.Error("forwarding failed",
"filename", filename,
"body", msg.String()+dots,
"status_code", res.StatusCode)
f.storeFailed(filename, doc, sha256, sha512)
} else {
f.succeeded++
Expand Down

0 comments on commit ff3ae16

Please sign in to comment.