Skip to content
Myr

set proper charset in content-type for res.send

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This will write strings to sockets with an explicit "utf8" encoding (which is the default) and will override the charset in the Content-Type so it properly relfects the encoding of the response. closes #1631 closes #2092

authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent 084f5d8 · Browse files · 9 files+179 added−143 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

9 files+179 added−143 removed

  • tests:+126 added−142 removed
  • other:+53 added−1 removed

9 files+179 added−143 removed

modified:lib/response.js+16 added-1 removed
7 unmodified lines
8 , utils = connect.utils9 , sign = require('cookie-signature').sign10 , normalizeType = require('./utils').normalizeType11 , normalizeTypes = require('./utils').normalizeTypes12 , setCharset = require('./utils').setCharset13 , deprecate = require('./utils').deprecate14 , etag = require('./utils').etag15 , statusCodes = http.STATUS_CODES16 , cookie = require('cookie')
66 unmodified lines
8384res.send = function(body){85 var req = this.req;86 var head = 'HEAD' == req.method;87 var type;88 var encoding;89 var len;9091 // settings92 var app = this.app;
49 unmodified lines
142 this.set('ETag', etag(body));143 }144 }145146 // write strings in utf-8147 if ('string' === typeof body) {148 encoding = 'utf8';149 type = this.get('Content-Type');150151 // reflect this in content-type152 if ('string' === typeof type) {153 this.set('Content-Type', setCharset(type, 'utf-8'));154 }155 }156157 // freshness158 if (req.fresh) this.statusCode = 304;159160 // strip irrelevant headers
4 unmodified lines
165 body = '';166 }167168 // respond155 this.end(head ? null : body);169 this.end((head ? null : body), encoding);170171 return this;172};173174/**
663 unmodified lines
modified:lib/utils.js+36 added
13 unmodified lines
1415var toString = {}.toString;1617/**18 * Simple detection of charset parameter in content-type19 */20var charsetRegExp = /;\s*charset\s*=/;2122/**23 * Deprecate function, like core `util.deprecate`,24 * but with NODE_ENV and color support.25 *26 * @param {Function} fn
344 unmodified lines
371 }372373 return proxyaddr.compile(val || []);374}375376/**377 * Set the charset in a given Content-Type string.378 *379 * @param {String} type380 * @param {String} charset381 * @return {String}382 * @api private383 */384385exports.setCharset = function(type, charset){386 if (!type || !charset) return type;387388 var exists = charsetRegExp.test(type);389390 // removing existing charset391 if (exists) {392 var parts = type.split(';');393394 for (var i = 1; i < parts.length; i++) {395 if (charsetRegExp.test(';' + parts[i])) {396 parts.splice(i, 1);397 break;398 }399 }400401 type = parts.join(';');402 }403404 return type + '; charset=' + charset;405};
modified:test/res.attachment.js+1 added-1 removed
modified:test/res.charset.js+2 added-2 removed
modified:test/res.format.js+3 added-3 removed
109 unmodified lines
110 it('should default the Content-Type', function(done){111 request(app)112 .get('/')113 .set('Accept', 'text/html; q=.5, text/plain')114 .expect('Content-Type', 'text/plain; charset=UTF-8')114 .expect('Content-Type', 'text/plain; charset=utf-8')115 .expect('hey', done);116 })117118 it('should set the correct charset for the Content-Type', function() {119 request(app)120 .get('/')121 .set('Accept', 'text/html')122 .expect('Content-Type', 'text/html; charset=UTF-8');122 .expect('Content-Type', 'text/html; charset=utf-8');123124 request(app)125 .get('/')126 .set('Accept', 'text/plain')127 .expect('Content-Type', 'text/plain; charset=UTF-8');127 .expect('Content-Type', 'text/plain; charset=utf-8');128129 request(app)130 .get('/')131 .set('Accept', 'application/json')
28 unmodified lines
modified:test/res.json.js+36 added-59 removed
15 unmodified lines
16 .get('/?callback=foo')17 .expect('{"foo":"bar"}', done);18 })1920 it('should not override previous Content-Types', function(done){21 var app = express();2223 app.get('/', function(req, res){24 res.type('application/vnd.example+json');25 res.json({ hello: 'world' });26 });2728 request(app)29 .get('/')30 .expect('Content-Type', 'application/vnd.example+json; charset=utf-8')31 .expect(200, '{"hello":"world"}', done);32 })3334 describe('when given primitives', function(){35 it('should respond with json for null', function(done){36 var app = express();37
2 unmodified lines
40 });4142 request(app)43 .get('/')30 .end(function(err, res){31 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');32 res.text.should.equal('null');33 done();34 })44 .expect('Content-Type', 'application/json; charset=utf-8')45 .expect(200, 'null', done)46 })4748 it('should respond with json for Number', function(done){49 var app = express();
3 unmodified lines
53 });5455 request(app)56 .get('/')46 .end(function(err, res){47 res.statusCode.should.equal(200);48 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');49 res.text.should.equal('300');50 done();51 })57 .expect('Content-Type', 'application/json; charset=utf-8')58 .expect(200, '300', done)59 })6061 it('should respond with json for String', function(done){62 var app = express();
3 unmodified lines
66 });6768 request(app)69 .get('/')63 .end(function(err, res){64 res.statusCode.should.equal(200);65 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');66 res.text.should.equal('"str"');67 done();68 })70 .expect('Content-Type', 'application/json; charset=utf-8')71 .expect(200, '"str"', done)72 })73 })7475 describe('when given an array', function(){
5 unmodified lines
81 });8283 request(app)84 .get('/')82 .end(function(err, res){83 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');84 res.text.should.equal('["foo","bar","baz"]');85 done();86 })85 .expect('Content-Type', 'application/json; charset=utf-8')86 .expect(200, '["foo","bar","baz"]', done)87 })88 })89 90 describe('when given an object', function(){
5 unmodified lines
96 });9798 request(app)99 .get('/')100 .end(function(err, res){101 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');102 res.text.should.equal('{"name":"tobi"}');103 done();104 })100 .expect('Content-Type', 'application/json; charset=utf-8')101 .expect(200, '{"name":"tobi"}', done)102 })103 })104105 describe('"json replacer" setting', function(){
11 unmodified lines
117 });118119 request(app)120 .get('/')124 .end(function(err, res){125 res.text.should.equal('{"name":"tobi"}');126 done();127 });121 .expect('Content-Type', 'application/json; charset=utf-8')122 .expect(200, '{"name":"tobi"}', done)123 })124 })125126 describe('"json spaces" setting', function(){
19 unmodified lines
146 });147148 request(app)149 .get('/')155 .end(function(err, res){156 res.text.should.equal('{\n "name": "tobi",\n "age": 2\n}');157 done();158 });150 .expect('Content-Type', 'application/json; charset=utf-8')151 .expect(200, '{\n "name": "tobi",\n "age": 2\n}', done)152 })153 })154 })155
6 unmodified lines
162 });163164 request(app)165 .get('/')173 .end(function(err, res){174 res.statusCode.should.equal(201);175 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');176 res.text.should.equal('{"id":1}');177 done();178 })166 .expect('Content-Type', 'application/json; charset=utf-8')167 .expect(201, '{"id":1}', done)168 })169 })170171 describe('.json(object, status)', function(){
5 unmodified lines
177 });178179 request(app)180 .get('/')192 .end(function(err, res){193 res.statusCode.should.equal(201);194 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');195 res.text.should.equal('{"id":1}');196 done();197 })181 .expect('Content-Type', 'application/json; charset=utf-8')182 .expect(201, '{"id":1}', done)183 })184185 it('should use status as second number for backwards compat', function(done){186 var app = express();
3 unmodified lines
190 });191192 request(app)193 .get('/')209 .end(function(err, res){210 res.statusCode.should.equal(201);211 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');212 res.text.should.equal('200');213 done();214 })194 .expect('Content-Type', 'application/json; charset=utf-8')195 .expect(201, '200', done)196 })197 })198199 it('should not override previous Content-Types', function(done){
5 unmodified lines
205 });206207 request(app)208 .get('/')228 .end(function(err, res){229 res.statusCode.should.equal(200);230 res.headers.should.have.property('content-type', 'application/vnd.example+json');231 res.text.should.equal('{"hello":"world"}');232 done();233 })209 .expect('content-type', 'application/vnd.example+json; charset=utf-8')210 .expect(200, '{"hello":"world"}', done)211 })212})
modified:test/res.jsonp.js+54 added-69 removed
44 unmodified lines
45 });4647 request(app)48 .get('/?callback[a]=something')49 .end(function(err, res){50 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');51 res.text.should.equal('{"count":1}');52 done();53 })49 .expect('Content-Type', 'application/json; charset=utf-8')50 .expect(200, '{"count":1}', done)51 })5253 it('should allow renaming callback', function(done){54 var app = express();
60 unmodified lines
115 done();116 });117 });118119 it('should not override previous Content-Types with no callback', function(done){120 var app = express();121122 app.get('/', function(req, res){123 res.type('application/vnd.example+json');124 res.jsonp({ hello: 'world' });125 });126127 request(app)128 .get('/')129 .expect('Content-Type', 'application/vnd.example+json; charset=utf-8')130 .expect(200, '{"hello":"world"}', done);131 })132133 it('should override previous Content-Types with callback', function(done){134 var app = express();135136 app.get('/', function(req, res){137 res.type('application/vnd.example+json');138 res.jsonp({ hello: 'world' });139 });140141 request(app)142 .get('/?callback=cb')143 .expect('Content-Type', 'text/javascript; charset=utf-8')144 .expect(200, /cb\(\{"hello":"world"\}\);$/, done);145 })146147 describe('when given primitives', function(){148 it('should respond with json', function(done){149 var app = express();150
2 unmodified lines
153 });154155 request(app)156 .get('/')132 .end(function(err, res){133 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');134 res.text.should.equal('null');135 done();136 })157 .expect('Content-Type', 'application/json; charset=utf-8')158 .expect(200, 'null', done)159 })160 })161162 describe('when given an array', function(){
5 unmodified lines
168 });169170 request(app)171 .get('/')150 .end(function(err, res){151 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');152 res.text.should.equal('["foo","bar","baz"]');153 done();154 })172 .expect('Content-Type', 'application/json; charset=utf-8')173 .expect(200, '["foo","bar","baz"]', done)174 })175 })176177 describe('when given an object', function(){
5 unmodified lines
183 });184185 request(app)186 .get('/')168 .end(function(err, res){169 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');170 res.text.should.equal('{"name":"tobi"}');171 done();172 })187 .expect('Content-Type', 'application/json; charset=utf-8')188 .expect(200, '{"name":"tobi"}', done)189 })190 })191192 describe('when given primitives', function(){
5 unmodified lines
198 });199200 request(app)201 .get('/')186 .end(function(err, res){187 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');188 res.text.should.equal('null');189 done();190 })202 .expect('Content-Type', 'application/json; charset=utf-8')203 .expect(200, 'null', done)204 })205206 it('should respond with json for Number', function(done){207 var app = express();
3 unmodified lines
211 });212213 request(app)214 .get('/')202 .end(function(err, res){203 res.statusCode.should.equal(200);204 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');205 res.text.should.equal('300');206 done();207 })215 .expect('Content-Type', 'application/json; charset=utf-8')216 .expect(200, '300', done)217 })218219 it('should respond with json for String', function(done){220 var app = express();
3 unmodified lines
224 });225226 request(app)227 .get('/')219 .end(function(err, res){220 res.statusCode.should.equal(200);221 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');222 res.text.should.equal('"str"');223 done();224 })228 .expect('Content-Type', 'application/json; charset=utf-8')229 .expect(200, '"str"', done)230 })231 })232233 describe('"json replacer" setting', function(){
11 unmodified lines
245 });246247 request(app)248 .get('/')244 .end(function(err, res){245 res.text.should.equal('{"name":"tobi"}');246 done();247 });249 .expect('Content-Type', 'application/json; charset=utf-8')250 .expect(200, '{"name":"tobi"}', done)251 })252 })253254 describe('"json spaces" setting', function(){
19 unmodified lines
274 });275276 request(app)277 .get('/')275 .end(function(err, res){276 res.text.should.equal('{\n "name": "tobi",\n "age": 2\n}');277 done();278 });278 .expect('Content-Type', 'application/json; charset=utf-8')279 .expect(200, '{\n "name": "tobi",\n "age": 2\n}', done)280 })281 })282 })283
6 unmodified lines
290 });291292 request(app)293 .get('/')293 .end(function(err, res){294 res.statusCode.should.equal(201);295 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');296 res.text.should.equal('{"id":1}');297 done();298 })294 .expect('Content-Type', 'application/json; charset=utf-8')295 .expect(201, '{"id":1}', done)296 })297 })298299 describe('.jsonp(object, status)', function(){
5 unmodified lines
305 });306307 request(app)308 .get('/')312 .end(function(err, res){313 res.statusCode.should.equal(201);314 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');315 res.text.should.equal('{"id":1}');316 done();317 })309 .expect('Content-Type', 'application/json; charset=utf-8')310 .expect(201, '{"id":1}', done)311 })312313 it('should use status as second number for backwards compat', function(done){314 var app = express();
3 unmodified lines
318 });319320 request(app)321 .get('/')329 .end(function(err, res){330 res.statusCode.should.equal(201);331 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');332 res.text.should.equal('200');333 done();334 })322 .expect('Content-Type', 'application/json; charset=utf-8')323 .expect(201, '200', done)324 })325 })326327 it('should not override previous Content-Types', function(done){
5 unmodified lines
333 });334335 request(app)336 .get('/')348 .end(function(err, res){349 res.statusCode.should.equal(200);350 res.headers.should.have.property('content-type', 'application/vnd.example+json');351 res.text.should.equal('{"hello":"world"}');352 done();353 })337 .expect('content-type', 'application/vnd.example+json; charset=utf-8')338 .expect(200, '{"hello":"world"}', done)339 })340})
modified:test/res.send.js+30 added-8 removed
133 unmodified lines
134 });135136 request(app)137 .get('/')138 .expect('Content-Type', 'text/plain')139 .expect('hey')140 .expect(200, done);138 .expect('Content-Type', 'text/plain; charset=utf-8')139 .expect(200, 'hey', done);140 })141142 it('should override charset in Content-Type', function(done){143 var app = express();144145 app.use(function(req, res){146 res.set('Content-Type', 'text/plain; charset=iso-8859-1').send('hey');147 });148149 request(app)150 .get('/')151 .expect('Content-Type', 'text/plain; charset=utf-8')152 .expect(200, 'hey', done);153 })154155 it('should keep charset in Content-Type for Buffers', function(done){156 var app = express();157158 app.use(function(req, res){159 res.set('Content-Type', 'text/plain; charset=iso-8859-1').send(new Buffer('hi'));160 });161162 request(app)163 .get('/')164 .expect('Content-Type', 'text/plain; charset=iso-8859-1')165 .expect(200, 'hi', done);166 })167 })168169 describe('.send(Buffer)', function(){170 it('should send as octet-stream', function(done){
54 unmodified lines
225 });226227 request(app)228 .get('/')204 .end(function(err, res){205 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');206 res.text.should.equal('{"name":"tobi"}');207 done();208 })229 .expect('Content-Type', 'application/json; charset=utf-8')230 .expect(200, '{"name":"tobi"}', done)231 })232 })233234 describe('when the request method is HEAD', function(){
182 unmodified lines
modified:History.md+1 added
13.x2===34 * keep previous `Content-Type` for `res.jsonp`5 * set proper `charset` in `Content-Type` for `res.send`6 * update connect to 2.17.17 - fix `res.charset` appending charset when `content-type` has one8 - deps: express-session@1.2.09 - deps: morgan@1.1.1
1341 unmodified lines