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.
17 type contextContext interface {
21 func serverConnBaseContext(c net.Conn, opts *ServeConnOpts) (ctx contextContext, cancel func()) {
22 ctx, cancel = context.WithCancel(context.Background())
23 ctx = context.WithValue(ctx, http.LocalAddrContextKey, c.LocalAddr())
24 if hs := opts.baseConfig(); hs != nil {
25 ctx = context.WithValue(ctx, http.ServerContextKey, hs)
30 func contextWithCancel(ctx contextContext) (_ contextContext, cancel func()) {
31 return context.WithCancel(ctx)
34 func requestWithContext(req *http.Request, ctx contextContext) *http.Request {
35 return req.WithContext(ctx)
38 type clientTrace httptrace.ClientTrace
40 func reqContext(r *http.Request) context.Context { return r.Context() }
42 func (t *Transport) idleConnTimeout() time.Duration {
44 return t.t1.IdleConnTimeout
49 func setResponseUncompressed(res *http.Response) { res.Uncompressed = true }
51 func traceGotConn(req *http.Request, cc *ClientConn) {
52 trace := httptrace.ContextClientTrace(req.Context())
53 if trace == nil || trace.GotConn == nil {
56 ci := httptrace.GotConnInfo{Conn: cc.tconn}
58 ci.Reused = cc.nextStreamID > 1
59 ci.WasIdle = len(cc.streams) == 0 && ci.Reused
60 if ci.WasIdle && !cc.lastActive.IsZero() {
61 ci.IdleTime = time.Now().Sub(cc.lastActive)
68 func traceWroteHeaders(trace *clientTrace) {
69 if trace != nil && trace.WroteHeaders != nil {
74 func traceGot100Continue(trace *clientTrace) {
75 if trace != nil && trace.Got100Continue != nil {
76 trace.Got100Continue()
80 func traceWait100Continue(trace *clientTrace) {
81 if trace != nil && trace.Wait100Continue != nil {
82 trace.Wait100Continue()
86 func traceWroteRequest(trace *clientTrace, err error) {
87 if trace != nil && trace.WroteRequest != nil {
88 trace.WroteRequest(httptrace.WroteRequestInfo{Err: err})
92 func traceFirstResponseByte(trace *clientTrace) {
93 if trace != nil && trace.GotFirstResponseByte != nil {
94 trace.GotFirstResponseByte()
98 func requestTrace(req *http.Request) *clientTrace {
99 trace := httptrace.ContextClientTrace(req.Context())
100 return (*clientTrace)(trace)
103 // Ping sends a PING frame to the server and waits for the ack.
104 func (cc *ClientConn) Ping(ctx context.Context) error {