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.
- Loading branch information
1 parent
2d91254
commit bcb2cf3
Showing
8 changed files
with
501 additions
and
158 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,53 @@ | ||
package checksum | ||
|
||
import ( | ||
"crypto/md5" | ||
"encoding/base64" | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/aws/request" | ||
) | ||
|
||
const contentMD5Header = "Content-Md5" | ||
|
||
// AddBodyContentMD5Handler computes and sets the HTTP Content-MD5 header for requests that | ||
// require it. | ||
func AddBodyContentMD5Handler(r *request.Request) { | ||
// if Content-MD5 header is already present, return | ||
if v := r.HTTPRequest.Header.Get(contentMD5Header); len(v) != 0 { | ||
return | ||
} | ||
|
||
// if S3DisableContentMD5Validation flag is set, return | ||
if aws.BoolValue(r.Config.S3DisableContentMD5Validation) { | ||
return | ||
} | ||
|
||
// if request is presigned, return | ||
if r.IsPresigned() { | ||
return | ||
} | ||
|
||
// if body is not seekable, return | ||
if !aws.IsReaderSeekable(r.Body) { | ||
if r.Config.Logger != nil { | ||
r.Config.Logger.Log(fmt.Sprintf( | ||
"Unable to compute Content-MD5 for unseekable body, S3.%s", | ||
r.Operation.Name)) | ||
} | ||
return | ||
} | ||
|
||
h := md5.New() | ||
|
||
if _, err := aws.CopySeekableBody(h, r.Body); err != nil { | ||
r.Error = awserr.New("ContentMD5", "failed to compute body MD5", err) | ||
return | ||
} | ||
|
||
// encode the md5 checksum in base64 and set the request header. | ||
v := base64.StdEncoding.EncodeToString(h.Sum(nil)) | ||
r.HTTPRequest.Header.Set(contentMD5Header, v) | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.