Merge lp:~julian-edwards/gwacl/content-length into lp:gwacl

Proposed by Julian Edwards
Status: Merged
Approved by: Gavin Panella
Approved revision: 44
Merged at revision: 45
Proposed branch: lp:~julian-edwards/gwacl/content-length
Merge into: lp:gwacl
Diff against target: 74 lines (+22/-5)
2 files modified
storage.go (+8/-3)
storage_test.go (+14/-2)
To merge this branch: bzr merge lp:~julian-edwards/gwacl/content-length
Reviewer Review Type Date Requested Status
Gavin Panella Approve
Review via email: mp+154601@code.launchpad.net

Commit message

Ensure that the content-length and content-md5 headers are set on outgoing requests with a body.

To post a comment you must log in.
Revision history for this message
Gavin Panella (allenap) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'storage.go'
2--- storage.go 2013-03-21 04:28:26 +0000
3+++ storage.go 2013-03-21 07:00:34 +0000
4@@ -151,9 +151,12 @@
5 req.Header.Set("x-ms-version", ms_version)
6 }
7
8-// Calculate the mD5sum for the request payload and add it as the Content-MD5
9-// header.
10-func addMD5Header(req *http.Request) {
11+// Calculate the mD5sum and content length for the request payload and add
12+// as the Content-MD5 header and Content-Length header respectively.
13+func addContentHeaders(req *http.Request) {
14+ if req.Body == nil {
15+ return
16+ }
17 hash := md5.New()
18 reqdata, err := ioutil.ReadAll(req.Body)
19 if err != nil {
20@@ -168,6 +171,7 @@
21 }
22 sum := hash.Sum(nil)
23 req.Header.Set("Content-MD5", hex.EncodeToString(sum))
24+ req.Header.Set("Content-Length", fmt.Sprintf("%d", len(reqdata)))
25 }
26
27 // Add a Date: header in RFC1123 format.
28@@ -182,6 +186,7 @@
29 func addStandardHeaders(req *http.Request, account, key, ms_version string) {
30 addVersionHeader(req, ms_version)
31 addDateHeader(req)
32+ addContentHeaders(req)
33 signRequest(req, account, key)
34 }
35
36
37=== modified file 'storage_test.go'
38--- storage_test.go 2013-03-21 04:31:32 +0000
39+++ storage_test.go 2013-03-21 07:00:34 +0000
40@@ -196,20 +196,32 @@
41 c.Assert(req.Header.Get("x-ms-version"), Equals, "2012-02-12")
42 }
43
44-func (suite *TestRequestHeaders) TestMD5Header(c *C) {
45+func (suite *TestRequestHeaders) TestContentHeader(c *C) {
46 data := "test data"
47 req, err := http.NewRequest("GET", "http://example.com/", strings.NewReader(data))
48 c.Assert(err, IsNil)
49- addMD5Header(req)
50+ addContentHeaders(req)
51 expected := "eb733a00c0c9d336e65691a37ab54293"
52 observed := req.Header.Get("Content-MD5")
53 c.Assert(observed, Equals, expected)
54+ c.Assert(
55+ req.Header.Get("Content-Length"), Equals, fmt.Sprintf("%d", len(data)))
56
57 // Ensure that reading the request data didn't destroy it.
58 reqdata, _ := ioutil.ReadAll(req.Body)
59 c.Assert(data, Equals, string(reqdata))
60 }
61
62+func (suite *TestRequestHeaders) TestContentHeaderWithNoBody(c *C) {
63+ req, err := http.NewRequest("GET", "http://example.com/", nil)
64+ c.Assert(err, IsNil)
65+ addContentHeaders(req)
66+ _, md5_present := req.Header[http.CanonicalHeaderKey("Content-MD5")]
67+ c.Check(md5_present, Equals, false)
68+ _, content_present := req.Header[http.CanonicalHeaderKey("Content-Length")]
69+ c.Check(content_present, Equals, false)
70+}
71+
72 func (suite *TestRequestHeaders) TestDateHeader(c *C) {
73 req, err := http.NewRequest("GET", "http://example.com/", nil)
74 c.Assert(err, IsNil)

Subscribers

People subscribed via source and target branches

to all changes: