Beispiel #1
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()
	}
}
Beispiel #2
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))
}
Beispiel #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
}
Beispiel #4
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
}
Beispiel #5
0
func main() {
	log.Printf("running apt-proxy %s", version)

	if debug {
		httpcache.DebugLogging = true
	}

	cache, err := httpcache.NewDiskCache(dir)
	if err != nil {
		log.Fatal(err)
	}

	ap := proxy.NewAptProxyFromDefaults()
	ap.Handler = httpcache.NewHandler(cache, ap.Handler)

	logger := httplog.NewResponseLogger(ap.Handler)
	logger.DumpRequests = debug
	logger.DumpResponses = debug
	logger.DumpErrors = debug
	ap.Handler = logger

	log.Printf("proxy listening on %s", listen)
	log.Fatal(http.ListenAndServe(listen, ap))
}