agent/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
package agent import ( "encoding/json" "fmt" "io" "net" "net/http" "strings" "time" "unicode" "git.j3s.sh/cascade/lib" ) // MethodNotAllowedError should be returned by a handler when the HTTP method is not allowed. type MethodNotAllowedError struct { Method string Allow []string } func (e MethodNotAllowedError) Error() string { return fmt.Sprintf("method %s not allowed", e.Method) } // BadRequestError should be returned by a handler when parameters or the payload are not valid type BadRequestError struct { Reason string } func (e BadRequestError) Error() string { return fmt.Sprintf("Bad request: %s", e.Reason) } // NotFoundError should be returned by a handler when a resource specified does not exist type NotFoundError struct { Reason string } func (e NotFoundError) Error() string { return e.Reason } // CodeWithPayloadError allow returning non HTTP 200 // Error codes while not returning PlainText payload type CodeWithPayloadError struct { Reason string StatusCode int ContentType string } func (e CodeWithPayloadError) Error() string { return e.Reason } type ForbiddenError struct { } func (e ForbiddenError) Error() string { return "Access is restricted" } // HTTPHandlers provides an HTTP api for an agent. // agent is copied into this struct because we need // to call some of its functions & access some of its data type HTTPHandlers struct { h http.Handler agent *Agent } // handler is used to initialize the Handler. // In agent code we only ever call this once. func (s *HTTPHandlers) handler() http.Handler { mux := http.NewServeMux() // handleFuncMetrics takes the given pattern and handler and wraps to produce // metrics based on the pattern and request. handleFuncMetrics := func(pattern string, handler http.HandlerFunc) { // Get the parts of the pattern. We omit any initial empty for the // leading slash, and put an underscore as a "thing" placeholder if we // see a trailing slash, which means the part after is parsed. This lets // us distinguish from things like /v1/query and /v1/query/<query id>. var parts []string for i, part := range strings.Split(pattern, "/") { if part == "" { if i == 0 { continue } part = "_" } parts = append(parts, part) } // Tranform the pattern to a valid label by replacing the '/' by '_'. // Omit the leading slash. // Distinguish thing like /v1/query from /v1/query/<query_id> by having // an extra underscore. path_label := strings.Replace(pattern[1:], "/", "_", -1) // Register the wrapper. wrapper := func(resp http.ResponseWriter, req *http.Request) { start := time.Now() handler(resp, req) s.agent.logger.Warn("request metrics", "method", req.Method, "path", path_label, "latency", time.Since(start)) } mux.Handle(pattern, http.HandlerFunc(wrapper)) } mux.HandleFunc("/", s.Index) endpoints := map[string]func(resp http.ResponseWriter, req *http.Request){ "/v1/agent/members": s.agentMembers, } for pattern, fn := range endpoints { handleFuncMetrics(pattern, fn) } // This handler bans URLs with non-printable characters h := printablePathCheckHandler(mux, nil) return h } type HandlerInput struct { ErrStatus int } func printablePathCheckHandler(next http.Handler, input *HandlerInput) http.Handler { // Nil-check on input to make it optional if input == nil { input = &HandlerInput{ ErrStatus: http.StatusBadRequest, } } // Default to http.StatusBadRequest on error if input.ErrStatus == 0 { input.ErrStatus = http.StatusBadRequest } return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r != nil { // Check URL path for non-printable characters idx := strings.IndexFunc(r.URL.Path, func(c rune) bool { return !unicode.IsPrint(c) }) if idx != -1 { w.WriteHeader(input.ErrStatus) return } if next != nil { next.ServeHTTP(w, r) } } return }) } // wrap is used to wrap functions to make them more convenient // func (s *HTTPHandlers) wrap(handler http.HandlerFunc) http.HandlerFunc { // httpLogger := s.agent.logger // return func(resp http.ResponseWriter, req *http.Request) { // setHeaders(resp, s.agent.Config.HTTPResponseHeaders) // // addAllowHeader := func(methods []string) { // resp.Header().Add("Allow", strings.Join(methods, ",")) // } // // logURL := req.URL.String() // // handleErr := func(err error) { // httpLogger.Error("Request error", err, // "method", req.Method, // "url", logURL, // "from", req.RemoteAddr, // "error", err, // ) // switch { // default: // resp.WriteHeader(http.StatusInternalServerError) // fmt.Fprint(resp, err.Error()) // } // } // // start := time.Now() // defer func() { // httpLogger.Debug("Request finished", // "method", req.Method, // "url", logURL, // "from", req.RemoteAddr, // "latency", time.Since(start).String(), // ) // }() // // contentType := "application/json" // httpCode := http.StatusOK // var buf []byte // if contentType == "application/json" { // buf, err = s.marshalJSON(req, handler(resp, req)) // if err != nil { // handleErr(err) // return // } // } else { // if strings.HasPrefix(contentType, "text/") { // if val, ok := obj.(string); ok { // buf = []byte(val) // } // } // } // resp.Header().Set("Content-Type", contentType) // resp.WriteHeader(httpCode) // resp.Write(buf) // } // } // marshalJSON marshals the object into JSON func (s *HTTPHandlers) marshalJSON(req *http.Request, obj interface{}) ([]byte, error) { buf, err := json.Marshal(obj) if err != nil { return nil, err } return buf, nil } // Renders a simple index page func (s *HTTPHandlers) Index(resp http.ResponseWriter, req *http.Request) { // Send special headers too since this endpoint isn't wrapped with something // that sends them. // setHeaders(resp, s.agent.Config.HTTPResponseHeaders) // Check if this is a non-index path if req.URL.Path != "/" { resp.WriteHeader(http.StatusNotFound) return } // Give them something helpful if there's no UI so they at least know // what this server is. fmt.Fprint(resp, "cascade agent\n") return } func decodeBody(body io.Reader, out interface{}) error { return lib.DecodeJSON(body, out) } // setHeaders is used to set canonical response header fields func setHeaders(resp http.ResponseWriter, headers map[string]string) { for field, value := range headers { resp.Header().Set(http.CanonicalHeaderKey(field), value) } } // serveHandlerWithHeaders is used to serve a http.Handler with the specified headers func serveHandlerWithHeaders(h http.Handler, headers map[string]string) http.HandlerFunc { return func(resp http.ResponseWriter, req *http.Request) { setHeaders(resp, headers) h.ServeHTTP(resp, req) } } func sourceAddrFromRequest(req *http.Request) string { xff := req.Header.Get("X-Forwarded-For") forwardHosts := strings.Split(xff, ",") if len(forwardHosts) > 0 { forwardIp := net.ParseIP(strings.TrimSpace(forwardHosts[0])) if forwardIp != nil { return forwardIp.String() } } host, _, err := net.SplitHostPort(req.RemoteAddr) if err != nil { return "" } ip := net.ParseIP(host) if ip != nil { return ip.String() } else { return "" } } func (s *HTTPHandlers) parseFilter(req *http.Request, filter *string) { if other := req.URL.Query().Get("filter"); other != "" { *filter = other } }