-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
191 lines (165 loc) · 4.73 KB
/
handler.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
package gcs
import (
"context"
"io"
"net/http"
"strconv"
"strings"
"time"
"cloud.google.com/go/storage"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
"github.com/ripta/ssp/proxy"
"github.com/rs/zerolog"
"github.com/rs/zerolog/hlog"
)
type handler struct {
Client *storage.Client
Bucket string
KeyFile string
proxy.Options
}
func NewHandler(bucket, keyFile string, opts proxy.Options) (http.Handler, error) {
c, err := newClient(context.Background(), keyFile)
if err != nil {
return nil, err
}
h := handler{
Client: c,
Bucket: bucket,
KeyFile: keyFile,
Options: opts,
}
return &h, nil
}
func newClient(ctx context.Context, keyFile string) (*storage.Client, error) {
if keyFile == "" {
return storage.NewClient(ctx)
}
return storage.NewClient(ctx, option.WithCredentialsFile(keyFile))
}
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
if path != "/" {
path = strings.TrimPrefix(path, "/")
}
log := hlog.FromRequest(r)
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
return c.
Str("gcs_bucket", h.Bucket).
Str("gcs_key", path)
})
if strings.HasSuffix(path, "/") {
var foundPath string
for _, candidate := range h.Options.IndexFiles {
if h.hasObject(r, path+candidate) {
foundPath = path + candidate
break
}
}
if foundPath == "" {
if h.Options.Autoindex != nil && *h.Options.Autoindex {
if path == "/" {
h.serveDirectoryListing(w, r, "")
} else {
h.serveDirectoryListing(w, r, path)
}
} else {
http.Error(w, "Could not find a valid index file. Additionally, directory listing was denied.", http.StatusForbidden)
}
return
}
path = foundPath
}
h.serveFile(w, r, path)
}
func (h *handler) getObject(r *http.Request, path string) (*storage.ObjectHandle, *storage.ObjectAttrs, error) {
obj := h.Client.Bucket(h.Bucket).Object(path)
attrs, err := obj.Attrs(r.Context())
if err != nil {
return nil, nil, err
}
return obj, attrs, nil
}
func (h *handler) hasObject(r *http.Request, path string) bool {
_, err := h.Client.Bucket(h.Bucket).Object(path).Attrs(r.Context())
return err == nil
}
func (h *handler) listObjects(r *http.Request, prefix string) *storage.ObjectIterator {
q := storage.Query{
Delimiter: "/",
Prefix: prefix,
}
return h.Client.Bucket(h.Bucket).Objects(r.Context(), &q)
}
func (h *handler) serveDirectoryListing(w http.ResponseWriter, r *http.Request, path string) {
log := hlog.FromRequest(r)
var files []proxy.DirectoryEntry
it := h.listObjects(r, path)
for {
obj, err := it.Next()
if err != nil {
if err == iterator.Done {
break
}
log.Error().Err(err).Msg("generic GCS listing error")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
files = append(files, proxy.DirectoryEntry{
Name: strings.TrimPrefix(obj.Name, path),
Size: obj.Size,
ModTime: &obj.Created,
})
}
listing := proxy.DirectoryListing{
Entries: files,
}
if err := proxy.RenderDirectoryListing(w, listing); err != nil {
log.Error().Err(err).Msg("directory listing render error")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
}
func (h *handler) serveFile(w http.ResponseWriter, r *http.Request, path string) {
log := hlog.FromRequest(r)
// Request from GCS
obj, attrs, err := h.getObject(r, path)
if err != nil {
log.Error().Err(err).Msg("")
http.Error(w, http.StatusText(http.StatusServiceUnavailable), http.StatusServiceUnavailable)
return
}
if attrs.Prefix != "" {
dirpath := r.RequestURI + "/"
w.Header().Add("Location", dirpath)
w.WriteHeader(http.StatusTemporaryRedirect)
return
}
// Copy common headers from S3 to the response
copyStringHeader(w, "Cache-Control", attrs.CacheControl)
copyStringHeader(w, "Content-Disposition", attrs.ContentDisposition)
copyStringHeader(w, "Content-Encoding", attrs.ContentEncoding)
copyStringHeader(w, "Content-Language", attrs.ContentLanguage)
copyStringHeader(w, "Content-Type", attrs.ContentType)
// Copy the Last-Modified header as long as it's not the zero value
if t := attrs.Created; !t.Equal(time.Time{}) {
s := t.UTC().Format(http.TimeFormat)
copyStringHeader(w, "Last-Modified", s)
}
// Return "204 No Content" only if a Content-Length header in fact exists AND it's zero
if attrs.Size == 0 {
w.WriteHeader(http.StatusNoContent)
return
}
copyStringHeader(w, "Content-Length", strconv.FormatInt(attrs.Size, 10))
body, err := obj.NewReader(r.Context())
if n, err := io.Copy(w, body); err != nil {
log.Error().Err(err).Int64("bytes_written", n).Msg("")
return
}
}
func copyStringHeader(w http.ResponseWriter, k, v string) {
if v != "" {
w.Header().Add(k, v)
}
}