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 var errCanceled = context.Canceled
23 func serverConnBaseContext(c net.Conn, opts *ServeConnOpts) (ctx contextContext, cancel func()) {
24 ctx, cancel = context.WithCancel(context.Background())
25 ctx = context.WithValue(ctx, http.LocalAddrContextKey, c.LocalAddr())
26 if hs := opts.baseConfig(); hs != nil {
27 ctx = context.WithValue(ctx, http.ServerContextKey, hs)
32 func contextWithCancel(ctx contextContext) (_ contextContext, cancel func()) {
33 return context.WithCancel(ctx)
36 func requestWithContext(req *http.Request, ctx contextContext) *http.Request {
37 return req.WithContext(ctx)
40 type clientTrace httptrace.ClientTrace
42 func reqContext(r *http.Request) context.Context { return r.Context() }
44 func (t *Transport) idleConnTimeout() time.Duration {
46 return t.t1.IdleConnTimeout
51 func setResponseUncompressed(res *http.Response) { res.Uncompressed = true }
53 func traceGetConn(req *http.Request, hostPort string) {
54 trace := httptrace.ContextClientTrace(req.Context())
55 if trace == nil || trace.GetConn == nil {
58 trace.GetConn(hostPort)
61 func traceGotConn(req *http.Request, cc *ClientConn) {
62 trace := httptrace.ContextClientTrace(req.Context())
63 if trace == nil || trace.GotConn == nil {
66 ci := httptrace.GotConnInfo{Conn: cc.tconn}
68 ci.Reused = cc.nextStreamID > 1
69 ci.WasIdle = len(cc.streams) == 0 && ci.Reused
70 if ci.WasIdle && !cc.lastActive.IsZero() {
71 ci.IdleTime = time.Now().Sub(cc.lastActive)
78 func traceWroteHeaders(trace *clientTrace) {
79 if trace != nil && trace.WroteHeaders != nil {
84 func traceGot100Continue(trace *clientTrace) {
85 if trace != nil && trace.Got100Continue != nil {
86 trace.Got100Continue()
90 func traceWait100Continue(trace *clientTrace) {
91 if trace != nil && trace.Wait100Continue != nil {
92 trace.Wait100Continue()
96 func traceWroteRequest(trace *clientTrace, err error) {
97 if trace != nil && trace.WroteRequest != nil {
98 trace.WroteRequest(httptrace.WroteRequestInfo{Err: err})
102 func traceFirstResponseByte(trace *clientTrace) {
103 if trace != nil && trace.GotFirstResponseByte != nil {
104 trace.GotFirstResponseByte()
108 func requestTrace(req *http.Request) *clientTrace {
109 trace := httptrace.ContextClientTrace(req.Context())
110 return (*clientTrace)(trace)
113 // Ping sends a PING frame to the server and waits for the ack.
114 func (cc *ClientConn) Ping(ctx context.Context) error {
118 // Shutdown gracefully closes the client connection, waiting for running streams to complete.
119 func (cc *ClientConn) Shutdown(ctx context.Context) error {
120 return cc.shutdown(ctx)