Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cast string bodies to buffers to ensure correct encoding used #1303

Merged
merged 2 commits into from Jan 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/next-release/bugfix-S3-3a275bd9.json
@@ -0,0 +1,5 @@
{
"type": "bugfix",
"category": "S3",
"description": "Convert string bodies to buffers to ensure correct encoding is used"
}
14 changes: 14 additions & 0 deletions features/s3/objects.feature
Expand Up @@ -27,6 +27,20 @@ Feature: Working with Objects in S3
Then I get the object "contentlength"
And the object "contentlength" should contain "foo"

@multi-byte
Scenario: Putting multi-byte metadata
When I put a buffer containing "foo" to the key "mbmd" with the metadata key "mdkey" and value "voilà ça marche"
Then the object "mbmd" should exist
Then I get the object "mbmd"
And the object "mbmd" should contain "foo"

@multi-byte
Scenario: Putting multi-byte metadata
When I put "foo" to the key "mbmd" with the metadata key "mdkey" and value "voilà ça marche"
Then the object "mbmd" should exist
Then I get the object "mbmd"
And the object "mbmd" should contain "foo"

@multi-byte
Scenario: Putting a multi-byte string to an object
When I put "åß∂ƒ©" to the key "multi"
Expand Down
16 changes: 16 additions & 0 deletions features/s3/step_definitions/objects.js
Expand Up @@ -29,6 +29,22 @@ module.exports = function () {
this.request('s3nochecksums', 'putObject', params, next);
});

this.When(/^I put "([^"]*)" to the key "([^"]*)" with the metadata key "([^"]*)" and value "([^"]*)"$/, function(contents, key, mdKey, mdValue, next) {
var metadata = {};
metadata[mdKey] = mdValue;
var params = {Bucket: this.sharedBucket, Key: key, Body: contents, Metadata: metadata };
this.s3nochecksums = new this.AWS.S3({computeChecksums: false});
this.request('s3nochecksums', 'putObject', params, next);
});

this.When(/^I put a buffer containing "([^"]*)" to the key "([^"]*)" with the metadata key "([^"]*)" and value "([^"]*)"$/, function(contents, key, mdKey, mdValue, next) {
var metadata = {};
metadata[mdKey] = mdValue;
var params = {Bucket: this.sharedBucket, Key: key, Body: new Buffer(contents), Metadata: metadata };
this.s3nochecksums = new this.AWS.S3({computeChecksums: false});
this.request('s3nochecksums', 'putObject', params, next);
});

this.Then(/^the object "([^"]*)" should contain "([^"]*)"$/, function(key, contents, next) {
this.assert.equal(this.data.Body.toString().replace('\n', ''), contents);
next();
Expand Down
4 changes: 4 additions & 0 deletions lib/http/node.js
Expand Up @@ -100,6 +100,10 @@ AWS.NodeHttpClient = AWS.util.inherit({
total: totalBytes
});
});
if (typeof body === 'string') {
body = typeof Buffer.from === 'function' ?
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This unfortunately is broken on Node 4.4.5 :-( Buffer.from is a function but fails (because it is actually Uint8Array.from)

root@45d8cedbf66f:/app# node --version
v4.4.5
root@45d8cedbf66f:/app# node
> Buffer.from === Uint8Array.from
true
> typeof Buffer.from
'function'
> Buffer.from("foo")
TypeError: this is not a typed array.
    at Function.from (native)
...

Buffer.from(body) : new Buffer(body);
}
stream.end(body);
} else {
// no request body
Expand Down