debug.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. Copyright (c) 2015 VMware, Inc. All Rights Reserved.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package soap
  14. import (
  15. "fmt"
  16. "io"
  17. "net/http"
  18. "net/http/httputil"
  19. "sync/atomic"
  20. "github.com/vmware/govmomi/vim25/debug"
  21. )
  22. var (
  23. // Trace reads an http request or response from rc and writes to w.
  24. // The content type (kind) should be one of "xml" or "json".
  25. Trace = func(rc io.ReadCloser, w io.Writer, kind string) io.ReadCloser {
  26. return debug.NewTeeReader(rc, w)
  27. }
  28. )
  29. // debugRoundTrip contains state and logic needed to debug a single round trip.
  30. type debugRoundTrip struct {
  31. cn uint64 // Client number
  32. rn uint64 // Request number
  33. cs []io.Closer // Files that need closing when done
  34. }
  35. func (d *debugRoundTrip) enabled() bool {
  36. return d != nil
  37. }
  38. func (d *debugRoundTrip) done() {
  39. for _, c := range d.cs {
  40. c.Close()
  41. }
  42. }
  43. func (d *debugRoundTrip) newFile(suffix string) io.WriteCloser {
  44. return debug.NewFile(fmt.Sprintf("%d-%04d.%s", d.cn, d.rn, suffix))
  45. }
  46. func (d *debugRoundTrip) ext(h http.Header) string {
  47. const json = "application/json"
  48. ext := "xml"
  49. if h.Get("Accept") == json || h.Get("Content-Type") == json {
  50. ext = "json"
  51. }
  52. return ext
  53. }
  54. func (d *debugRoundTrip) debugRequest(req *http.Request) string {
  55. if d == nil {
  56. return ""
  57. }
  58. // Capture headers
  59. var wc io.WriteCloser = d.newFile("req.headers")
  60. b, _ := httputil.DumpRequest(req, false)
  61. wc.Write(b)
  62. wc.Close()
  63. ext := d.ext(req.Header)
  64. // Capture body
  65. wc = d.newFile("req." + ext)
  66. if req.Body != nil {
  67. req.Body = Trace(req.Body, wc, ext)
  68. }
  69. // Delay closing until marked done
  70. d.cs = append(d.cs, wc)
  71. return ext
  72. }
  73. func (d *debugRoundTrip) debugResponse(res *http.Response, ext string) {
  74. if d == nil {
  75. return
  76. }
  77. // Capture headers
  78. var wc io.WriteCloser = d.newFile("res.headers")
  79. b, _ := httputil.DumpResponse(res, false)
  80. wc.Write(b)
  81. wc.Close()
  82. // Capture body
  83. wc = d.newFile("res." + ext)
  84. res.Body = Trace(res.Body, wc, ext)
  85. // Delay closing until marked done
  86. d.cs = append(d.cs, wc)
  87. }
  88. var cn uint64 // Client counter
  89. // debugContainer wraps the debugging state for a single client.
  90. type debugContainer struct {
  91. cn uint64 // Client number
  92. rn uint64 // Request counter
  93. }
  94. func newDebug() *debugContainer {
  95. d := debugContainer{
  96. cn: atomic.AddUint64(&cn, 1),
  97. rn: 0,
  98. }
  99. if !debug.Enabled() {
  100. return nil
  101. }
  102. return &d
  103. }
  104. func (d *debugContainer) newRoundTrip() *debugRoundTrip {
  105. if d == nil {
  106. return nil
  107. }
  108. drt := debugRoundTrip{
  109. cn: d.cn,
  110. rn: atomic.AddUint64(&d.rn, 1),
  111. }
  112. return &drt
  113. }