go17.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2016 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build go1.7
  5. package http2
  6. import (
  7. "context"
  8. "net"
  9. "net/http"
  10. "net/http/httptrace"
  11. "time"
  12. )
  13. type contextContext interface {
  14. context.Context
  15. }
  16. var errCanceled = context.Canceled
  17. func serverConnBaseContext(c net.Conn, opts *ServeConnOpts) (ctx contextContext, cancel func()) {
  18. ctx, cancel = context.WithCancel(context.Background())
  19. ctx = context.WithValue(ctx, http.LocalAddrContextKey, c.LocalAddr())
  20. if hs := opts.baseConfig(); hs != nil {
  21. ctx = context.WithValue(ctx, http.ServerContextKey, hs)
  22. }
  23. return
  24. }
  25. func contextWithCancel(ctx contextContext) (_ contextContext, cancel func()) {
  26. return context.WithCancel(ctx)
  27. }
  28. func requestWithContext(req *http.Request, ctx contextContext) *http.Request {
  29. return req.WithContext(ctx)
  30. }
  31. type clientTrace httptrace.ClientTrace
  32. func reqContext(r *http.Request) context.Context { return r.Context() }
  33. func (t *Transport) idleConnTimeout() time.Duration {
  34. if t.t1 != nil {
  35. return t.t1.IdleConnTimeout
  36. }
  37. return 0
  38. }
  39. func setResponseUncompressed(res *http.Response) { res.Uncompressed = true }
  40. func traceGetConn(req *http.Request, hostPort string) {
  41. trace := httptrace.ContextClientTrace(req.Context())
  42. if trace == nil || trace.GetConn == nil {
  43. return
  44. }
  45. trace.GetConn(hostPort)
  46. }
  47. func traceGotConn(req *http.Request, cc *ClientConn) {
  48. trace := httptrace.ContextClientTrace(req.Context())
  49. if trace == nil || trace.GotConn == nil {
  50. return
  51. }
  52. ci := httptrace.GotConnInfo{Conn: cc.tconn}
  53. cc.mu.Lock()
  54. ci.Reused = cc.nextStreamID > 1
  55. ci.WasIdle = len(cc.streams) == 0 && ci.Reused
  56. if ci.WasIdle && !cc.lastActive.IsZero() {
  57. ci.IdleTime = time.Now().Sub(cc.lastActive)
  58. }
  59. cc.mu.Unlock()
  60. trace.GotConn(ci)
  61. }
  62. func traceWroteHeaders(trace *clientTrace) {
  63. if trace != nil && trace.WroteHeaders != nil {
  64. trace.WroteHeaders()
  65. }
  66. }
  67. func traceGot100Continue(trace *clientTrace) {
  68. if trace != nil && trace.Got100Continue != nil {
  69. trace.Got100Continue()
  70. }
  71. }
  72. func traceWait100Continue(trace *clientTrace) {
  73. if trace != nil && trace.Wait100Continue != nil {
  74. trace.Wait100Continue()
  75. }
  76. }
  77. func traceWroteRequest(trace *clientTrace, err error) {
  78. if trace != nil && trace.WroteRequest != nil {
  79. trace.WroteRequest(httptrace.WroteRequestInfo{Err: err})
  80. }
  81. }
  82. func traceFirstResponseByte(trace *clientTrace) {
  83. if trace != nil && trace.GotFirstResponseByte != nil {
  84. trace.GotFirstResponseByte()
  85. }
  86. }
  87. func requestTrace(req *http.Request) *clientTrace {
  88. trace := httptrace.ContextClientTrace(req.Context())
  89. return (*clientTrace)(trace)
  90. }
  91. // Ping sends a PING frame to the server and waits for the ack.
  92. func (cc *ClientConn) Ping(ctx context.Context) error {
  93. return cc.ping(ctx)
  94. }
  95. // Shutdown gracefully closes the client connection, waiting for running streams to complete.
  96. func (cc *ClientConn) Shutdown(ctx context.Context) error {
  97. return cc.shutdown(ctx)
  98. }