-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
172 lines (142 loc) · 3.7 KB
/
http.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
/* Copyright 2015 Ron Zhang <ronzxy@mx.aketi.cn>. All rights reserved.
*
* Licensed under the Apache License, version 2.0 (the "License").
* You may not use this work except in compliance with the License, which is
* available at www.apache.org/licenses/LICENSE-2.0
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied, as more fully set forth in the License.
*
* See the NOTICE file distributed with this work for information regarding copyright ownership.
*/
package helper
import (
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"strings"
"time"
)
var Http = NewHttpHelper(90, false)
type HttpHelper struct {
client *http.Client
UserAgent string
}
func NewHttpHelper(timeout int, insecure bool) *HttpHelper {
this := &HttpHelper{}
this.client = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: time.Duration(timeout) * time.Second,
KeepAlive: time.Duration(timeout) * time.Second,
}).DialContext,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: insecure,
},
MaxIdleConns: 64,
MaxIdleConnsPerHost: 64,
IdleConnTimeout: time.Duration(timeout) * time.Second,
},
}
return this
}
func (this *HttpHelper) Do(r *http.Request) (*http.Response, error) {
return this.client.Do(r)
}
func (this *HttpHelper) NewRequest(method, addr string, body io.Reader) (*http.Request, error) {
var (
r *http.Request
err error
)
r, err = http.NewRequest(method, addr, body)
if err != nil {
return nil, err
}
if NewStringHelper().IsNotEmpty(this.UserAgent) {
r.Header.Set("User-Agent", this.UserAgent)
}
return r, nil
}
func (this *HttpHelper) Get(addr string) ([]byte, error) {
var (
request *http.Request
response *http.Response
err error
)
request, err = this.NewRequest("GET", addr, nil)
if err != nil {
return nil, err
}
response, err = this.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("server return status code %d", response.StatusCode)
}
return ioutil.ReadAll(response.Body)
}
func (this *HttpHelper) Post(addr string, body io.Reader) ([]byte, error) {
var (
request *http.Request
response *http.Response
err error
)
request, err = this.NewRequest("POST", addr, body)
if err != nil {
return nil, err
}
response, err = this.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("server return status code %d", response.StatusCode)
}
return ioutil.ReadAll(response.Body)
}
func (this *HttpHelper) PostForm(addr string, data url.Values) ([]byte, error) {
var (
request *http.Request
response *http.Response
err error
)
request, err = this.NewRequest("POST", addr, strings.NewReader(data.Encode()))
if err != nil {
return nil, err
}
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
response, err = this.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("server return status code %d", response.StatusCode)
}
return ioutil.ReadAll(response.Body)
}
func (this *HttpHelper) RemoteAddr(req *http.Request) string {
addr := req.Header.Get("X-Real-IP")
if len(addr) == 0 {
addr = req.Header.Get("X-Forwarded-For")
if addr == "" {
addr = req.RemoteAddr
if i := strings.LastIndex(addr, ":"); i > -1 {
addr = addr[:i]
}
} else {
if i := strings.LastIndex(addr, ","); i > -1 {
addr = addr[:i]
}
}
}
return addr
}