Permalink
Browse files

Fix incorrect vendoring for swift library

(vendored a feature branch by accident)
1 parent 1cad759 commit 5c89fd679df60d11642ec2b2afb10d8f3f095195 @ncw committed Jan 3, 2017
View
@@ -213,7 +213,7 @@
},
{
"ImportPath": "github.com/ncw/swift",
- "Rev": "95fc38e8de85903f4bf5b8d62721f0b9e106747a"
+ "Rev": "6c1b1510538e1f00d49a558b7b9b87d71bc454d6"
},
{
"ImportPath": "github.com/pkg/errors",
@@ -1,22 +0,0 @@
-// Show that we can use `Expect: 100-continue` in go versions >= 1.6
-//
-// +build go1.6
-
-package swift
-
-import (
- "net/http"
- "time"
-)
-
-// DisableExpectContinue indicates whether this version of go supports
-// expect continue.
-const DisableExpectContinue = false
-
-// SetExpectContinueTimeout sets ExpectContinueTimeout in the
-// transport to the default value if not set.
-func SetExpectContinueTimeout(transport *http.Transport, t time.Duration) {
- if transport.ExpectContinueTimeout == 0 {
- transport.ExpectContinueTimeout = t
- }
-}
@@ -1,20 +0,0 @@
-// Show that we can't use `Expect: 100-continue` in go versions < 1.6
-//
-// +build !go1.6
-
-package swift
-
-import (
- "net/http"
- "time"
-)
-
-// DisableExpectContinue indicates whether this version of go supports
-// expect continue.
-const DisableExpectContinue = true
-
-// SetExpectContinueTimeout sets ExpectContinueTimeout in the
-// transport to the default value if not set.
-func SetExpectContinueTimeout(transport *http.Transport, t time.Duration) {
- // can't do anything so ignore
-}
@@ -102,13 +102,6 @@ type Connection struct {
TenantDomainId string // Id of the tenant's domain (v3 auth only), only needed if it differs the from user domain
TrustId string // Id of the trust (v3 auth only)
Transport http.RoundTripper `json:"-" xml:"-"` // Optional specialised http.Transport (eg. for Google Appengine)
- // If set then we won't send `Expect: 100-continue` headers.
- // This is automatically disabled on go < 1.6 which doesn't
- // support it and if a 417 error is received. Note that if
- // you are using a custom Transport and you wish the `Expect:
- // 100-continue` mechanism to work, you'll need to set
- // ExpectContinueTimeout to a non-zero value.
- DisableExpectContinue bool
// These are filled in after Authenticate is called as are the defaults for above
StorageUrl string
AuthToken string
@@ -156,7 +149,6 @@ var (
TimeoutError = newError(408, "Timeout when reading or writing data")
Forbidden = newError(403, "Operation forbidden")
TooLargeObject = newError(413, "Too Large Object")
- RetryNeeded = newError(401, "Retry needed after token expired")
// Mappings for authentication errors
authErrorMap = errorMap{
@@ -262,17 +254,12 @@ func (c *Connection) setDefaults() {
c.Timeout = 60 * time.Second
}
if c.Transport == nil {
- tr := &http.Transport{
+ c.Transport = &http.Transport{
// TLSClientConfig: &tls.Config{RootCAs: pool},
// DisableCompression: true,
Proxy: http.ProxyFromEnvironment,
MaxIdleConnsPerHost: 2048,
}
- // If using ExpectContinue make sure it is set in the transport
- if !c.DisableExpectContinue {
- SetExpectContinueTimeout(tr, 5*time.Second)
- }
- c.Transport = tr
}
if c.client == nil {
c.client = &http.Client{
@@ -476,13 +463,6 @@ func (c *Connection) Call(targetUrl string, p RequestOpts) (resp *http.Response,
if retries == 0 {
retries = c.Retries
}
- reader := p.Body
- var wdReader *watchdogReader
- timer := time.NewTimer(c.ConnectTimeout)
- if reader != nil {
- wdReader = newWatchdogReader(reader, c.Timeout, timer)
- reader = wdReader
- }
var req *http.Request
for {
var authToken string
@@ -503,6 +483,11 @@ func (c *Connection) Call(targetUrl string, p RequestOpts) (resp *http.Response,
if p.Parameters != nil {
URL.RawQuery = p.Parameters.Encode()
}
+ timer := time.NewTimer(c.ConnectTimeout)
+ reader := p.Body
+ if reader != nil {
+ reader = newWatchdogReader(reader, c.Timeout, timer)
+ }
req, err = http.NewRequest(p.Operation, URL.String(), reader)
if err != nil {
return
@@ -523,12 +508,6 @@ func (c *Connection) Call(targetUrl string, p RequestOpts) (resp *http.Response,
}
req.Header.Add("User-Agent", c.UserAgent)
req.Header.Add("X-Auth-Token", authToken)
- // Use `Expect: 100-continue` if body set and not disabled
- usingExpectContinue := false
- if !DisableExpectContinue && !c.DisableExpectContinue && p.Body != nil {
- req.Header.Set("Expect", "100-continue")
- usingExpectContinue = true
- }
resp, err = c.doTimeoutRequest(timer, req)
if err != nil {
if (p.Operation == "HEAD" || p.Operation == "GET") && retries > 0 {
@@ -537,27 +516,14 @@ func (c *Connection) Call(targetUrl string, p RequestOpts) (resp *http.Response,
}
return nil, nil, err
}
- if retries <= 0 {
- break
- }
- if resp.StatusCode == 417 && usingExpectContinue {
- // Disable `Expect: 100-continue` if get 417 error in response
- c.DisableExpectContinue = true
- } else if resp.StatusCode == 401 {
- // Token has expired
+ // Check to see if token has expired
+ if resp.StatusCode == 401 && retries > 0 {
+ _ = resp.Body.Close()
c.UnAuthenticate()
- // If we were reading from a reader and we
- // can't reset it, then fail here, otherwise
- // retry.
- if wdReader != nil && !wdReader.Reset() {
- return resp, headers, RetryNeeded
- }
+ retries--
} else {
break
}
- // Retry
- _ = resp.Body.Close()
- retries--
}
if err = c.parseHeaders(resp, p.ErrorMap); err != nil {
@@ -7,18 +7,12 @@ import (
// An io.Reader which resets a watchdog timer whenever data is read
type watchdogReader struct {
- timeout time.Duration
- reader io.Reader
- timer *time.Timer
- bytes int64
- replayFirst bool
- readFirst bool
- first byte
+ timeout time.Duration
+ reader io.Reader
+ timer *time.Timer
}
// Returns a new reader which will kick the watchdog timer whenever data is read
-//
-// It also has a 1 byte buffer which can be reset with the Reset() method.
func newWatchdogReader(reader io.Reader, timeout time.Duration, timer *time.Timer) *watchdogReader {
return &watchdogReader{
timeout: timeout,
@@ -29,43 +23,12 @@ func newWatchdogReader(reader io.Reader, timeout time.Duration, timer *time.Time
// Read reads up to len(p) bytes into p
func (t *watchdogReader) Read(p []byte) (n int, err error) {
- if len(p) == 0 {
- return 0, nil
- }
// FIXME limit the amount of data read in one chunk so as to not exceed the timeout?
resetTimer(t.timer, t.timeout)
- if t.replayFirst {
- p[0] = t.first
- t.replayFirst = false
- n = 1
- err = nil
- } else {
- n, err = t.reader.Read(p)
- if !t.readFirst && n > 0 {
- t.first = p[0]
- t.readFirst = true
- }
- t.bytes += int64(n)
- }
+ n, err = t.reader.Read(p)
resetTimer(t.timer, t.timeout)
return
}
-// Resets the buffer if only 1 byte read
-//
-// Returns true if successful
-func (t *watchdogReader) Reset() bool {
- switch t.bytes {
- case 0:
- t.replayFirst = false
- return true
- case 1:
- t.replayFirst = true
- return true
- default:
- return false
- }
-}
-
// Check it satisfies the interface
var _ io.Reader = &watchdogReader{}

0 comments on commit 5c89fd6

Please sign in to comment.