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.
 
 
 

139 lines
4.2 KiB

  1. // Copyright 2015 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package transport/http supports network connections to HTTP servers.
  15. // This package is not intended for use by end developers. Use the
  16. // google.golang.org/api/option package to configure API clients.
  17. package http
  18. import (
  19. "errors"
  20. "net/http"
  21. "golang.org/x/net/context"
  22. "golang.org/x/oauth2"
  23. "google.golang.org/api/googleapi/transport"
  24. "google.golang.org/api/internal"
  25. "google.golang.org/api/option"
  26. )
  27. // NewClient returns an HTTP client for use communicating with a Google cloud
  28. // service, configured with the given ClientOptions. It also returns the endpoint
  29. // for the service as specified in the options.
  30. func NewClient(ctx context.Context, opts ...option.ClientOption) (*http.Client, string, error) {
  31. settings, err := newSettings(opts)
  32. if err != nil {
  33. return nil, "", err
  34. }
  35. // TODO(cbro): consider injecting the User-Agent even if an explicit HTTP client is provided?
  36. if settings.HTTPClient != nil {
  37. return settings.HTTPClient, settings.Endpoint, nil
  38. }
  39. trans, err := newTransport(ctx, defaultBaseTransport(ctx), settings)
  40. if err != nil {
  41. return nil, "", err
  42. }
  43. return &http.Client{Transport: trans}, settings.Endpoint, nil
  44. }
  45. // NewTransport creates an http.RoundTripper for use communicating with a Google
  46. // cloud service, configured with the given ClientOptions. Its RoundTrip method delegates to base.
  47. func NewTransport(ctx context.Context, base http.RoundTripper, opts ...option.ClientOption) (http.RoundTripper, error) {
  48. settings, err := newSettings(opts)
  49. if err != nil {
  50. return nil, err
  51. }
  52. if settings.HTTPClient != nil {
  53. return nil, errors.New("transport/http: WithHTTPClient passed to NewTransport")
  54. }
  55. return newTransport(ctx, base, settings)
  56. }
  57. func newTransport(ctx context.Context, base http.RoundTripper, settings *internal.DialSettings) (http.RoundTripper, error) {
  58. trans := base
  59. trans = userAgentTransport{
  60. base: trans,
  61. userAgent: settings.UserAgent,
  62. }
  63. trans = addOCTransport(trans)
  64. switch {
  65. case settings.NoAuth:
  66. // Do nothing.
  67. case settings.APIKey != "":
  68. trans = &transport.APIKey{
  69. Transport: trans,
  70. Key: settings.APIKey,
  71. }
  72. default:
  73. creds, err := internal.Creds(ctx, settings)
  74. if err != nil {
  75. return nil, err
  76. }
  77. trans = &oauth2.Transport{
  78. Base: trans,
  79. Source: creds.TokenSource,
  80. }
  81. }
  82. return trans, nil
  83. }
  84. func newSettings(opts []option.ClientOption) (*internal.DialSettings, error) {
  85. var o internal.DialSettings
  86. for _, opt := range opts {
  87. opt.Apply(&o)
  88. }
  89. if err := o.Validate(); err != nil {
  90. return nil, err
  91. }
  92. if o.GRPCConn != nil {
  93. return nil, errors.New("unsupported gRPC connection specified")
  94. }
  95. return &o, nil
  96. }
  97. type userAgentTransport struct {
  98. userAgent string
  99. base http.RoundTripper
  100. }
  101. func (t userAgentTransport) RoundTrip(req *http.Request) (*http.Response, error) {
  102. rt := t.base
  103. if rt == nil {
  104. return nil, errors.New("transport: no Transport specified")
  105. }
  106. if t.userAgent == "" {
  107. return rt.RoundTrip(req)
  108. }
  109. newReq := *req
  110. newReq.Header = make(http.Header)
  111. for k, vv := range req.Header {
  112. newReq.Header[k] = vv
  113. }
  114. // TODO(cbro): append to existing User-Agent header?
  115. newReq.Header["User-Agent"] = []string{t.userAgent}
  116. return rt.RoundTrip(&newReq)
  117. }
  118. // Set at init time by dial_appengine.go. If nil, we're not on App Engine.
  119. var appengineUrlfetchHook func(context.Context) http.RoundTripper
  120. // defaultBaseTransport returns the base HTTP transport.
  121. // On App Engine, this is urlfetch.Transport, otherwise it's http.DefaultTransport.
  122. func defaultBaseTransport(ctx context.Context) http.RoundTripper {
  123. if appengineUrlfetchHook != nil {
  124. return appengineUrlfetchHook(ctx)
  125. }
  126. return http.DefaultTransport
  127. }