From 6218a8ca50d7f6d70a8810b08d346919c6c10e96 Mon Sep 17 00:00:00 2001 From: Tyler Date: Sun, 7 Mar 2021 00:18:28 -0500 Subject: [PATCH] Ensure callback is called --- main.go | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index f28be9f..d4ad86f 100644 --- a/main.go +++ b/main.go @@ -18,7 +18,7 @@ import ( "time" ) -type callbackFunc func(rules yara.MatchRules) +type callbackFunc func(*yara.MatchRules, error) type Job struct { Data io.ReadCloser @@ -106,11 +106,16 @@ func scanHandler(w http.ResponseWriter, r *http.Request) { wg := &sync.WaitGroup{} - results := make([]yara.MatchRules, 0) + results := make([]*yara.MatchRules, 0) - jobCallback := func(m yara.MatchRules) { - results = append(results, m) + jobCallback := func(m *yara.MatchRules, err error) { wg.Done() + + if err != nil { + return + } + + results = append(results, m) } log.WithField("contentType", contentType).Debug("Adding files from multipart form as jobs") @@ -143,12 +148,20 @@ func scanHandler(w http.ResponseWriter, r *http.Request) { wg.Wait() + log.WithField("matches", w).Debug("Matched rules") json.NewEncoder(w).Encode(results) default: job := &Job{ Data: r.Body, - Callback: func(m yara.MatchRules) { - json.NewEncoder(w).Encode(m) + Callback: func(m *yara.MatchRules, err error) { + log.WithField("match", m).Debug("Matched rules") + + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + } else { + json.NewEncoder(w).Encode(m) + } }, } @@ -235,19 +248,16 @@ func processJob(s *yara.Scanner, job *Job) { b, err := io.ReadAll(job.Data) if err != nil { + job.Callback(nil, err) return } err = s.SetCallback(&m).ScanMem(b) if err != nil { + job.Callback(nil, err) return } - // Respond with job - if len(m) < 1 { - return - } - - job.Callback(m) + job.Callback(&m, nil) }