Skip to content

Commit 858c2ad

Browse files
Tim Cooperbradfitz
Tim Cooper
authored andcommitted
oauth2: turn Transport.CancelRequest into a no-op
Request cancellation should be done via http.Request.Context. Fixes golang#271 Change-Id: Ia6251898e55bd15b27968504fc6efe14f05b1def Reviewed-on: https://go-review.googlesource.com/c/oauth2/+/121438 Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 5d9234d commit 858c2ad

File tree

1 file changed

+12
-67
lines changed

1 file changed

+12
-67
lines changed

transport.go

+12-67
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package oauth2
66

77
import (
88
"errors"
9-
"io"
9+
"log"
1010
"net/http"
1111
"sync"
1212
)
@@ -25,9 +25,6 @@ type Transport struct {
2525
// Base is the base RoundTripper used to make HTTP requests.
2626
// If nil, http.DefaultTransport is used.
2727
Base http.RoundTripper
28-
29-
mu sync.Mutex // guards modReq
30-
modReq map[*http.Request]*http.Request // original -> modified
3128
}
3229

3330
// RoundTrip authorizes and authenticates the request with an
@@ -52,35 +49,22 @@ func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
5249

5350
req2 := cloneRequest(req) // per RoundTripper contract
5451
token.SetAuthHeader(req2)
55-
t.setModReq(req, req2)
56-
res, err := t.base().RoundTrip(req2)
5752

58-
// req.Body is assumed to have been closed by the base RoundTripper.
53+
// req.Body is assumed to be closed by the base RoundTripper.
5954
reqBodyClosed = true
60-
61-
if err != nil {
62-
t.setModReq(req, nil)
63-
return nil, err
64-
}
65-
res.Body = &onEOFReader{
66-
rc: res.Body,
67-
fn: func() { t.setModReq(req, nil) },
68-
}
69-
return res, nil
55+
return t.base().RoundTrip(req2)
7056
}
7157

72-
// CancelRequest cancels an in-flight request by closing its connection.
58+
var cancelOnce sync.Once
59+
60+
// CancelRequest does nothing. It used to be a legacy cancellation mechanism
61+
// but now only it only logs on first use to warn that it's deprecated.
62+
//
63+
// Deprecated: use contexts for cancellation instead.
7364
func (t *Transport) CancelRequest(req *http.Request) {
74-
type canceler interface {
75-
CancelRequest(*http.Request)
76-
}
77-
if cr, ok := t.base().(canceler); ok {
78-
t.mu.Lock()
79-
modReq := t.modReq[req]
80-
delete(t.modReq, req)
81-
t.mu.Unlock()
82-
cr.CancelRequest(modReq)
83-
}
65+
cancelOnce.Do(func() {
66+
log.Printf("deprecated: golang.org/x/oauth2: Transport.CancelRequest no longer does anything; use contexts")
67+
})
8468
}
8569

8670
func (t *Transport) base() http.RoundTripper {
@@ -90,19 +74,6 @@ func (t *Transport) base() http.RoundTripper {
9074
return http.DefaultTransport
9175
}
9276

93-
func (t *Transport) setModReq(orig, mod *http.Request) {
94-
t.mu.Lock()
95-
defer t.mu.Unlock()
96-
if t.modReq == nil {
97-
t.modReq = make(map[*http.Request]*http.Request)
98-
}
99-
if mod == nil {
100-
delete(t.modReq, orig)
101-
} else {
102-
t.modReq[orig] = mod
103-
}
104-
}
105-
10677
// cloneRequest returns a clone of the provided *http.Request.
10778
// The clone is a shallow copy of the struct and its Header map.
10879
func cloneRequest(r *http.Request) *http.Request {
@@ -116,29 +87,3 @@ func cloneRequest(r *http.Request) *http.Request {
11687
}
11788
return r2
11889
}
119-
120-
type onEOFReader struct {
121-
rc io.ReadCloser
122-
fn func()
123-
}
124-
125-
func (r *onEOFReader) Read(p []byte) (n int, err error) {
126-
n, err = r.rc.Read(p)
127-
if err == io.EOF {
128-
r.runFunc()
129-
}
130-
return
131-
}
132-
133-
func (r *onEOFReader) Close() error {
134-
err := r.rc.Close()
135-
r.runFunc()
136-
return err
137-
}
138-
139-
func (r *onEOFReader) runFunc() {
140-
if fn := r.fn; fn != nil {
141-
fn()
142-
r.fn = nil
143-
}
144-
}

0 commit comments

Comments
 (0)