Merge lp:~axwalk/gwacl/retry-rewind-request-body into lp:gwacl

Proposed by Andrew Wilkins
Status: Merged
Approved by: Andrew Wilkins
Approved revision: 233
Merged at revision: 232
Proposed branch: lp:~axwalk/gwacl/retry-rewind-request-body
Merge into: lp:gwacl
Diff against target: 80 lines (+39/-2)
2 files modified
x509dispatcher.go (+14/-2)
x509dispatcher_test.go (+25/-0)
To merge this branch: bzr merge lp:~axwalk/gwacl/retry-rewind-request-body
Reviewer Review Type Date Requested Status
Ian Booth Approve
Review via email: mp+210118@code.launchpad.net

Commit message

Rewind body when retrying POST/PUT/etc.

Azure may tell us to retry POST/PUT, which
may have a body. We must rewind the reader
if this happens.

Description of the change

Rewind body when retrying POST/PUT/etc.

Azure may tell us to retry POST/PUT, which
may have a body. We must rewind the reader
if this happens.

To post a comment you must log in.
Revision history for this message
Ian Booth (wallyworld) wrote :

Looks good.
Small typo:
+// to the beginning of the reader upong Close being called.

review: Approve
Revision history for this message
Go Bot (go-bot) wrote :

The attempt to merge lp:~axwalk/gwacl/retry-rewind-request-body into lp:gwacl failed. Below is the output from the failed tests.

./utilities/format -s
? launchpad.net/gwacl/example/management [no test files]
? launchpad.net/gwacl/example/storage [no test files]
? launchpad.net/gwacl/fork/http [no test files]
? launchpad.net/gwacl/fork/tls [no test files]

# launchpad.net/gwacl
deletedisk_test.go:8:5: cannot find package "launchpad.net/gocheck" in any of:
 /usr/lib/go/src/pkg/launchpad.net/gocheck (from $GOROOT)
 /home/tarmac/gwacl-trees/src/launchpad.net/gocheck (from $GOPATH)
# launchpad.net/gwacl/dedent
deletedisk_test.go:8:5: cannot find package "launchpad.net/gocheck" in any of:
 /usr/lib/go/src/pkg/launchpad.net/gocheck (from $GOROOT)
 /home/tarmac/gwacl-trees/src/launchpad.net/gocheck (from $GOPATH)
# launchpad.net/gwacl/logging
deletedisk_test.go:8:5: cannot find package "launchpad.net/gocheck" in any of:
 /usr/lib/go/src/pkg/launchpad.net/gocheck (from $GOROOT)
 /home/tarmac/gwacl-trees/src/launchpad.net/gocheck (from $GOPATH)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'x509dispatcher.go'
--- x509dispatcher.go 2014-02-05 04:46:09 +0000
+++ x509dispatcher.go 2014-03-11 03:39:32 +0000
@@ -6,7 +6,7 @@
6import (6import (
7 "bytes"7 "bytes"
8 "fmt"8 "fmt"
9 "io/ioutil"9 "io"
10 "launchpad.net/gwacl/fork/http"10 "launchpad.net/gwacl/fork/http"
11 . "launchpad.net/gwacl/logging"11 . "launchpad.net/gwacl/logging"
12 "net/url"12 "net/url"
@@ -70,6 +70,17 @@
70 Header http.Header70 Header http.Header
71}71}
7272
73// rewindCloser returns an io.ReadCloser that seeks back
74// to the beginning of the reader upon Close being called.
75type rewindCloser struct {
76 io.ReadSeeker
77}
78
79func (c rewindCloser) Close() error {
80 _, err := c.Seek(0, 0)
81 return err
82}
83
73func performX509Request(session *x509Session, request *X509Request) (*x509Response, error) {84func performX509Request(session *x509Session, request *X509Request) (*x509Response, error) {
74 response := &x509Response{}85 response := &x509Response{}
7586
@@ -78,11 +89,12 @@
78 Debugf("Request body:\n%s", request.Payload)89 Debugf("Request body:\n%s", request.Payload)
79 }90 }
8091
81 bodyReader := ioutil.NopCloser(bytes.NewReader(request.Payload))92 bodyReader := rewindCloser{bytes.NewReader(request.Payload)}
82 httpRequest, err := http.NewRequest(request.Method, request.URL, bodyReader)93 httpRequest, err := http.NewRequest(request.Method, request.URL, bodyReader)
83 if err != nil {94 if err != nil {
84 return nil, err95 return nil, err
85 }96 }
97 httpRequest.ContentLength = int64(len(request.Payload))
86 httpRequest.Header.Set("Content-Type", request.ContentType)98 httpRequest.Header.Set("Content-Type", request.ContentType)
87 httpRequest.Header.Set("x-ms-version", request.APIVersion)99 httpRequest.Header.Set("x-ms-version", request.APIVersion)
88 retrier := session.retryPolicy.getForkedHttpRetrier(session.client)100 retrier := session.retryPolicy.getForkedHttpRetrier(session.client)
89101
=== modified file 'x509dispatcher_test.go'
--- x509dispatcher_test.go 2014-02-05 04:46:09 +0000
+++ x509dispatcher_test.go 2014-03-11 03:39:32 +0000
@@ -219,6 +219,31 @@
219 }219 }
220}220}
221221
222func (*x509DispatcherSuite) TestRedirectRewindsBody(c *C) {
223 httpRequests := make(chan *Request, 2)
224 serverConflict := makeRecordingHTTPServer(httpRequests, http.StatusConflict, nil, nil)
225 defer serverConflict.Close()
226 redirPath := "/else/where"
227 responseHeaders := make(http.Header)
228 responseHeaders.Set("Location", serverConflict.URL+redirPath)
229 serverRedir := makeRecordingHTTPServer(httpRequests, http.StatusTemporaryRedirect, nil, responseHeaders)
230 defer serverRedir.Close()
231 session, err := newX509Session("subscriptionid", "", "West US", NoRetryPolicy)
232 c.Assert(err, IsNil)
233 path := "/foo/bar"
234 version := "test-version"
235 content := []byte("ponies")
236 contentType := "text/plain"
237
238 request := newX509RequestPOST(serverRedir.URL+path, version, content, contentType)
239 response, err := performX509Request(session, request)
240 c.Assert(err, IsNil)
241 c.Assert(response.StatusCode, Equals, http.StatusConflict)
242 c.Assert(httpRequests, HasLen, 2)
243 c.Assert((<-httpRequests).BodyContent, DeepEquals, content)
244 c.Assert((<-httpRequests).BodyContent, DeepEquals, content)
245}
246
222func (*x509DispatcherSuite) TestRequestsLimitRedirects(c *C) {247func (*x509DispatcherSuite) TestRequestsLimitRedirects(c *C) {
223 httpRequests := make(chan *Request, 10)248 httpRequests := make(chan *Request, 10)
224 serverRedir := makeRecordingHTTPServer(httpRequests, http.StatusTemporaryRedirect, nil, nil)249 serverRedir := makeRecordingHTTPServer(httpRequests, http.StatusTemporaryRedirect, nil, nil)

Subscribers

People subscribed via source and target branches

to all changes: