forked from perkeep/perkeep
/
camweb.go
541 lines (476 loc) · 15.4 KB
/
camweb.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
/*
Copyright 2011 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"bytes"
"flag"
"fmt"
"html/template"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
txttemplate "text/template"
"time"
"camlistore.org/pkg/deploy/gce"
"camlistore.org/pkg/netutil"
"camlistore.org/pkg/types/camtypes"
)
const defaultAddr = ":31798" // default webserver address
var h1TitlePattern = regexp.MustCompile(`<h1>([^<]+)</h1>`)
var (
httpAddr = flag.String("http", defaultAddr, "HTTP service address (e.g., '"+defaultAddr+"')")
httpsAddr = flag.String("https", "", "HTTPS service address")
root = flag.String("root", "", "Website root (parent of 'static', 'content', and 'tmpl")
logDir = flag.String("logdir", "", "Directory to write log files to (one per hour), or empty to not log.")
logStdout = flag.Bool("logstdout", true, "Write to stdout?")
tlsCertFile = flag.String("tlscert", "", "TLS cert file")
tlsKeyFile = flag.String("tlskey", "", "TLS private key file")
buildbotBackend = flag.String("buildbot_backend", "", "Build bot status backend URL")
buildbotHost = flag.String("buildbot_host", "", "Hostname to map to the buildbot_backend. If an HTTP request with this hostname is received, it proxies to buildbot_backend.")
alsoRun = flag.String("also_run", "", "Optional path to run as a child process. (Used to run camlistore.org's ./scripts/run-blob-server)")
pageHTML, errorHTML, camliErrorHTML *template.Template
packageHTML *txttemplate.Template
)
var fmap = template.FuncMap{
"": textFmt,
"html": htmlFmt,
"htmlesc": htmlEscFmt,
}
// Template formatter for "" (default) format.
func textFmt(w io.Writer, format string, x ...interface{}) string {
writeAny(w, false, x[0])
return ""
}
// Template formatter for "html" format.
func htmlFmt(w io.Writer, format string, x ...interface{}) string {
writeAny(w, true, x[0])
return ""
}
// Template formatter for "htmlesc" format.
func htmlEscFmt(w io.Writer, format string, x ...interface{}) string {
var buf bytes.Buffer
writeAny(&buf, false, x[0])
template.HTMLEscape(w, buf.Bytes())
return ""
}
// Write anything to w; optionally html-escaped.
func writeAny(w io.Writer, html bool, x interface{}) {
switch v := x.(type) {
case []byte:
writeText(w, v, html)
case string:
writeText(w, []byte(v), html)
default:
if html {
var buf bytes.Buffer
fmt.Fprint(&buf, x)
writeText(w, buf.Bytes(), true)
} else {
fmt.Fprint(w, x)
}
}
}
// Write text to w; optionally html-escaped.
func writeText(w io.Writer, text []byte, html bool) {
if html {
template.HTMLEscape(w, text)
return
}
w.Write(text)
}
func applyTemplate(t *template.Template, name string, data interface{}) []byte {
var buf bytes.Buffer
if err := t.Execute(&buf, data); err != nil {
log.Printf("%s.Execute: %s", name, err)
}
return buf.Bytes()
}
func servePage(w http.ResponseWriter, title, subtitle string, content []byte) {
// insert an "install command" if it applies
if strings.Contains(title, cmdPattern) && subtitle != cmdPattern {
toInsert := `
<h3>Installation</h3>
<pre>go get camlistore.org/cmd/` + subtitle + `</pre>
<h3>Overview</h3><p>`
content = bytes.Replace(content, []byte("<p>"), []byte(toInsert), 1)
}
d := struct {
Title string
Subtitle string
Content template.HTML
}{
title,
subtitle,
template.HTML(content),
}
if err := pageHTML.ExecuteTemplate(w, "page", &d); err != nil {
log.Printf("godocHTML.Execute: %s", err)
}
}
func readTemplate(name string) *template.Template {
fileName := filepath.Join(*root, "tmpl", name)
data, err := ioutil.ReadFile(fileName)
if err != nil {
log.Fatalf("ReadFile %s: %v", fileName, err)
}
t, err := template.New(name).Funcs(fmap).Parse(string(data))
if err != nil {
log.Fatalf("%s: %v", fileName, err)
}
return t
}
func readTemplates() {
pageHTML = readTemplate("page.html")
errorHTML = readTemplate("error.html")
camliErrorHTML = readTemplate("camlierror.html")
// TODO(mpl): see about not using text template anymore?
packageHTML = readTextTemplate("package.html")
}
func serveError(w http.ResponseWriter, r *http.Request, relpath string, err error) {
contents := applyTemplate(errorHTML, "errorHTML", err) // err may contain an absolute path!
w.WriteHeader(http.StatusNotFound)
servePage(w, "File "+relpath, "", contents)
}
const gerritURLPrefix = "https://camlistore.googlesource.com/camlistore/+/"
var commitHash = regexp.MustCompile(`^p=camlistore.git;a=commit;h=([0-9a-f]+)$`)
// empty return value means don't redirect.
func redirectPath(u *url.URL) string {
// Example:
// /code/?p=camlistore.git;a=commit;h=b0d2a8f0e5f27bbfc025a96ec3c7896b42d198ed
if strings.HasPrefix(u.Path, "/code/") {
m := commitHash.FindStringSubmatch(u.RawQuery)
if len(m) == 2 {
return gerritURLPrefix + m[1]
}
}
if strings.HasPrefix(u.Path, "/gw/") {
path := strings.TrimPrefix(u.Path, "/gw/")
if strings.HasPrefix(path, "doc") || strings.HasPrefix(path, "clients") {
return gerritURLPrefix + "master/" + path
}
// Assume it's a commit
return gerritURLPrefix + path
}
return ""
}
func mainHandler(rw http.ResponseWriter, req *http.Request) {
if target := redirectPath(req.URL); target != "" {
http.Redirect(rw, req, target, http.StatusFound)
return
}
if dest, ok := issueRedirect(req.URL.Path); ok {
http.Redirect(rw, req, dest, http.StatusFound)
return
}
relPath := req.URL.Path[1:] // serveFile URL paths start with '/'
if strings.Contains(relPath, "..") {
return
}
absPath := filepath.Join(*root, "content", relPath)
fi, err := os.Lstat(absPath)
if err != nil {
log.Print(err)
serveError(rw, req, relPath, err)
return
}
if fi.IsDir() {
relPath += "/index.html"
absPath = filepath.Join(*root, "content", relPath)
fi, err = os.Lstat(absPath)
if err != nil {
log.Print(err)
serveError(rw, req, relPath, err)
return
}
}
if !fi.IsDir() {
if checkLastModified(rw, req, fi.ModTime()) {
return
}
serveFile(rw, req, relPath, absPath)
}
}
// modtime is the modification time of the resource to be served, or IsZero().
// return value is whether this request is now complete.
func checkLastModified(w http.ResponseWriter, r *http.Request, modtime time.Time) bool {
if modtime.IsZero() {
return false
}
// The Date-Modified header truncates sub-second precision, so
// use mtime < t+1s instead of mtime <= t to check for unmodified.
if t, err := time.Parse(http.TimeFormat, r.Header.Get("If-Modified-Since")); err == nil && modtime.Before(t.Add(1*time.Second)) {
h := w.Header()
delete(h, "Content-Type")
delete(h, "Content-Length")
w.WriteHeader(http.StatusNotModified)
return true
}
w.Header().Set("Last-Modified", modtime.UTC().Format(http.TimeFormat))
return false
}
func serveFile(rw http.ResponseWriter, req *http.Request, relPath, absPath string) {
data, err := ioutil.ReadFile(absPath)
if err != nil {
serveError(rw, req, absPath, err)
return
}
title := ""
if m := h1TitlePattern.FindSubmatch(data); len(m) > 1 {
title = string(m[1])
}
servePage(rw, title, "", data)
}
func isBot(r *http.Request) bool {
agent := r.Header.Get("User-Agent")
return strings.Contains(agent, "Baidu") || strings.Contains(agent, "bingbot") ||
strings.Contains(agent, "Ezooms") || strings.Contains(agent, "Googlebot")
}
type noWwwHandler struct {
Handler http.Handler
}
func (h *noWwwHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
// Some bots (especially Baidu) don't seem to respect robots.txt and swamp gitweb.cgi,
// so explicitly protect it from bots.
if ru := r.URL.RequestURI(); strings.Contains(ru, "/code/") && strings.Contains(ru, "?") && isBot(r) {
http.Error(rw, "bye", http.StatusUnauthorized)
log.Printf("bot denied")
return
}
host := strings.ToLower(r.Host)
if host == "www.camlistore.org" {
scheme := "https"
if r.TLS == nil {
scheme = "http"
}
http.Redirect(rw, r, scheme+"://camlistore.org"+r.URL.RequestURI(), http.StatusFound)
return
}
h.Handler.ServeHTTP(rw, r)
}
// runAsChild runs res as a child process and
// does not wait for it to finish.
func runAsChild(res string) {
cmdName, err := exec.LookPath(res)
if err != nil {
log.Fatalf("Could not find %v in $PATH: %v", res, err)
}
cmd := exec.Command(cmdName)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
log.Printf("Running %v", res)
if err := cmd.Start(); err != nil {
log.Fatalf("Program %v failed to start: %v", res, err)
}
go func() {
if err := cmd.Wait(); err != nil {
log.Fatalf("Program %s did not end successfully: %v", res, err)
}
}()
}
// gceDeployHandler conditionally returns an http.Handler for a GCE launcher,
// configured to run at /prefix/ (the trailing slash can be omitted).
// If CAMLI_GCE_CLIENTID is not set, the launcher-config.json file, if present,
// is used instead of environment variables to initialize the launcher. If a
// launcher isn't enabled, gceDeployHandler returns nil. If another error occurs,
// log.Fatal is called.
func gceDeployHandler(prefix string) http.Handler {
hostPort, err := netutil.HostPort("https://" + *httpsAddr)
if err != nil {
hostPort = "camlistore.org:443"
}
var gceh http.Handler
if e := os.Getenv("CAMLI_GCE_CLIENTID"); e != "" {
gceh, err = gce.NewDeployHandler(hostPort, prefix)
} else {
config := filepath.Join(*root, "launcher-config.json")
if _, err := os.Stat(config); err != nil {
if os.IsNotExist(err) {
return nil
}
log.Fatalf("Could not stat launcher-config.json: %v", err)
}
gceh, err = gce.NewDeployHandlerFromConfig(hostPort, prefix, config)
}
if err != nil {
log.Fatalf("Error initializing gce deploy handler: %v", err)
}
pageBytes, err := ioutil.ReadFile(filepath.Join(*root, "tmpl", "page.html"))
if err != nil {
log.Fatalf("Error initializing gce deploy handler: %v", err)
}
if err := gceh.(*gce.DeployHandler).AddTemplateTheme(string(pageBytes)); err != nil {
log.Fatalf("Error initializing gce deploy handler: %v", err)
}
log.Printf("Starting Camlistore launcher on https://%s%s", hostPort, prefix)
return gceh
}
func main() {
flag.Parse()
if *root == "" {
var err error
*root, err = os.Getwd()
if err != nil {
log.Fatalf("Failed to getwd: %v", err)
}
}
readTemplates()
mux := http.DefaultServeMux
mux.Handle("/favicon.ico", http.FileServer(http.Dir(filepath.Join(*root, "static"))))
mux.Handle("/robots.txt", http.FileServer(http.Dir(filepath.Join(*root, "static"))))
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(filepath.Join(*root, "static")))))
mux.Handle("/talks/", http.StripPrefix("/talks/", http.FileServer(http.Dir(filepath.Join(*root, "talks")))))
mux.Handle(pkgPattern, godocHandler{})
mux.Handle(cmdPattern, godocHandler{})
mux.HandleFunc(errPattern, errHandler)
mux.HandleFunc("/r/", gerritRedirect)
mux.HandleFunc("/debugz/ip", ipHandler)
mux.Handle("/docs/contributing", redirTo("/code#contributing"))
mux.Handle("/lists", redirTo("/community"))
mux.HandleFunc("/contributors", contribHandler())
mux.HandleFunc("/", mainHandler)
if *buildbotHost != "" && *buildbotBackend != "" {
buildbotUrl, err := url.Parse(*buildbotBackend)
if err != nil {
log.Fatalf("Failed to parse %v as a URL: %v", *buildbotBackend, err)
}
buildbotHandler := httputil.NewSingleHostReverseProxy(buildbotUrl)
bbhpattern := strings.TrimRight(*buildbotHost, "/") + "/"
mux.Handle(bbhpattern, buildbotHandler)
}
if *httpsAddr != "" {
if launcher := gceDeployHandler("/launch/"); launcher != nil {
mux.Handle("/launch/", launcher)
}
}
var handler http.Handler = &noWwwHandler{Handler: mux}
if *logDir != "" || *logStdout {
handler = NewLoggingHandler(handler, *logDir, *logStdout)
}
errc := make(chan error)
startEmailCommitLoop(errc)
if *alsoRun != "" {
runAsChild(*alsoRun)
}
httpServer := &http.Server{
Addr: *httpAddr,
Handler: handler,
ReadTimeout: 5 * time.Minute,
WriteTimeout: 30 * time.Minute,
}
go func() {
errc <- httpServer.ListenAndServe()
}()
if *httpsAddr != "" {
log.Printf("Starting TLS server on %s", *httpsAddr)
httpsServer := new(http.Server)
*httpsServer = *httpServer
httpsServer.Addr = *httpsAddr
go func() {
errc <- httpsServer.ListenAndServeTLS(*tlsCertFile, *tlsKeyFile)
}()
}
log.Fatalf("Serve error: %v", <-errc)
}
var issueNum = regexp.MustCompile(`^/(?:issue|bug)s?(/\d*)?$`)
// issueRedirect returns whether the request should be redirected to the
// issues tracker, and the url for that redirection if yes, the empty
// string otherwise.
func issueRedirect(urlPath string) (string, bool) {
m := issueNum.FindStringSubmatch(urlPath)
if m == nil {
return "", false
}
issueNumber := strings.TrimPrefix(m[1], "/")
suffix := ""
if issueNumber != "" {
suffix = "/" + issueNumber
}
return "https://github.com/camlistore/camlistore/issues" + suffix, true
}
func gerritRedirect(w http.ResponseWriter, r *http.Request) {
dest := "https://camlistore-review.googlesource.com/"
if len(r.URL.Path) > len("/r/") {
dest += r.URL.Path[1:]
}
http.Redirect(w, r, dest, http.StatusFound)
}
func redirTo(dest string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, dest, http.StatusFound)
})
}
// Not sure what's making these broken URLs like:
//
// http://localhost:8080/code/?p=camlistore.git%3Bf=doc/json-signing/json-signing.txt%3Bhb=master
//
// ... but something is. Maybe Buzz? For now just re-write them
// . Doesn't seem to be a bug in the CGI implementation, though, which
// is what I'd originally suspected.
/*
func (fu *fixUpGitwebUrls) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
oldUrl := req.URL.String()
newUrl := strings.Replace(oldUrl, "%3B", ";", -1)
if newUrl == oldUrl {
fu.handler.ServeHTTP(rw, req)
return
}
http.Redirect(rw, req, newUrl, http.StatusFound)
}
*/
func ipHandler(w http.ResponseWriter, r *http.Request) {
out, _ := exec.Command("ip", "-f", "inet", "addr", "show", "dev", "eth0").Output()
str := string(out)
pos := strings.Index(str, "inet ")
if pos == -1 {
return
}
str = str[pos+5:]
pos = strings.Index(str, "/")
if pos == -1 {
return
}
str = str[:pos]
w.Write([]byte(str))
}
const (
errPattern = "/err/"
toHyperlink = `<a href="$1$2">$1$2</a>`
)
var camliURLPattern = regexp.MustCompile(`(https?://camlistore.org)([a-zA-Z0-9\-\_/]+)?`)
func errHandler(w http.ResponseWriter, r *http.Request) {
errString := strings.TrimPrefix(r.URL.Path, errPattern)
defer func() {
if x := recover(); x != nil {
http.Error(w, fmt.Sprintf("unknown error: %v", errString), http.StatusNotFound)
}
}()
err := camtypes.Err(errString)
data := struct {
Code string
Description template.HTML
}{
Code: errString,
Description: template.HTML(camliURLPattern.ReplaceAllString(err.Error(), toHyperlink)),
}
contents := applyTemplate(camliErrorHTML, "camliErrorHTML", data)
w.WriteHeader(http.StatusFound)
servePage(w, errString, "", contents)
}