-
Notifications
You must be signed in to change notification settings - Fork 4
/
website.go
727 lines (570 loc) · 16.2 KB
/
website.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
package aspen
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime"
"net/http"
"os"
"os/exec"
"path"
"regexp"
"sort"
"strings"
"sync"
)
var (
DefaultCharsetDynamic = "utf-8"
DefaultCharsetStatic = DefaultCharsetDynamic
DefaultContentType = "application/octet-stream"
DefaultIndicesArray = []string{"index.html", "index.json", "index.txt"}
DefaultIndices = strings.Join(DefaultIndicesArray, ",")
DefaultConfig = &WebsiteConfigurer{}
initialized = false
websites = map[string]*Website{}
protoWebsite = &Website{
PackageName: DefaultGenPackage,
WwwRoot: ".",
CharsetDynamic: DefaultCharsetDynamic,
CharsetStatic: DefaultCharsetStatic,
DefaultContentType: DefaultContentType,
Indices: DefaultIndicesArray,
ListDirs: false,
Debug: false,
}
)
type Website struct {
PackageName string
WwwRoot string
CharsetDynamic string
CharsetStatic string
DefaultContentType string
Indices []string
ListDirs bool
Debug bool
configured bool
s *serverContext
ph *websitePipelineHandler
}
type pipelineHandler interface {
http.Handler
NextHandler() pipelineHandler
String() string
}
type websitePipelineHandler struct {
w *Website
nh pipelineHandler
r map[string]*handlerFuncRegistration
l sync.RWMutex
patternHandler *websitePatternHandler
strMatchHandler *websiteStringMatchHandler
}
type websiteStringMatchHandler struct {
w *Website
nh pipelineHandler
r map[string]*handlerFuncRegistration
l sync.RWMutex
}
type websitePatternHandler struct {
w *Website
nh pipelineHandler
r map[string]*handlerFuncRegistration
c map[string]*regexp.Regexp
l sync.RWMutex
}
type WebsiteConfigurer struct{}
func EnsureInitialized() *Website {
if initialized {
return protoWebsite
}
if len(os.Getenv("__ASPEN_GO_PARENT_PROCESS")) > 0 {
return protoWebsite
}
configScripts := os.Getenv("ASPEN_GO_CONFIGURATION_SCRIPTS")
if len(configScripts) > 0 {
*(&protoWebsite) = loadProtoWebsite(configScripts, protoWebsite)
}
*(&initialized) = true
return protoWebsite
}
func DeclareWebsite(packageName string) *Website {
if w, ok := websites[packageName]; ok {
return w
}
newSite := &Website{
PackageName: packageName,
WwwRoot: protoWebsite.WwwRoot,
CharsetDynamic: protoWebsite.CharsetDynamic,
CharsetStatic: protoWebsite.CharsetStatic,
Indices: protoWebsite.Indices,
ListDirs: protoWebsite.ListDirs,
Debug: protoWebsite.Debug,
}
staticHandler := &websiteStaticHandler{
w: newSite,
}
patternHandler := &websitePatternHandler{
w: newSite,
r: map[string]*handlerFuncRegistration{},
c: map[string]*regexp.Regexp{},
nh: staticHandler,
}
strMatchHandler := &websiteStringMatchHandler{
w: newSite,
r: map[string]*handlerFuncRegistration{},
nh: patternHandler,
}
ph := &websitePipelineHandler{
w: newSite,
nh: strMatchHandler,
}
ph.patternHandler = patternHandler
ph.strMatchHandler = strMatchHandler
newSite.ph = ph
websites[packageName] = newSite
return newSite
}
func loadProtoWebsite(configScripts string, proto *Website) *Website {
var err error
scripts := strings.Split(configScripts, ",")
w := proto
for _, script := range scripts {
w, err = loadWebsiteFromScript(strings.TrimSpace(script), w)
if err != nil {
fmt.Fprintf(os.Stderr, "aspen: CONFIG ERROR: %v\n", err)
}
}
return w
}
func loadWebsiteFromScript(script string, w *Website) (*Website, error) {
encoded, err := json.Marshal(w)
if err != nil {
return nil, err
}
cmd := exec.Command("go", "run", script)
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, fmt.Sprintf("__ASPEN_GO_PARENT_PROCESS=%d", os.Getpid()))
cmd.Stderr = os.Stderr
inbuf, err := cmd.StdinPipe()
if err != nil {
return nil, err
}
outbuf, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
err = cmd.Start()
if err != nil {
return nil, err
}
_, err = inbuf.Write(encoded)
if err != nil {
cmd.Wait()
return nil, err
}
err = inbuf.Close()
if err != nil {
cmd.Wait()
return nil, err
}
outbytes, err := ioutil.ReadAll(outbuf)
if err != nil {
cmd.Wait()
return nil, err
}
err = json.Unmarshal(outbytes, w)
if err != nil {
cmd.Wait()
return nil, err
}
debugf("Loaded website from config script %v: %+v", script, w)
err = cmd.Wait()
if err != nil {
fmt.Fprintf(os.Stderr, "aspen: CONFIG ERROR: %v\n", err)
}
return w, nil
}
func (me *Website) NewHTTPResponseWrapper(w http.ResponseWriter, req *http.Request) *HTTPResponseWrapper {
return &HTTPResponseWrapper{
website: me,
w: w,
req: req,
statusCode: http.StatusOK,
bodyBytes: []byte(""),
contentType: "text/html",
contentTypeHandlers: make(map[string]func(*HTTPResponseWrapper)),
err: nil,
}
}
func (me *Website) RegisterSimplate(simplateType, siteRoot, requestPath string,
handler http.HandlerFunc) *handlerFuncRegistration {
return me.ph.NewHandlerFuncRegistration(requestPath,
simplateType, handler, false)
}
func (me *websitePipelineHandler) NewHandlerFuncRegistration(requestPath,
simplateType string, handler http.HandlerFunc, isDir bool) *handlerFuncRegistration {
debugf("NewHandlerFuncRegistration(%q, %q, <func>, %v)", requestPath, simplateType, isDir)
isVirtual := vPathPart.MatchString(requestPath)
debugf("Setting `Virtual` to %v for %q", isVirtual, requestPath)
if simplateType == SimplateTypeNegotiated {
// directly add a 404 for the non-pattern path, which may be tho wrong
// behavior, but at least we aren't serving the simplate source.
me.strMatchHandler.AddHandlerFuncReg(requestPath,
&handlerFuncRegistration{
RequestPath: requestPath,
HandlerFunc: serve404,
})
return me.patternHandler.NewHandlerFuncRegistration(requestPath,
simplateType, handler, isDir, isVirtual)
}
if isVirtual {
return me.patternHandler.NewHandlerFuncRegistration(requestPath,
simplateType, handler, isDir, isVirtual)
}
return me.strMatchHandler.NewHandlerFuncRegistration(requestPath,
simplateType, handler, isDir)
}
func (me *websitePatternHandler) NewHandlerFuncRegistration(requestPath,
simplateType string, handler http.HandlerFunc,
isDir, isVirtual bool) *handlerFuncRegistration {
debugf("Pattern handler checking if %q can be registered", requestPath)
if len(requestPath) < 1 {
panic(fmt.Errorf("Invalid request path %q", requestPath))
}
requestPathPattern := requestPath
if isVirtual {
requestPathPattern = virtualToRegexp(requestPath)
}
if simplateType == SimplateTypeNegotiated {
pathRegexp := requestPathPattern + "\\.[^\\.]+"
debugf("Registering %q as a negotiated simplate", pathRegexp)
me.AddHandlerFuncReg(requestPath, &handlerFuncRegistration{
RequestPath: pathRegexp,
HandlerFunc: handler,
Negotiated: true,
Virtual: isVirtual,
Regexp: true,
w: me.w,
})
return me.HandlerFuncAt(requestPath)
}
me.AddHandlerFuncReg(requestPath, &handlerFuncRegistration{
RequestPath: requestPathPattern,
HandlerFunc: handler,
Virtual: isVirtual,
Negotiated: simplateType == SimplateTypeNegotiated,
Regexp: isVirtual,
w: me.w,
})
return me.HandlerFuncAt(requestPath)
}
func (me *websiteStringMatchHandler) NewHandlerFuncRegistration(requestPath,
simplateType string, handler http.HandlerFunc,
isDir bool) *handlerFuncRegistration {
if simplateType == SimplateTypeNegotiated {
debugf("Ignoring negotiated simplate registration for %q", requestPath)
return nil
}
pathBase := path.Base(requestPath)
pathDir := path.Dir(requestPath)
debugf("Checking if %q matches any of %v", pathBase, me.w.Indices)
var reg *handlerFuncRegistration
for _, idx := range me.w.Indices {
if pathBase == idx {
reqPath := pathDir + "/"
reg = &handlerFuncRegistration{
RequestPath: reqPath,
HandlerFunc: handler,
w: me.w,
}
debugf("Registering %q with same handler as %q", reqPath, pathBase)
me.AddHandlerFuncReg(pathDir, &handlerFuncRegistration{
RequestPath: pathDir,
HandlerFunc: func(w http.ResponseWriter, req *http.Request) {
h := http.RedirectHandler(reqPath, http.StatusMovedPermanently)
h.ServeHTTP(w, req)
},
w: me.w,
})
me.AddHandlerFuncReg(reqPath, reg)
}
}
return reg
}
func virtualToRegexp(requestPath string) string {
return vPathPart.ReplaceAllString(requestPath, vPathPartRep)
}
func (me *websitePipelineHandler) registerSpecialCases() {
idxPath := "/" + SiteIndexFilename
debugf("Registering special case of %q -> 404", idxPath)
me.strMatchHandler.AddHandlerFuncReg(idxPath, &handlerFuncRegistration{
RequestPath: idxPath,
HandlerFunc: serve404,
})
}
func (me *websitePipelineHandler) registerSelfAtRoot() {
debugf(`Registering pipeline handler at "/"`)
http.Handle("/", me)
}
func (me *Website) Configure(serverBind, wwwRoot, charsetDynamic,
charsetStatic, indices string, debug, listDirs bool) {
debugf("website.Configure(%q, %q, %q, %q, %q, %v, %v)", serverBind, wwwRoot,
charsetDynamic, charsetStatic, indices, debug, listDirs)
me.WwwRoot = wwwRoot
me.CharsetDynamic = charsetDynamic
me.CharsetStatic = charsetStatic
me.ListDirs = listDirs
me.Debug = debug
sortedIndices := make([]string, len(me.Indices))
copy(sortedIndices, me.Indices)
sort.Strings(sortedIndices)
for _, part := range strings.Split(indices, ",") {
trimmed := strings.TrimSpace(part)
if sort.SearchStrings(sortedIndices, trimmed) > -1 {
debugf("*NOT* appending duplicate index name %q into %v",
trimmed, me.Indices)
} else {
debugf("Adding index name %q to %v", trimmed, me.Indices)
me.Indices = append(me.Indices, trimmed)
}
}
if me.s == nil {
me.s = newServerContext(me,
me.PackageName, serverBind, me.WwwRoot, debug)
}
me.configured = true
}
func (me *websitePipelineHandler) NextHandler() pipelineHandler {
return me.nh
}
func (me *websitePipelineHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
me.injectCustomHeaders(req)
h := me.NextHandler()
if h != nil {
debugf("Pipeline handler sending %q to %s", req.URL.Path, h)
h.ServeHTTP(w, req)
}
}
func (me *websitePipelineHandler) String() string {
return fmt.Sprintf("*websitePipelineHandler{"+
"patternHandler: %s, "+
"strMatchHandler: %s, "+
"r: %+v}", me.patternHandler, me.strMatchHandler, me.r)
}
func (me *websitePipelineHandler) injectCustomHeaders(req *http.Request) {
me.updateNegType(req, req.URL.Path)
req.Header.Set("X-AspenGo-PackageName", me.w.PackageName)
req.Header.Set("X-AspenGo-WwwRoot", me.w.WwwRoot)
req.Header.Set("X-AspenGo-CharsetStatic", me.w.CharsetStatic)
req.Header.Set("X-AspenGo-CharsetDynamic", me.w.CharsetDynamic)
}
func (me *websitePipelineHandler) updateNegType(req *http.Request, filename string) {
mediaType := mime.TypeByExtension(path.Ext(filename))
if len(mediaType) == 0 {
mediaType = me.w.DefaultContentType
}
req.Header.Set(internalAcceptHeader, mediaType)
}
func (me *websitePatternHandler) NextHandler() pipelineHandler {
return me.nh
}
func (me *websitePatternHandler) AddHandlerFuncReg(requestPath string,
r *handlerFuncRegistration) {
if r.Negotiated && !r.Regexp {
debugf("Intercepting non-regexp negotiated registration for %q, "+
"replacing with 404 handler", requestPath)
r = &handlerFuncRegistration{
RequestPath: requestPath,
HandlerFunc: serve404,
w: me.w,
}
}
debugf("Adding handler func registration for %q: %+v", requestPath, r)
me.l.RLock()
defer me.l.RUnlock()
if _, ok := me.r[requestPath]; ok {
debugf("Ignoring additional registration for %q", requestPath)
return
}
me.c[requestPath] = regexp.MustCompile(r.RequestPath)
debugf("Setting handler for %q", requestPath)
me.r[requestPath] = r
}
func (me *websitePatternHandler) HandlerFuncAt(requestPath string) *handlerFuncRegistration {
if r, ok := me.r[requestPath]; ok {
return r
}
return nil
}
func (me *websitePatternHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
debugf("Pattern handler looking for registration that matches %q", req.URL.Path)
// Loop through the non-regexp request paths and their registrations.
for requestPath, reg := range me.r {
// Get the compiled regexp for the registered request path, and if the
// incoming request URL Path matches, call the regitration's HandlerFunc.
re := me.c[requestPath]
if re.MatchString(req.URL.Path) {
reg.HandlerFunc(w, req)
return
}
}
h := me.NextHandler()
if h != nil {
debugf("Pattern handler falling through to %s", h)
h.ServeHTTP(w, req)
return
}
debugf("Pattern handler falling through to 404 because next handler is %v", h)
serve404(w, req)
}
func (me *websitePatternHandler) String() string {
return fmt.Sprintf("*websitePatternHandler{r: %v}", me.r)
}
func (me *websitePatternHandler) findVpathRegexp(vPathString string) *regexp.Regexp {
if re, ok := me.c[vPathString]; ok {
return re
}
return nil
}
func (me *websiteStringMatchHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
debugf("String match handler looking for registration that matches %q",
req.URL.Path)
reg := me.match(req.URL.Path)
if reg != nil {
debugf("String match handler found match! %+v", reg)
reg.HandlerFunc(w, req)
return
}
h := me.NextHandler()
if h != nil {
debugf("String match handler falling through to %+v", h)
h.ServeHTTP(w, req)
return
}
debugf("String match handler falling through to 404")
serve404(w, req)
return
}
func (me *websiteStringMatchHandler) String() string {
return fmt.Sprintf("*websiteStringMatchHandler{r: %v}", me.r)
}
func (me *websiteStringMatchHandler) NextHandler() pipelineHandler {
return me.nh
}
func (me *websiteStringMatchHandler) AddHandlerFuncReg(requestPath string,
reg *handlerFuncRegistration) {
me.l.RLock()
defer me.l.RUnlock()
debugf("String match handler adding func reg at %q: %+v",
requestPath, reg)
me.r[requestPath] = reg
}
func (me *websiteStringMatchHandler) match(requestPath string) *handlerFuncRegistration {
var h *handlerFuncRegistration
n := 0
for k, v := range me.r {
if !pathMatch(k, requestPath) {
continue
}
if h == nil || len(k) > n {
n = len(k)
h = v
}
}
debugf("String match handler 'match' returning %+v", h)
return h
}
func (me *Website) UpdateContextFromVirtualPaths(ctx *map[string]interface{},
requestPath, vPathString string) {
// FIXME Demeter!
vPath := me.ph.patternHandler.findVpathRegexp(vPathString)
if vPath == nil {
debugf("No matching regexp for vpath %q. Not updating context.",
vPathString)
return
}
matches := vPath.FindStringSubmatch(requestPath)
if len(matches) == 0 {
debugf("Request path %q does not match %q. Not updating context.",
requestPath, vPath.String())
return
}
realCtx := *ctx
names := vPath.SubexpNames()
for i, match := range matches {
if len(names[i]) > 0 {
realCtx[names[i]] = match
}
}
}
func (me *Website) RunServer() error {
if !me.configured {
return fmt.Errorf("Can't run the server when we aren't configured!")
}
me.ph.registerSpecialCases()
me.ph.registerSelfAtRoot()
if isDebug {
debugf("Website about to run server with pipeline:\n\t%s", me.ph)
debugf("String matches registered:")
for m, _ := range me.ph.strMatchHandler.r {
debugf(" %s", m)
}
debugf("Patterns registered:")
for _, re := range me.ph.patternHandler.c {
debugf(" %s", re)
}
}
return me.s.Run()
}
func (me *Website) DebugNewRequest(simplatePath string, req *http.Request) {
debugf("%q handling new request %q", simplatePath, req.URL)
}
func (me *WebsiteConfigurer) Load(r io.Reader) (*Website, error) {
if r == nil {
r = os.Stdin
}
raw, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
w := &Website{}
err = json.Unmarshal(raw, w)
if err != nil {
return nil, err
}
return w, nil
}
func (me *WebsiteConfigurer) Dump(website *Website, w io.Writer) error {
if w == nil {
w = os.Stdout
}
encoded, err := json.Marshal(website)
if err != nil {
return err
}
_, err = w.Write(encoded)
if err != nil {
return err
}
return nil
}
func (me *WebsiteConfigurer) MustLoad(r io.Reader) *Website {
w, err := me.Load(r)
if err != nil {
panic(err)
}
return w
}
func (me *WebsiteConfigurer) MustDump(website *Website, w io.Writer) {
err := me.Dump(website, w)
if err != nil {
panic(err)
}
}
func MustLoadWebsite() *Website {
return DefaultConfig.MustLoad(os.Stdin)
}
func MustDumpWebsite(w *Website) {
DefaultConfig.MustDump(w, os.Stdout)
}