debug.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. "strings"
  20. "sync/atomic"
  21. "time"
  22. "github.com/vmware/govmomi/vim25/debug"
  23. )
  24. // teeReader wraps io.TeeReader and patches through the Close() function.
  25. type teeReader struct {
  26. io.Reader
  27. io.Closer
  28. }
  29. func newTeeReader(rc io.ReadCloser, w io.Writer) io.ReadCloser {
  30. return teeReader{
  31. Reader: io.TeeReader(rc, w),
  32. Closer: rc,
  33. }
  34. }
  35. // debugRoundTrip contains state and logic needed to debug a single round trip.
  36. type debugRoundTrip struct {
  37. cn uint64 // Client number
  38. rn uint64 // Request number
  39. log io.WriteCloser // Request log
  40. cs []io.Closer // Files that need closing when done
  41. }
  42. func (d *debugRoundTrip) logf(format string, a ...interface{}) {
  43. now := time.Now().Format("2006-01-02T15-04-05.000000000")
  44. fmt.Fprintf(d.log, "%s - %04d: ", now, d.rn)
  45. fmt.Fprintf(d.log, format, a...)
  46. fmt.Fprintf(d.log, "\n")
  47. }
  48. func (d *debugRoundTrip) enabled() bool {
  49. return d != nil
  50. }
  51. func (d *debugRoundTrip) done() {
  52. for _, c := range d.cs {
  53. c.Close()
  54. }
  55. }
  56. func (d *debugRoundTrip) newFile(suffix string) io.WriteCloser {
  57. return debug.NewFile(fmt.Sprintf("%d-%04d.%s", d.cn, d.rn, suffix))
  58. }
  59. func (d *debugRoundTrip) ext(h http.Header) string {
  60. ext := "xml"
  61. if strings.Contains(h.Get("Content-Type"), "/json") {
  62. ext = "json"
  63. }
  64. return ext
  65. }
  66. func (d *debugRoundTrip) debugRequest(req *http.Request) {
  67. if d == nil {
  68. return
  69. }
  70. var wc io.WriteCloser
  71. // Capture headers
  72. wc = d.newFile("req.headers")
  73. b, _ := httputil.DumpRequest(req, false)
  74. wc.Write(b)
  75. wc.Close()
  76. // Capture body
  77. wc = d.newFile("req." + d.ext(req.Header))
  78. req.Body = newTeeReader(req.Body, wc)
  79. // Delay closing until marked done
  80. d.cs = append(d.cs, wc)
  81. }
  82. func (d *debugRoundTrip) debugResponse(res *http.Response) {
  83. if d == nil {
  84. return
  85. }
  86. var wc io.WriteCloser
  87. // Capture headers
  88. wc = d.newFile("res.headers")
  89. b, _ := httputil.DumpResponse(res, false)
  90. wc.Write(b)
  91. wc.Close()
  92. // Capture body
  93. wc = d.newFile("res." + d.ext(res.Header))
  94. res.Body = newTeeReader(res.Body, wc)
  95. // Delay closing until marked done
  96. d.cs = append(d.cs, wc)
  97. }
  98. var cn uint64 // Client counter
  99. // debugContainer wraps the debugging state for a single client.
  100. type debugContainer struct {
  101. cn uint64 // Client number
  102. rn uint64 // Request counter
  103. log io.WriteCloser // Request log
  104. }
  105. func newDebug() *debugContainer {
  106. d := debugContainer{
  107. cn: atomic.AddUint64(&cn, 1),
  108. rn: 0,
  109. }
  110. if !debug.Enabled() {
  111. return nil
  112. }
  113. d.log = debug.NewFile(fmt.Sprintf("%d-client.log", d.cn))
  114. return &d
  115. }
  116. func (d *debugContainer) newRoundTrip() *debugRoundTrip {
  117. if d == nil {
  118. return nil
  119. }
  120. drt := debugRoundTrip{
  121. cn: d.cn,
  122. rn: atomic.AddUint64(&d.rn, 1),
  123. log: d.log,
  124. }
  125. return &drt
  126. }