forked from fanux/lhttp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upstreamFilter.go
62 lines (49 loc) · 1.16 KB
/
upstreamFilter.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
package lhttp
import (
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
)
type upstreamHeadFilter struct {
*HeadFilterBase
}
func (*upstreamHeadFilter) AfterRequestFilterHandle(ws *WsHandler) {
var value string
if value = ws.GetHeader(HEADER_KEY_UPSTREAM); value == "" {
log.Print("no upstream header found:", ws.message.message, ws.message.headers)
return
}
u := &url.URL{}
values := strings.Split(value, " ")
ws.upstreamURL, _ = u.Parse(values[1])
log.Print("upstream method:", values[0], "url: ", ws.upstreamURL.String())
httpClient := &http.Client{}
_ = httpClient
var req *http.Request
var err error
if values[0] == UPSTREAM_HTTP_METHOD_GET {
req, err = http.NewRequest(UPSTREAM_HTTP_METHOD_GET, ws.upstreamURL.String(), nil)
if err != nil {
_ = req
return
}
} else {
req, err = http.NewRequest(values[0], ws.upstreamURL.String(), strings.NewReader(ws.GetBody()))
if err != nil {
_ = req
return
}
}
for k, v := range ws.message.headers {
req.Header.Set(k, v)
}
resp, err := httpClient.Do(req)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
log.Print(string(body))
}