Exemple #1
0
// NewCachingSingleHostReverseProxy constructs a caching reverse proxy handler for
// target. If cache is nil, a volatile, in-memory cache is used.
func NewCachingSingleHostReverseProxy(target *url.URL, cache httpcache.Cache) *httputil.ReverseProxy {
	proxy := NewSingleHostReverseProxy(target)
	if cache == nil {
		cache = httpcache.NewMemoryCache()
	}
	proxy.Transport = httpcache.NewTransport(cache)
	return proxy
}
Exemple #2
0
// newAPIClient creates a new Sourcegraph API client for the endpoint
// given by endpointURL (a global) and that is authenticated using the
// credentials in ua (if non-nil) and perm grant ticket (if one is
// provided in SRCLIB_TICKET).
func newAPIClient(ua *userEndpointAuth, cache bool) *sourcegraph.Client {
	endpointURL := getEndpointURL()

	var transport http.RoundTripper
	if cache {
		tempDir := filepath.Join(os.TempDir(), "srclib-cache")
		transport = http.RoundTripper(httpcache.NewTransport(diskcache.New(tempDir)))
	} else {
		transport = http.DefaultTransport
	}

	if tickets := getPermGrantTickets(); len(tickets) > 0 {
		log.Println("# Using perm grant ticket from SRCLIB_TICKET.")
		transport = &auth.TicketAuthedTransport{SignedTicketStrings: tickets, Transport: transport}
	}

	if ua == nil {
		// Unauthenticated API client.
		if GlobalOpt.Verbose {
			log.Printf("# Using unauthenticated API client for endpoint %s.", endpointURL)
		}
	} else {
		// Authenticated API client.
		if GlobalOpt.Verbose {
			log.Printf("# Using authenticated API client for endpoint %s (UID %d).", endpointURL, ua.UID)
		}
		transport = &auth.BasicAuthTransport{Username: strconv.Itoa(ua.UID), Password: ua.Key, Transport: transport}
	}

	if v, _ := strconv.ParseBool(os.Getenv("SRC_TRACE")); v {
		transport = &tracingTransport{Writer: os.Stderr, Transport: transport}
	}

	c := sourcegraph.NewClient(&http.Client{Transport: transport})
	c.BaseURL = endpointURL
	return c
}