round_trippers.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  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 transport
  14. import (
  15. "crypto/tls"
  16. "fmt"
  17. "net/http"
  18. "net/http/httptrace"
  19. "strings"
  20. "sync"
  21. "time"
  22. "golang.org/x/oauth2"
  23. utilnet "k8s.io/apimachinery/pkg/util/net"
  24. "k8s.io/klog/v2"
  25. )
  26. // HTTPWrappersForConfig wraps a round tripper with any relevant layered
  27. // behavior from the config. Exposed to allow more clients that need HTTP-like
  28. // behavior but then must hijack the underlying connection (like WebSocket or
  29. // HTTP2 clients). Pure HTTP clients should use the RoundTripper returned from
  30. // New.
  31. func HTTPWrappersForConfig(config *Config, rt http.RoundTripper) (http.RoundTripper, error) {
  32. if config.WrapTransport != nil {
  33. rt = config.WrapTransport(rt)
  34. }
  35. rt = DebugWrappers(rt)
  36. // Set authentication wrappers
  37. switch {
  38. case config.HasBasicAuth() && config.HasTokenAuth():
  39. return nil, fmt.Errorf("username/password or bearer token may be set, but not both")
  40. case config.HasTokenAuth():
  41. var err error
  42. rt, err = NewBearerAuthWithRefreshRoundTripper(config.BearerToken, config.BearerTokenFile, rt)
  43. if err != nil {
  44. return nil, err
  45. }
  46. case config.HasBasicAuth():
  47. rt = NewBasicAuthRoundTripper(config.Username, config.Password, rt)
  48. }
  49. if len(config.UserAgent) > 0 {
  50. rt = NewUserAgentRoundTripper(config.UserAgent, rt)
  51. }
  52. if len(config.Impersonate.UserName) > 0 ||
  53. len(config.Impersonate.UID) > 0 ||
  54. len(config.Impersonate.Groups) > 0 ||
  55. len(config.Impersonate.Extra) > 0 {
  56. rt = NewImpersonatingRoundTripper(config.Impersonate, rt)
  57. }
  58. return rt, nil
  59. }
  60. // DebugWrappers wraps a round tripper and logs based on the current log level.
  61. func DebugWrappers(rt http.RoundTripper) http.RoundTripper {
  62. switch {
  63. case bool(klog.V(9).Enabled()):
  64. rt = NewDebuggingRoundTripper(rt, DebugCurlCommand, DebugURLTiming, DebugDetailedTiming, DebugResponseHeaders)
  65. case bool(klog.V(8).Enabled()):
  66. rt = NewDebuggingRoundTripper(rt, DebugJustURL, DebugRequestHeaders, DebugResponseStatus, DebugResponseHeaders)
  67. case bool(klog.V(7).Enabled()):
  68. rt = NewDebuggingRoundTripper(rt, DebugJustURL, DebugRequestHeaders, DebugResponseStatus)
  69. case bool(klog.V(6).Enabled()):
  70. rt = NewDebuggingRoundTripper(rt, DebugURLTiming)
  71. }
  72. return rt
  73. }
  74. type authProxyRoundTripper struct {
  75. username string
  76. groups []string
  77. extra map[string][]string
  78. rt http.RoundTripper
  79. }
  80. var _ utilnet.RoundTripperWrapper = &authProxyRoundTripper{}
  81. // NewAuthProxyRoundTripper provides a roundtripper which will add auth proxy fields to requests for
  82. // authentication terminating proxy cases
  83. // assuming you pull the user from the context:
  84. // username is the user.Info.GetName() of the user
  85. // groups is the user.Info.GetGroups() of the user
  86. // extra is the user.Info.GetExtra() of the user
  87. // extra can contain any additional information that the authenticator
  88. // thought was interesting, for example authorization scopes.
  89. // In order to faithfully round-trip through an impersonation flow, these keys
  90. // MUST be lowercase.
  91. func NewAuthProxyRoundTripper(username string, groups []string, extra map[string][]string, rt http.RoundTripper) http.RoundTripper {
  92. return &authProxyRoundTripper{
  93. username: username,
  94. groups: groups,
  95. extra: extra,
  96. rt: rt,
  97. }
  98. }
  99. func (rt *authProxyRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
  100. req = utilnet.CloneRequest(req)
  101. SetAuthProxyHeaders(req, rt.username, rt.groups, rt.extra)
  102. return rt.rt.RoundTrip(req)
  103. }
  104. // SetAuthProxyHeaders stomps the auth proxy header fields. It mutates its argument.
  105. func SetAuthProxyHeaders(req *http.Request, username string, groups []string, extra map[string][]string) {
  106. req.Header.Del("X-Remote-User")
  107. req.Header.Del("X-Remote-Group")
  108. for key := range req.Header {
  109. if strings.HasPrefix(strings.ToLower(key), strings.ToLower("X-Remote-Extra-")) {
  110. req.Header.Del(key)
  111. }
  112. }
  113. req.Header.Set("X-Remote-User", username)
  114. for _, group := range groups {
  115. req.Header.Add("X-Remote-Group", group)
  116. }
  117. for key, values := range extra {
  118. for _, value := range values {
  119. req.Header.Add("X-Remote-Extra-"+headerKeyEscape(key), value)
  120. }
  121. }
  122. }
  123. func (rt *authProxyRoundTripper) CancelRequest(req *http.Request) {
  124. tryCancelRequest(rt.WrappedRoundTripper(), req)
  125. }
  126. func (rt *authProxyRoundTripper) WrappedRoundTripper() http.RoundTripper { return rt.rt }
  127. type userAgentRoundTripper struct {
  128. agent string
  129. rt http.RoundTripper
  130. }
  131. var _ utilnet.RoundTripperWrapper = &userAgentRoundTripper{}
  132. // NewUserAgentRoundTripper will add User-Agent header to a request unless it has already been set.
  133. func NewUserAgentRoundTripper(agent string, rt http.RoundTripper) http.RoundTripper {
  134. return &userAgentRoundTripper{agent, rt}
  135. }
  136. func (rt *userAgentRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
  137. if len(req.Header.Get("User-Agent")) != 0 {
  138. return rt.rt.RoundTrip(req)
  139. }
  140. req = utilnet.CloneRequest(req)
  141. req.Header.Set("User-Agent", rt.agent)
  142. return rt.rt.RoundTrip(req)
  143. }
  144. func (rt *userAgentRoundTripper) CancelRequest(req *http.Request) {
  145. tryCancelRequest(rt.WrappedRoundTripper(), req)
  146. }
  147. func (rt *userAgentRoundTripper) WrappedRoundTripper() http.RoundTripper { return rt.rt }
  148. type basicAuthRoundTripper struct {
  149. username string
  150. password string `datapolicy:"password"`
  151. rt http.RoundTripper
  152. }
  153. var _ utilnet.RoundTripperWrapper = &basicAuthRoundTripper{}
  154. // NewBasicAuthRoundTripper will apply a BASIC auth authorization header to a
  155. // request unless it has already been set.
  156. func NewBasicAuthRoundTripper(username, password string, rt http.RoundTripper) http.RoundTripper {
  157. return &basicAuthRoundTripper{username, password, rt}
  158. }
  159. func (rt *basicAuthRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
  160. if len(req.Header.Get("Authorization")) != 0 {
  161. return rt.rt.RoundTrip(req)
  162. }
  163. req = utilnet.CloneRequest(req)
  164. req.SetBasicAuth(rt.username, rt.password)
  165. return rt.rt.RoundTrip(req)
  166. }
  167. func (rt *basicAuthRoundTripper) CancelRequest(req *http.Request) {
  168. tryCancelRequest(rt.WrappedRoundTripper(), req)
  169. }
  170. func (rt *basicAuthRoundTripper) WrappedRoundTripper() http.RoundTripper { return rt.rt }
  171. // These correspond to the headers used in pkg/apis/authentication. We don't want the package dependency,
  172. // but you must not change the values.
  173. const (
  174. // ImpersonateUserHeader is used to impersonate a particular user during an API server request
  175. ImpersonateUserHeader = "Impersonate-User"
  176. // ImpersonateUIDHeader is used to impersonate a particular UID during an API server request
  177. ImpersonateUIDHeader = "Impersonate-Uid"
  178. // ImpersonateGroupHeader is used to impersonate a particular group during an API server request.
  179. // It can be repeated multiplied times for multiple groups.
  180. ImpersonateGroupHeader = "Impersonate-Group"
  181. // ImpersonateUserExtraHeaderPrefix is a prefix for a header used to impersonate an entry in the
  182. // extra map[string][]string for user.Info. The key for the `extra` map is suffix.
  183. // The same key can be repeated multiple times to have multiple elements in the slice under a single key.
  184. // For instance:
  185. // Impersonate-Extra-Foo: one
  186. // Impersonate-Extra-Foo: two
  187. // results in extra["Foo"] = []string{"one", "two"}
  188. ImpersonateUserExtraHeaderPrefix = "Impersonate-Extra-"
  189. )
  190. type impersonatingRoundTripper struct {
  191. impersonate ImpersonationConfig
  192. delegate http.RoundTripper
  193. }
  194. var _ utilnet.RoundTripperWrapper = &impersonatingRoundTripper{}
  195. // NewImpersonatingRoundTripper will add an Act-As header to a request unless it has already been set.
  196. func NewImpersonatingRoundTripper(impersonate ImpersonationConfig, delegate http.RoundTripper) http.RoundTripper {
  197. return &impersonatingRoundTripper{impersonate, delegate}
  198. }
  199. func (rt *impersonatingRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
  200. // use the user header as marker for the rest.
  201. if len(req.Header.Get(ImpersonateUserHeader)) != 0 {
  202. return rt.delegate.RoundTrip(req)
  203. }
  204. req = utilnet.CloneRequest(req)
  205. req.Header.Set(ImpersonateUserHeader, rt.impersonate.UserName)
  206. if rt.impersonate.UID != "" {
  207. req.Header.Set(ImpersonateUIDHeader, rt.impersonate.UID)
  208. }
  209. for _, group := range rt.impersonate.Groups {
  210. req.Header.Add(ImpersonateGroupHeader, group)
  211. }
  212. for k, vv := range rt.impersonate.Extra {
  213. for _, v := range vv {
  214. req.Header.Add(ImpersonateUserExtraHeaderPrefix+headerKeyEscape(k), v)
  215. }
  216. }
  217. return rt.delegate.RoundTrip(req)
  218. }
  219. func (rt *impersonatingRoundTripper) CancelRequest(req *http.Request) {
  220. tryCancelRequest(rt.WrappedRoundTripper(), req)
  221. }
  222. func (rt *impersonatingRoundTripper) WrappedRoundTripper() http.RoundTripper { return rt.delegate }
  223. type bearerAuthRoundTripper struct {
  224. bearer string
  225. source oauth2.TokenSource
  226. rt http.RoundTripper
  227. }
  228. var _ utilnet.RoundTripperWrapper = &bearerAuthRoundTripper{}
  229. // NewBearerAuthRoundTripper adds the provided bearer token to a request
  230. // unless the authorization header has already been set.
  231. func NewBearerAuthRoundTripper(bearer string, rt http.RoundTripper) http.RoundTripper {
  232. return &bearerAuthRoundTripper{bearer, nil, rt}
  233. }
  234. // NewBearerAuthWithRefreshRoundTripper adds the provided bearer token to a request
  235. // unless the authorization header has already been set.
  236. // If tokenFile is non-empty, it is periodically read,
  237. // and the last successfully read content is used as the bearer token.
  238. // If tokenFile is non-empty and bearer is empty, the tokenFile is read
  239. // immediately to populate the initial bearer token.
  240. func NewBearerAuthWithRefreshRoundTripper(bearer string, tokenFile string, rt http.RoundTripper) (http.RoundTripper, error) {
  241. if len(tokenFile) == 0 {
  242. return &bearerAuthRoundTripper{bearer, nil, rt}, nil
  243. }
  244. source := NewCachedFileTokenSource(tokenFile)
  245. if len(bearer) == 0 {
  246. token, err := source.Token()
  247. if err != nil {
  248. return nil, err
  249. }
  250. bearer = token.AccessToken
  251. }
  252. return &bearerAuthRoundTripper{bearer, source, rt}, nil
  253. }
  254. func (rt *bearerAuthRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
  255. if len(req.Header.Get("Authorization")) != 0 {
  256. return rt.rt.RoundTrip(req)
  257. }
  258. req = utilnet.CloneRequest(req)
  259. token := rt.bearer
  260. if rt.source != nil {
  261. if refreshedToken, err := rt.source.Token(); err == nil {
  262. token = refreshedToken.AccessToken
  263. }
  264. }
  265. req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
  266. return rt.rt.RoundTrip(req)
  267. }
  268. func (rt *bearerAuthRoundTripper) CancelRequest(req *http.Request) {
  269. tryCancelRequest(rt.WrappedRoundTripper(), req)
  270. }
  271. func (rt *bearerAuthRoundTripper) WrappedRoundTripper() http.RoundTripper { return rt.rt }
  272. // requestInfo keeps track of information about a request/response combination
  273. type requestInfo struct {
  274. RequestHeaders http.Header `datapolicy:"token"`
  275. RequestVerb string
  276. RequestURL string
  277. ResponseStatus string
  278. ResponseHeaders http.Header
  279. ResponseErr error
  280. muTrace sync.Mutex // Protect trace fields
  281. DNSLookup time.Duration
  282. Dialing time.Duration
  283. GetConnection time.Duration
  284. TLSHandshake time.Duration
  285. ServerProcessing time.Duration
  286. ConnectionReused bool
  287. Duration time.Duration
  288. }
  289. // newRequestInfo creates a new RequestInfo based on an http request
  290. func newRequestInfo(req *http.Request) *requestInfo {
  291. return &requestInfo{
  292. RequestURL: req.URL.String(),
  293. RequestVerb: req.Method,
  294. RequestHeaders: req.Header,
  295. }
  296. }
  297. // complete adds information about the response to the requestInfo
  298. func (r *requestInfo) complete(response *http.Response, err error) {
  299. if err != nil {
  300. r.ResponseErr = err
  301. return
  302. }
  303. r.ResponseStatus = response.Status
  304. r.ResponseHeaders = response.Header
  305. }
  306. // toCurl returns a string that can be run as a command in a terminal (minus the body)
  307. func (r *requestInfo) toCurl() string {
  308. headers := ""
  309. for key, values := range r.RequestHeaders {
  310. for _, value := range values {
  311. value = maskValue(key, value)
  312. headers += fmt.Sprintf(` -H %q`, fmt.Sprintf("%s: %s", key, value))
  313. }
  314. }
  315. return fmt.Sprintf("curl -v -X%s %s '%s'", r.RequestVerb, headers, r.RequestURL)
  316. }
  317. // debuggingRoundTripper will display information about the requests passing
  318. // through it based on what is configured
  319. type debuggingRoundTripper struct {
  320. delegatedRoundTripper http.RoundTripper
  321. levels map[DebugLevel]bool
  322. }
  323. var _ utilnet.RoundTripperWrapper = &debuggingRoundTripper{}
  324. // DebugLevel is used to enable debugging of certain
  325. // HTTP requests and responses fields via the debuggingRoundTripper.
  326. type DebugLevel int
  327. const (
  328. // DebugJustURL will add to the debug output HTTP requests method and url.
  329. DebugJustURL DebugLevel = iota
  330. // DebugURLTiming will add to the debug output the duration of HTTP requests.
  331. DebugURLTiming
  332. // DebugCurlCommand will add to the debug output the curl command equivalent to the
  333. // HTTP request.
  334. DebugCurlCommand
  335. // DebugRequestHeaders will add to the debug output the HTTP requests headers.
  336. DebugRequestHeaders
  337. // DebugResponseStatus will add to the debug output the HTTP response status.
  338. DebugResponseStatus
  339. // DebugResponseHeaders will add to the debug output the HTTP response headers.
  340. DebugResponseHeaders
  341. // DebugDetailedTiming will add to the debug output the duration of the HTTP requests events.
  342. DebugDetailedTiming
  343. )
  344. // NewDebuggingRoundTripper allows to display in the logs output debug information
  345. // on the API requests performed by the client.
  346. func NewDebuggingRoundTripper(rt http.RoundTripper, levels ...DebugLevel) http.RoundTripper {
  347. drt := &debuggingRoundTripper{
  348. delegatedRoundTripper: rt,
  349. levels: make(map[DebugLevel]bool, len(levels)),
  350. }
  351. for _, v := range levels {
  352. drt.levels[v] = true
  353. }
  354. return drt
  355. }
  356. func (rt *debuggingRoundTripper) CancelRequest(req *http.Request) {
  357. tryCancelRequest(rt.WrappedRoundTripper(), req)
  358. }
  359. var knownAuthTypes = map[string]bool{
  360. "bearer": true,
  361. "basic": true,
  362. "negotiate": true,
  363. }
  364. // maskValue masks credential content from authorization headers
  365. // See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization
  366. func maskValue(key string, value string) string {
  367. if !strings.EqualFold(key, "Authorization") {
  368. return value
  369. }
  370. if len(value) == 0 {
  371. return ""
  372. }
  373. var authType string
  374. if i := strings.Index(value, " "); i > 0 {
  375. authType = value[0:i]
  376. } else {
  377. authType = value
  378. }
  379. if !knownAuthTypes[strings.ToLower(authType)] {
  380. return "<masked>"
  381. }
  382. if len(value) > len(authType)+1 {
  383. value = authType + " <masked>"
  384. } else {
  385. value = authType
  386. }
  387. return value
  388. }
  389. func (rt *debuggingRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
  390. reqInfo := newRequestInfo(req)
  391. if rt.levels[DebugJustURL] {
  392. klog.Infof("%s %s", reqInfo.RequestVerb, reqInfo.RequestURL)
  393. }
  394. if rt.levels[DebugCurlCommand] {
  395. klog.Infof("%s", reqInfo.toCurl())
  396. }
  397. if rt.levels[DebugRequestHeaders] {
  398. klog.Info("Request Headers:")
  399. for key, values := range reqInfo.RequestHeaders {
  400. for _, value := range values {
  401. value = maskValue(key, value)
  402. klog.Infof(" %s: %s", key, value)
  403. }
  404. }
  405. }
  406. startTime := time.Now()
  407. if rt.levels[DebugDetailedTiming] {
  408. var getConn, dnsStart, dialStart, tlsStart, serverStart time.Time
  409. var host string
  410. trace := &httptrace.ClientTrace{
  411. // DNS
  412. DNSStart: func(info httptrace.DNSStartInfo) {
  413. reqInfo.muTrace.Lock()
  414. defer reqInfo.muTrace.Unlock()
  415. dnsStart = time.Now()
  416. host = info.Host
  417. },
  418. DNSDone: func(info httptrace.DNSDoneInfo) {
  419. reqInfo.muTrace.Lock()
  420. defer reqInfo.muTrace.Unlock()
  421. reqInfo.DNSLookup = time.Since(dnsStart)
  422. klog.Infof("HTTP Trace: DNS Lookup for %s resolved to %v", host, info.Addrs)
  423. },
  424. // Dial
  425. ConnectStart: func(network, addr string) {
  426. reqInfo.muTrace.Lock()
  427. defer reqInfo.muTrace.Unlock()
  428. dialStart = time.Now()
  429. },
  430. ConnectDone: func(network, addr string, err error) {
  431. reqInfo.muTrace.Lock()
  432. defer reqInfo.muTrace.Unlock()
  433. reqInfo.Dialing = time.Since(dialStart)
  434. if err != nil {
  435. klog.Infof("HTTP Trace: Dial to %s:%s failed: %v", network, addr, err)
  436. } else {
  437. klog.Infof("HTTP Trace: Dial to %s:%s succeed", network, addr)
  438. }
  439. },
  440. // TLS
  441. TLSHandshakeStart: func() {
  442. tlsStart = time.Now()
  443. },
  444. TLSHandshakeDone: func(_ tls.ConnectionState, _ error) {
  445. reqInfo.muTrace.Lock()
  446. defer reqInfo.muTrace.Unlock()
  447. reqInfo.TLSHandshake = time.Since(tlsStart)
  448. },
  449. // Connection (it can be DNS + Dial or just the time to get one from the connection pool)
  450. GetConn: func(hostPort string) {
  451. getConn = time.Now()
  452. },
  453. GotConn: func(info httptrace.GotConnInfo) {
  454. reqInfo.muTrace.Lock()
  455. defer reqInfo.muTrace.Unlock()
  456. reqInfo.GetConnection = time.Since(getConn)
  457. reqInfo.ConnectionReused = info.Reused
  458. },
  459. // Server Processing (time since we wrote the request until first byte is received)
  460. WroteRequest: func(info httptrace.WroteRequestInfo) {
  461. reqInfo.muTrace.Lock()
  462. defer reqInfo.muTrace.Unlock()
  463. serverStart = time.Now()
  464. },
  465. GotFirstResponseByte: func() {
  466. reqInfo.muTrace.Lock()
  467. defer reqInfo.muTrace.Unlock()
  468. reqInfo.ServerProcessing = time.Since(serverStart)
  469. },
  470. }
  471. req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
  472. }
  473. response, err := rt.delegatedRoundTripper.RoundTrip(req)
  474. reqInfo.Duration = time.Since(startTime)
  475. reqInfo.complete(response, err)
  476. if rt.levels[DebugURLTiming] {
  477. klog.Infof("%s %s %s in %d milliseconds", reqInfo.RequestVerb, reqInfo.RequestURL, reqInfo.ResponseStatus, reqInfo.Duration.Nanoseconds()/int64(time.Millisecond))
  478. }
  479. if rt.levels[DebugDetailedTiming] {
  480. stats := ""
  481. if !reqInfo.ConnectionReused {
  482. stats += fmt.Sprintf(`DNSLookup %d ms Dial %d ms TLSHandshake %d ms`,
  483. reqInfo.DNSLookup.Nanoseconds()/int64(time.Millisecond),
  484. reqInfo.Dialing.Nanoseconds()/int64(time.Millisecond),
  485. reqInfo.TLSHandshake.Nanoseconds()/int64(time.Millisecond),
  486. )
  487. } else {
  488. stats += fmt.Sprintf(`GetConnection %d ms`, reqInfo.GetConnection.Nanoseconds()/int64(time.Millisecond))
  489. }
  490. if reqInfo.ServerProcessing != 0 {
  491. stats += fmt.Sprintf(` ServerProcessing %d ms`, reqInfo.ServerProcessing.Nanoseconds()/int64(time.Millisecond))
  492. }
  493. stats += fmt.Sprintf(` Duration %d ms`, reqInfo.Duration.Nanoseconds()/int64(time.Millisecond))
  494. klog.Infof("HTTP Statistics: %s", stats)
  495. }
  496. if rt.levels[DebugResponseStatus] {
  497. klog.Infof("Response Status: %s in %d milliseconds", reqInfo.ResponseStatus, reqInfo.Duration.Nanoseconds()/int64(time.Millisecond))
  498. }
  499. if rt.levels[DebugResponseHeaders] {
  500. klog.Info("Response Headers:")
  501. for key, values := range reqInfo.ResponseHeaders {
  502. for _, value := range values {
  503. klog.Infof(" %s: %s", key, value)
  504. }
  505. }
  506. }
  507. return response, err
  508. }
  509. func (rt *debuggingRoundTripper) WrappedRoundTripper() http.RoundTripper {
  510. return rt.delegatedRoundTripper
  511. }
  512. func legalHeaderByte(b byte) bool {
  513. return int(b) < len(legalHeaderKeyBytes) && legalHeaderKeyBytes[b]
  514. }
  515. func shouldEscape(b byte) bool {
  516. // url.PathUnescape() returns an error if any '%' is not followed by two
  517. // hexadecimal digits, so we'll intentionally encode it.
  518. return !legalHeaderByte(b) || b == '%'
  519. }
  520. func headerKeyEscape(key string) string {
  521. buf := strings.Builder{}
  522. for i := 0; i < len(key); i++ {
  523. b := key[i]
  524. if shouldEscape(b) {
  525. // %-encode bytes that should be escaped:
  526. // https://tools.ietf.org/html/rfc3986#section-2.1
  527. fmt.Fprintf(&buf, "%%%02X", b)
  528. continue
  529. }
  530. buf.WriteByte(b)
  531. }
  532. return buf.String()
  533. }
  534. // legalHeaderKeyBytes was copied from net/http/lex.go's isTokenTable.
  535. // See https://httpwg.github.io/specs/rfc7230.html#rule.token.separators
  536. var legalHeaderKeyBytes = [127]bool{
  537. '%': true,
  538. '!': true,
  539. '#': true,
  540. '$': true,
  541. '&': true,
  542. '\'': true,
  543. '*': true,
  544. '+': true,
  545. '-': true,
  546. '.': true,
  547. '0': true,
  548. '1': true,
  549. '2': true,
  550. '3': true,
  551. '4': true,
  552. '5': true,
  553. '6': true,
  554. '7': true,
  555. '8': true,
  556. '9': true,
  557. 'A': true,
  558. 'B': true,
  559. 'C': true,
  560. 'D': true,
  561. 'E': true,
  562. 'F': true,
  563. 'G': true,
  564. 'H': true,
  565. 'I': true,
  566. 'J': true,
  567. 'K': true,
  568. 'L': true,
  569. 'M': true,
  570. 'N': true,
  571. 'O': true,
  572. 'P': true,
  573. 'Q': true,
  574. 'R': true,
  575. 'S': true,
  576. 'T': true,
  577. 'U': true,
  578. 'W': true,
  579. 'V': true,
  580. 'X': true,
  581. 'Y': true,
  582. 'Z': true,
  583. '^': true,
  584. '_': true,
  585. '`': true,
  586. 'a': true,
  587. 'b': true,
  588. 'c': true,
  589. 'd': true,
  590. 'e': true,
  591. 'f': true,
  592. 'g': true,
  593. 'h': true,
  594. 'i': true,
  595. 'j': true,
  596. 'k': true,
  597. 'l': true,
  598. 'm': true,
  599. 'n': true,
  600. 'o': true,
  601. 'p': true,
  602. 'q': true,
  603. 'r': true,
  604. 's': true,
  605. 't': true,
  606. 'u': true,
  607. 'v': true,
  608. 'w': true,
  609. 'x': true,
  610. 'y': true,
  611. 'z': true,
  612. '|': true,
  613. '~': true,
  614. }