1
// Copyright 2009 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.
18
ErrPersistEOF = &http.ProtocolError{ErrorString: "persistent connection closed"}
19
ErrClosed = &http.ProtocolError{ErrorString: "connection closed by user"}
20
ErrPipeline = &http.ProtocolError{ErrorString: "pipeline error"}
23
// This is an API usage error - the local side is closed.
24
// ErrPersistEOF (above) reports that the remote side is closed.
25
var errClosed = errors.New("i/o operation on closed connection")
27
// A ServerConn reads requests and sends responses over an underlying
28
// connection, until the HTTP keepalive logic commands an end. ServerConn
29
// also allows hijacking the underlying connection by calling Hijack
30
// to regain control over the connection. ServerConn supports pipe-lining,
31
// i.e. requests can be read out of sync (but in the same order) while the
32
// respective responses are sent.
34
// ServerConn is low-level and old. Applications should instead use Server
35
// in the net/http package.
36
type ServerConn struct {
37
lk sync.Mutex // read-write protects the following fields
40
re, we error // read/write errors
41
lastbody io.ReadCloser
43
pipereq map[*http.Request]uint
45
pipe textproto.Pipeline
48
// NewServerConn returns a new ServerConn reading and writing c. If r is not
49
// nil, it is the buffer to use when reading c.
51
// ServerConn is low-level and old. Applications should instead use Server
52
// in the net/http package.
53
func NewServerConn(c net.Conn, r *bufio.Reader) *ServerConn {
55
r = bufio.NewReader(c)
57
return &ServerConn{c: c, r: r, pipereq: make(map[*http.Request]uint)}
60
// Hijack detaches the ServerConn and returns the underlying connection as well
61
// as the read-side bufio which may have some left over data. Hijack may be
62
// called before Read has signaled the end of the keep-alive logic. The user
63
// should not call Hijack while Read or Write is in progress.
64
func (sc *ServerConn) Hijack() (c net.Conn, r *bufio.Reader) {
74
// Close calls Hijack and then also closes the underlying connection
75
func (sc *ServerConn) Close() error {
83
// Read returns the next request on the wire. An ErrPersistEOF is returned if
84
// it is gracefully determined that there are no more requests (e.g. after the
85
// first request on an HTTP/1.0 connection, or after a Connection:close on a
86
// HTTP/1.1 connection).
87
func (sc *ServerConn) Read() (req *http.Request, err error) {
89
// Ensure ordered execution of Reads and Writes
91
sc.pipe.StartRequest(id)
93
sc.pipe.EndRequest(id)
95
sc.pipe.StartResponse(id)
96
sc.pipe.EndResponse(id)
98
// Remember the pipeline id of this request
106
if sc.we != nil { // no point receiving if write-side broken or closed
114
if sc.r == nil { // connection closed by user in the meantime
116
return nil, errClosed
119
lastbody := sc.lastbody
123
// Make sure body is fully consumed, even if user does not call body.Close
125
// body.Close is assumed to be idempotent and multiple calls to
126
// it should return the error that its first invocation
128
err = lastbody.Close()
137
req, err = http.ReadRequest(r)
141
if err == io.ErrUnexpectedEOF {
142
// A close from the opposing client is treated as a
143
// graceful close, even if there was some unparse-able
144
// data before the close.
145
sc.re = ErrPersistEOF
152
sc.lastbody = req.Body
155
sc.re = ErrPersistEOF
161
// Pending returns the number of unanswered requests
162
// that have been received on the connection.
163
func (sc *ServerConn) Pending() int {
166
return sc.nread - sc.nwritten
169
// Write writes resp in response to req. To close the connection gracefully, set the
170
// Response.Close field to true. Write should be considered operational until
171
// it returns an error, regardless of any errors returned on the Read side.
172
func (sc *ServerConn) Write(req *http.Request, resp *http.Response) error {
174
// Retrieve the pipeline ID of this request/response pair
176
id, ok := sc.pipereq[req]
177
delete(sc.pipereq, req)
184
// Ensure pipeline order
185
sc.pipe.StartResponse(id)
186
defer sc.pipe.EndResponse(id)
193
if sc.c == nil { // connection closed by user in the meantime
198
if sc.nread <= sc.nwritten {
200
return errors.New("persist server pipe count")
203
// After signaling a keep-alive close, any pipelined unread
204
// requests will be lost. It is up to the user to drain them
206
sc.re = ErrPersistEOF
222
// A ClientConn sends request and receives headers over an underlying
223
// connection, while respecting the HTTP keepalive logic. ClientConn
224
// supports hijacking the connection calling Hijack to
225
// regain control of the underlying net.Conn and deal with it as desired.
227
// ClientConn is low-level and old. Applications should instead use
228
// Client or Transport in the net/http package.
229
type ClientConn struct {
230
lk sync.Mutex // read-write protects the following fields
233
re, we error // read/write errors
234
lastbody io.ReadCloser
236
pipereq map[*http.Request]uint
238
pipe textproto.Pipeline
239
writeReq func(*http.Request, io.Writer) error
242
// NewClientConn returns a new ClientConn reading and writing c. If r is not
243
// nil, it is the buffer to use when reading c.
245
// ClientConn is low-level and old. Applications should use Client or
246
// Transport in the net/http package.
247
func NewClientConn(c net.Conn, r *bufio.Reader) *ClientConn {
249
r = bufio.NewReader(c)
254
pipereq: make(map[*http.Request]uint),
255
writeReq: (*http.Request).Write,
259
// NewProxyClientConn works like NewClientConn but writes Requests
260
// using Request's WriteProxy method.
262
// New code should not use NewProxyClientConn. See Client or
263
// Transport in the net/http package instead.
264
func NewProxyClientConn(c net.Conn, r *bufio.Reader) *ClientConn {
265
cc := NewClientConn(c, r)
266
cc.writeReq = (*http.Request).WriteProxy
270
// Hijack detaches the ClientConn and returns the underlying connection as well
271
// as the read-side bufio which may have some left over data. Hijack may be
272
// called before the user or Read have signaled the end of the keep-alive
273
// logic. The user should not call Hijack while Read or Write is in progress.
274
func (cc *ClientConn) Hijack() (c net.Conn, r *bufio.Reader) {
284
// Close calls Hijack and then also closes the underlying connection
285
func (cc *ClientConn) Close() error {
293
// Write writes a request. An ErrPersistEOF error is returned if the connection
294
// has been closed in an HTTP keepalive sense. If req.Close equals true, the
295
// keepalive connection is logically closed after this request and the opposing
296
// server is informed. An ErrUnexpectedEOF indicates the remote closed the
297
// underlying TCP connection, which is usually considered as graceful close.
298
func (cc *ClientConn) Write(req *http.Request) (err error) {
300
// Ensure ordered execution of Writes
302
cc.pipe.StartRequest(id)
304
cc.pipe.EndRequest(id)
306
cc.pipe.StartResponse(id)
307
cc.pipe.EndResponse(id)
309
// Remember the pipeline id of this request
317
if cc.re != nil { // no point sending if read-side closed or broken
325
if cc.c == nil { // connection closed by user in the meantime
331
// We write the EOF to the write-side error, because there
332
// still might be some pipelined reads
333
cc.we = ErrPersistEOF
337
err = cc.writeReq(req, c)
349
// Pending returns the number of unanswered requests
350
// that have been sent on the connection.
351
func (cc *ClientConn) Pending() int {
354
return cc.nwritten - cc.nread
357
// Read reads the next response from the wire. A valid response might be
358
// returned together with an ErrPersistEOF, which means that the remote
359
// requested that this be the last request serviced. Read can be called
360
// concurrently with Write, but not with another Read.
361
func (cc *ClientConn) Read(req *http.Request) (resp *http.Response, err error) {
362
// Retrieve the pipeline ID of this request/response pair
364
id, ok := cc.pipereq[req]
365
delete(cc.pipereq, req)
368
return nil, ErrPipeline
372
// Ensure pipeline order
373
cc.pipe.StartResponse(id)
374
defer cc.pipe.EndResponse(id)
381
if cc.r == nil { // connection closed by user in the meantime
383
return nil, errClosed
386
lastbody := cc.lastbody
390
// Make sure body is fully consumed, even if user does not call body.Close
392
// body.Close is assumed to be idempotent and multiple calls to
393
// it should return the error that its first invocation
395
err = lastbody.Close()
404
resp, err = http.ReadResponse(r, req)
411
cc.lastbody = resp.Body
416
cc.re = ErrPersistEOF // don't send any more requests
422
// Do is convenience method that writes a request and reads a response.
423
func (cc *ClientConn) Do(req *http.Request) (resp *http.Response, err error) {