Skip to content

Commit

Permalink
feat: deflate support
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgtaylor committed Aug 2, 2023
1 parent 2242e20 commit 6bd162a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Features include:
- CBOR ([RFC 7049](https://tools.ietf.org/html/rfc7049), <http://cbor.io/>)
- MessagePack (<https://msgpack.org/>)
- Amazon Ion (<http://amzn.github.io/ion-docs/>)
- Gzip ([RFC 1952](https://tools.ietf.org/html/rfc1952)) and Brotli ([RFC 7932](https://tools.ietf.org/html/rfc7932)) content encoding
- Gzip ([RFC 1952](https://tools.ietf.org/html/rfc1952)), Deflate ([RFC 1951](https://datatracker.ietf.org/doc/html/rfc1951)), and Brotli ([RFC 7932](https://tools.ietf.org/html/rfc7932)) content encoding
- Standardized [hypermedia](https://smartbear.com/learn/api-design/what-is-hypermedia/) parsing into queryable/followable response links:
- HTTP Link relation headers ([RFC 5988](https://tools.ietf.org/html/rfc5988#section-6.2.2))
- [HAL](http://stateless.co/hal_specification.html)
Expand Down
1 change: 1 addition & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ func initCache(appName string) {
// the CLI.
func Defaults() {
// Register content encodings
AddEncoding("deflate", &DeflateEncoding{})
AddEncoding("gzip", &GzipEncoding{})
AddEncoding("br", &BrotliEncoding{})

Expand Down
9 changes: 9 additions & 0 deletions cli/encoding.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"compress/flate"
"compress/gzip"
"fmt"
"io"
Expand Down Expand Up @@ -62,6 +63,14 @@ func DecodeResponse(resp *http.Response) error {
return nil
}

// DeflateEncoding supports gzip-encoded response content.
type DeflateEncoding struct{}

// Reader returns a new reader for the stream that removes the gzip encoding.
func (g DeflateEncoding) Reader(stream io.Reader) (io.Reader, error) {
return flate.NewReader(stream), nil
}

// GzipEncoding supports gzip-encoded response content.
type GzipEncoding struct{}

Expand Down
10 changes: 10 additions & 0 deletions cli/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"bytes"
"compress/flate"
"compress/gzip"
"io"
"net/http"
Expand All @@ -19,6 +20,14 @@ func gzipEnc(data string) []byte {
return b.Bytes()
}

func deflateEnc(data string) []byte {
b := bytes.NewBuffer(nil)
w, _ := flate.NewWriter(b, 1)
w.Write([]byte(data))
w.Close()
return b.Bytes()
}

func brEnc(data string) []byte {
b := bytes.NewBuffer(nil)
w := brotli.NewWriter(b)
Expand All @@ -34,6 +43,7 @@ var encodingTests = []struct {
}{
{"none", "", []byte("hello world")},
{"gzip", "gzip", gzipEnc("hello world")},
{"deflate", "deflate", deflateEnc("hello world")},
{"brotli", "br", brEnc("hello world")},
}

Expand Down
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Start with the [guide](/guide.md) to learn how to install and configure Restish
- CBOR ([RFC 7049](https://tools.ietf.org/html/rfc7049), <http://cbor.io/>)
- MessagePack (<https://msgpack.org/>)
- Amazon Ion (<http://amzn.github.io/ion-docs/>)
- Gzip ([RFC 1952](https://tools.ietf.org/html/rfc1952)) and Brotli ([RFC 7932](https://tools.ietf.org/html/rfc7932)) content encoding
- Gzip ([RFC 1952](https://tools.ietf.org/html/rfc1952)), Deflate ([RFC 1951](https://datatracker.ietf.org/doc/html/rfc1951)), and Brotli ([RFC 7932](https://tools.ietf.org/html/rfc7932)) content encoding
- Standardized [hypermedia](https://smartbear.com/learn/api-design/what-is-hypermedia/) parsing into queryable/followable response links:
- HTTP Link relation headers ([RFC 5988](https://tools.ietf.org/html/rfc5988#section-6.2.2))
- [HAL](http://stateless.co/hal_specification.html)
Expand Down

0 comments on commit 6bd162a

Please sign in to comment.