Skip to content

Commit edde24b

Browse files
committed
Add Brotli decompression to Http Client
1 parent 02e975d commit edde24b

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module miniflux.app
44

55
require (
66
github.com/PuerkitoBio/goquery v1.8.0
7+
github.com/andybalholm/brotli v1.0.4
78
github.com/coreos/go-oidc v2.2.1+incompatible
89
github.com/felixge/httpsnoop v1.0.1 // indirect
910
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuy
4242
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
4343
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
4444
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
45+
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
46+
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
4547
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
4648
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
4749
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=

http/client/client.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"miniflux.app/errors"
2222
"miniflux.app/logger"
2323
"miniflux.app/timer"
24+
25+
"github.com/andybalholm/brotli"
2426
)
2527

2628
const (
@@ -185,6 +187,25 @@ func (c *Client) PostJSON(data interface{}) (*Response, error) {
185187
return c.executeRequest(request)
186188
}
187189

190+
func isBodyBrotliEncoded(resp *http.Response) bool {
191+
for _, encoding := range resp.Header["Content-Encoding"] {
192+
if encoding == "br" {
193+
return true
194+
}
195+
}
196+
197+
return false
198+
}
199+
200+
func decodeBody(resp *http.Response) ([]byte, error) {
201+
if isBodyBrotliEncoded(resp) {
202+
reader := brotli.NewReader(resp.Body)
203+
return io.ReadAll(reader)
204+
}
205+
206+
return io.ReadAll(resp.Body)
207+
}
208+
188209
func (c *Client) executeRequest(request *http.Request) (*Response, error) {
189210
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[HttpClient] inputURL=%s", c.inputURL))
190211

@@ -227,7 +248,7 @@ func (c *Client) executeRequest(request *http.Request) (*Response, error) {
227248
return nil, fmt.Errorf("client: response too large (%d bytes)", resp.ContentLength)
228249
}
229250

230-
buf, err := io.ReadAll(resp.Body)
251+
buf, err := decodeBody(resp)
231252
if err != nil {
232253
return nil, fmt.Errorf("client: error while reading body %v", err)
233254
}

http/client/client_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package client // import "miniflux.app/http/client"
66

77
import (
88
"testing"
9+
"unicode/utf8"
10+
"io"
911
)
1012

1113
func TestClientWithDelay(t *testing.T) {
@@ -43,6 +45,28 @@ func TestClientWithResponseTooLarge(t *testing.T) {
4345
}
4446
}
4547

48+
func TestClientWithBrotliResponse(t *testing.T) {
49+
clt := New("http://httpbin.org/brotli")
50+
response, err := clt.Get()
51+
52+
if err != nil {
53+
t.Fatal(err)
54+
}
55+
56+
if response.StatusCode != 200 {
57+
t.Fatalf(`Unexpected response status code: %d`, response.StatusCode)
58+
}
59+
60+
buf, err := io.ReadAll(response.Body)
61+
if err != nil {
62+
t.Fatal(err)
63+
}
64+
65+
if !utf8.Valid(buf) {
66+
t.Fatal("Expected to decode brotli response")
67+
}
68+
}
69+
4670
func TestClientWithBasicAuth(t *testing.T) {
4771
clt := New("http://httpbin.org/basic-auth/testuser/testpassword")
4872
clt.WithCredentials("testuser", "testpassword")

0 commit comments

Comments
 (0)