Skip to content

Commit

Permalink
fix the match_header function (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
sillygod authored Jun 23, 2020
1 parent 9991671 commit b4509e2
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 15 deletions.
15 changes: 7 additions & 8 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,17 +228,16 @@ func getCacheStatus(req *http.Request, response *Response, config *Config) (bool
}

for _, rule := range config.RuleMatchers {
if rule.matches(req, response.Code, response.snapHeader) {

if expiration.Before(time.Now()) {
expiration = time.Now().Add(config.DefaultMaxAge)
}

return true, expiration
if !rule.matches(req, response.Code, response.snapHeader) {
return false, time.Now()
}
}

return false, time.Now()
if expiration.Before(time.Now()) {
expiration = time.Now().Add(config.DefaultMaxAge)
}

return true, expiration
}

func matchVary(curReq *http.Request, entry *Entry) bool {
Expand Down
4 changes: 1 addition & 3 deletions caddyfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,9 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
d, _ := json.Marshal(cacheRule)

config.RuleMatchersRaws = append(config.RuleMatchersRaws, RuleMatcherRawWithType{
Type: MatcherTypePath,
Type: MatcherTypeHeader,
Data: d,
})
// config.RuleMatchers = append(config.RuleMatchers, cacheRule)

case keyMatchPath:
if len(args) != 1 {
Expand All @@ -209,7 +208,6 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
Type: MatcherTypePath,
Data: d,
})
// config.RuleMatchers = append(config.RuleMatchers, cacheRule)

case keyCacheKey:
if len(args) != 1 {
Expand Down
5 changes: 1 addition & 4 deletions cmd/Caddyfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

cache_type in_memory
match_path /
match_header Content-Type image/jpg image/png "text/plain; charset=utf-8"

}

Expand All @@ -24,10 +25,6 @@
level debug
}

#log {
# output file /var/logs/caddy/access.log
# format single_field common_log
#}
}

:9995 {
Expand Down
1 change: 1 addition & 0 deletions example/file_cache/Caddyfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
cache_type file
path /tmp/cache
match_path /
match_header Content-Type image/jpg image/png "text/plain; charset=utf-8"
}
}

Expand Down
1 change: 1 addition & 0 deletions example/inmemory_cache/Caddyfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
http_cache {
cache_type in_memory
match_path /
match_header Content-Type image/jpg image/png "text/plain; charset=utf-8"
}
}

Expand Down
2 changes: 2 additions & 0 deletions extends/distributed/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (c *ConsulService) Cleanup() error {
return nil
}

// Provision init the consul's agent and establish connection
func (c *ConsulService) Provision(ctx caddy.Context) error {
// init the consul api client here
if c.Config == nil {
Expand Down Expand Up @@ -127,6 +128,7 @@ func (c *ConsulService) Provision(ctx caddy.Context) error {
return nil
}

// GetPeers get the peers in the same cluster
func (c *ConsulService) GetPeers() ([]string, error) {

peerMap := make(map[string]struct{})
Expand Down
1 change: 1 addition & 0 deletions purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func (cachePurge) CaddyModule() caddy.ModuleInfo {
}
}

// Purge deletes the cache
func (cachePurge) Purge(cacheHandler *HTTPCache, conds string) error {
// Regular expression will be a little slow.
// In fact, there will not be so many keys in real world case
Expand Down
5 changes: 5 additions & 0 deletions readme.org
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@

*** match_header
only the req's header match the condtions
ex.

#+begin_quote
match_header Content-Type image/jpg image/png "text/plain; charset=utf-8"
#+end_quote

*** path
The position where to save the file. Only applied when the =cache_type= is =file=.
Expand Down

0 comments on commit b4509e2

Please sign in to comment.