cloud.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2014 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 internal provides support for the cloud packages.
  15. //
  16. // Users should not import this package directly.
  17. package internal
  18. import (
  19. "fmt"
  20. "net/http"
  21. )
  22. const userAgent = "gcloud-golang/0.1"
  23. // Transport is an http.RoundTripper that appends Google Cloud client's
  24. // user-agent to the original request's user-agent header.
  25. type Transport struct {
  26. // TODO(bradfitz): delete internal.Transport. It's too wrappy for what it does.
  27. // Do User-Agent some other way.
  28. // Base is the actual http.RoundTripper
  29. // requests will use. It must not be nil.
  30. Base http.RoundTripper
  31. }
  32. // RoundTrip appends a user-agent to the existing user-agent
  33. // header and delegates the request to the base http.RoundTripper.
  34. func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
  35. req = cloneRequest(req)
  36. ua := req.Header.Get("User-Agent")
  37. if ua == "" {
  38. ua = userAgent
  39. } else {
  40. ua = fmt.Sprintf("%s %s", ua, userAgent)
  41. }
  42. req.Header.Set("User-Agent", ua)
  43. return t.Base.RoundTrip(req)
  44. }
  45. // cloneRequest returns a clone of the provided *http.Request.
  46. // The clone is a shallow copy of the struct and its Header map.
  47. func cloneRequest(r *http.Request) *http.Request {
  48. // shallow copy of the struct
  49. r2 := new(http.Request)
  50. *r2 = *r
  51. // deep copy of the Header
  52. r2.Header = make(http.Header)
  53. for k, s := range r.Header {
  54. r2.Header[k] = s
  55. }
  56. return r2
  57. }