-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse.go
More file actions
172 lines (152 loc) · 3.89 KB
/
response.go
File metadata and controls
172 lines (152 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package reqx
import (
"bytes"
"compress/flate"
"compress/gzip"
"compress/zlib"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"sync"
// "github.com/andybalholm/brotli" // Descomenta si usas brotli externo
)
type Response struct {
resp *http.Response
body []byte
once sync.Once
readErr error
}
// Decodifica automáticamente según Content-Encoding
func (r *Response) cacheBody() {
r.once.Do(func() {
reader := r.resp.Body
defer reader.Close()
// Siempre leemos el cuerpo "tal cual" primero.
raw, err := io.ReadAll(reader)
if err != nil {
r.readErr = fmt.Errorf("read raw body failed (status=%d, content-encoding=%q): %w",
r.resp.StatusCode, r.resp.Header.Get("Content-Encoding"), err)
return
}
// Si el Transport ya lo descomprimió, no intentes descomprimir otra vez.
// (http.Transport lo hace cuando él mismo añadió "Accept-Encoding: gzip")
if r.resp.Uncompressed {
r.body = raw
r.readErr = nil
return
}
encodings := strings.TrimSpace(r.resp.Header.Get("Content-Encoding"))
enc := strings.ToLower(encodings)
if i := strings.Index(enc, ","); i >= 0 {
enc = strings.TrimSpace(enc[:i])
}
switch enc {
case "gzip":
gr, err := gzip.NewReader(bytes.NewReader(raw))
if err != nil {
// Error con contexto
r.readErr = fmt.Errorf("gzip.NewReader failed (status=%d, content-encoding=%q): %w",
r.resp.StatusCode, encodings, err)
// Si todo falla, deja crudo
r.body = raw
return
}
defer gr.Close()
dec, derr := io.ReadAll(gr)
if derr != nil {
r.readErr = fmt.Errorf("read gzip body failed (status=%d, content-encoding=%q): %w",
r.resp.StatusCode, encodings, derr)
// Si todo falla, deja crudo
r.body = raw
return
}
r.body = dec
r.readErr = nil
case "deflate":
if zr, err := zlib.NewReader(bytes.NewReader(raw)); err == nil {
defer zr.Close()
if dec, derr := io.ReadAll(zr); derr == nil {
r.body = dec
r.readErr = nil
return
} else {
r.readErr = fmt.Errorf("read deflate(zlib) body failed (status=%d, content-encoding=%q): %w",
r.resp.StatusCode, encodings, derr)
}
}
// fallback a raw deflate
fr := flate.NewReader(bytes.NewReader(raw))
defer fr.Close()
if dec, derr := io.ReadAll(fr); derr == nil {
r.body = dec
r.readErr = nil
return
}
r.readErr = fmt.Errorf("read deflate(raw) body failed (status=%d, content-encoding=%q)", r.resp.StatusCode, encodings)
// Si todo falla, deja crudo
r.body = raw
case "br":
// Brotli no soportado nativamente
dec, derr := io.ReadAll(bytes.NewReader(raw))
if derr != nil {
r.readErr = fmt.Errorf("read br body failed (status=%d, content-encoding=%q): %w",
r.resp.StatusCode, encodings, derr)
// Si todo falla, deja crudo
r.body = raw
return
}
r.body = dec
r.readErr = nil
default:
// Sin codificación o desconocida -> crudo
r.body = raw
r.readErr = nil
}
})
}
func (r *Response) Bytes() ([]byte, error) {
r.cacheBody()
return r.body, r.readErr
}
func (r *Response) String() (string, error) {
r.cacheBody()
return string(r.body), r.readErr
}
func (r *Response) Json(v interface{}) error {
r.cacheBody()
if r.readErr != nil {
return r.readErr
}
return json.Unmarshal(r.body, v)
}
func (r *Response) Status() int {
return r.resp.StatusCode
}
func (r *Response) StatusText() string {
return r.resp.Status
}
func (r *Response) Header() http.Header {
return r.resp.Header
}
func (r *Response) Cookies() []*http.Cookie {
return r.resp.Cookies()
}
func (r *Response) Location() (*url.URL, error) {
return r.resp.Location()
}
func (r *Response) Raw() *http.Response {
return r.resp
}
func (r *Response) ToFile(filename string) error {
if r.readErr != nil {
return r.readErr
}
return os.WriteFile("continue.html", r.body, 0644)
}
func (r *Response) IsOK() bool {
return r.resp.StatusCode == http.StatusOK
}