Exemple #1
0
func main() {
	proxy := &httputil.ReverseProxy{
		Director: func(r *http.Request) {
		},
	}

	var cache *httpcache.Cache

	if useDisk || dir != "" {
		log.Printf("storing cached resources in %s", dir)
		if err := os.MkdirAll(dir, 0700); err != nil {
			log.Fatal(err)
		}
		var err error
		cache, err = httpcache.NewDiskCache(dir)
		if err != nil {
			log.Fatal(err)
		}
	} else {
		cache = httpcache.NewMemoryCache()
	}

	handler := httpcache.NewHandler(cache, proxy)
	handler.Shared = !private

	respLogger := httplog.NewResponseLogger(handler)
	respLogger.DumpRequests = dumpHttp
	respLogger.DumpResponses = dumpHttp
	respLogger.DumpErrors = dumpHttp

	log.Printf("listening on http://%s", listen)
	log.Fatal(http.ListenAndServe(listen, respLogger))
}
Exemple #2
0
func BenchmarkCachingFiles(b *testing.B) {
	backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Add("Cache-Control", "max-age=100000")
		fmt.Fprintf(w, "cache server payload")
	}))
	defer backend.Close()

	u, err := url.Parse(backend.URL)
	if err != nil {
		b.Fatal(err)
	}

	handler := httpcache.NewHandler(httpcache.NewMemoryCache(), httputil.NewSingleHostReverseProxy(u))
	handler.Shared = true
	cacheServer := httptest.NewServer(handler)
	defer cacheServer.Close()

	for n := 0; n < b.N; n++ {
		client := http.Client{}
		resp, err := client.Get(fmt.Sprintf("%s/llamas/%d", cacheServer.URL, n))
		if err != nil {
			b.Fatal(err)
		}
		resp.Body.Close()
	}
}
Exemple #3
0
func testSetup() (*client, *upstreamServer) {
	upstream := &upstreamServer{
		Body:    []byte("llamas"),
		asserts: []func(r *http.Request){},
		Now:     time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
		Header:  http.Header{},
	}

	httpcache.Clock = func() time.Time {
		return upstream.Now
	}

	cacheHandler := httpcache.NewHandler(
		httpcache.NewMemoryCache(),
		upstream,
	)

	var handler http.Handler = cacheHandler

	if testing.Verbose() {
		rlogger := httplog.NewResponseLogger(cacheHandler)
		rlogger.DumpRequests = true
		rlogger.DumpResponses = true
		handler = rlogger
		httpcache.DebugLogging = true
	} else {
		log.SetOutput(ioutil.Discard)
	}

	return &client{handler, cacheHandler}, upstream
}
Exemple #4
0
func TestSaveResourceWithIncorrectContentLength(t *testing.T) {
	var body = "llamas"
	var cache = httpcache.NewMemoryCache()

	res := httpcache.NewResourceBytes(http.StatusOK, []byte(body), http.Header{
		"Llamas":         []string{"true"},
		"Content-Length": []string{"10"},
	})

	if err := cache.Store(res, "testkey"); err == nil {
		t.Fatal("Entry should have generated an error")
	}

	_, err := cache.Retrieve("testkey")
	if err != httpcache.ErrNotFoundInCache {
		t.Fatal("Entry shouldn't have been cached")
	}
}
Exemple #5
0
func TestSaveResource(t *testing.T) {
	var body = strings.Repeat("llamas", 5000)
	var cache = httpcache.NewMemoryCache()

	res := httpcache.NewResourceBytes(http.StatusOK, []byte(body), http.Header{
		"Llamas": []string{"true"},
	})

	if err := cache.Store(res, "testkey"); err != nil {
		t.Fatal(err)
	}

	resOut, err := cache.Retrieve("testkey")
	if err != nil {
		t.Fatal(err)
	}

	require.NotNil(t, resOut)
	require.Equal(t, res.Header(), resOut.Header())
	require.Equal(t, body, readAllString(resOut))
}
Exemple #6
0
func cacheHandler(cacheOpt string, h http.Handler) http.Handler {
	if cacheOpt == "" {
		return h
	}
	var cache *httpcache.Cache
	if cacheOpt == "mem" {
		cache = httpcache.NewMemoryCache()
		log.Printf("Using in-memory HTTP cache.")
	} else if strings.HasPrefix(cacheOpt, "disk:") {
		dir := cacheOpt[len("disk:"):]
		log.Printf("Using on-disk HTTP cache at %q.", dir)
		var err error
		cache, err = httpcache.NewDiskCache(dir)
		if err != nil {
			log.Fatalf("Error creating HTTP disk cache at dir %q: %s.", dir, err)
		}
	} else {
		log.Fatalf("Invalid -cache option: %q.", cacheOpt)
	}
	ch := httpcache.NewHandler(cache, h)
	httpcache.DebugLogging, _ = strconv.ParseBool(os.Getenv("LOG_CACHE"))
	return ch
}