forked from weibocom/motan-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
308 lines (281 loc) · 8.98 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package provider
import (
"bytes"
"errors"
"fmt"
motan "github.com/weibocom/motan-go/core"
"github.com/weibocom/motan-go/log"
"io"
"io/ioutil"
"net"
"net/http"
URL "net/url"
"reflect"
"strings"
"time"
)
type sConfT map[string]string
type srvConfT map[string]sConfT
type srvURLMapT map[string]srvConfT
// HTTPProvider struct
type HTTPProvider struct {
url *motan.URL
httpClient http.Client
srvURLMap srvURLMapT
gctx *motan.Context
mixVars []string
}
const (
// DefaultMotanMethodConfKey for default motan method conf, when make a http call without a specific motan method
DefaultMotanMethodConfKey = "http_default_motan_method"
// DefaultMotanHTTPMethod set a default http method
DefaultMotanHTTPMethod = "GET"
// MotanRequestHTTPMethodKey http method key in a motan request attachment
MotanRequestHTTPMethodKey = "HTTP_Method"
)
// Initialize http provider
func (h *HTTPProvider) Initialize() {
h.httpClient = http.Client{Timeout: 1 * time.Second}
h.srvURLMap = make(srvURLMapT)
urlConf, _ := h.gctx.Config.GetSection("http-service")
if urlConf != nil {
for confID, info := range urlConf {
srvConf := make(srvConfT)
for methodArrStr, getSrvConf := range info.(map[interface{}]interface{}) {
methodArr := strings.Split(methodArrStr.(string), ",")
for _, method := range methodArr {
sconf := make(sConfT)
for k, v := range getSrvConf.(map[interface{}]interface{}) {
// @TODO gracful panic when got a conf err, like more %s in URL_FORMAT
sconf[k.(string)] = v.(string)
}
srvConf[method] = sconf
}
}
h.srvURLMap[confID.(string)] = srvConf
}
}
}
// Destroy a HTTPProvider
func (h *HTTPProvider) Destroy() {
}
// SetSerialization for set a motan.SetSerialization to HTTPProvider
func (h *HTTPProvider) SetSerialization(s motan.Serialization) {}
// SetProxy for HTTPProvider
func (h *HTTPProvider) SetProxy(proxy bool) {}
// SetContext use to set globle config to HTTPProvider
func (h *HTTPProvider) SetContext(context *motan.Context) {
h.gctx = context
}
func buildReqURL(request motan.Request, h *HTTPProvider) (string, string, error) {
method := request.GetMethod()
httpReqURLFmt := h.url.Parameters["URL_FORMAT"]
httpReqMethod := ""
if getHTTPReqMethod, ok := h.url.Parameters["HTTP_REQUEST_METHOD"]; ok {
httpReqMethod = getHTTPReqMethod
} else {
httpReqMethod = DefaultMotanHTTPMethod
}
// when set a extconf check the specific method conf first,then use the DefaultMotanMethodConfKey conf
if _, haveExtConf := h.srvURLMap[h.url.Parameters[motan.URLConfKey]]; haveExtConf {
var specificConf = make(map[string]string, 2)
if getSpecificConf, ok := h.srvURLMap[h.url.Parameters[motan.URLConfKey]][method]; ok {
specificConf = getSpecificConf
} else if getSpecificConf, ok := h.srvURLMap[h.url.Parameters[motan.URLConfKey]][DefaultMotanMethodConfKey]; ok {
specificConf = getSpecificConf
}
if getHTTPReqURL, ok := specificConf["URL_FORMAT"]; ok {
httpReqURLFmt = getHTTPReqURL
}
if getHTTPReqMethod, ok := specificConf["HTTP_REQUEST_METHOD"]; ok {
httpReqMethod = getHTTPReqMethod
}
}
// when motan request have a http method specific in attachment use this method
if motanRequestHTTPMethod, ok := request.GetAttachments()[MotanRequestHTTPMethodKey]; ok {
httpReqMethod = motanRequestHTTPMethod
}
var httpReqURL string
if count := strings.Count(httpReqURLFmt, "%s"); count > 0 {
if count > 1 {
errMsg := "Get err URL_FORMAT: " + httpReqURLFmt
vlog.Errorln(errMsg)
return httpReqURL, httpReqMethod, errors.New(errMsg)
}
httpReqURL = fmt.Sprintf(httpReqURLFmt, method)
} else {
httpReqURL = httpReqURLFmt
}
return httpReqURL, httpReqMethod, nil
}
func buildQueryStr(request motan.Request, url *motan.URL, mixVars []string) (res string, err error) {
paramsTmp := request.GetArguments()
var buffer bytes.Buffer
if paramsTmp != nil && len(paramsTmp) > 0 {
// @if is simple, then only have paramsTmp[0]
// @TODO multi value support
vparamsTmp := reflect.ValueOf(paramsTmp[0])
t := fmt.Sprintf("%s", vparamsTmp.Type())
buffer.WriteString("requestIdFromClient=")
buffer.WriteString(fmt.Sprintf("%d", request.GetRequestID()))
switch t {
case "map[string]string":
params := paramsTmp[0].(map[string]string)
if mixVars != nil {
for _, k := range mixVars {
if _, contains := params[k]; !contains {
if value, ok := request.GetAttachments()[k]; ok {
params[k] = value
}
}
}
}
for k, v := range params {
buffer.WriteString("&")
buffer.WriteString(k)
buffer.WriteString("=")
buffer.WriteString(URL.QueryEscape(v))
}
case "string":
buffer.WriteString(URL.QueryEscape(paramsTmp[0].(string)))
}
}
res = buffer.String()
return res, err
}
// Call for do a motan call through this provider
func (h *HTTPProvider) Call(request motan.Request) motan.Response {
defer func() {
if err := recover(); err != nil {
vlog.Errorln("http provider call error! ", err)
}
}()
t := time.Now().UnixNano()
resp := &motan.MotanResponse{Attachment: make(map[string]string)}
toType := make([]interface{}, 1)
if err := request.ProcessDeserializable(toType); err != nil {
fillException(resp, t, err)
return resp
}
resp.RequestID = request.GetRequestID()
httpReqURL, httpReqMethod, err := buildReqURL(request, h)
if err != nil {
fillException(resp, t, err)
return resp
}
//vlog.Infof("HTTPProvider read to call: Method:%s, URL:%s", httpReqMethod, httpReqURL)
queryStr, err := buildQueryStr(request, h.url, h.mixVars)
if err != nil {
fillException(resp, t, err)
return resp
}
var reqBody io.Reader
if httpReqMethod == "GET" {
httpReqURL = httpReqURL + "?" + queryStr
} else if httpReqMethod == "POST" {
data, err := URL.ParseQuery(queryStr)
if err != nil {
vlog.Errorf("new HTTP Provider ParseQuery err: %v", err)
}
reqBody = strings.NewReader(data.Encode())
}
req, err := http.NewRequest(httpReqMethod, httpReqURL, reqBody)
if err != nil {
vlog.Errorf("new HTTP Provider NewRequest err: %v", err)
fillException(resp, t, err)
return resp
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") //设置后,post参数才可正常传递
for k, v := range request.GetAttachments() {
k = strings.Replace(k, "M_", "MOTAN-", -1)
req.Header.Add(k, v)
}
ip := ""
if remoteIP, exist := request.GetAttachments()[motan.RemoteIPKey]; exist {
ip = remoteIP
} else {
ip = request.GetAttachment(motan.HostKey)
}
req.Header.Add("x-forwarded-for", ip)
req.Header.Set("Accept-Encoding", "") //强制不走gzip
timeout := h.url.GetTimeDuration("requestTimeout", time.Millisecond, 1000*time.Millisecond)
c := http.Client{
Transport: &http.Transport{
Dial: func(netw, addr string) (net.Conn, error) {
deadline := time.Now().Add(timeout)
c, err := net.DialTimeout(netw, addr, timeout)
if err != nil {
return nil, err
}
c.SetDeadline(deadline)
return c, nil
},
},
}
httpResp, err := c.Do(req)
if err != nil {
vlog.Errorf("new HTTP Provider Do HTTP Call err: %v", err)
fillException(resp, t, err)
return resp
}
headers := httpResp.Header
statusCode := httpResp.StatusCode
defer httpResp.Body.Close()
body, err := ioutil.ReadAll(httpResp.Body)
l := len(body)
if l == 0 {
vlog.Warningf("server_agent result is empty :%d,%d,%s\n", statusCode, request.GetRequestID(), httpReqURL)
}
resp.ProcessTime = int64((time.Now().UnixNano() - t) / 1e6)
if err != nil {
vlog.Errorf("new HTTP Provider Read body err: %v", err)
resp.Exception = &motan.Exception{ErrCode: statusCode,
ErrMsg: fmt.Sprintf("%s", err), ErrType: http.StatusServiceUnavailable}
return resp
}
for k, v := range request.GetAttachments() {
resp.SetAttachment(k, v)
}
for k, v := range headers {
resp.SetAttachment(k, v[0])
}
resp.Value = string(body)
return resp
}
// GetName return this provider name
func (h *HTTPProvider) GetName() string {
return "HTTPProvider"
}
// GetURL return the url that represent for this provider
func (h *HTTPProvider) GetURL() *motan.URL {
return h.url
}
// SetURL to set a motan to represent for this provider
func (h *HTTPProvider) SetURL(url *motan.URL) {
h.url = url
}
// GetMixVars return the HTTPProvider mixVars
func (h *HTTPProvider) GetMixVars() []string {
return h.mixVars
}
// SetMixVars to set HTTPProvider mixVars to this provider
func (h *HTTPProvider) SetMixVars(mixVars []string) {
h.mixVars = mixVars
}
// IsAvailable to check if this provider is sitll working well
func (h *HTTPProvider) IsAvailable() bool {
//TODO Provider 是否可用
return true
}
// SetService to set services to this provider that wich can handle
func (h *HTTPProvider) SetService(s interface{}) {
}
// GetPath return current url path from the provider's url
func (h *HTTPProvider) GetPath() string {
return h.url.Path
}
func fillException(resp *motan.MotanResponse, start int64, err error) {
resp.ProcessTime = int64((time.Now().UnixNano() - start) / 1e6)
resp.Exception = &motan.Exception{ErrCode: http.StatusServiceUnavailable,
ErrMsg: fmt.Sprintf("%s", err), ErrType: http.StatusServiceUnavailable}
}