Ensure callback is called
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Tyler 2021-03-07 00:18:28 -05:00
parent 78c3e3158a
commit 6218a8ca50
1 changed files with 22 additions and 12 deletions

34
main.go
View File

@ -18,7 +18,7 @@ import (
"time" "time"
) )
type callbackFunc func(rules yara.MatchRules) type callbackFunc func(*yara.MatchRules, error)
type Job struct { type Job struct {
Data io.ReadCloser Data io.ReadCloser
@ -106,11 +106,16 @@ func scanHandler(w http.ResponseWriter, r *http.Request) {
wg := &sync.WaitGroup{} wg := &sync.WaitGroup{}
results := make([]yara.MatchRules, 0) results := make([]*yara.MatchRules, 0)
jobCallback := func(m yara.MatchRules) { jobCallback := func(m *yara.MatchRules, err error) {
results = append(results, m)
wg.Done() wg.Done()
if err != nil {
return
}
results = append(results, m)
} }
log.WithField("contentType", contentType).Debug("Adding files from multipart form as jobs") 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() wg.Wait()
log.WithField("matches", w).Debug("Matched rules")
json.NewEncoder(w).Encode(results) json.NewEncoder(w).Encode(results)
default: default:
job := &Job{ job := &Job{
Data: r.Body, Data: r.Body,
Callback: func(m yara.MatchRules) { Callback: func(m *yara.MatchRules, err error) {
json.NewEncoder(w).Encode(m) 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) b, err := io.ReadAll(job.Data)
if err != nil { if err != nil {
job.Callback(nil, err)
return return
} }
err = s.SetCallback(&m).ScanMem(b) err = s.SetCallback(&m).ScanMem(b)
if err != nil { if err != nil {
job.Callback(nil, err)
return return
} }
// Respond with job job.Callback(&m, nil)
if len(m) < 1 {
return
}
job.Callback(m)
} }