-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.go
179 lines (154 loc) · 3.68 KB
/
util.go
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
173
174
175
176
177
178
179
package rawhttp
import (
"bytes"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
stdurl "net/url"
"strings"
"github.com/zema1/rawhttp/client"
)
// StatusError is a HTTP status error object
type StatusError struct {
client.Status
}
func (s *StatusError) Error() string {
return s.Status.String()
}
type readCloser struct {
io.Reader
io.Closer
}
func toRequest(method string, path string, query []string, headers map[string][]string, body io.Reader, options *Options) *client.Request {
if len(options.CustomRawBytes) > 0 {
return &client.Request{RawBytes: options.CustomRawBytes}
}
reqHeaders := toHeaders(headers)
if len(options.CustomHeaders) > 0 {
reqHeaders = options.CustomHeaders
}
return &client.Request{
Method: method,
Path: path,
Query: query,
Version: client.HTTP_1_1,
Headers: reqHeaders,
Body: body,
}
}
func toHTTPResponse(conn net.Conn, resp *client.Response) (*http.Response, error) {
rheaders := fromHeaders(resp.Headers)
r := http.Response{
ProtoMinor: resp.Version.Minor,
ProtoMajor: resp.Version.Major,
Status: resp.Status.String(),
StatusCode: resp.Status.Code,
Header: rheaders,
ContentLength: resp.ContentLength(),
}
var err error
rbody := resp.Body
if headerValue(rheaders, "Content-Encoding") == "gzip" {
rbody, err = gzip.NewReader(rbody)
if err != nil {
return nil, err
}
}
rc := &readCloser{rbody, conn}
r.Body = rc
return &r, nil
}
func toHeaders(h map[string][]string) []client.Header {
var r []client.Header
for k, v := range h {
for _, v := range v {
r = append(r, client.Header{Key: k, Value: v})
}
}
return r
}
func fromHeaders(h []client.Header) map[string][]string {
if h == nil {
return nil
}
var r = make(map[string][]string)
for _, hh := range h {
r[hh.Key] = append(r[hh.Key], hh.Value)
}
return r
}
func headerValue(headers map[string][]string, key string) string {
return strings.Join(headers[key], " ")
}
func firstErr(err1, err2 error) error {
if err1 != nil {
return err1
}
if err2 != nil {
return err2
}
return nil
}
// DumpRequestRaw to string
func DumpRequestRaw(method, url, uripath string, headers map[string][]string, body io.Reader, options *Options) ([]byte, error) {
if len(options.CustomRawBytes) > 0 {
return options.CustomRawBytes, nil
}
if headers == nil {
headers = make(map[string][]string)
}
u, err := stdurl.ParseRequestURI(url)
if err != nil {
return nil, err
}
// Handle only if host header is missing
_, hasHostHeader := headers["Host"]
if !hasHostHeader {
host := u.Host
headers["Host"] = []string{host}
}
// standard path
path := u.Path
if path == "" {
path = "/"
}
if u.RawQuery != "" {
path += "?" + u.RawQuery
}
// override if custom one is specified
if uripath != "" {
path = uripath
}
req := toRequest(method, path, nil, headers, body, options)
b := strings.Builder{}
q := strings.Join(req.Query, "&")
if len(q) > 0 {
q = "?" + q
}
b.WriteString(fmt.Sprintf("%s %s%s %s"+client.NewLine, req.Method, req.Path, q, req.Version.String()))
for _, header := range req.Headers {
if header.Value != "" {
b.WriteString(fmt.Sprintf("%s: %s"+client.NewLine, header.Key, header.Value))
} else {
b.WriteString(fmt.Sprintf("%s"+client.NewLine, header.Key))
}
}
l := req.ContentLength()
if req.AutomaticContentLength && l >= 0 {
b.WriteString(fmt.Sprintf("Content-Length: %d"+client.NewLine, l))
}
b.WriteString(client.NewLine)
if req.Body != nil {
var buf bytes.Buffer
tee := io.TeeReader(req.Body, &buf)
body, err := ioutil.ReadAll(tee)
if err != nil {
return nil, err
}
b.Write(body)
}
return []byte(strings.ReplaceAll(b.String(), "\n", client.NewLine)), nil
}