/
tfe.go
192 lines (161 loc) · 4.84 KB
/
tfe.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
package tfe
/*
* A simple front end server that proxies request, as in Tfe
*/
import (
"github.com/xianxu/rpcx"
"io"
"log"
"net/http"
"strconv"
"strings"
"time"
)
var (
contentLength0 = []string{"0"}
)
type Rules []Rule
/*
* A Tfe is basically set of rules handled by it. The set of rules is expressed as port number to
* a list of rules.
*/
type Tfe struct {
// Note: rules can't be added dynamically for now.
BindingToRules map[string]Rules
}
/*
* A routing rule encapsulates routes to a service, it provides three things:
* - to determine whether a rule can be applied to a given request
* - to transform request before forwarding to downstream
* - to transform response before replying upstream
*
* It also provides a way to report stats on the service.
*/
type Rule interface {
// name of this rule
GetName() string
// which service to use, this is the actual entity that is capable of handle http.Request
GetService() rpcx.Service
// whether this rule handles this request
HandlesRequest(*http.Request) bool
// mutate request
TransformRequest(*http.Request)
// mutate response
TransformResponse(*http.Response)
// get a reporter to report overall response stats
GetServiceReporter() rpcx.ServiceReporter
}
/*
* Simple rule implementation that allows filter based on Host/port and resource prefix.
*/
type PrefixRewriteRule struct {
Name string
// transformation rules
SourceHost string // "" matches all
SourcePathPrefix string
ProxiedPathPrefix string
ProxiedAttachHeaders map[string][]string
//TODO: how to enforce some type checking, we don't want any service, but some HttpService
Service rpcx.Service
Reporter rpcx.ServiceReporter
}
func (p *PrefixRewriteRule) HandlesRequest(r *http.Request) bool {
return (p.SourceHost == "" || p.SourceHost == r.Host) &&
strings.HasPrefix(r.URL.Path, p.SourcePathPrefix)
}
func (p *PrefixRewriteRule) TransformRequest(r *http.Request) {
r.URL.Path = p.ProxiedPathPrefix + r.URL.Path[len(p.SourcePathPrefix):len(r.URL.Path)]
r.RequestURI = ""
if p.ProxiedAttachHeaders != nil {
for k, v := range p.ProxiedAttachHeaders {
r.Header[k] = v
}
}
}
func (p *PrefixRewriteRule) TransformResponse(rsp *http.Response) {
//TODO
return
}
func (p *PrefixRewriteRule) GetService() rpcx.Service {
return p.Service
}
func (p *PrefixRewriteRule) GetName() string {
return p.Name
}
func (p *PrefixRewriteRule) GetServiceReporter() rpcx.ServiceReporter {
return p.Reporter
}
func report(reporter rpcx.ServiceReporter, req *http.Request, rsp interface{}, err error, l int64) {
if reporter != nil {
reporter.Report(req, rsp, err, l)
}
}
// Tfe HTTP serving endpoint.
func (rs *Rules) ServeHTTP(w http.ResponseWriter, r *http.Request) {
then := time.Now()
headers := w.Header()
for _, rule := range ([]Rule)(*rs) {
if rule.HandlesRequest(r) {
ruleName := rule.GetName()
rule.TransformRequest(r)
s := rule.GetService()
reporter := rule.GetServiceReporter()
if s == nil {
log.Printf("No service defined for rule %v\n", ruleName)
headers["Content-Length"] = contentLength0
w.WriteHeader(404)
report(reporter, r, &SimpleResponseForStat{404, 0}, nil, rpcx.MicroTilNow(then))
return
}
// replace r.Body with CachedReader so if we need to retry, we can.
var err error
if r.Body != nil {
if r.Body, err = NewCachedReader(r.Body); err != nil {
// if we can't read request body, just fail
log.Printf("Error occurred while reading request body for rule %v: %v\n",
ruleName, err.Error())
headers["Content-Length"] = contentLength0
w.WriteHeader(503)
report(reporter, r, &SimpleResponseForStat{503, 0}, nil, rpcx.MicroTilNow(then))
return
}
}
var rsp http.Response
err = s.Serve(r, &rsp, nil)
if err != nil {
log.Printf("Error occurred while proxying for rule %v: %v\n", ruleName, err.Error())
headers["Content-Length"] = contentLength0
w.WriteHeader(503)
report(reporter, r, &SimpleResponseForStat{503, 0}, nil, rpcx.MicroTilNow(then))
return
}
rule.TransformResponse(&rsp)
for k, v := range rsp.Header {
headers[k] = v
}
if body, ok := rsp.Body.(*CachedReader); ok {
// output content length if we know it
headers["Content-Length"] = []string{strconv.Itoa(len(body.Bytes))}
}
w.WriteHeader(rsp.StatusCode)
if rsp.StatusCode >= 300 && rsp.StatusCode < 400 {
// if redirects
report(reporter, r, &rsp, nil, rpcx.MicroTilNow(then))
return
}
// all other with body
_, err = io.Copy(w, rsp.Body)
// log error while copying
if err != nil {
// TODO, shouldn't happen, but it does happen :S
// TODO, stats report
log.Printf("err while piping bytes for rule %v: %v\n", ruleName, err)
}
report(reporter, r, &rsp, nil, rpcx.MicroTilNow(then))
return
}
}
headers["Content-Length"] = contentLength0
w.WriteHeader(404)
return
}