-
Notifications
You must be signed in to change notification settings - Fork 8
/
config.go
308 lines (269 loc) · 9.18 KB
/
config.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
// Copyright 2015-present, Cyrill @ Schumacher.fm and the CoreStore contributors
//
// 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 caddyesi
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"path/filepath"
"strings"
"sync"
"time"
"github.com/corestoreio/caddy-esi/bufpool"
"github.com/corestoreio/caddy-esi/esitag"
"github.com/corestoreio/caddy-esi/helper"
"github.com/corestoreio/errors"
"github.com/corestoreio/log"
"github.com/mholt/caddy/caddyhttp/httpserver"
"github.com/pierrec/xxHash/xxHash64"
)
// DefaultTimeOut to a backend resource
const DefaultTimeOut = 20 * time.Second
// DefaultMaxBodySize the body size of a retrieved request to a resource. 5 MB
// is a lot of text.
const DefaultMaxBodySize uint64 = 5 << 20
// DefaultOnError default error message when a backend service cannot be
// requested. Only when config value on_error in Caddyfile has not been
// supplied.
const DefaultOnError = `Resource not available`
// PathConfigs contains the configuration for each path prefix
type PathConfigs []*PathConfig
// ConfigForPath selects in the ServeHTTP function the config for a path.
func (pc PathConfigs) ConfigForPath(r *http.Request) *PathConfig {
for _, c := range pc {
if httpserver.Path(r.URL.Path).Matches(c.Scope) { // not negated
// match also all sub paths ...
return c
}
}
return nil
}
// String prints debug information. Very slow ...
func (pc PathConfigs) String() string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "PathConfig Count: %d", len(pc))
buf.WriteRune('\n')
for _, c := range pc {
buf.WriteString(c.String())
buf.WriteRune('\n')
}
return buf.String()
}
// PathConfig per path prefix
type PathConfig struct {
// Scope sets the base path to match used as path prefix
Scope string
// MaxBodySize defaults to 5MB and limits the size of the returned body from a
// backend resource.
MaxBodySize uint64
// Timeout global. Time when a request to a source should be canceled.
// Default value from the constant DefaultTimeOut.
Timeout time.Duration
// TTL global time-to-live in the storage backend for Tag data. Defaults to
// zero, caching globally disabled until an Tag tag or this configuration
// value contains the TTL attribute.
TTL time.Duration
// CmdHeaderName if set allows to execute certain maintenance functions to
// e.g. purge the cache. For security reasons an empty string means, feature
// has been disabled.
CmdHeaderName string
// PageIDSource defines a slice of possible parameters which gets extracted
// from the http.Request object. All these parameters will be used to
// extract the values and calculate a unique hash for the current requested
// page to identify the already parsed Tag tags in the cache.
PageIDSource []string
// AllowedMethods list of all benchIsResponseAllowed methods, defaults to GET
AllowedMethods []string
// OnError gets output when a request to a backend service fails.
OnError []byte
// LogFile where to write the log output? Either any file name or stderr or
// stdout. If empty logging disabled.
LogFile string
esiMU sync.RWMutex
// LogLevel can have the values info, debug, fatal. If empty logging disabled.
LogLevel string
// Log gets set up during setup
Log log.Logger
// esiCache identifies all parsed Tag tags in a page for specific path
// prefix. uint64 represents the hash for the current request calculated by
// pageID function. Long term "bug": Maybe we need here another algorithm
// instead of the map. Due to a higher granularity of the pageID the map
// gets filled fast without dropping old entries. This will blow up the
// memory.
esiCache map[uint64]esitag.Entities // TODO after refacotring other stuff replace with EntitiesMap but run before benchmarks and after ;-)
}
// NewPathConfig creates a configuration for a unique path prefix and
// initializes the internal maps.
func NewPathConfig() *PathConfig {
return &PathConfig{
Timeout: DefaultTimeOut,
esiCache: make(map[uint64]esitag.Entities),
}
}
func (pc *PathConfig) parseOnError(val string) (err error) {
var fileExt string
if li := strings.LastIndexByte(val, '.'); li > 0 {
fileExt = strings.ToLower(val[li+1:])
}
switch fileExt {
case "html", "htm", "xml", "txt", "json":
pc.OnError, err = ioutil.ReadFile(filepath.Clean(val))
if err != nil {
return errors.Fatal.Newf("[caddyesi] PathConfig.parseOnError. Failed to process %q with error: %s. Scope %q", val, err, pc.Scope)
}
default:
pc.OnError = []byte(val)
}
return nil
}
// ESITagsByRequest selects in the ServeHTTP function all ESITags identified by
// their pageIDs. Returns a nil t when the entry does not exists.
func (pc *PathConfig) ESITagsByRequest(r *http.Request) (pageID uint64, t esitag.Entities) {
pageID = pc.pageID(r)
pc.esiMU.RLock()
t = pc.esiCache[pageID]
pc.esiMU.RUnlock()
return
}
// UpsertESITags processes each Tag entity to update their default values with
// the supplied global PathConfig value. Then inserts the Tag entities with its
// associated page ID in the internal Tag cache. These writes to esitag.Entity
// happens in a locked environment. So there should be no race condition.
func (pc *PathConfig) UpsertESITags(pageID uint64, entities esitag.Entities) {
for _, et := range entities {
et.Log = pc.Log
if len(et.OnError) == 0 {
et.OnError = pc.OnError
}
// add here the KVFetcher ...
// create sync.pool of arguments for the resources. Now with all correct
// default values.
et.SetDefaultConfig(esitag.Config{
Log: pc.Log,
MaxBodySize: pc.MaxBodySize,
Timeout: pc.Timeout,
TTL: pc.TTL,
})
}
pc.esiMU.Lock()
pc.esiCache[pageID] = entities
pc.esiMU.Unlock()
}
// IsRequestAllowed decides if a request should be processed based on the
// request method. The benchIsResponseAllowed response content-type is text only.
func (pc *PathConfig) IsRequestAllowed(r *http.Request) bool {
if len(pc.AllowedMethods) == 0 {
return r.Method == http.MethodGet
}
for _, m := range pc.AllowedMethods {
if r.Method == m {
return true
}
}
return false
}
var defaultPageIDSource = [...]string{"host", "path"}
// pageID uses the configuration to extract certain parameters from the request
// to generate a hash to identify a page.
func (pc *PathConfig) pageID(r *http.Request) uint64 {
src := pc.PageIDSource
if len(src) == 0 {
src = defaultPageIDSource[:]
}
h, ok := pageID(src, r)
if !ok {
h, _ = pageID(defaultPageIDSource[:], r)
}
return h
}
func pageID(source []string, r *http.Request) (_ uint64, ok bool) {
const (
pageIDConfigHeader = `header`
pageIDConfigCookie = `cookie`
)
buf := bufpool.Get()
defer bufpool.Put(buf)
for _, key := range source {
{
var keyPrefix string
var keySuffix string
if len(key) > 7 {
// "Header" and "Cookie" are equally long which makes things easier
// Cookie-__Host-user_session_same_site
// Header-Server
keyPrefix = key[:6] // Contains e.g. "header" or "cookie"
keySuffix = key[7:] // Contains e.g. "__Host-user_session_same_site" or "Server"
}
switch keyPrefix {
case pageIDConfigCookie:
if keks, _ := r.Cookie(keySuffix); keks != nil {
_, _ = buf.WriteString(keks.Value)
}
case pageIDConfigHeader:
if v := r.Header.Get(keySuffix); v != "" {
_, _ = buf.WriteString(v)
}
}
}
switch key {
case "remoteaddr":
_, _ = buf.WriteString(r.RemoteAddr)
case "realip":
_, _ = buf.WriteString(helper.RealIP(r))
case "scheme":
_, _ = buf.WriteString(r.URL.Scheme)
case "host":
_, _ = buf.WriteString(r.URL.Host)
case "path":
_, _ = buf.WriteString(r.URL.Path)
case "rawpath":
_, _ = buf.WriteString(r.URL.RawPath)
case "rawquery":
_, _ = buf.WriteString(r.URL.RawQuery)
case "url":
_, _ = buf.WriteString(r.URL.String())
}
}
l := uint64(buf.Len())
if l == 0 {
return 0, false
}
return xxHash64.Checksum(buf.Bytes(), l), true
}
// String used for log information output
func (pc *PathConfig) String() string {
pc.esiMU.RLock()
el := len(pc.esiCache)
pc.esiMU.RUnlock()
return fmt.Sprintf("Scope:%q; MaxBodySize:%d; Timeout:%s; PageIDSource:%v; AllowedMethods:%v; LogFile:%q; LogLevel:%q; EntityCount: %d",
pc.Scope, pc.MaxBodySize, pc.Timeout, pc.PageIDSource, pc.AllowedMethods, pc.LogFile, pc.LogLevel, el,
)
}
func (pc *PathConfig) purgeESICache() (itemsInMap int) {
pc.esiMU.Lock()
itemsInMap = len(pc.esiCache)
pc.esiCache = make(map[uint64]esitag.Entities)
pc.esiMU.Unlock()
if pc.Log.IsDebug() {
pc.Log.Debug("caddyesi.PathConfig.purgeESICache", log.String("path_scope", pc.Scope))
}
return
}
// isResponseAllowed uses https://golang.org/pkg/net/http/#DetectContentType
// it must read at least 512 bytes.
func isResponseAllowed(buf []byte) bool {
fileType := http.DetectContentType(buf)
return strings.HasPrefix(fileType, "text/")
}