1// Copyright 2014 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package internal
6
7import (
8 "net/http"
9
10 "golang.org/x/net/context"
11)
12
13// HTTPClient is the context key to use with golang.org/x/net/context's
14// WithValue function to associate an *http.Client value with a context.
15var HTTPClient ContextKey
16
17// ContextKey is just an empty struct. It exists so HTTPClient can be
18// an immutable public variable with a unique type. It's immutable
19// because nobody else can create a ContextKey, being unexported.
20type ContextKey struct{}
21
22var appengineClientHook func(context.Context) *http.Client
23
24func ContextClient(ctx context.Context) *http.Client {
25 if ctx != nil {
26 if hc, ok := ctx.Value(HTTPClient).(*http.Client); ok {
27 return hc
28 }
29 }
30 if appengineClientHook != nil {
31 return appengineClientHook(ctx)
32 }
33 return http.DefaultClient
34}