forked from fullstorydev/grpcui
/
standalone.go
308 lines (278 loc) · 9.21 KB
/
standalone.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 standalone
import (
"bytes"
"context"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"fmt"
"html/template"
"io"
"io/ioutil"
"mime"
"net/http"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"github.com/jhump/protoreflect/desc"
"github.com/jhump/protoreflect/dynamic/grpcdynamic"
"google.golang.org/grpc"
"github.com/echo-health/grpcui"
"github.com/echo-health/grpcui/internal/resources/standalone"
)
const csrfCookieName = "_grpcui_csrf_token"
const csrfHeaderName = "x-grpcui-csrf-token"
// Handler returns an HTTP handler that provides a fully-functional gRPC web
// UI, including the main index (with the HTML form), all needed CSS and JS
// assets, and the handlers that provide schema metadata and perform RPC
// invocations. The HTML index, CSS, and JS files can be customized and
// augmented with opts.
//
// All RPC invocations are sent to the given channel. The given target is shown
// in the header of the web UI, to show the user where their requests are being
// sent. The given methods enumerate all supported RPC methods, and the given
// files enumerate all known protobuf (for enumerating all supported message
// types, to support the use of google.protobuf.Any messages).
//
// The returned handler expects to serve resources from "/". If it will instead
// be handling a sub-path (e.g. handling "/rpc-ui/") then use http.StripPrefix.
func Handler(ch grpcdynamic.Channel, target string, methods []*desc.MethodDescriptor, files []*desc.FileDescriptor, opts ...HandlerOption) http.Handler {
uiOpts := &handlerOptions{
indexTmpl: defaultIndexTemplate,
css: grpcui.WebFormSampleCSS(),
cssPublic: true,
}
for _, o := range opts {
o.apply(uiOpts)
}
var mux http.ServeMux
// Add go-bindata resources bundled in standalone package TOC
for _, assetName := range standalone.AssetNames() {
// the index file will be handled separately
if assetName == standalone.IndexTemplateName {
continue
}
resourcePath := "/" + assetName
handle(&mux, newResource(resourcePath, standalone.MustAsset(assetName), "", true))
}
// Add resources from WebFormPackage
handle(&mux, newResource("/grpc-web-form.js", grpcui.WebFormScript(), "text/javascript; charset=UTF-8", true))
handle(&mux, newResource("/grpc-web-form.css", uiOpts.css, "text/css; charset=UTF-8", uiOpts.cssPublic))
// Add optional resources to mux
for _, res := range uiOpts.addlServedResources() {
handle(&mux, res)
}
// Add the index page (not bundled in standalone)
formOpts := grpcui.WebFormOptions{
DefaultMetadata: uiOpts.defaultMetadata,
Debug: uiOpts.debug,
}
webFormHTML := grpcui.WebFormContentsWithOptions("invoke", "metadata", methods, formOpts)
indexContents := getIndexContents(uiOpts.indexTmpl, target, webFormHTML, uiOpts.tmplResources)
indexResource := newResource("/", indexContents, "text/html; charset=utf-8", false)
indexResource.MustRevalidate = true
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
indexResource.ServeHTTP(w, r)
} else {
http.NotFound(w, r)
}
})
invokeOpts := grpcui.InvokeOptions{
ExtraMetadata: uiOpts.extraMetadata,
PreserveHeaders: uiOpts.preserveHeaders,
Verbosity: uiOpts.invokeVerbosity,
}
rpcInvokeHandler := http.StripPrefix("/invoke", grpcui.RPCInvokeHandlerWithOptions(ch, methods, invokeOpts))
mux.HandleFunc("/invoke/", func(w http.ResponseWriter, r *http.Request) {
// CSRF protection
c, _ := r.Cookie(csrfCookieName)
h := r.Header.Get(csrfHeaderName)
if c == nil || c.Value == "" || c.Value != h {
http.Error(w, "incorrect CSRF token", http.StatusUnauthorized)
return
}
rpcInvokeHandler.ServeHTTP(w, r)
})
rpcMetadataHandler := grpcui.RPCMetadataHandler(methods, files)
mux.Handle("/metadata", rpcMetadataHandler)
mux.HandleFunc("/examples", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(200)
if len(uiOpts.examples) > 0 {
w.Write(uiOpts.examples)
} else {
w.Write([]byte("[]"))
}
})
// make sure we always have a csrf token cookie
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if _, err := r.Cookie(csrfCookieName); err != nil {
tokenBytes := make([]byte, 32)
if _, err := rand.Read(tokenBytes); err != nil {
http.Error(w, "failed to create CSRF token", http.StatusInternalServerError)
return
}
c := &http.Cookie{
Name: csrfCookieName,
Value: base64.RawURLEncoding.EncodeToString(tokenBytes),
}
http.SetCookie(w, c)
}
mux.ServeHTTP(w, r)
})
}
var defaultIndexTemplate = template.Must(template.New("index.html").Parse(string(standalone.IndexTemplate())))
func getIndexContents(tmpl *template.Template, target string, webFormHTML []byte, addlResources []*resource) []byte {
addlHTML := make([]template.HTML, 0, len(addlResources))
for _, res := range addlResources {
tag := res.AsHTMLTag()
if tag != "" {
addlHTML = append(addlHTML, template.HTML(tag))
}
}
data := WebFormContainerTemplateData{
Target: target,
WebFormContents: template.HTML(webFormHTML),
AddlResources: addlHTML,
}
var indexBuf bytes.Buffer
if err := tmpl.Execute(&indexBuf, data); err != nil {
panic(err)
}
return indexBuf.Bytes()
}
type resource struct {
Path string
Len int
Open func(string) (io.ReadCloser, error)
ContentType string
ETag string
Public bool
MustRevalidate bool
}
func newResource(uriPath string, data []byte, contentType string, public bool) *resource {
if contentType == "" {
contentType = mime.TypeByExtension(path.Ext(uriPath))
}
return &resource{
Path: uriPath,
Open: func(_ string) (io.ReadCloser, error) {
return ioutil.NopCloser(bytes.NewReader(data)), nil
},
Len: len(data),
ContentType: contentType,
ETag: computeETag(data),
Public: public,
}
}
func newDeferredResource(uriPath string, open func() (io.ReadCloser, error), contentType string) *resource {
if contentType == "" {
contentType = mime.TypeByExtension(path.Ext(uriPath))
}
return &resource{
Path: uriPath,
Open: func(_ string) (io.ReadCloser, error) {
return open()
},
ContentType: contentType,
}
}
func newDeferredResourceFolder(uriPath string, open func(string) (io.ReadCloser, error)) *resource {
return &resource{
Path: uriPath + "/",
Open: func(filename string) (io.ReadCloser, error) {
return open(filename)
},
}
}
func handle(mux *http.ServeMux, res *resource) {
mux.Handle(res.Path, res)
if withoutSlash := strings.TrimSuffix(res.Path, "/"); withoutSlash != res.Path {
// if res.Path is a folder, return a 404 if the base directory is
// requested (default behavior is a redirect to URI with trailing slash)
mux.Handle(withoutSlash, http.NotFoundHandler())
}
}
func (res *resource) ServeHTTP(w http.ResponseWriter, r *http.Request) {
name, err := filepath.Rel(res.Path, r.URL.Path)
var reader io.ReadCloser
if err == nil {
reader, err = res.Open(name)
}
if err != nil {
if os.IsNotExist(err) {
http.NotFound(w, r)
} else {
http.Error(w, fmt.Sprintf("failed to open file %q: %v", r.URL.Path, err), http.StatusInternalServerError)
}
return
}
defer func() {
_ = reader.Close()
}()
etag := r.Header.Get("If-None-Match")
if etag != "" && etag == res.ETag {
w.WriteHeader(http.StatusNotModified)
return
}
ct := res.ContentType
if ct == "" {
ct = mime.TypeByExtension(path.Ext(r.URL.Path))
}
if ct != "" {
w.Header().Set("Content-Type", ct)
}
var cacheSuffix string
if res.MustRevalidate {
cacheSuffix = "must-revalidate"
} else {
cacheSuffix = "max-age=3600"
}
if res.Public {
w.Header().Set("Cache-Control", "public, "+cacheSuffix)
} else {
w.Header().Set("Cache-Control", "private, "+cacheSuffix)
}
if res.ETag != "" {
w.Header().Set("ETag", res.ETag)
}
if res.Len > 0 {
w.Header().Set("Content-Length", strconv.Itoa(res.Len))
}
_, _ = io.Copy(w, reader)
}
// AsHTMLTag returns an HTML string corresponding to a tag that would load this resource (by inspecting ContentType).
// Only supports "text/javascript" and "text/css" for ContentType.
// Returns empty string if we do not support the ContentType.
func (res *resource) AsHTMLTag() string {
if strings.HasPrefix(res.ContentType, "text/javascript") {
return fmt.Sprintf("<script src=\"%s\"></script>", strings.TrimLeft(res.Path, "/"))
} else if strings.HasPrefix(res.ContentType, "text/css") {
return fmt.Sprintf("<link rel=\"stylesheet\" href=\"%s\">", strings.TrimLeft(res.Path, "/"))
}
// Fallthrough as a no-op
return ""
}
func computeETag(contents []byte) string {
hasher := sha256.New()
hasher.Write(contents)
return base64.RawURLEncoding.EncodeToString(hasher.Sum(nil))
}
// HandlerViaReflection tries to query the provided connection for all services
// and methods supported by the server, and constructs a handler to serve the UI.
//
// The handler has the same properties as the one returned by Handler.
func HandlerViaReflection(ctx context.Context, cc grpc.ClientConnInterface, target string, opts ...HandlerOption) (http.Handler, error) {
m, err := grpcui.AllMethodsViaReflection(ctx, cc)
if err != nil {
return nil, err
}
f, err := grpcui.AllFilesViaReflection(ctx, cc)
if err != nil {
return nil, err
}
return Handler(cc, target, m, f, opts...), nil
}