Fix incorrect vendoring for swift library

(vendored a feature branch by accident)
This commit is contained in:
Nick Craig-Wood 2017-01-03 17:39:56 +00:00
parent 1cad759306
commit 5c89fd679d
5 changed files with 15 additions and 128 deletions

2
Godeps/Godeps.json generated
View file

@ -213,7 +213,7 @@
}, },
{ {
"ImportPath": "github.com/ncw/swift", "ImportPath": "github.com/ncw/swift",
"Rev": "95fc38e8de85903f4bf5b8d62721f0b9e106747a" "Rev": "6c1b1510538e1f00d49a558b7b9b87d71bc454d6"
}, },
{ {
"ImportPath": "github.com/pkg/errors", "ImportPath": "github.com/pkg/errors",

View file

@ -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
}
}

View file

@ -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
}

54
vendor/github.com/ncw/swift/swift.go generated vendored
View file

@ -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 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) TrustId string // Id of the trust (v3 auth only)
Transport http.RoundTripper `json:"-" xml:"-"` // Optional specialised http.Transport (eg. for Google Appengine) 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 // These are filled in after Authenticate is called as are the defaults for above
StorageUrl string StorageUrl string
AuthToken string AuthToken string
@ -156,7 +149,6 @@ var (
TimeoutError = newError(408, "Timeout when reading or writing data") TimeoutError = newError(408, "Timeout when reading or writing data")
Forbidden = newError(403, "Operation forbidden") Forbidden = newError(403, "Operation forbidden")
TooLargeObject = newError(413, "Too Large Object") TooLargeObject = newError(413, "Too Large Object")
RetryNeeded = newError(401, "Retry needed after token expired")
// Mappings for authentication errors // Mappings for authentication errors
authErrorMap = errorMap{ authErrorMap = errorMap{
@ -262,17 +254,12 @@ func (c *Connection) setDefaults() {
c.Timeout = 60 * time.Second c.Timeout = 60 * time.Second
} }
if c.Transport == nil { if c.Transport == nil {
tr := &http.Transport{ c.Transport = &http.Transport{
// TLSClientConfig: &tls.Config{RootCAs: pool}, // TLSClientConfig: &tls.Config{RootCAs: pool},
// DisableCompression: true, // DisableCompression: true,
Proxy: http.ProxyFromEnvironment, Proxy: http.ProxyFromEnvironment,
MaxIdleConnsPerHost: 2048, 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 { if c.client == nil {
c.client = &http.Client{ c.client = &http.Client{
@ -476,13 +463,6 @@ func (c *Connection) Call(targetUrl string, p RequestOpts) (resp *http.Response,
if retries == 0 { if retries == 0 {
retries = c.Retries 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 var req *http.Request
for { for {
var authToken string var authToken string
@ -503,6 +483,11 @@ func (c *Connection) Call(targetUrl string, p RequestOpts) (resp *http.Response,
if p.Parameters != nil { if p.Parameters != nil {
URL.RawQuery = p.Parameters.Encode() 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) req, err = http.NewRequest(p.Operation, URL.String(), reader)
if err != nil { if err != nil {
return 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("User-Agent", c.UserAgent)
req.Header.Add("X-Auth-Token", authToken) 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) resp, err = c.doTimeoutRequest(timer, req)
if err != nil { if err != nil {
if (p.Operation == "HEAD" || p.Operation == "GET") && retries > 0 { 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 return nil, nil, err
} }
if retries <= 0 { // Check to see if token has expired
break if resp.StatusCode == 401 && retries > 0 {
} _ = resp.Body.Close()
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
c.UnAuthenticate() c.UnAuthenticate()
// If we were reading from a reader and we retries--
// can't reset it, then fail here, otherwise
// retry.
if wdReader != nil && !wdReader.Reset() {
return resp, headers, RetryNeeded
}
} else { } else {
break break
} }
// Retry
_ = resp.Body.Close()
retries--
} }
if err = c.parseHeaders(resp, p.ErrorMap); err != nil { if err = c.parseHeaders(resp, p.ErrorMap); err != nil {

View file

@ -7,18 +7,12 @@ import (
// An io.Reader which resets a watchdog timer whenever data is read // An io.Reader which resets a watchdog timer whenever data is read
type watchdogReader struct { type watchdogReader struct {
timeout time.Duration timeout time.Duration
reader io.Reader reader io.Reader
timer *time.Timer timer *time.Timer
bytes int64
replayFirst bool
readFirst bool
first byte
} }
// Returns a new reader which will kick the watchdog timer whenever data is read // 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 { func newWatchdogReader(reader io.Reader, timeout time.Duration, timer *time.Timer) *watchdogReader {
return &watchdogReader{ return &watchdogReader{
timeout: timeout, 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 // Read reads up to len(p) bytes into p
func (t *watchdogReader) Read(p []byte) (n int, err error) { 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? // FIXME limit the amount of data read in one chunk so as to not exceed the timeout?
resetTimer(t.timer, t.timeout) resetTimer(t.timer, t.timeout)
if t.replayFirst { n, err = t.reader.Read(p)
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)
}
resetTimer(t.timer, t.timeout) resetTimer(t.timer, t.timeout)
return 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 // Check it satisfies the interface
var _ io.Reader = &watchdogReader{} var _ io.Reader = &watchdogReader{}