1 | /* Vuls - Vulnerability Scanner |
||
2 | Copyright (C) 2016 Future Architect, Inc. Japan. |
||
3 | |||
4 | This program is free software: you can redistribute it and/or modify |
||
5 | it under the terms of the GNU General Public License as published by |
||
6 | the Free Software Foundation, either version 3 of the License, or |
||
7 | (at your option) any later version. |
||
8 | |||
9 | This program is distributed in the hope that it will be useful, |
||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
12 | GNU General Public License for more details. |
||
13 | |||
14 | You should have received a copy of the GNU General Public License |
||
15 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
16 | */ |
||
17 | |||
18 | package report |
||
19 | |||
20 | import ( |
||
21 | "bytes" |
||
22 | "encoding/json" |
||
23 | "net/http" |
||
24 | |||
25 | c "github.com/future-architect/vuls/config" |
||
26 | "github.com/future-architect/vuls/models" |
||
27 | "golang.org/x/xerrors" |
||
28 | ) |
||
29 | |||
30 | // HTTPRequestWriter writes results to HTTP request |
||
31 | type HTTPRequestWriter struct{} |
||
32 | |||
33 | // Write sends results as HTTP response |
||
34 | func (w HTTPRequestWriter) Write(rs ...models.ScanResult) (err error) { |
||
35 | for _, r := range rs { |
||
36 | b := new(bytes.Buffer) |
||
37 | json.NewEncoder(b).Encode(r) |
||
38 | _, err = http.Post(c.Conf.HTTP.URL, "application/json; charset=utf-8", b) |
||
39 | if err != nil { |
||
40 | return err |
||
41 | } |
||
42 | } |
||
43 | return nil |
||
44 | } |
||
45 | |||
46 | // HTTPResponseWriter writes results to HTTP response |
||
47 | type HTTPResponseWriter struct { |
||
48 | Writer http.ResponseWriter |
||
49 | } |
||
50 | |||
51 | // Write sends results as HTTP response |
||
52 | func (w HTTPResponseWriter) Write(rs ...models.ScanResult) (err error) { |
||
53 | res, err := json.Marshal(rs) |
||
54 | if err != nil { |
||
55 | return xerrors.Errorf("Failed to marshal scah results: %w", err) |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
56 | } |
||
57 | w.Writer.Header().Set("Content-Type", "application/json") |
||
58 | _, err = w.Writer.Write(res) |
||
59 | |||
60 | return err |
||
61 | } |
||
62 |