You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

34 lines
923 B

  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. package internal
  5. import (
  6. "context"
  7. "net/http"
  8. )
  9. // HTTPClient is the context key to use with golang.org/x/net/context's
  10. // WithValue function to associate an *http.Client value with a context.
  11. var HTTPClient ContextKey
  12. // ContextKey is just an empty struct. It exists so HTTPClient can be
  13. // an immutable public variable with a unique type. It's immutable
  14. // because nobody else can create a ContextKey, being unexported.
  15. type ContextKey struct{}
  16. var appengineClientHook func(context.Context) *http.Client
  17. func ContextClient(ctx context.Context) *http.Client {
  18. if ctx != nil {
  19. if hc, ok := ctx.Value(HTTPClient).(*http.Client); ok {
  20. return hc
  21. }
  22. }
  23. if appengineClientHook != nil {
  24. return appengineClientHook(ctx)
  25. }
  26. return http.DefaultClient
  27. }