Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions chan.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ func (re *chanResponseEmitter) SetLength(l uint64) {
}
}

func (re *chanResponseEmitter) SetEncodingType(encType EncodingType) {}

func (re *chanResponseEmitter) CloseWithError(err error) error {
re.wl.Lock()
defer re.wl.Unlock()
Expand Down
4 changes: 4 additions & 0 deletions cli/responseemitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func (re *responseEmitter) SetLength(l uint64) {
re.length = l
}

func (re *responseEmitter) SetEncodingType(encType cmds.EncodingType) {
re.encType = encType
}

func (re *responseEmitter) isClosed() bool {
re.l.Lock()
defer re.l.Unlock()
Expand Down
2 changes: 2 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,8 @@ func (s *testEmitterWithError) Close() error {
return nil
}

func (s *testEmitterWithError) SetEncodingType(EncodingType) {}

func (s *testEmitterWithError) SetLength(_ uint64) {}

func (s *testEmitterWithError) CloseWithError(err error) error {
Expand Down
4 changes: 4 additions & 0 deletions encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
Protobuf = "protobuf"
Text = "text"
TextNewline = "textnl"
Gzip = "gzip"

// PostRunTypes
CLI = "cli"
Expand All @@ -56,6 +57,9 @@ var Encoders = EncoderMap{
XML: func(req *Request) func(io.Writer) Encoder {
return func(w io.Writer) Encoder { return xml.NewEncoder(w) }
},
Gzip: func(req *Request) func(io.Writer) Encoder {
return func(w io.Writer) Encoder { return TextEncoder{w: w} }
},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mayme we should also add a Tar item here also?

JSON: func(req *Request) func(io.Writer) Encoder {
return func(w io.Writer) Encoder { return json.NewEncoder(w) }
},
Expand Down
3 changes: 3 additions & 0 deletions helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ func (s *testEmitter) Close() error {
}

func (s *testEmitter) SetLength(_ uint64) {}

func (s *testEmitter) SetEncodingType(EncodingType) {}

func (s *testEmitter) CloseWithError(err error) error {
if err != nil {
(*testing.T)(s).Error(err)
Expand Down
2 changes: 1 addition & 1 deletion http/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func parseResponse(httpRes *http.Response, req *cmds.Request) (cmds.Response, er
makeDec, ok := cmds.Decoders[encType]
if ok {
res.dec = makeDec(res.rr)
} else if encType != "text" {
} else if encType != "text" && encType != "gzip" {
log.Errorf("could not find decoder for encoding %q", encType)
} // else we have an io.Reader, which is okay
} else {
Expand Down
7 changes: 4 additions & 3 deletions http/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import (

var (
MIMEEncodings = map[string]cmds.EncodingType{
"application/json": cmds.JSON,
"application/xml": cmds.XML,
"text/plain": cmds.Text,
"application/json": cmds.JSON,
"application/x-gzip": cmds.Gzip,
"application/xml": cmds.XML,
"text/plain": cmds.Text,
}
)

Expand Down
21 changes: 15 additions & 6 deletions http/responseemitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
cmds.JSON: "application/json",
cmds.XML: "application/xml",
cmds.Text: "text/plain",
cmds.Gzip: "application/x-gzip; charset=binary",
}
)

Expand Down Expand Up @@ -143,6 +144,13 @@ func (re *responseEmitter) Emit(value interface{}) error {
return err
}

func (re *responseEmitter) SetEncodingType(encType cmds.EncodingType) {
re.l.Lock()
defer re.l.Unlock()

re.encType = encType
}

func (re *responseEmitter) SetLength(l uint64) {
re.l.Lock()
defer re.l.Unlock()
Expand Down Expand Up @@ -252,10 +260,7 @@ func (re *responseEmitter) sendErr(err *cmds.Error) {
}

func (re *responseEmitter) doPreamble(value interface{}) {
var (
h = re.w.Header()
mime string
)
h := re.w.Header()

// Common Headers

Expand Down Expand Up @@ -283,6 +288,8 @@ func (re *responseEmitter) doPreamble(value interface{}) {
}
}

var mime string

switch v := value.(type) {
case *cmds.Error:
re.sendErr(v)
Expand All @@ -293,7 +300,10 @@ func (re *responseEmitter) doPreamble(value interface{}) {
h.Set(streamHeader, "1")
re.streaming = true

mime = "text/plain"
if re.encType == cmds.JSON {
mime = "text/plain"
}

case cmds.Single:
// don't set stream/channel header
default:
Expand All @@ -302,7 +312,6 @@ func (re *responseEmitter) doPreamble(value interface{}) {

if mime == "" {
var ok bool

// lookup mime type from map
mime, ok = mimeTypes[re.encType]
if !ok {
Expand Down
3 changes: 3 additions & 0 deletions responseemitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ type ResponseEmitter interface {
// SetLength sets the length of the output
SetLength(length uint64)

// SetEncodingType sets the encoding type of the output.
SetEncodingType(encType EncodingType)

// Emit sends a value.
// If value is io.Reader we just copy that to the connection
// other values are marshalled.
Expand Down
2 changes: 2 additions & 0 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ func (re *writerResponseEmitter) SetLength(length uint64) {
re.length = &length
}

func (re *writerResponseEmitter) SetEncodingType(encType EncodingType) {}

func (re *writerResponseEmitter) Close() error {
if re.closed {
return ErrClosingClosedEmitter
Expand Down
Loading