forked from aws/aws-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
aws/request: Fix for Go 1.8 request incorrectly sent with body
Go 1.8 tightened and clarified the rules code needs to use when building requests with the http package. Go 1.8 removed the automatic detection of if the Request.Body was empty, or actually had bytes in it. The SDK always sets the Request.Body even if it is empty and should not actually be sent. This is incorrect. Go 1.8 did add a http.NoBody value that the SDK can use to tell the http client that the request really should be sent without a body. The Request.Body cannot be set to nil, which is preferable, because the field is exported and could introduce nil pointer dereferences for users of the SDK if they used that field. Related golang/go#18257 Fix aws#984
- Loading branch information
Showing
5 changed files
with
117 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// +build go1.8 | ||
|
||
package request | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
) | ||
|
||
func resetBody(r *Request) { | ||
curOffset, _ := r.Body.Seek(0, io.SeekCurrent) | ||
endOffset, _ := r.Body.Seek(0, io.SeekEnd) | ||
r.Body.Seek(r.BodyStart, io.SeekStart) | ||
|
||
r.safeBody = newOffsetReader(r.Body, r.BodyStart) | ||
|
||
if endOffset-curOffset == 0 { | ||
r.HTTPRequest.Body = http.NoBody | ||
} else { | ||
r.HTTPRequest.Body = r.safeBody | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// +build !go1.8 | ||
|
||
package request | ||
|
||
func resetBody(r *Request) { | ||
r.safeBody = newOffsetReader(r.Body, r.BodyStart) | ||
r.HTTPRequest.Body = r.safeBody | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// +build !go1.8 | ||
|
||
package request | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestResetBody_WithBodyContents(t *testing.T) { | ||
r := Request{ | ||
HTTPRequest: &http.Request{}, | ||
} | ||
|
||
reader := strings.NewReader("abc") | ||
r.Body = reader | ||
|
||
r.ResetBody() | ||
|
||
if v, ok := r.HTTPRequest.Body.(*offsetReader); !ok || v == nil { | ||
t.Errorf("expected request body to be set to reader, got %#v", | ||
r.HTTPRequest.Body) | ||
} | ||
} | ||
|
||
func TestResetBody_WithEmptyBody(t *testing.T) { | ||
r := Request{ | ||
HTTPRequest: &http.Request{}, | ||
} | ||
|
||
reader := strings.NewReader("") | ||
r.Body = reader | ||
|
||
r.ResetBody() | ||
|
||
if v, ok := r.HTTPRequest.Body.(*offsetReader); !ok || v == nil { | ||
t.Errorf("expected request body to be set to reader, got %#v", | ||
r.HTTPRequest.Body) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// +build go1.8 | ||
|
||
package request | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestResetBody_WithBodyContents(t *testing.T) { | ||
r := Request{ | ||
HTTPRequest: &http.Request{}, | ||
} | ||
|
||
reader := strings.NewReader("abc") | ||
r.Body = reader | ||
|
||
r.ResetBody() | ||
|
||
if v, ok := r.HTTPRequest.Body.(*offsetReader); !ok || v == nil { | ||
t.Errorf("expected request body to be set to reader, got %#v", | ||
r.HTTPRequest.Body) | ||
} | ||
} | ||
|
||
func TestResetBody_WithEmptyBody(t *testing.T) { | ||
r := Request{ | ||
HTTPRequest: &http.Request{}, | ||
} | ||
|
||
reader := strings.NewReader("") | ||
r.Body = reader | ||
|
||
r.ResetBody() | ||
|
||
if a, e := r.HTTPRequest.Body, http.NoBody; a != e { | ||
t.Errorf("expected request body to be set to reader, got %#v", | ||
r.HTTPRequest.Body) | ||
} | ||
} |