/
main.go
263 lines (222 loc) · 7.89 KB
/
main.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
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"net/http/httputil"
"strings"
"sync"
"time"
"github.com/TykTechnologies/tyk/ctx"
"github.com/cespare/xxhash/v2"
"github.com/darkweak/souin/context"
"github.com/darkweak/souin/pkg/middleware"
"github.com/darkweak/souin/pkg/rfc"
"github.com/darkweak/souin/pkg/storage/types"
"github.com/pquerna/cachecontrol/cacheobject"
)
var definitions map[string]*souinInstance = make(map[string]*souinInstance)
func getInstanceFromRequest(r *http.Request) (s *souinInstance) {
def := ctx.GetDefinition(r)
var found bool
if s, found = definitions[def.APIID]; !found {
s = parseConfiguration(def.APIID, def.ConfigData)
}
return s
}
// SouinResponseHandler stores the response before sent to the client if possible, only returns otherwise
func SouinResponseHandler(rw http.ResponseWriter, rs *http.Response, baseRq *http.Request) {
if rs.Header.Get("Cache-Status") != "" {
return
}
baseRq.URL.Path = baseRq.RequestURI
customWriter := NewCustomWriter(baseRq, rw, bytes.NewBuffer([]byte{}))
s := getInstanceFromRequest(baseRq)
rq := s.context.SetContext(s.context.SetBaseContext(baseRq), baseRq)
cacheName := rq.Context().Value(context.CacheName).(string)
if rq.Header.Get("Upgrade") == "websocket" || (s.SouinBaseHandler.ExcludeRegex != nil && s.SouinBaseHandler.ExcludeRegex.MatchString(rq.RequestURI)) {
rw.Header().Set("Cache-Status", cacheName+"; fwd=bypass; detail=EXCLUDED-REQUEST-URI")
return
}
if !rq.Context().Value(context.SupportedMethod).(bool) {
rw.Header().Set("Cache-Status", cacheName+"; fwd=bypass; detail=UNSUPPORTED-METHOD")
return
}
switch customWriter.statusCode {
case 500, 502, 503, 504:
return
}
responseCc, _ := cacheobject.ParseResponseCacheControl(customWriter.Header().Get("Cache-Control"))
currentMatchedURL := s.SouinBaseHandler.DefaultMatchedUrl
if regexpURL := s.SouinBaseHandler.RegexpUrls.FindString(rq.Host + rq.URL.Path); regexpURL != "" {
u := s.SouinBaseHandler.Configuration.GetUrls()[regexpURL]
if u.TTL.Duration != 0 {
currentMatchedURL.TTL = u.TTL
}
if len(u.Headers) != 0 {
currentMatchedURL.Headers = u.Headers
}
}
ma := currentMatchedURL.TTL.Duration
if responseCc.MaxAge > 0 {
ma = time.Duration(responseCc.MaxAge) * time.Second
} else if responseCc.SMaxAge > 0 {
ma = time.Duration(responseCc.SMaxAge) * time.Second
}
if ma > currentMatchedURL.TTL.Duration {
ma = currentMatchedURL.TTL.Duration
}
date, _ := http.ParseTime(time.Now().UTC().Format(http.TimeFormat))
customWriter.Headers.Set(rfc.StoredTTLHeader, ma.String())
ma = ma - time.Since(date)
requestCc, coErr := cacheobject.ParseRequestCacheControl(rq.Header.Get("Cache-Control"))
if coErr != nil || requestCc == nil {
rs.Header.Set("Cache-Status", "Souin; fwd=bypass; detail=CACHE-CONTROL-EXTRACTION-ERROR")
return
}
status := fmt.Sprintf("%s; fwd=uri-miss", rq.Context().Value(context.CacheName))
if !requestCc.NoStore && !responseCc.NoStore {
_, _ = io.Copy(customWriter, rs.Body)
rs.Body = io.NopCloser(bytes.NewBuffer(customWriter.Buf.Bytes()))
res := http.Response{
StatusCode: customWriter.statusCode,
Body: io.NopCloser(bytes.NewBuffer(customWriter.Buf.Bytes())),
Header: customWriter.Headers,
}
res.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat))
res.Request = rq
response, err := httputil.DumpResponse(&res, true)
cachedKey := rq.Context().Value(context.Key).(string)
if err == nil {
variedHeaders, isVaryStar := rfc.VariedHeaderAllCommaSepValues(res.Header)
if isVaryStar {
// "Implies that the response is uncacheable"
status += "; detail=UPSTREAM-VARY-STAR"
} else {
cachedKey += rfc.GetVariedCacheKey(rq, variedHeaders)
var wg sync.WaitGroup
mu := sync.Mutex{}
fails := []string{}
select {
case <-rq.Context().Done():
status += "; detail=REQUEST-CANCELED-OR-UPSTREAM-BROKEN-PIPE"
default:
for _, storer := range s.SouinBaseHandler.Storers {
wg.Add(1)
go func(currentStorer types.Storer) {
defer wg.Done()
if currentStorer.Set(cachedKey, response, currentMatchedURL, ma) != nil {
mu.Lock()
fails = append(fails, fmt.Sprintf("; detail=%s-INSERTION-ERROR", currentStorer.Name()))
mu.Unlock()
}
}(storer)
}
wg.Wait()
if len(fails) < len(s.SouinBaseHandler.Storers) {
go func(rs http.Response, key string) {
_ = s.SouinBaseHandler.SurrogateKeyStorer.Store(&rs, key)
}(res, cachedKey)
status += "; stored"
}
if len(fails) > 0 {
status += strings.Join(fails, "")
}
}
}
}
} else {
status += "; detail=NO-STORE-DIRECTIVE"
}
rs.Header.Set("Cache-Status", status+"; key="+rfc.GetCacheKeyFromCtx(rq.Context()))
}
// SouinRequestHandler handle the Tyk request
func SouinRequestHandler(rw http.ResponseWriter, baseRq *http.Request) {
s := getInstanceFromRequest(baseRq)
if b, handler := s.SouinBaseHandler.HandleInternally(baseRq); b {
handler(rw, baseRq)
return
}
rq := s.context.SetBaseContext(baseRq)
cacheName := rq.Context().Value(context.CacheName).(string)
if rq.Header.Get("Upgrade") == "websocket" || (s.SouinBaseHandler.ExcludeRegex != nil && s.SouinBaseHandler.ExcludeRegex.MatchString(rq.RequestURI)) {
rw.Header().Set("Cache-Status", cacheName+"; fwd=bypass; detail=EXCLUDED-REQUEST-URI")
return
}
if !rq.Context().Value(context.SupportedMethod).(bool) {
rw.Header().Set("Cache-Status", cacheName+"; fwd=bypass; detail=UNSUPPORTED-METHOD")
return
}
requestCc, coErr := cacheobject.ParseRequestCacheControl(rq.Header.Get("Cache-Control"))
modeContext := rq.Context().Value(context.Mode).(*context.ModeContext)
if coErr != nil || requestCc == nil {
rw.Header().Set("Cache-Status", cacheName+"; fwd=bypass; detail=CACHE-CONTROL-EXTRACTION-ERROR")
return
}
rq = s.context.SetContext(rq, baseRq)
cachedKey := rq.Context().Value(context.Key).(string)
bufPool := s.bufPool.Get().(*bytes.Buffer)
bufPool.Reset()
defer s.bufPool.Put(bufPool)
if !requestCc.NoCache {
validator := rfc.ParseRequest(rq)
var fresh, stale *http.Response
finalKey := cachedKey
if rq.Context().Value(context.Hashed).(bool) {
finalKey = fmt.Sprint(xxhash.Sum64String(finalKey))
}
for _, currentStorer := range s.Storers {
fresh, stale = currentStorer.GetMultiLevel(finalKey, rq, validator)
if fresh != nil || stale != nil {
fmt.Printf("Found at least one valid response in the %s storage\n", currentStorer.Name())
break
}
}
if fresh != nil && (!modeContext.Strict || rfc.ValidateCacheControl(fresh, requestCc)) {
response := fresh
rfc.SetCacheStatusHeader(response)
if rfc.ValidateMaxAgeCachedResponse(requestCc, response) != nil {
for hn, hv := range response.Header {
rw.Header().Set(hn, strings.Join(hv, ", "))
}
_, _ = io.Copy(rw, response.Body)
return
}
} else if !requestCc.OnlyIfCached && (requestCc.MaxStaleSet || requestCc.MaxStale > -1) {
response := stale
if nil != response && rfc.ValidateCacheControl(response, requestCc) {
addTime, _ := time.ParseDuration(response.Header.Get(rfc.StoredTTLHeader))
rfc.SetCacheStatusHeader(response)
responseCc, _ := cacheobject.ParseResponseCacheControl(response.Header.Get("Cache-Control"))
if responseCc.StaleIfError > 0 {
h := response.Header
rfc.HitStaleCache(&h)
for hn, hv := range h {
h.Set(hn, strings.Join(hv, ", "))
}
_, _ = io.Copy(rw, response.Body)
return
}
if rfc.ValidateMaxAgeCachedStaleResponse(requestCc, response, int(addTime.Seconds())) != nil {
h := response.Header
rfc.HitStaleCache(&h)
for hn, hv := range h {
h.Set(hn, strings.Join(hv, ", "))
}
_, _ = io.Copy(rw, response.Body)
return
}
}
}
}
}
type souinInstance struct {
*middleware.SouinBaseHandler
context *context.Context
bufPool *sync.Pool
}
func init() {
fmt.Println(`message="Souin configuration is now loaded."`)
}
func main() {}