github.com/gogf/gf/v2@v2.7.4/net/gclient/gclient_tracer.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gclient
     8  
     9  import (
    10  	"crypto/tls"
    11  	"net/http/httptrace"
    12  	"net/textproto"
    13  )
    14  
    15  type clientTracer struct {
    16  	*httptrace.ClientTrace
    17  }
    18  
    19  // newClientTracer creates and returns object of httptrace.ClientTrace.
    20  func newClientTracer(baseClientTracer *httptrace.ClientTrace) *httptrace.ClientTrace {
    21  	c := &clientTracer{
    22  		ClientTrace: baseClientTracer,
    23  	}
    24  	return &httptrace.ClientTrace{
    25  		GetConn:              c.GetConn,
    26  		GotConn:              c.GotConn,
    27  		PutIdleConn:          c.PutIdleConn,
    28  		GotFirstResponseByte: c.GotFirstResponseByte,
    29  		Got100Continue:       c.Got100Continue,
    30  		Got1xxResponse:       c.Got1xxResponse,
    31  		DNSStart:             c.DNSStart,
    32  		DNSDone:              c.DNSDone,
    33  		ConnectStart:         c.ConnectStart,
    34  		ConnectDone:          c.ConnectDone,
    35  		TLSHandshakeStart:    c.TLSHandshakeStart,
    36  		TLSHandshakeDone:     c.TLSHandshakeDone,
    37  		WroteHeaderField:     c.WroteHeaderField,
    38  		WroteHeaders:         c.WroteHeaders,
    39  		Wait100Continue:      c.Wait100Continue,
    40  		WroteRequest:         c.WroteRequest,
    41  	}
    42  }
    43  
    44  // GetConn is called before a connection is created or
    45  // retrieved from an idle pool. The hostPort is the
    46  // "host:port" of the target or proxy. GetConn is called even
    47  // if there's already an idle cached connection available.
    48  func (ct *clientTracer) GetConn(hostPort string) {
    49  	ct.ClientTrace.GetConn(hostPort)
    50  }
    51  
    52  // GotConn is called after a successful connection is
    53  // obtained. There is no hook for failure to obtain a
    54  // connection; instead, use the error from
    55  // Transport.RoundTrip.
    56  func (ct *clientTracer) GotConn(info httptrace.GotConnInfo) {
    57  	ct.ClientTrace.GotConn(info)
    58  }
    59  
    60  // PutIdleConn is called when the connection is returned to
    61  // the idle pool. If err is nil, the connection was
    62  // successfully returned to the idle pool. If err is non-nil,
    63  // it describes why not. PutIdleConn is not called if
    64  // connection reuse is disabled via Transport.DisableKeepAlives.
    65  // PutIdleConn is called before the caller's Response.Body.Close
    66  // call returns.
    67  // For HTTP/2, this hook is not currently used.
    68  func (ct *clientTracer) PutIdleConn(err error) {
    69  	ct.ClientTrace.PutIdleConn(err)
    70  }
    71  
    72  // GotFirstResponseByte is called when the first byte of the response
    73  // headers is available.
    74  func (ct *clientTracer) GotFirstResponseByte() {
    75  	ct.ClientTrace.GotFirstResponseByte()
    76  }
    77  
    78  // Got100Continue is called if the server replies with a "100
    79  // Continue" response.
    80  func (ct *clientTracer) Got100Continue() {
    81  	ct.ClientTrace.Got100Continue()
    82  }
    83  
    84  // Got1xxResponse is called for each 1xx informational response header
    85  // returned before the final non-1xx response. Got1xxResponse is called
    86  // for "100 Continue" responses, even if Got100Continue is also defined.
    87  // If it returns an error, the client request is aborted with that error value.
    88  func (ct *clientTracer) Got1xxResponse(code int, header textproto.MIMEHeader) error {
    89  	return ct.ClientTrace.Got1xxResponse(code, header)
    90  }
    91  
    92  // DNSStart is called when a DNS lookup begins.
    93  func (ct *clientTracer) DNSStart(info httptrace.DNSStartInfo) {
    94  	ct.ClientTrace.DNSStart(info)
    95  }
    96  
    97  // DNSDone is called when a DNS lookup ends.
    98  func (ct *clientTracer) DNSDone(info httptrace.DNSDoneInfo) {
    99  	ct.ClientTrace.DNSDone(info)
   100  }
   101  
   102  // ConnectStart is called when a new connection's Dial begins.
   103  // If net.Dialer.DualStack (IPv6 "Happy Eyeballs") support is
   104  // enabled, this may be called multiple times.
   105  func (ct *clientTracer) ConnectStart(network, addr string) {
   106  	ct.ClientTrace.ConnectStart(network, addr)
   107  }
   108  
   109  // ConnectDone is called when a new connection's Dial
   110  // completes. The provided err indicates whether the
   111  // connection completed successfully.
   112  // If net.Dialer.DualStack ("Happy Eyeballs") support is
   113  // enabled, this may be called multiple times.
   114  func (ct *clientTracer) ConnectDone(network, addr string, err error) {
   115  	ct.ClientTrace.ConnectDone(network, addr, err)
   116  }
   117  
   118  // TLSHandshakeStart is called when the TLS handshake is started. When
   119  // connecting to an HTTPS site via an HTTP proxy, the handshake happens
   120  // after the CONNECT request is processed by the proxy.
   121  func (ct *clientTracer) TLSHandshakeStart() {
   122  	ct.ClientTrace.TLSHandshakeStart()
   123  }
   124  
   125  // TLSHandshakeDone is called after the TLS handshake with either the
   126  // successful handshake's connection state, or a non-nil error on handshake
   127  // failure.
   128  func (ct *clientTracer) TLSHandshakeDone(state tls.ConnectionState, err error) {
   129  	ct.ClientTrace.TLSHandshakeDone(state, err)
   130  }
   131  
   132  // WroteHeaderField is called after the Transport has written
   133  // each request header. At the time of this call the values
   134  // might be buffered and not yet written to the network.
   135  func (ct *clientTracer) WroteHeaderField(key string, value []string) {
   136  	ct.ClientTrace.WroteHeaderField(key, value)
   137  }
   138  
   139  // WroteHeaders is called after the Transport has written
   140  // all request headers.
   141  func (ct *clientTracer) WroteHeaders() {
   142  	ct.ClientTrace.WroteHeaders()
   143  }
   144  
   145  // Wait100Continue is called if the Request specified
   146  // "Expect: 100-continue" and the Transport has written the
   147  // request headers but is waiting for "100 Continue" from the
   148  // server before writing the request body.
   149  func (ct *clientTracer) Wait100Continue() {
   150  	ct.ClientTrace.Wait100Continue()
   151  }
   152  
   153  // WroteRequest is called with the result of writing the
   154  // request and any body. It may be called multiple times
   155  // in the case of retried requests.
   156  func (ct *clientTracer) WroteRequest(info httptrace.WroteRequestInfo) {
   157  	ct.ClientTrace.WroteRequest(info)
   158  }