Gzip data for upload

Relevant issue: #121

This tiny PR gzips the body of the POST request before uploading it,
and adds the appropriate `Accept-Encoding` header to the request. As
far as I can tell, no change needs to be made on the Rails end, since
`Net:HTTP` knows to automatically decompress gzipped requests if they
contain the `Accept-Encoding: gzip` header.

I tested this by compiling locally and uploading a session
([![asciicast](https://asciinema.org/a/5a4ro8drmyekusbrzbrt3n5ur.png)](https://asciinema.org/a/5a4ro8drmyekusbrzbrt3n5ur)) and it seemed to work.
This commit is contained in:
Ben Tranter
2016-02-11 20:56:38 -05:00
parent e3b8299109
commit f54b0a9793

View File

@@ -2,6 +2,7 @@ package api
import (
"bytes"
"compress/gzip"
"errors"
"fmt"
"io"
@@ -74,7 +75,8 @@ func (a *AsciinemaAPI) urlForUpload() string {
func (a *AsciinemaAPI) headersForUpload() map[string]string {
return map[string]string{
"User-Agent": fmt.Sprintf("asciinema/%s %s/%s %s-%s", a.version, runtime.Compiler, runtime.Version(), runtime.GOOS, runtime.GOARCH),
"User-Agent": fmt.Sprintf("asciinema/%s %s/%s %s-%s", a.version, runtime.Compiler, runtime.Version(), runtime.GOOS, runtime.GOARCH),
"Content-Encoding": "gzip",
}
}
@@ -84,7 +86,12 @@ func filesForUpload(path string) (map[string]io.ReadCloser, error) {
return nil, err
}
return map[string]io.ReadCloser{"asciicast:asciicast.json": file}, nil
zippedFile, err := gzip.NewReader(file)
if err != nil {
return nil, err
}
return map[string]io.ReadCloser{"asciicast:asciicast.json": zippedFile}, nil
}
func extractWarningMessage(response *http.Response) string {