]>
Commit | Line | Data |
---|---|---|
bae9f6d2 JC |
1 | package s3 |
2 | ||
3 | import ( | |
4 | "crypto/md5" | |
5 | "encoding/base64" | |
6 | "io" | |
7 | ||
8 | "github.com/aws/aws-sdk-go/aws/awserr" | |
9 | "github.com/aws/aws-sdk-go/aws/request" | |
10 | ) | |
11 | ||
12 | // contentMD5 computes and sets the HTTP Content-MD5 header for requests that | |
13 | // require it. | |
14 | func contentMD5(r *request.Request) { | |
15 | h := md5.New() | |
16 | ||
17 | // hash the body. seek back to the first position after reading to reset | |
18 | // the body for transmission. copy errors may be assumed to be from the | |
19 | // body. | |
20 | _, err := io.Copy(h, r.Body) | |
21 | if err != nil { | |
22 | r.Error = awserr.New("ContentMD5", "failed to read body", err) | |
23 | return | |
24 | } | |
25 | _, err = r.Body.Seek(0, 0) | |
26 | if err != nil { | |
27 | r.Error = awserr.New("ContentMD5", "failed to seek body", err) | |
28 | return | |
29 | } | |
30 | ||
31 | // encode the md5 checksum in base64 and set the request header. | |
32 | sum := h.Sum(nil) | |
33 | sum64 := make([]byte, base64.StdEncoding.EncodedLen(len(sum))) | |
34 | base64.StdEncoding.Encode(sum64, sum) | |
35 | r.HTTPRequest.Header.Set("Content-MD5", string(sum64)) | |
36 | } |