diff options
| author | Aleksandr Nogikh <nogikh@google.com> | 2024-02-14 12:32:35 +0100 |
|---|---|---|
| committer | Aleksandr Nogikh <nogikh@google.com> | 2024-02-15 10:01:11 +0000 |
| commit | 148218342ea9b3b8fb78866b86b20155a6ef9b98 (patch) | |
| tree | 667d79d19d5eb06e526fe8e76f8cf98e48aaf3ca /dashboard/dashapi/dashapi.go | |
| parent | d7504175cf351ad7a257fc74e51a66b31866ede0 (diff) | |
dashapi: send requests using multipart/form-data
This format is much better suitable for our API since we're including a
potentially very big binary blob into every request.
No changes have to be made on the server side -- it already supports
both formats.
Hopefully it will help remediate #4495.
Diffstat (limited to 'dashboard/dashapi/dashapi.go')
| -rw-r--r-- | dashboard/dashapi/dashapi.go | 34 |
1 files changed, 23 insertions, 11 deletions
diff --git a/dashboard/dashapi/dashapi.go b/dashboard/dashapi/dashapi.go index 96bbe7e5c..e4d2fb234 100644 --- a/dashboard/dashapi/dashapi.go +++ b/dashboard/dashapi/dashapi.go @@ -11,11 +11,10 @@ import ( "encoding/json" "fmt" "io" + "mime/multipart" "net/http" "net/mail" - "net/url" "reflect" - "strings" "time" "github.com/google/syzkaller/pkg/auth" @@ -969,30 +968,43 @@ func (dash *Dashboard) queryImpl(method string, req, reply interface{}) error { } reflect.ValueOf(reply).Elem().Set(reflect.New(typ.Elem()).Elem()) } - values := make(url.Values) - values.Add("client", dash.Client) - values.Add("key", dash.Key) - values.Add("method", method) + body := &bytes.Buffer{} + mWriter := multipart.NewWriter(body) + err := mWriter.WriteField("client", dash.Client) + if err != nil { + return err + } + err = mWriter.WriteField("key", dash.Key) + if err != nil { + return err + } + err = mWriter.WriteField("method", method) + if err != nil { + return err + } if req != nil { + w, err := mWriter.CreateFormField("payload") + if err != nil { + return err + } data, err := json.Marshal(req) if err != nil { return fmt.Errorf("failed to marshal request: %w", err) } - buf := new(bytes.Buffer) - gz := gzip.NewWriter(buf) + gz := gzip.NewWriter(w) if _, err := gz.Write(data); err != nil { return err } if err := gz.Close(); err != nil { return err } - values.Add("payload", buf.String()) } - r, err := dash.ctor("POST", fmt.Sprintf("%v/api", dash.Addr), strings.NewReader(values.Encode())) + mWriter.Close() + r, err := dash.ctor("POST", fmt.Sprintf("%v/api", dash.Addr), body) if err != nil { return err } - r.Header.Set("Content-Type", "application/x-www-form-urlencoded") + r.Header.Set("Content-Type", mWriter.FormDataContentType()) resp, err := dash.doer(r) if err != nil { return fmt.Errorf("http request failed: %w", err) |
