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
|
package main
import (
"crypto/tls"
"errors"
"fmt"
"io"
"mime"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"gitlab.com/gitlab-org/gitlab-pages/internal/httputil"
)
type domain struct {
Group string
Project string
Config *domainConfig
certificate *tls.Certificate
certificateError error
}
func acceptsGZip(r *http.Request) bool {
if r.Header.Get("Range") != "" {
return false
}
offers := []string{"gzip", "identity"}
acceptedEncoding := httputil.NegotiateContentEncoding(r, offers)
return acceptedEncoding == "gzip"
}
func (d *domain) serveFile(w http.ResponseWriter, r *http.Request, fullPath string) error {
// Open and serve content of file
if acceptsGZip(r) {
if fi, err := os.Lstat(fullPath + ".gz"); err == nil && fi.Mode().IsRegular() {
// Set the content type based on the non-gzipped extension
_, haveType := w.Header()["Content-Type"]
if !haveType {
ctype := mime.TypeByExtension(filepath.Ext(fullPath))
w.Header().Set("Content-Type", ctype)
}
// Serve up the gzipped version
fullPath += ".gz"
w.Header().Set("Content-Encoding", "gzip")
}
}
file, err := os.Open(fullPath)
if err != nil {
return err
}
defer file.Close()
fi, err := file.Stat()
if err != nil {
return err
}
fmt.Println("Serving", fullPath, "for", r.URL.Path)
http.ServeContent(w, r, filepath.Base(file.Name()), fi.ModTime(), file)
return nil
}
func (d *domain) serveCustomFile(w http.ResponseWriter, r *http.Request, code int, fullPath string) error {
// Open and serve content of file
ext := filepath.Ext(fullPath)
if acceptsGZip(r) {
if fi, err := os.Lstat(fullPath + ".gz"); err == nil && fi.Mode().IsRegular() {
// Serve up the gzipped version
fullPath += ".gz"
w.Header().Set("Content-Encoding", "gzip")
}
}
file, err := os.Open(fullPath)
if err != nil {
return err
}
defer file.Close()
fi, err := file.Stat()
if err != nil {
return err
}
fmt.Println("Serving", fullPath, "for", r.URL.Path, "with", code)
// Serve the file
_, haveType := w.Header()["Content-Type"]
if !haveType {
ctype := mime.TypeByExtension(ext)
w.Header().Set("Content-Type", ctype)
}
w.Header().Set("Content-Length", strconv.FormatInt(fi.Size(), 10))
w.WriteHeader(code)
if r.Method != "HEAD" {
io.CopyN(w, file, fi.Size())
}
return nil
}
func (d *domain) resolvePath(projectName string, subPath ...string) (fullPath string, err error) {
publicPath := filepath.Join(d.Group, projectName, "public")
fullPath = filepath.Join(publicPath, filepath.Join(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) tryNotFound(w http.ResponseWriter, r *http.Request, projectName string) error {
page404, err := d.resolvePath(projectName, "404.html")
if err != nil {
return err
}
// Make sure that file is not symlink
fi, err := os.Lstat(page404)
if err != nil && !fi.Mode().IsRegular() {
return err
}
err = d.serveCustomFile(w, r, http.StatusNotFound, page404)
if err != nil {
return err
}
return nil
}
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
}
switch {
// If the URL doesn't end with /, send location to client
case fi.IsDir() && !endsWithSlash(r.URL.Path):
newURL := *r.URL
newURL.Path += "/"
http.Redirect(w, r, newURL.String(), 302)
// If this is directory, we try the index.html
case fi.IsDir():
fullPath = filepath.Join(fullPath, "index.html")
fi, err = os.Lstat(fullPath)
if err != nil {
return
}
// We don't allow to open the regular file
case !fi.Mode().IsRegular():
err = fmt.Errorf("%s: is not a regular file", fullPath)
}
return
}
func (d *domain) tryFile(w http.ResponseWriter, r *http.Request, projectName string, subPath ...string) error {
path, err := d.resolvePath(projectName, subPath...)
if err != nil {
return err
}
path, err = d.checkPath(w, r, path)
if err != nil {
return err
}
return d.serveFile(w, r, path)
}
func (d *domain) serveFromGroup(w http.ResponseWriter, r *http.Request) {
// The Path always contains "/" at the beginning
split := strings.SplitN(r.URL.Path, "/", 3)
// Try to serve file for http://group.example.com/subpath/... => /group/subpath/...
if len(split) >= 2 && d.tryFile(w, r, split[1], split[2:]...) == nil {
return
}
// Try to serve file for http://group.example.com/... => /group/group.example.com/...
if r.Host != "" && d.tryFile(w, r, strings.ToLower(r.Host), r.URL.Path) == nil {
return
}
// Try serving not found page for http://group.example.com/subpath/ => /group/subpath/404.html
if len(split) >= 2 && d.tryNotFound(w, r, split[1]) == nil {
return
}
// Try serving not found page for http://group.example.com/ => /group/group.example.com/404.html
if r.Host != "" && d.tryNotFound(w, r, strings.ToLower(r.Host)) == nil {
return
}
// Serve generic not found
serve404(w)
}
func (d *domain) serveFromConfig(w http.ResponseWriter, r *http.Request) {
// Try to serve file for http://host/... => /group/project/...
if d.tryFile(w, r, d.Project, r.URL.Path) == nil {
return
}
// Try serving not found page for http://host/ => /group/project/404.html
if d.tryNotFound(w, r, d.Project) == nil {
return
}
// Serve generic not found
serve404(w)
}
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)
}
}
|