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
Provenance
- Commit
f14e39d- Tree
f980f51- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
084f5d8
9 files changed+179 added−143 removed
modified:lib/response.js+16 added-1 removed
7 unmodified lines8 , 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 lines83 84 res.send = function(body){85 var req = this.req;86 var head = 'HEAD' == req.method;87+ var type;88+ var encoding;89 var len;90 91 // settings92 var app = this.app;49 unmodified lines142 this.set('ETag', etag(body));143 }144 }145 146+ // write strings in utf-8147+ if ('string' === typeof body) {148+ encoding = 'utf8';149+ type = this.get('Content-Type');150+151+ // reflect this in content-type152+ if ('string' === typeof type) {153+ this.set('Content-Type', setCharset(type, 'utf-8'));154+ }155+ }156+157 // freshness158 if (req.fresh) this.statusCode = 304;159 160 // strip irrelevant headers4 unmodified lines165 body = '';166 }167 168 // respond155- this.end(head ? null : body);169+ this.end((head ? null : body), encoding);170+171 return this;172 };173 174 /**663 unmodified linesmodified:lib/utils.js+36 added
13 unmodified lines14 15 var toString = {}.toString;16 17 /**18+ * Simple detection of charset parameter in content-type19+ */20+var charsetRegExp = /;\s*charset\s*=/;21+22+/**23 * Deprecate function, like core `util.deprecate`,24 * but with NODE_ENV and color support.25 *26 * @param {Function} fn344 unmodified lines371 }372 373 return proxyaddr.compile(val || []);374 }375+376+/**377+ * Set the charset in a given Content-Type string.378+ *379+ * @param {String} type380+ * @param {String} charset381+ * @return {String}382+ * @api private383+ */384+385+exports.setCharset = function(type, charset){386+ if (!type || !charset) return type;387+388+ var exists = charsetRegExp.test(type);389+390+ // removing existing charset391+ if (exists) {392+ var parts = type.split(';');393+394+ for (var i = 1; i < parts.length; i++) {395+ if (charsetRegExp.test(';' + parts[i])) {396+ parts.splice(i, 1);397+ break;398+ }399+ }400+401+ type = parts.join(';');402+ }403+404+ return type + '; charset=' + charset;405+};modified:test/res.attachment.js+1 added-1 removed
34 unmodified lines353637383939404142434 unmodified lines34 unmodified lines var app = express();
app.use(function(req, res){ res.attachment('/path/to/image.png'); res.send('foo'); res.send(new Buffer(4)); });
request(app) .get('/')4 unmodified linesmodified:test/res.charset.js+2 added-2 removed
16 unmodified lines171819202121222324252627282930313132333416 unmodified lines .get('/') .expect("text/x-foo; charset=utf-8", done); })
it('should take precedence over res.send() defaults', function(done){ it('should be replaced by real charset in res.send', function(done){ var app = express();
app.use(function(req, res){ res.charset = 'whoop'; res.send('hey'); });
request(app) .get('/') .expect('Content-Type', 'text/html; charset=whoop', done); .expect('Content-Type', 'text/html; charset=utf-8', done); }) })})modified:test/res.format.js+3 added-3 removed
109 unmodified lines110 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 })117 118 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');123 124 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');128 129 request(app)130 .get('/')131 .set('Accept', 'application/json')28 unmodified linesmodified:test/res.json.js+36 added-59 removed
15 unmodified lines16 .get('/?callback=foo')17 .expect('{"foo":"bar"}', done);18 })19 20+ it('should not override previous Content-Types', function(done){21+ var app = express();22+23+ app.get('/', function(req, res){24+ res.type('application/vnd.example+json');25+ res.json({ hello: 'world' });26+ });27+28+ request(app)29+ .get('/')30+ .expect('Content-Type', 'application/vnd.example+json; charset=utf-8')31+ .expect(200, '{"hello":"world"}', done);32+ })33+34 describe('when given primitives', function(){35 it('should respond with json for null', function(done){36 var app = express();37 2 unmodified lines40 });41 42 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 })47 48 it('should respond with json for Number', function(done){49 var app = express();3 unmodified lines53 });54 55 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 })60 61 it('should respond with json for String', function(done){62 var app = express();3 unmodified lines66 });67 68 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 })74 75 describe('when given an array', function(){5 unmodified lines81 });82 83 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 lines96 });97 98 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 })104 105 describe('"json replacer" setting', function(){11 unmodified lines117 });118 119 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 })125 126 describe('"json spaces" setting', function(){19 unmodified lines146 });147 148 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 lines162 });163 164 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 })170 171 describe('.json(object, status)', function(){5 unmodified lines177 });178 179 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 })184 185 it('should use status as second number for backwards compat', function(done){186 var app = express();3 unmodified lines190 });191 192 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 })198 199 it('should not override previous Content-Types', function(done){5 unmodified lines205 });206 207 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 lines45 });46 47 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 })52 53 it('should allow renaming callback', function(done){54 var app = express();60 unmodified lines115 done();116 });117 });118 119+ it('should not override previous Content-Types with no callback', function(done){120+ var app = express();121+122+ app.get('/', function(req, res){123+ res.type('application/vnd.example+json');124+ res.jsonp({ hello: 'world' });125+ });126+127+ request(app)128+ .get('/')129+ .expect('Content-Type', 'application/vnd.example+json; charset=utf-8')130+ .expect(200, '{"hello":"world"}', done);131+ })132+133+ it('should override previous Content-Types with callback', function(done){134+ var app = express();135+136+ app.get('/', function(req, res){137+ res.type('application/vnd.example+json');138+ res.jsonp({ hello: 'world' });139+ });140+141+ request(app)142+ .get('/?callback=cb')143+ .expect('Content-Type', 'text/javascript; charset=utf-8')144+ .expect(200, /cb\(\{"hello":"world"\}\);$/, done);145+ })146+147 describe('when given primitives', function(){148 it('should respond with json', function(done){149 var app = express();150 2 unmodified lines153 });154 155 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 })161 162 describe('when given an array', function(){5 unmodified lines168 });169 170 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 })176 177 describe('when given an object', function(){5 unmodified lines183 });184 185 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 })191 192 describe('when given primitives', function(){5 unmodified lines198 });199 200 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 })205 206 it('should respond with json for Number', function(done){207 var app = express();3 unmodified lines211 });212 213 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 })218 219 it('should respond with json for String', function(done){220 var app = express();3 unmodified lines224 });225 226 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 })232 233 describe('"json replacer" setting', function(){11 unmodified lines245 });246 247 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 })253 254 describe('"json spaces" setting', function(){19 unmodified lines274 });275 276 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 lines290 });291 292 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 })298 299 describe('.jsonp(object, status)', function(){5 unmodified lines305 });306 307 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 })312 313 it('should use status as second number for backwards compat', function(done){314 var app = express();3 unmodified lines318 });319 320 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 })326 327 it('should not override previous Content-Types', function(done){5 unmodified lines333 });334 335 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 lines134 });135 136 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 })141+142+ it('should override charset in Content-Type', function(done){143+ var app = express();144+145+ app.use(function(req, res){146+ res.set('Content-Type', 'text/plain; charset=iso-8859-1').send('hey');147+ });148+149+ request(app)150+ .get('/')151+ .expect('Content-Type', 'text/plain; charset=utf-8')152+ .expect(200, 'hey', done);153+ })154+155+ it('should keep charset in Content-Type for Buffers', function(done){156+ var app = express();157+158+ app.use(function(req, res){159+ res.set('Content-Type', 'text/plain; charset=iso-8859-1').send(new Buffer('hi'));160+ });161+162+ request(app)163+ .get('/')164+ .expect('Content-Type', 'text/plain; charset=iso-8859-1')165+ .expect(200, 'hi', done);166+ })167 })168 169 describe('.send(Buffer)', function(){170 it('should send as octet-stream', function(done){54 unmodified lines225 });226 227 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 })233 234 describe('when the request method is HEAD', function(){182 unmodified linesmodified:History.md+1 added
1 3.x2 ===3 4 * 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.11341 unmodified lines