Skip to content

Commit

Permalink
Merge pull request bmizerany#1 from mrallen1/bug/nilbody
Browse files Browse the repository at this point in the history
Handle nil http request body
  • Loading branch information
bmizerany committed Oct 25, 2014
2 parents b247975 + 96cc1ca commit 5fb2e72
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
14 changes: 14 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,17 @@ func Example_formEncodedBody() {
// Output:
// 200
}

func ExampleSignGlacier() {
r, _ := http.NewRequest("GET", "https://glacier.us-east-1.amazonaws.com/-/vaults", nil)
r.Header.Set("X-Amz-Glacier-Version", "2012-06-01")

resp, err := aws4.DefaultClient.Do(r)
if err != nil {
log.Fatal(err)
}

fmt.Println(resp.StatusCode)
// Output:
// 200
}
16 changes: 12 additions & 4 deletions sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,19 @@ func (s *Service) writeHeaderList(w io.Writer, r *http.Request) {
}

func (s *Service) writeBody(w io.Writer, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
var b []byte
// If the payload is empty, use the empty string as the input to the SHA256 function
// http://docs.amazonwebservices.com/general/latest/gr/sigv4-create-canonical-request.html
if r.Body == nil {
b = []byte("")
} else {
var err error
b, err = ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
r.Body = ioutil.NopCloser(bytes.NewBuffer(b))
}
r.Body = ioutil.NopCloser(bytes.NewBuffer(b))

h := sha256.New()
h.Write(b)
Expand Down

0 comments on commit 5fb2e72

Please sign in to comment.