diff options
Diffstat (limited to 'libgo/go/net/http/httputil/persist.go')
-rw-r--r-- | libgo/go/net/http/httputil/persist.go | 182 |
1 files changed, 92 insertions, 90 deletions
diff --git a/libgo/go/net/http/httputil/persist.go b/libgo/go/net/http/httputil/persist.go index 987bcc96ba..cbedf25ad1 100644 --- a/libgo/go/net/http/httputil/persist.go +++ b/libgo/go/net/http/httputil/persist.go @@ -15,26 +15,27 @@ import ( ) var ( + // Deprecated: No longer used. ErrPersistEOF = &http.ProtocolError{ErrorString: "persistent connection closed"} - ErrClosed = &http.ProtocolError{ErrorString: "connection closed by user"} - ErrPipeline = &http.ProtocolError{ErrorString: "pipeline error"} + + // Deprecated: No longer used. + ErrClosed = &http.ProtocolError{ErrorString: "connection closed by user"} + + // Deprecated: No longer used. + ErrPipeline = &http.ProtocolError{ErrorString: "pipeline error"} ) // This is an API usage error - the local side is closed. // ErrPersistEOF (above) reports that the remote side is closed. var errClosed = errors.New("i/o operation on closed connection") -// A ServerConn reads requests and sends responses over an underlying -// connection, until the HTTP keepalive logic commands an end. ServerConn -// also allows hijacking the underlying connection by calling Hijack -// to regain control over the connection. ServerConn supports pipe-lining, -// i.e. requests can be read out of sync (but in the same order) while the -// respective responses are sent. +// ServerConn is an artifact of Go's early HTTP implementation. +// It is low-level, old, and unused by Go's current HTTP stack. +// We should have deleted it before Go 1. // -// ServerConn is low-level and old. Applications should instead use Server -// in the net/http package. +// Deprecated: Use the Server in package net/http instead. type ServerConn struct { - lk sync.Mutex // read-write protects the following fields + mu sync.Mutex // read-write protects the following fields c net.Conn r *bufio.Reader re, we error // read/write errors @@ -45,11 +46,11 @@ type ServerConn struct { pipe textproto.Pipeline } -// NewServerConn returns a new ServerConn reading and writing c. If r is not -// nil, it is the buffer to use when reading c. +// NewServerConn is an artifact of Go's early HTTP implementation. +// It is low-level, old, and unused by Go's current HTTP stack. +// We should have deleted it before Go 1. // -// ServerConn is low-level and old. Applications should instead use Server -// in the net/http package. +// Deprecated: Use the Server in package net/http instead. func NewServerConn(c net.Conn, r *bufio.Reader) *ServerConn { if r == nil { r = bufio.NewReader(c) @@ -61,17 +62,17 @@ func NewServerConn(c net.Conn, r *bufio.Reader) *ServerConn { // as the read-side bufio which may have some left over data. Hijack may be // called before Read has signaled the end of the keep-alive logic. The user // should not call Hijack while Read or Write is in progress. -func (sc *ServerConn) Hijack() (c net.Conn, r *bufio.Reader) { - sc.lk.Lock() - defer sc.lk.Unlock() - c = sc.c - r = sc.r +func (sc *ServerConn) Hijack() (net.Conn, *bufio.Reader) { + sc.mu.Lock() + defer sc.mu.Unlock() + c := sc.c + r := sc.r sc.c = nil sc.r = nil - return + return c, r } -// Close calls Hijack and then also closes the underlying connection +// Close calls Hijack and then also closes the underlying connection. func (sc *ServerConn) Close() error { c, _ := sc.Hijack() if c != nil { @@ -84,7 +85,9 @@ func (sc *ServerConn) Close() error { // it is gracefully determined that there are no more requests (e.g. after the // first request on an HTTP/1.0 connection, or after a Connection:close on a // HTTP/1.1 connection). -func (sc *ServerConn) Read() (req *http.Request, err error) { +func (sc *ServerConn) Read() (*http.Request, error) { + var req *http.Request + var err error // Ensure ordered execution of Reads and Writes id := sc.pipe.Next() @@ -96,29 +99,29 @@ func (sc *ServerConn) Read() (req *http.Request, err error) { sc.pipe.EndResponse(id) } else { // Remember the pipeline id of this request - sc.lk.Lock() + sc.mu.Lock() sc.pipereq[req] = id - sc.lk.Unlock() + sc.mu.Unlock() } }() - sc.lk.Lock() + sc.mu.Lock() if sc.we != nil { // no point receiving if write-side broken or closed - defer sc.lk.Unlock() + defer sc.mu.Unlock() return nil, sc.we } if sc.re != nil { - defer sc.lk.Unlock() + defer sc.mu.Unlock() return nil, sc.re } if sc.r == nil { // connection closed by user in the meantime - defer sc.lk.Unlock() + defer sc.mu.Unlock() return nil, errClosed } r := sc.r lastbody := sc.lastbody sc.lastbody = nil - sc.lk.Unlock() + sc.mu.Unlock() // Make sure body is fully consumed, even if user does not call body.Close if lastbody != nil { @@ -127,16 +130,16 @@ func (sc *ServerConn) Read() (req *http.Request, err error) { // returned. err = lastbody.Close() if err != nil { - sc.lk.Lock() - defer sc.lk.Unlock() + sc.mu.Lock() + defer sc.mu.Unlock() sc.re = err return nil, err } } req, err = http.ReadRequest(r) - sc.lk.Lock() - defer sc.lk.Unlock() + sc.mu.Lock() + defer sc.mu.Unlock() if err != nil { if err == io.ErrUnexpectedEOF { // A close from the opposing client is treated as a @@ -161,8 +164,8 @@ func (sc *ServerConn) Read() (req *http.Request, err error) { // Pending returns the number of unanswered requests // that have been received on the connection. func (sc *ServerConn) Pending() int { - sc.lk.Lock() - defer sc.lk.Unlock() + sc.mu.Lock() + defer sc.mu.Unlock() return sc.nread - sc.nwritten } @@ -172,31 +175,31 @@ func (sc *ServerConn) Pending() int { func (sc *ServerConn) Write(req *http.Request, resp *http.Response) error { // Retrieve the pipeline ID of this request/response pair - sc.lk.Lock() + sc.mu.Lock() id, ok := sc.pipereq[req] delete(sc.pipereq, req) if !ok { - sc.lk.Unlock() + sc.mu.Unlock() return ErrPipeline } - sc.lk.Unlock() + sc.mu.Unlock() // Ensure pipeline order sc.pipe.StartResponse(id) defer sc.pipe.EndResponse(id) - sc.lk.Lock() + sc.mu.Lock() if sc.we != nil { - defer sc.lk.Unlock() + defer sc.mu.Unlock() return sc.we } if sc.c == nil { // connection closed by user in the meantime - defer sc.lk.Unlock() + defer sc.mu.Unlock() return ErrClosed } c := sc.c if sc.nread <= sc.nwritten { - defer sc.lk.Unlock() + defer sc.mu.Unlock() return errors.New("persist server pipe count") } if resp.Close { @@ -205,11 +208,11 @@ func (sc *ServerConn) Write(req *http.Request, resp *http.Response) error { // before signaling. sc.re = ErrPersistEOF } - sc.lk.Unlock() + sc.mu.Unlock() err := resp.Write(c) - sc.lk.Lock() - defer sc.lk.Unlock() + sc.mu.Lock() + defer sc.mu.Unlock() if err != nil { sc.we = err return err @@ -219,15 +222,13 @@ func (sc *ServerConn) Write(req *http.Request, resp *http.Response) error { return nil } -// A ClientConn sends request and receives headers over an underlying -// connection, while respecting the HTTP keepalive logic. ClientConn -// supports hijacking the connection calling Hijack to -// regain control of the underlying net.Conn and deal with it as desired. +// ClientConn is an artifact of Go's early HTTP implementation. +// It is low-level, old, and unused by Go's current HTTP stack. +// We should have deleted it before Go 1. // -// ClientConn is low-level and old. Applications should instead use -// Client or Transport in the net/http package. +// Deprecated: Use Client or Transport in package net/http instead. type ClientConn struct { - lk sync.Mutex // read-write protects the following fields + mu sync.Mutex // read-write protects the following fields c net.Conn r *bufio.Reader re, we error // read/write errors @@ -239,11 +240,11 @@ type ClientConn struct { writeReq func(*http.Request, io.Writer) error } -// NewClientConn returns a new ClientConn reading and writing c. If r is not -// nil, it is the buffer to use when reading c. +// NewClientConn is an artifact of Go's early HTTP implementation. +// It is low-level, old, and unused by Go's current HTTP stack. +// We should have deleted it before Go 1. // -// ClientConn is low-level and old. Applications should use Client or -// Transport in the net/http package. +// Deprecated: Use the Client or Transport in package net/http instead. func NewClientConn(c net.Conn, r *bufio.Reader) *ClientConn { if r == nil { r = bufio.NewReader(c) @@ -256,11 +257,11 @@ func NewClientConn(c net.Conn, r *bufio.Reader) *ClientConn { } } -// NewProxyClientConn works like NewClientConn but writes Requests -// using Request's WriteProxy method. +// NewProxyClientConn is an artifact of Go's early HTTP implementation. +// It is low-level, old, and unused by Go's current HTTP stack. +// We should have deleted it before Go 1. // -// New code should not use NewProxyClientConn. See Client or -// Transport in the net/http package instead. +// Deprecated: Use the Client or Transport in package net/http instead. func NewProxyClientConn(c net.Conn, r *bufio.Reader) *ClientConn { cc := NewClientConn(c, r) cc.writeReq = (*http.Request).WriteProxy @@ -272,8 +273,8 @@ func NewProxyClientConn(c net.Conn, r *bufio.Reader) *ClientConn { // called before the user or Read have signaled the end of the keep-alive // logic. The user should not call Hijack while Read or Write is in progress. func (cc *ClientConn) Hijack() (c net.Conn, r *bufio.Reader) { - cc.lk.Lock() - defer cc.lk.Unlock() + cc.mu.Lock() + defer cc.mu.Unlock() c = cc.c r = cc.r cc.c = nil @@ -281,7 +282,7 @@ func (cc *ClientConn) Hijack() (c net.Conn, r *bufio.Reader) { return } -// Close calls Hijack and then also closes the underlying connection +// Close calls Hijack and then also closes the underlying connection. func (cc *ClientConn) Close() error { c, _ := cc.Hijack() if c != nil { @@ -295,7 +296,8 @@ func (cc *ClientConn) Close() error { // keepalive connection is logically closed after this request and the opposing // server is informed. An ErrUnexpectedEOF indicates the remote closed the // underlying TCP connection, which is usually considered as graceful close. -func (cc *ClientConn) Write(req *http.Request) (err error) { +func (cc *ClientConn) Write(req *http.Request) error { + var err error // Ensure ordered execution of Writes id := cc.pipe.Next() @@ -307,23 +309,23 @@ func (cc *ClientConn) Write(req *http.Request) (err error) { cc.pipe.EndResponse(id) } else { // Remember the pipeline id of this request - cc.lk.Lock() + cc.mu.Lock() cc.pipereq[req] = id - cc.lk.Unlock() + cc.mu.Unlock() } }() - cc.lk.Lock() + cc.mu.Lock() if cc.re != nil { // no point sending if read-side closed or broken - defer cc.lk.Unlock() + defer cc.mu.Unlock() return cc.re } if cc.we != nil { - defer cc.lk.Unlock() + defer cc.mu.Unlock() return cc.we } if cc.c == nil { // connection closed by user in the meantime - defer cc.lk.Unlock() + defer cc.mu.Unlock() return errClosed } c := cc.c @@ -332,11 +334,11 @@ func (cc *ClientConn) Write(req *http.Request) (err error) { // still might be some pipelined reads cc.we = ErrPersistEOF } - cc.lk.Unlock() + cc.mu.Unlock() err = cc.writeReq(req, c) - cc.lk.Lock() - defer cc.lk.Unlock() + cc.mu.Lock() + defer cc.mu.Unlock() if err != nil { cc.we = err return err @@ -349,8 +351,8 @@ func (cc *ClientConn) Write(req *http.Request) (err error) { // Pending returns the number of unanswered requests // that have been sent on the connection. func (cc *ClientConn) Pending() int { - cc.lk.Lock() - defer cc.lk.Unlock() + cc.mu.Lock() + defer cc.mu.Unlock() return cc.nwritten - cc.nread } @@ -360,32 +362,32 @@ func (cc *ClientConn) Pending() int { // concurrently with Write, but not with another Read. func (cc *ClientConn) Read(req *http.Request) (resp *http.Response, err error) { // Retrieve the pipeline ID of this request/response pair - cc.lk.Lock() + cc.mu.Lock() id, ok := cc.pipereq[req] delete(cc.pipereq, req) if !ok { - cc.lk.Unlock() + cc.mu.Unlock() return nil, ErrPipeline } - cc.lk.Unlock() + cc.mu.Unlock() // Ensure pipeline order cc.pipe.StartResponse(id) defer cc.pipe.EndResponse(id) - cc.lk.Lock() + cc.mu.Lock() if cc.re != nil { - defer cc.lk.Unlock() + defer cc.mu.Unlock() return nil, cc.re } if cc.r == nil { // connection closed by user in the meantime - defer cc.lk.Unlock() + defer cc.mu.Unlock() return nil, errClosed } r := cc.r lastbody := cc.lastbody cc.lastbody = nil - cc.lk.Unlock() + cc.mu.Unlock() // Make sure body is fully consumed, even if user does not call body.Close if lastbody != nil { @@ -394,16 +396,16 @@ func (cc *ClientConn) Read(req *http.Request) (resp *http.Response, err error) { // returned. err = lastbody.Close() if err != nil { - cc.lk.Lock() - defer cc.lk.Unlock() + cc.mu.Lock() + defer cc.mu.Unlock() cc.re = err return nil, err } } resp, err = http.ReadResponse(r, req) - cc.lk.Lock() - defer cc.lk.Unlock() + cc.mu.Lock() + defer cc.mu.Unlock() if err != nil { cc.re = err return resp, err @@ -420,10 +422,10 @@ func (cc *ClientConn) Read(req *http.Request) (resp *http.Response, err error) { } // Do is convenience method that writes a request and reads a response. -func (cc *ClientConn) Do(req *http.Request) (resp *http.Response, err error) { - err = cc.Write(req) +func (cc *ClientConn) Do(req *http.Request) (*http.Response, error) { + err := cc.Write(req) if err != nil { - return + return nil, err } return cc.Read(req) } |