Skip to content

Commit abda0a2

Browse files
committed
fix \x1f on gzip resp
1 parent 5ecbf81 commit abda0a2

File tree

12 files changed

+34
-24
lines changed

12 files changed

+34
-24
lines changed

config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Domofon",
3-
"version": "0.3.5",
3+
"version": "0.3.6",
44
"slug": "domofon",
55
"description": "",
66
"startup": "application",

handlers/auth.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/json"
77
"fmt"
88
"html/template"
9-
"io"
109
"log"
1110
"net/http"
1211
"strconv"
@@ -300,7 +299,7 @@ func (h *Handler) Accounts(username *string) (a []Account, err error) {
300299
return nil, fmt.Errorf("token can't be refreshed")
301300
}
302301

303-
if body, err = io.ReadAll(resp.Body); err != nil {
302+
if body, err = ReadResponseBody(resp); err != nil {
304303
return nil, err
305304
}
306305

@@ -369,7 +368,7 @@ func (h *Handler) RequestCode(username *string, account Account) (result bool, e
369368
return true, nil
370369
}
371370

372-
if body, err = io.ReadAll(resp.Body); err != nil {
371+
if body, err = ReadResponseBody(resp); err != nil {
373372
return false, err
374373
}
375374

@@ -445,7 +444,7 @@ func (h *Handler) SendCode(r *http.Request) (authToken, refreshToken string, err
445444
return "", "", fmt.Errorf("token can't be refreshed")
446445
}
447446

448-
if body, err = io.ReadAll(resp.Body); err != nil {
447+
if body, err = ReadResponseBody(resp); err != nil {
449448
return "", "", err
450449
}
451450

handlers/cameras.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package handlers
22

33
import (
44
"context"
5-
"io"
65
"log"
76
"net/http"
87
"strconv"
@@ -58,7 +57,7 @@ func (h *Handler) Cameras() (string, error) {
5857
return "token can't be refreshed", nil
5958
}
6059

61-
if body, err = io.ReadAll(resp.Body); err != nil {
60+
if body, err = ReadResponseBody(resp); err != nil {
6261
return "", err
6362
}
6463

handlers/door.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8-
"io"
98
"log"
109
"net/http"
1110
"strconv"
@@ -76,7 +75,7 @@ func (h *Handler) Door(r *http.Request) (string, error) {
7675
return "token can't be refreshed", nil
7776
}
7877

79-
if body, err = io.ReadAll(resp.Body); err != nil {
78+
if body, err = ReadResponseBody(resp); err != nil {
8079
return "", err
8180
}
8281

handlers/events.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7-
"io"
87
"log"
98
"net/http"
109
"strconv"
@@ -90,7 +89,7 @@ func (h *Handler) Events(w http.ResponseWriter, r *http.Request) (string, error)
9089
return "token can't be refreshed", nil
9190
}
9291

93-
if body, err = io.ReadAll(resp.Body); err != nil {
92+
if body, err = ReadResponseBody(resp); err != nil {
9493
return "", err
9594
}
9695

@@ -155,7 +154,7 @@ func (h *Handler) LastEvent(w http.ResponseWriter, r *http.Request) (events Even
155154
return events, fmt.Errorf("%s", "token can't be refreshed")
156155
}
157156

158-
if body, err = io.ReadAll(resp.Body); err != nil {
157+
if body, err = ReadResponseBody(resp); err != nil {
159158
return events, err
160159
}
161160

handlers/finances.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7-
"io"
87
"log"
98
"net/http"
109
"strconv"
@@ -65,7 +64,7 @@ func (h *Handler) Finances() ([]byte, error) {
6564
return []byte("token can't be refreshed"), nil
6665
}
6766

68-
if body, err = io.ReadAll(resp.Body); err != nil {
67+
if body, err = ReadResponseBody(resp); err != nil {
6968
return nil, err
7069
}
7170

handlers/handlers.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package handlers
22

33
import (
4+
"compress/gzip"
45
"embed"
6+
"io"
57
"net/http"
8+
"strings"
69

710
"github.com/ad/domru/config"
811
)
@@ -47,3 +50,20 @@ func (h Header) RoundTrip(req *http.Request) (*http.Response, error) {
4750

4851
return h.rt.RoundTrip(req)
4952
}
53+
54+
// ReadResponseBody читает тело ответа с поддержкой gzip декомпрессии
55+
func ReadResponseBody(resp *http.Response) ([]byte, error) {
56+
var reader io.Reader = resp.Body
57+
58+
// Проверяем, сжат ли ответ gzip
59+
if strings.Contains(strings.ToLower(resp.Header.Get("Content-Encoding")), "gzip") {
60+
gzipReader, err := gzip.NewReader(resp.Body)
61+
if err != nil {
62+
return nil, err
63+
}
64+
defer gzipReader.Close()
65+
reader = gzipReader
66+
}
67+
68+
return io.ReadAll(reader)
69+
}

handlers/operators.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package handlers
22

33
import (
44
"context"
5-
"io"
65
"log"
76
"net/http"
87
"time"
@@ -47,7 +46,7 @@ func (h *Handler) Operators() (string, error) {
4746

4847
// log.Printf("%#v", resp)
4948

50-
if body, err = io.ReadAll(resp.Body); err != nil {
49+
if body, err = ReadResponseBody(resp); err != nil {
5150
return "", err
5251
}
5352

handlers/places.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package handlers
22

33
import (
44
"context"
5-
"io"
65
"log"
76
"net/http"
87
"strconv"
@@ -56,7 +55,7 @@ func (h *Handler) Places() (string, error) {
5655
return "token can't be refreshed", nil
5756
}
5857

59-
if body, err = io.ReadAll(resp.Body); err != nil {
58+
if body, err = ReadResponseBody(resp); err != nil {
6059
return "", err
6160
}
6261

handlers/snapshot.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"context"
66
"fmt"
77
jpeg "image/jpeg"
8-
"io"
98
"log"
109
"net/http"
1110
"strconv"
@@ -68,7 +67,7 @@ func (h *Handler) SnapshotHandler(w http.ResponseWriter, r *http.Request) {
6867
}
6968
}()
7069

71-
body, err = io.ReadAll(resp.Body)
70+
body, err = ReadResponseBody(resp)
7271

7372
if err == nil {
7473
contentType := http.DetectContentType(body)

0 commit comments

Comments
 (0)