Skip to content

Commit bb58876

Browse files
committed
Add cookie cache decider
1 parent dd00c7c commit bb58876

3 files changed

Lines changed: 30 additions & 0 deletions

File tree

fixtures/index.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
setcookie('wordpress_logged_in_123123', 'abcdefg');
4+
35
header('x-test: testing');
46

57
echo 'hi';

handlers/cookie.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package handlers
2+
3+
import (
4+
"net/http"
5+
"regexp"
6+
7+
"github.com/golevi/cache-handler/config"
8+
)
9+
10+
// Cookie compares the config cookie values against the ones sent by the request
11+
// and if one matches, we want to bypass the cache.
12+
func Cookie(c config.Config, w http.ResponseWriter, r *http.Request) bool {
13+
// Loop through all the cookies from the request
14+
for _, cookie := range r.Cookies() {
15+
// Loop through all the regexp cookie names from the config
16+
for _, cc := range c.Cookie {
17+
// Compile the regexp
18+
rge := regexp.MustCompile(cc)
19+
// If we find a cookie, return true that we should bypass the cache.
20+
if rge.Find([]byte(cookie.Name)) != nil {
21+
return true
22+
}
23+
}
24+
}
25+
26+
return false
27+
}

httpcache.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ func (c *Cache) Provision(ctx caddy.Context) error {
258258

259259
c.Deciders = append(c.Deciders, handlers.URI)
260260
c.Deciders = append(c.Deciders, handlers.Method)
261+
c.Deciders = append(c.Deciders, handlers.Cookie)
261262

262263
return nil
263264
}

0 commit comments

Comments
 (0)