-
Notifications
You must be signed in to change notification settings - Fork 25
/
server.go
130 lines (116 loc) · 3.71 KB
/
server.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
// Copyright 2015 Google Inc. All Rights Reserved.
//
// 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 server
import (
"net/http"
"time"
"golang.org/x/net/context"
"github.com/google/weasel"
"google.golang.org/appengine"
"google.golang.org/appengine/log"
)
// storage is used by the weasel server to serve GCS objects.
var storage *weasel.Storage
func init() {
if err := readConfig(); err != nil {
panic(err)
}
storage = &weasel.Storage{Base: config.GCSBase, Index: config.Index}
for host, redir := range config.Redirects {
http.Handle(host, redirectHandler(redir, http.StatusMovedPermanently))
}
http.HandleFunc(config.WebRoot, serveObject)
http.HandleFunc(config.HookPath, storage.HandleChangeHook)
http.HandleFunc("/_ah/warmup", handleWarmup)
}
func handleWarmup(w http.ResponseWriter, r *http.Request) {
// nothing to do here
}
// serveObject responds with a GCS object contents, preserving its original headers
// listed in objectHeaders.
// The bucket is identifed by matching r.Host against config.Buckets map keys.
// Default bucket is used if no match is found.
//
// Only GET, HEAD and OPTIONS methods are allowed.
func serveObject(w http.ResponseWriter, r *http.Request) {
_, forceTLS := config.tlsOnly[r.Host]
if forceTLS && r.TLS != nil {
w.Header().Set("Strict-Transport-Security", "max-age=10886400; includeSubDomains; preload")
}
if !weasel.ValidMethod(r.Method) {
http.Error(w, "", http.StatusMethodNotAllowed)
return
}
if forceTLS && r.TLS == nil {
u := "https://" + r.Host + r.URL.Path
if r.URL.RawQuery != "" {
u += "?" + r.URL.RawQuery
}
http.Redirect(w, r, u, http.StatusMovedPermanently)
return
}
ctx := newContext(r)
bucket := bucketForHost(r.Host)
oname := r.URL.Path[1:]
o, err := storage.OpenFile(ctx, bucket, oname)
if err != nil {
code := http.StatusInternalServerError
if errf, ok := err.(*weasel.FetchError); ok {
code = errf.Code
}
serveError(w, code, "")
if code != http.StatusNotFound {
log.Errorf(ctx, "%s/%s: %v", bucket, oname, err)
}
return
}
if err := storage.ServeObject(w, r, o); err != nil {
log.Errorf(ctx, "%s/%s: %v", bucket, oname, err)
}
o.Body.Close()
}
// redirectHandler creates a new handler which redirects all requests
// to the specified url, preserving original path and raw query.
func redirectHandler(url string, code int) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
u := url + r.URL.Path
if r.URL.RawQuery != "" {
u += "?" + r.URL.RawQuery
}
http.Redirect(w, r, u, code)
})
}
func serveError(w http.ResponseWriter, code int, msg string) {
if msg == "" {
msg = http.StatusText(code)
}
w.WriteHeader(code)
// TODO: render some template
w.Write([]byte(msg))
}
// bucketForHost returns a bucket name mapped to the host.
// Default bucket name is return if no match found.
func bucketForHost(host string) string {
if b, ok := config.Buckets[host]; ok {
return b
}
return config.Buckets["default"]
}
// newContext creates a new context from a client in-flight request.
// It should not be used for server-to-server, such as web hooks.
func newContext(r *http.Request) context.Context {
c := appengine.NewContext(r)
c, _ = context.WithTimeout(c, 10*time.Second)
return c
}