-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathrest.go
More file actions
171 lines (139 loc) · 4.03 KB
/
Copy pathrest.go
File metadata and controls
171 lines (139 loc) · 4.03 KB
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
// Copyright 2015-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package cbft
import (
"fmt"
"net/http"
"runtime"
"github.com/gorilla/mux"
"github.com/couchbase/cbgt"
"github.com/couchbase/cbgt/rest"
audit "github.com/couchbase/goutils/go-cbaudit"
)
// MapRESTPathStats is keyed by path spec strings.
var MapRESTPathStats map[string]*rest.RESTPathStats
var indexPaths = map[string]bool{
"/api/index/{indexName}": true,
"/api/bucket/{bucketName}/scope/{scopeName}/index/{indexName}": true,
}
var queryPaths = map[string]bool{
"/api/index/{indexName}/query": true,
"/api/bucket/{bucketName}/scope/{scopeName}/index/{indexName}/query": true,
"/Search": true, // grpc requests from n1fty
}
func isIndexPath(path string) bool {
return indexPaths[path]
}
func isQueryPath(path string) bool {
return queryPaths[path]
}
// -----------------------------------------------------------------------------
func InitRESTPathStats() {
MapRESTPathStats = make(map[string]*rest.RESTPathStats)
for k := range queryPaths {
MapRESTPathStats[k] = &rest.RESTPathStats{}
}
}
// NewRESTRouter creates a mux.Router initialized with the REST
// API routes.
func NewRESTRouter(versionMain string, mgr *cbgt.Manager,
staticDir, staticETag string, mr *cbgt.MsgRing,
adtSvc *audit.AuditSvc) (
*mux.Router, map[string]rest.RESTMeta, error) {
wrapAuthVersionHandler := func(h http.Handler) http.Handler {
return &AuthVersionHandler{mgr: mgr, H: h, adtSvc: adtSvc}
}
var options = map[string]interface{}{
"auth": wrapAuthVersionHandler,
"mapRESTPathStats": MapRESTPathStats,
}
r := mux.NewRouter()
r.StrictSlash(true)
return rest.InitRESTRouterEx(
r, versionMain, mgr, staticDir, staticETag, mr,
nil, nil, options)
}
// --------------------------------------------------
type AuthVersionHandler struct {
mgr *cbgt.Manager
H http.Handler
adtSvc *audit.AuditSvc
}
func NewAuthVersionHandler(mgr *cbgt.Manager, adtSvc *audit.AuditSvc,
h http.Handler) *AuthVersionHandler {
return &AuthVersionHandler{
mgr: mgr,
H: h,
adtSvc: adtSvc,
}
}
func (c *AuthVersionHandler) ServeHTTP(
w http.ResponseWriter, req *http.Request) {
defer func() {
if err := recover(); err != nil {
buf := make([]byte, 2048)
n := runtime.Stack(buf, false)
cbgt.PublishCrashEvent(map[string]interface{}{
"stackTrace": string(buf[:n]),
"crashError": fmt.Sprintf("%v", err),
})
}
}()
if err := CheckAPIVersion(w, req); err != nil {
return
}
path := ""
if c.H != nil {
hwrm, ok := c.H.(*rest.HandlerWithRESTMeta)
if ok && hwrm.RESTMeta != nil {
path = hwrm.RESTMeta.Opts["_path"]
}
}
c.doAudit(req, path)
wEx := wrapResponseWriter(w)
ok, username := checkAPIAuth(c, wEx, req, path)
if !ok {
return
}
if c.H != nil {
c.H.ServeHTTP(wEx, req)
}
completeRequest(username, path, req, wEx.Length())
}
func (c *AuthVersionHandler) doAudit(req *http.Request, path string) {
if c.adtSvc == nil {
return
}
eventId, ok := restAuditMap[req.Method+":"+path]
if ok {
d := GetAuditEventData(eventId, req)
go c.adtSvc.Write(eventId, d)
}
}
// -----------------------------------------------------------------------------
func wrapResponseWriter(w http.ResponseWriter) *ResponseWriterEx {
return &ResponseWriterEx{w, 0}
}
// Wrapper for ResponseWriter to capture the number of bytes written to it
type ResponseWriterEx struct {
http.ResponseWriter
length int64
}
func (w *ResponseWriterEx) Write(b []byte) (n int, err error) {
n, err = w.ResponseWriter.Write(b)
w.length += int64(n)
return
}
func (w *ResponseWriterEx) Length() int64 {
return w.length
}
func (w *ResponseWriterEx) Flush() {
if flusher, ok := w.ResponseWriter.(http.Flusher); ok {
flusher.Flush()
}
}