本文整理汇总了Golang中net/http/httputil.ClientConn.Close方法的典型用法代码示例。如果您正苦于以下问题:Golang ClientConn.Close方法的具体用法?Golang ClientConn.Close怎么用?Golang ClientConn.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net/http/httputil.ClientConn
的用法示例。
在下文中一共展示了ClientConn.Close方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: handle
func (s *httpService) handle(req *http.Request, sc *httputil.ServerConn, tls, sticky bool) (done bool) {
req.Header.Set("X-Request-Start", strconv.FormatInt(time.Now().UnixNano()/int64(time.Millisecond), 10))
req.Header.Set("X-Request-Id", random.UUID())
var backend *httputil.ClientConn
var stickyCookie *http.Cookie
if sticky {
backend, stickyCookie = s.getBackendSticky(req)
} else {
backend = s.getBackend()
}
if backend == nil {
log.Println("no backend found")
fail(sc, req, 503, "Service Unavailable")
return
}
defer backend.Close()
req.Proto = "HTTP/1.1"
req.ProtoMajor = 1
req.ProtoMinor = 1
delete(req.Header, "Te")
delete(req.Header, "Transfer-Encoding")
if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
// If we aren't the first proxy retain prior
// X-Forwarded-For information as a comma+space
// separated list and fold multiple headers into one.
if prior, ok := req.Header["X-Forwarded-For"]; ok {
clientIP = strings.Join(prior, ", ") + ", " + clientIP
}
req.Header.Set("X-Forwarded-For", clientIP)
}
if tls {
req.Header.Set("X-Forwarded-Proto", "https")
} else {
req.Header.Set("X-Forwarded-Proto", "http")
}
// TODO: Set X-Forwarded-Port
// Pass the Request-URI verbatim without any modifications
req.URL.Opaque = strings.Split(strings.TrimPrefix(req.RequestURI, req.URL.Scheme+":"), "?")[0]
if err := backend.Write(req); err != nil {
log.Println("server write err:", err)
// TODO: return error to client here
return true
}
res, err := backend.Read(req)
if res != nil {
if stickyCookie != nil {
res.Header.Add("Set-Cookie", stickyCookie.String())
}
if res.StatusCode == http.StatusSwitchingProtocols {
res.Body = nil
}
if err := sc.Write(req, res); err != nil {
if err != io.EOF && err != httputil.ErrPersistEOF {
log.Println("client write err:", err)
// TODO: log error
}
return true
}
}
if err != nil {
if err != io.EOF && err != httputil.ErrPersistEOF {
log.Println("server read err:", err)
// TODO: log error
fail(sc, req, 502, "Bad Gateway")
}
return
}
// TODO: Proxy HTTP CONNECT? (example: Go RPC over HTTP)
if res.StatusCode == http.StatusSwitchingProtocols {
serverW, serverR := backend.Hijack()
clientW, clientR := sc.Hijack()
defer serverW.Close()
done := make(chan struct{})
go func() {
serverR.WriteTo(clientW)
if cw, ok := clientW.(writeCloser); ok {
cw.CloseWrite()
}
close(done)
}()
clientR.WriteTo(serverW)
serverW.(writeCloser).CloseWrite()
<-done
return true
}
return
}
示例2: handle
func (s *httpService) handle(req *http.Request, sc *httputil.ServerConn, tls, sticky bool) {
for {
req.Header.Set("X-Request-Start", strconv.FormatInt(time.Now().UnixNano()/int64(time.Millisecond), 10))
var backend *httputil.ClientConn
var stickyCookie *http.Cookie
if sticky {
backend, stickyCookie = s.getBackendSticky(req)
} else {
backend = s.getBackend()
}
if backend == nil {
log.Println("no backend found")
fail(sc, req, 503, "Service Unavailable")
return
}
if req.Method != "GET" && req.Method != "POST" && req.Method != "HEAD" &&
req.Method != "OPTIONS" && req.Method != "PUT" && req.Method != "DELETE" && req.Method != "TRACE" {
fail(sc, req, 405, "Method not allowed")
return
}
req.Proto = "HTTP/1.1"
req.ProtoMajor = 1
req.ProtoMinor = 1
delete(req.Header, "Te")
delete(req.Header, "Transfer-Encoding")
if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
// If we aren't the first proxy retain prior
// X-Forwarded-For information as a comma+space
// separated list and fold multiple headers into one.
if prior, ok := req.Header["X-Forwarded-For"]; ok {
clientIP = strings.Join(prior, ", ") + ", " + clientIP
}
req.Header.Set("X-Forwarded-For", clientIP)
}
if tls {
req.Header.Set("X-Forwarded-Proto", "https")
} else {
req.Header.Set("X-Forwarded-Proto", "http")
}
// TODO: Set X-Forwarded-Port
if err := backend.Write(req); err != nil {
log.Println("server write err:", err)
return
}
res, err := backend.Read(req)
if res != nil {
if stickyCookie != nil {
res.Header.Add("Set-Cookie", stickyCookie.String())
}
if res.StatusCode == http.StatusSwitchingProtocols {
res.Body = nil
}
if err := sc.Write(req, res); err != nil {
if err != io.EOF && err != httputil.ErrPersistEOF {
log.Println("client write err:", err)
// TODO: log error
}
return
}
}
if err != nil {
if err != io.EOF && err != httputil.ErrPersistEOF {
log.Println("server read err:", err)
// TODO: log error
fail(sc, req, 502, "Bad Gateway")
}
return
}
// TODO: Proxy HTTP CONNECT? (example: Go RPC over HTTP)
if res.StatusCode == http.StatusSwitchingProtocols {
serverW, serverR := backend.Hijack()
clientW, clientR := sc.Hijack()
defer serverW.Close()
done := make(chan struct{})
go func() {
serverR.WriteTo(clientW)
if cw, ok := clientW.(writeCloser); ok {
cw.CloseWrite()
}
close(done)
}()
clientR.WriteTo(serverW)
serverW.(writeCloser).CloseWrite()
<-done
return
}
// close the backend connection, so we don't accidently send to
// a closed socket on the backend
backend.Close()
// TODO: http pipelining
req, err = sc.Read()
if err != nil {
//.........这里部分代码省略.........
示例3: testHost
// function to do the actual testing and output the necessary bits
// we don't really care about ordering here otherwise we'd need to do a better job of organizing the output
func (w *Worker) testHost(work *Work) {
// more debug
w.dbg(3, "Ok.. prepping to test %s\n", work.target)
// pre-define myConn/tlsConn/client
var myConn net.Conn
var tlsConn *tls.Conn
var client *httputil.ClientConn
// need a couple of vars for holding ip addresses and default them to being empty
var ip4 string = ""
var ip6 string = ""
// and a var to stash our full host:port combo in
var address string = ""
// create a buffer we'll use later for reading client body
buffer := make([]byte, 1024)
// build our request
request, err := http.NewRequest("GET", work.url, nil)
if err != nil {
w.dbgError("Failed to build request for %s : %s\n", work.url, err)
return
}
if config.ua == "" {
request.Header.Set("User-Agent", version)
} else {
request.Header.Set("User-Agent", config.ua)
}
// debug
w.dbg(3, "Request: %+v\n", request)
// set a timer for the connection
timestart := time.Now()
ipaddr, ierr := net.LookupHost(work.target)
if ierr != nil {
w.dbgError("Failed to lookup %s : %s", work.target, ierr)
return
}
for _, x := range ipaddr {
if len(x) > 16 {
if ip6 == "" {
ip6 = x
}
} else if ip4 == "" {
ip4 = x
}
}
timestop := time.Now()
iplookup := (timestop.Sub(timestart))
// build our address string
if config.tcpfour {
address = fmt.Sprintf("%s:%d", ip4, work.port)
} else {
address = fmt.Sprintf("[%s]:%d", ip6, work.port)
}
// debug
w.dbg(1, "Attempting to connect to %s\n", address)
// since we want some very low level access to bits and pieces, we're going to have to use tcp dial vs the native http client
// create a net.conn
w.dbg(1, "Connecting to %s\n", address)
if config.tcpfour {
myConn, err = net.DialTimeout("tcp4", address, time.Duration(config.timeout)*time.Second)
} else if config.tcpsix {
myConn, err = net.DialTimeout("tcp6", address, time.Duration(config.timeout)*time.Second)
} else {
myConn, err = net.DialTimeout("tcp", address, time.Duration(config.timeout)*time.Second)
}
if err != nil {
w.dbgError("Could not connect to %s : %s\n", address, err)
return
}
w.dbg(2, "Connected to %s\n", address)
// get a time reading on how long it took to connect to the socket
timestop = time.Now()
tcpConnect := (timestop.Sub(timestart))
// defer close
defer myConn.Close()
// need to add some deadlines so we don't sit around indefintely - 5s is more than sufficient
myConn.SetDeadline(time.Now().Add(time.Duration(5 * time.Second)))
// if we're an ssl connection, we need a few extra steps here
if work.ssl {
w.dbg(1, "Starting SSL procedures...\n")
//.........这里部分代码省略.........