Created
June 30, 2016 13:22
-
-
Save shibukawa/f04f70655017d22060153baff37a7c5f to your computer and use it in GitHub Desktop.
no-cache, must-revalidate test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/k0kubun/pp" | |
"log" | |
"net/http" | |
"os" | |
"strconv" | |
) | |
type Cache struct { | |
CacheControl string | |
Etag string | |
Reject bool | |
Content string | |
} | |
var cache = map[string]Cache{ | |
"/": Cache{ | |
Reject: false, | |
Content: `<html><body>hello</body><script src="/test.js"></script></html>`, | |
}, | |
"/test.js": Cache{ | |
Reject: false, | |
Content: `console.log("test.js loaded");`, | |
}, | |
"/favicon.ico": Cache{ | |
Reject: true, | |
}, | |
} | |
var strategyId int | |
var cacheControls = []string{ | |
"no-cache, max-age=5", | |
"no-cache, max-age=0", | |
"must-revalidate, max-age=5", | |
"must-revalidate, max-age=0", | |
} | |
func useIf(flag bool, value string) string { | |
if flag { | |
return value | |
} | |
return "" | |
} | |
func getStrategy(filename string) Cache { | |
id := strategyId - 1 | |
if filename == "/" { | |
return Cache{ | |
Etag: useIf((id/32)%2 == 0, "1234"), | |
CacheControl: cacheControls[(id/4)%4], | |
Reject: false, | |
Content: `<html><body>hello</body><script src="/test.js"></script></html>`, | |
} | |
} else if filename == "/test.js" { | |
return Cache{ | |
Etag: useIf((id/16)%2 == 0, "5678"), | |
CacheControl: cacheControls[id%4], | |
Reject: false, | |
Content: `console.log("test.js loaded");`, | |
} | |
} | |
return Cache{ | |
Reject: true, | |
Content: "", | |
} | |
} | |
func handler(w http.ResponseWriter, r *http.Request) { | |
etags, ok := r.Header["If-None-Match"] | |
if ok { | |
pp.Println("request", r.Method, r.URL.String(), etags[0]) | |
} else { | |
pp.Println("request", r.Method, r.URL.String()) | |
} | |
strategy := getStrategy(r.URL.String()) | |
if strategy.Reject { | |
// favicon.ico | |
fmt.Println(" Status:", http.StatusNotFound) | |
w.WriteHeader(http.StatusNotFound) | |
return | |
} | |
if ok { | |
fmt.Println(" Status:", http.StatusNotModified) | |
w.WriteHeader(http.StatusNotModified) | |
return | |
} else { | |
fmt.Println(" Status:", http.StatusOK) | |
} | |
if strategy.Etag != "" { | |
fmt.Println(" Etag:", strategy.Etag) | |
w.Header().Set("Etag", strategy.Etag) | |
} | |
fmt.Println(" Cache-Control:", strategy.CacheControl) | |
w.Header().Set("Cache-Control", strategy.CacheControl) | |
fmt.Fprintf(w, strategy.Content) | |
} | |
func main() { | |
strategyId, _ = strconv.Atoi(os.Args[1]) | |
if strategyId > 64 || strategyId < 1 { | |
log.Fatalf("wrong id: %d", strategyId) | |
} | |
var httpServer http.Server | |
http.HandleFunc("/", handler) | |
log.Println("start http listening :18888") | |
httpServer.Addr = ":18888" | |
log.Println(httpServer.ListenAndServe()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment