-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.go
205 lines (176 loc) · 5.43 KB
/
client.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package rawhttp
import (
"fmt"
"github.com/zema1/rawhttp/client"
"io"
"io/ioutil"
"net"
"net/http"
stdurl "net/url"
"strings"
"time"
)
// Client is a client for making raw http requests with go
type Client struct {
dialer Dialer
Options *Options
}
// AutomaticHostHeader sets Host header for requests automatically
func AutomaticHostHeader(enable bool) {
DefaultClient.Options.AutomaticHostHeader = enable
}
// AutomaticContentLength performs automatic calculation of request content length.
func AutomaticContentLength(enable bool) {
DefaultClient.Options.AutomaticContentLength = enable
}
// NewClient creates a new rawhttp client with provided options
func NewClient(options *Options) *Client {
c := &Client{
dialer: new(dialer),
Options: options,
}
return c
}
// Head makes a HEAD request to a given URL
func (c *Client) Head(url string) (*http.Response, error) {
return c.DoRaw("HEAD", url, "", nil, nil)
}
// Get makes a GET request to a given URL
func (c *Client) Get(url string) (*http.Response, error) {
return c.DoRaw("GET", url, "", nil, nil)
}
// Post makes a POST request to a given URL
func (c *Client) Post(url string, mimetype string, body io.Reader) (*http.Response, error) {
headers := make(map[string][]string)
headers["Content-Type"] = []string{mimetype}
return c.DoRaw("POST", url, "", headers, body)
}
// Do sends a http request and returns a response
func (c *Client) Do(req *http.Request) (*http.Response, error) {
method := req.Method
headers := req.Header
url := req.URL.String()
body := req.Body
return c.DoRaw(method, url, "", headers, body)
}
// DoRaw does a raw request with some configuration
func (c *Client) DoRaw(method, url, uripath string, headers map[string][]string, body io.Reader) (*http.Response, error) {
redirectstatus := &RedirectStatus{
FollowRedirects: true,
MaxRedirects: c.Options.MaxRedirects,
}
resp, _, err := c.do(method, url, uripath, headers, body, redirectstatus, c.Options)
return resp, err
}
func (c *Client) DoRawHijack(method, url, uripath string, headers map[string][]string, body io.Reader) (*http.Response, net.Conn, error) {
redirectstatus := &RedirectStatus{
FollowRedirects: true,
MaxRedirects: c.Options.MaxRedirects,
}
return c.do(method, url, uripath, headers, body, redirectstatus, c.Options)
}
// DoRawWithOptions performs a raw request with additional options
func (c *Client) DoRawWithOptions(method, url, uripath string, headers map[string][]string, body io.Reader, options *Options) (*http.Response, error) {
redirectstatus := &RedirectStatus{
FollowRedirects: options.FollowRedirects,
MaxRedirects: c.Options.MaxRedirects,
}
resp, _, err := c.do(method, url, uripath, headers, body, redirectstatus, options)
return resp, err
}
func (c *Client) getConn(protocol, host string, options *Options) (net.Conn, error) {
if options.Proxy != "" {
return c.dialer.DialWithProxy(protocol, host, c.Options.Proxy, c.Options.ProxyDialTimeout, options)
}
var conn net.Conn
var err error
if options.Timeout > 0 {
conn, err = c.dialer.DialTimeout(protocol, host, options.Timeout, options)
} else {
conn, err = c.dialer.Dial(protocol, host, options)
}
return conn, err
}
func (c *Client) do(method, url, uripath string, headers map[string][]string, body io.Reader, redirectstatus *RedirectStatus, options *Options) (*http.Response, net.Conn, error) {
protocol := "http"
if strings.HasPrefix(strings.ToLower(url), "https://") {
protocol = "https"
}
if headers == nil {
headers = make(map[string][]string)
}
u, err := stdurl.ParseRequestURI(url)
if err != nil {
return nil, nil, err
}
host := u.Host
if options.AutomaticHostHeader {
// add automatic space
headers["Host"] = []string{fmt.Sprintf(" %s", host)}
}
if !strings.Contains(host, ":") {
if protocol == "https" {
host += ":443"
} else {
host += ":80"
}
}
// standard path
path := u.Path
if path == "" {
path = "/"
}
if u.RawQuery != "" {
path += "?" + u.RawQuery
}
// override if custom one is specified
if uripath != "" {
path = uripath
}
if strings.HasPrefix(url, "https://") {
protocol = "https"
}
conn, err := c.getConn(protocol, host, options)
if err != nil {
return nil, nil, err
}
req := toRequest(method, path, nil, headers, body, options)
req.AutomaticContentLength = options.AutomaticContentLength
req.AutomaticHost = options.AutomaticHostHeader
// set timeout if any
if options.Timeout > 0 {
_ = conn.SetDeadline(time.Now().Add(options.Timeout))
}
connClient := client.NewConnClient(conn)
if err := connClient.WriteRequest(req); err != nil {
return nil, nil, err
}
resp, err := connClient.ReadResponse(options.ForceReadAllBody)
if err != nil {
return nil, nil, err
}
r, err := toHTTPResponse(conn, resp)
if err != nil {
return nil, nil, err
}
if resp.Status.IsRedirect() && redirectstatus.FollowRedirects && redirectstatus.Current <= redirectstatus.MaxRedirects {
// consume the response body
_, err := io.Copy(ioutil.Discard, r.Body)
if err := firstErr(err, r.Body.Close()); err != nil {
return nil, nil, err
}
loc := headerValue(r.Header, "Location")
if strings.HasPrefix(loc, "/") {
loc = fmt.Sprintf("%s://%s%s", protocol, host, loc)
}
redirectstatus.Current++
return c.do(method, loc, uripath, headers, body, redirectstatus, options)
}
return r, conn, err
}
// RedirectStatus is the current redirect status for the request
type RedirectStatus struct {
FollowRedirects bool
MaxRedirects int
Current int
}