@@ -14,16 +14,19 @@ import (
1414 "go.uber.org/zap/zapcore"
1515 "io/ioutil"
1616 "net/http"
17+ "regexp"
1718 "strings"
1819 "sync"
1920)
2021
22+ // CustomWriter handles the response and provide the way to cache the value
2123type CustomWriter struct {
2224 Response * http.Response
2325 http.ResponseWriter
2426 BufPool * sync.Pool
2527}
2628
29+ // WriteHeader will write the response headers
2730func (r * CustomWriter ) WriteHeader (code int ) {
2831 if code == 0 {
2932 return
@@ -32,6 +35,7 @@ func (r *CustomWriter) WriteHeader(code int) {
3235 r .ResponseWriter .WriteHeader (code )
3336}
3437
38+ // Write will write the response body
3539func (r * CustomWriter ) Write (b []byte ) (int , error ) {
3640 buf := r .BufPool .Get ().(* bytes.Buffer )
3741 buf .Reset ()
@@ -45,6 +49,11 @@ func (r *CustomWriter) Write(b []byte) (int, error) {
4549 return r .ResponseWriter .Write (buf .Bytes ())
4650}
4751
52+ // CanHandle detect if the request can be handled by Souin
53+ func CanHandle (r * http.Request , re types.RetrieverResponsePropertiesInterface ) bool {
54+ return r .Header .Get ("Upgrade" ) != "websocket" && (re .GetExcludeRegexp () == nil || ! re .GetExcludeRegexp ().MatchString (r .RequestURI ))
55+ }
56+
4857// DefaultSouinPluginCallback is the default callback for plugins
4958func DefaultSouinPluginCallback (
5059 res http.ResponseWriter ,
@@ -133,6 +142,10 @@ func DefaultSouinPluginInitializerFromConfiguration(c configurationtypes.Abstrac
133142 regexpUrls := helpers .InitializeRegexp (c )
134143 transport := rfc .NewTransport (provider , ykeys .InitializeYKeys (c .GetYkeys ()))
135144 c .GetLogger ().Debug ("Transport initialized." )
145+ var excludedRegexp * regexp.Regexp = nil
146+ if c .GetDefaultCache ().GetRegex ().Exclude != "" {
147+ excludedRegexp = regexp .MustCompile (c .GetDefaultCache ().GetRegex ().Exclude )
148+ }
136149
137150 retriever := & types.RetrieverResponseProperties {
138151 MatchedURL : configurationtypes.URL {
@@ -143,6 +156,7 @@ func DefaultSouinPluginInitializerFromConfiguration(c configurationtypes.Abstrac
143156 Configuration : c ,
144157 RegexpUrls : regexpUrls ,
145158 Transport : transport ,
159+ ExcludeRegex : excludedRegexp ,
146160 }
147161 retriever .Transport .SetURL (retriever .MatchedURL )
148162 retriever .GetConfiguration ().GetLogger ().Info ("Souin configuration is now loaded." )
0 commit comments