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
|
package main
import (
"crypto/tls"
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
)
type domain struct {
Group string
Project string
Config *domainConfig
certificate *tls.Certificate
certificateError error
}
func (d *domain) notFound(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
}
func (d *domain) serveFile(w http.ResponseWriter, r *http.Request, fullPath string) bool {
// Open and serve content of file
file, err := os.Open(fullPath)
if err != nil {
return false
}
defer file.Close()
fi, err := file.Stat()
if err != nil {
return false
}
println("Serving", fullPath, "for", r.URL.Path)
http.ServeContent(w, r, filepath.Base(file.Name()), fi.ModTime(), file)
return true
}
func (d *domain) resolvePath(w http.ResponseWriter, r *http.Request, projectName, subPath string) (fullPath string, err error) {
publicPath := filepath.Join(d.Group, projectName, "public")
fullPath = filepath.Join(publicPath, subPath)
fullPath, err = filepath.EvalSymlinks(fullPath)
if err != nil {
return
}
if !strings.HasPrefix(fullPath, publicPath+"/") && fullPath != publicPath {
err = fmt.Errorf("%q should be in %q", fullPath, publicPath)
return
}
return
}
func (d *domain) checkPath(w http.ResponseWriter, r *http.Request, path string) (fullPath string, err error) {
fullPath = path
fi, err := os.Lstat(fullPath)
if err != nil {
return
}
// If this file is directory, open the index.html
if fi.IsDir() {
fullPath = filepath.Join(fullPath, "index.html")
fi, err = os.Lstat(fullPath)
if err != nil {
return
}
}
// We don't allow to open non-regular files
if !fi.Mode().IsRegular() {
err = fmt.Errorf("%s: is not a regular file", fullPath)
return
}
return
}
func (d *domain) tryFile(w http.ResponseWriter, r *http.Request, projectName, subPath string) bool {
path, err := d.resolvePath(w, r, projectName, subPath)
if err != nil {
return false
}
path, err = d.checkPath(w, r, path)
if err != nil {
return false
}
return d.serveFile(w, r, path)
}
func (d *domain) serveFromGroup(w http.ResponseWriter, r *http.Request) {
// The Path always contains "/" at the beggining
split := strings.SplitN(r.URL.Path, "/", 3)
if len(split) >= 2 {
subPath := ""
if len(split) >= 3 {
subPath = split[2]
}
if d.tryFile(w, r, split[1], subPath) {
return
}
}
if r.Host != "" && d.tryFile(w, r, strings.ToLower(r.Host), r.URL.Path) {
return
}
d.notFound(w, r)
}
func (d *domain) serveFromConfig(w http.ResponseWriter, r *http.Request) {
if d.tryFile(w, r, d.Project, r.URL.Path) {
return
}
d.notFound(w, r)
}
func (d *domain) ensureCertificate() (*tls.Certificate, error) {
if d.Config == nil {
return nil, errors.New("tls certificates can be loaded only for pages with configuration")
}
if d.certificate != nil || d.certificateError != nil {
return d.certificate, d.certificateError
}
tls, err := tls.X509KeyPair([]byte(d.Config.Certificate), []byte(d.Config.Key))
if err != nil {
d.certificateError = err
return nil, err
}
d.certificate = &tls
return d.certificate, nil
}
func (d *domain) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if d.Config != nil {
d.serveFromConfig(w, r)
} else {
d.serveFromGroup(w, r)
}
}
|