@@ -11,6 +11,7 @@ import (
1111 "sync"
1212
1313 "github.com/caddyserver/caddy/v2"
14+ "github.com/sillygod/cdp-cache/pkg/helper"
1415)
1516
1617var (
@@ -65,8 +66,8 @@ func (p *PurgePayload) parseURI() {
6566
6667func (p * PurgePayload ) pruneHost () {
6768
68- if strings .HasPrefix (p .Host , "http" ) {
69- p .Host = strings .Split (p .Host , ": " )[1 ]
69+ if strings .HasPrefix (p .Host , "http" ) || strings . HasPrefix ( p . Host , "https" ) {
70+ p .Host = strings .Split (p .Host , "// " )[1 ]
7071 }
7172
7273 if ! strings .HasSuffix (p .Host , "/" ) {
@@ -120,8 +121,8 @@ func (c cachePurge) Routes() []caddy.AdminRoute {
120121 Handler : caddy .AdminHandlerFunc (c .handlePurge ),
121122 },
122123 {
123- Pattern : "/caches" ,
124- Handler : caddy .AdminHandlerFunc (c .handleListCacheKeys ),
124+ Pattern : "/caches/ " ,
125+ Handler : caddy .AdminHandlerFunc (c .handleCacheEndpoints ),
125126 },
126127 }
127128}
@@ -132,7 +133,43 @@ func health(w http.ResponseWriter, r *http.Request) error {
132133 return nil
133134}
134135
136+ func (c cachePurge ) handleShowCache (w http.ResponseWriter , r * http.Request ) error {
137+ var err error
138+
139+ if r .Method != http .MethodGet {
140+ return caddy.APIError {
141+ Code : http .StatusMethodNotAllowed ,
142+ Err : fmt .Errorf ("method not allowed" ),
143+ }
144+ }
145+
146+ key := helper .TrimBy (r .URL .Path , "/" , 2 )
147+ cache := getHandlerCache ()
148+
149+ entry , exists := cache .Get (key , r )
150+ if exists {
151+ err = entry .WriteBodyTo (w )
152+ }
153+
154+ return err
155+ }
156+
157+ func (c cachePurge ) handleCacheEndpoints (w http.ResponseWriter , r * http.Request ) error {
158+ // a workaround for handling the wildcard endpoint. Caddy uses the standard library's mux
159+ // so it doesn't support this natively.
160+
161+ path := r .URL .Path
162+
163+ switch path {
164+ case "/caches/" :
165+ return c .handleListCacheKeys (w , r )
166+ default :
167+ return c .handleShowCache (w , r )
168+ }
169+ }
170+
135171func (c cachePurge ) handleListCacheKeys (w http.ResponseWriter , r * http.Request ) error {
172+
136173 if r .Method != http .MethodGet {
137174 return caddy.APIError {
138175 Code : http .StatusMethodNotAllowed ,
0 commit comments