Skip to content

Commit

Permalink
Fix encodings. Closes hapijs#1270
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed Dec 31, 2013
1 parent 879de08 commit 175e5b7
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 20 deletions.
4 changes: 2 additions & 2 deletions docs/Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ When creating a server instance, the following options configure the server's be
in the same engine. The layout template name must be 'layout.ext' where 'ext' is the engine's extension. Disable 'layout' when using Jade as
it will handle including any layout files independently of Hapi. Defaults to `false`.
- `layoutKeyword` - the key used by the template engine to denote where primary template content should go. Defaults to `'content'`.
- `encoding` - the text encoding used by the templates when reading the files and outputting the result. Defaults to `'utf-8'`.
- `encoding` - the text encoding used by the templates when reading the files and outputting the result. Defaults to `'utf8'`.
- `isCached` - if set to `false`, templates will not be cached (thus will be read from file on every use). Defaults to `true`.
- `allowAbsolutePaths` - if set to `true`, allows absolute template paths passed to `reply.view()`. Defaults to `false`.
- `allowInsecureAccess` - if set to `true`, allows template paths passed to `reply.view()` to contain '../'. Defaults to `false`.
Expand Down Expand Up @@ -1958,7 +1958,7 @@ Every response includes the following properties:
key is a plugin name and the value is the state.
- `settings` - response handling flags:
- `encoding` - the string encoding scheme used to serial data into the HTTP payload when `source` is a string or marshalls into a string.
Defaults to `'utf-8'`.
Defaults to `'utf8'`.
- `charset` - the 'Content-Type' HTTP header 'charset' property. Defaults to `'utf-8'`.
- `location` - the raw value used to set the HTTP 'Location' header (actual value set depends on the server
[`location`](#server.config.location) configuration option). Defaults to no header.
Expand Down
2 changes: 1 addition & 1 deletion lib/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ exports.views = {
runtimeOptions: {},
layout: false,
layoutKeyword: 'content',
encoding: 'utf-8',
encoding: 'utf8',
isCached: true,
allowAbsolutePaths: false,
allowInsecureAccess: false,
Expand Down
6 changes: 3 additions & 3 deletions lib/payload.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ internals.parse = function (payload, mime, headers, options, callback) {
// Text

if (mime.match(/^text\/.+$/)) {
return callback(null, payload.toString('utf-8'));
return callback(null, payload.toString('utf8'));
}

// JSON
Expand All @@ -214,7 +214,7 @@ internals.parse = function (payload, mime, headers, options, callback) {
// Form-encoded

if (mime === 'application/x-www-form-urlencoded') {
return callback(null, Querystring.parse(payload.toString('utf-8')));
return callback(null, Querystring.parse(payload.toString('utf8')));
}

// Multipart
Expand Down Expand Up @@ -306,7 +306,7 @@ internals.jsonParse = function (payload, next) {
var error = null;

try {
obj = JSON.parse(payload.toString('utf-8'));
obj = JSON.parse(payload.toString('utf8'));
}
catch (exp) {
error = Boom.badRequest('Invalid request payload JSON format');
Expand Down
6 changes: 4 additions & 2 deletions lib/response/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ var internals = {};

exports.apply = function (response, request, next) {

if (response._payload.size) {
response._header('content-length', response._payload.size(response.settings.encoding));
if (response._payload.size &&
typeof response._payload.size === 'function') {

response._header('content-length', response._payload.size());
}

if (response.settings.location) {
Expand Down
11 changes: 6 additions & 5 deletions lib/response/payload.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ exports = module.exports = internals.Payload = function (payload, request, optio
this._prefix = null;
this._suffix = null;
this._sizeOffset = 0;
this._encoding = options.encoding;

if (options && options.stringify) {
var space = options.stringify.space || request.server.settings.json.space;
Expand All @@ -29,20 +30,20 @@ Utils.inherits(internals.Payload, Stream.Readable);

internals.Payload.prototype._read = function (size) {

this._prefix && this.push(this._prefix);
this._data && this.push(this._data);
this._suffix && this.push(this._suffix);
this._prefix && this.push(this._prefix, this._encoding);
this._data && this.push(this._data, this._encoding);
this._suffix && this.push(this._suffix, this._encoding);
this.push(null);
};


internals.Payload.prototype.size = function (encoding) {
internals.Payload.prototype.size = function () {

if (this._data === null) {
return this._sizeOffset;
}

return (Buffer.isBuffer(this._data) ? this._data.length : Buffer.byteLength(this._data, encoding)) + this._sizeOffset;
return (Buffer.isBuffer(this._data) ? this._data.length : Buffer.byteLength(this._data, this._encoding)) + this._sizeOffset;
};


Expand Down
8 changes: 4 additions & 4 deletions lib/response/plain.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ exports = module.exports = internals.Plain = function (source, variety) {
this.plugins = {};

this.settings = {
encoding: 'utf-8',
charset: 'utf-8',
encoding: 'utf8',
charset: 'utf-8', // '-' required by IANA
location: null,
ttl: null,
stringify: null,
Expand Down Expand Up @@ -51,7 +51,7 @@ exports = module.exports = internals.Plain = function (source, variety) {
this.variety = 'stream';
this.passThrough = this._passThrough;
}
else {
else if (this.variety === 'plain') {
this.settings.stringify = {}; // JSON.stringify options

this.replacer = this._replacer;
Expand Down Expand Up @@ -335,6 +335,6 @@ Utils.inherits(internals.Peek, Stream.Transform);
internals.Peek.prototype._transform = function (chunk, encoding, callback) {

this.emit('peek', chunk);
this.push(chunk);
this.push(chunk, encoding);
callback();
};
2 changes: 1 addition & 1 deletion lib/views.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ internals.Response.prototype._marshall = function (request, next) {
return next(err);
}

self._payload = new Response.Payload(rendered);
self._payload = new Response.Payload(rendered, request, self.settings);
if (config.contentType) {
self.type(config.contentType);
}
Expand Down
4 changes: 2 additions & 2 deletions test/integration/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,12 @@ describe('Proxy', function () {

expect(res.statusCode).to.equal(200);

Fs.readFile(__dirname + '/../../package.json', { encoding: 'utf-8' }, function (err, file) {
Fs.readFile(__dirname + '/../../package.json', { encoding: 'utf8' }, function (err, file) {

Zlib.unzip(new Buffer(res.payload, 'binary'), function (err, unzipped) {

expect(err).to.not.exist;
expect(unzipped.toString('utf-8')).to.deep.equal(file);
expect(unzipped.toString('utf8')).to.deep.equal(file);
done();
});
});
Expand Down

0 comments on commit 175e5b7

Please sign in to comment.