go17.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. func serverConnBaseContext(c net.Conn, opts *ServeConnOpts) (ctx contextContext, cancel func()) {
  17. ctx, cancel = context.WithCancel(context.Background())
  18. ctx = context.WithValue(ctx, http.LocalAddrContextKey, c.LocalAddr())
  19. if hs := opts.baseConfig(); hs != nil {
  20. ctx = context.WithValue(ctx, http.ServerContextKey, hs)
  21. }
  22. return
  23. }
  24. func contextWithCancel(ctx contextContext) (_ contextContext, cancel func()) {
  25. return context.WithCancel(ctx)
  26. }
  27. func requestWithContext(req *http.Request, ctx contextContext) *http.Request {
  28. return req.WithContext(ctx)
  29. }
  30. type clientTrace httptrace.ClientTrace
  31. func reqContext(r *http.Request) context.Context { return r.Context() }
  32. func (t *Transport) idleConnTimeout() time.Duration {
  33. if t.t1 != nil {
  34. return t.t1.IdleConnTimeout
  35. }
  36. return 0
  37. }
  38. func setResponseUncompressed(res *http.Response) { res.Uncompressed = true }
  39. func traceGotConn(req *http.Request, cc *ClientConn) {
  40. trace := httptrace.ContextClientTrace(req.Context())
  41. if trace == nil || trace.GotConn == nil {
  42. return
  43. }
  44. ci := httptrace.GotConnInfo{Conn: cc.tconn}
  45. cc.mu.Lock()
  46. ci.Reused = cc.nextStreamID > 1
  47. ci.WasIdle = len(cc.streams) == 0 && ci.Reused
  48. if ci.WasIdle && !cc.lastActive.IsZero() {
  49. ci.IdleTime = time.Now().Sub(cc.lastActive)
  50. }
  51. cc.mu.Unlock()
  52. trace.GotConn(ci)
  53. }
  54. func traceWroteHeaders(trace *clientTrace) {
  55. if trace != nil && trace.WroteHeaders != nil {
  56. trace.WroteHeaders()
  57. }
  58. }
  59. func traceGot100Continue(trace *clientTrace) {
  60. if trace != nil && trace.Got100Continue != nil {
  61. trace.Got100Continue()
  62. }
  63. }
  64. func traceWait100Continue(trace *clientTrace) {
  65. if trace != nil && trace.Wait100Continue != nil {
  66. trace.Wait100Continue()
  67. }
  68. }
  69. func traceWroteRequest(trace *clientTrace, err error) {
  70. if trace != nil && trace.WroteRequest != nil {
  71. trace.WroteRequest(httptrace.WroteRequestInfo{Err: err})
  72. }
  73. }
  74. func traceFirstResponseByte(trace *clientTrace) {
  75. if trace != nil && trace.GotFirstResponseByte != nil {
  76. trace.GotFirstResponseByte()
  77. }
  78. }
  79. func requestTrace(req *http.Request) *clientTrace {
  80. trace := httptrace.ContextClientTrace(req.Context())
  81. return (*clientTrace)(trace)
  82. }
  83. // Ping sends a PING frame to the server and waits for the ack.
  84. func (cc *ClientConn) Ping(ctx context.Context) error {
  85. return cc.ping(ctx)
  86. }