Skip to content

Commit 8a24a1e

Browse files
committed
Set Content-Length header for OPTIONS requests
Web browsers automatically issue an OPTIONS request in advance of other HTTP requests when the document's domain does not match the target in order to facilitate Cross-Origin Resource Sharing. These requests are forbidden from specifying a body and typically do not specify an (unecessary) `Length` header. Node.js automatically adds the `Content-Encoding: chunked` header value to requests that do not specify a `Length` header. This makes proxied OPTIONS requests from web browsers invalid. Explicitly set the `Content-Length` header of all `OPTIONS` requests to "0", disabling the default "chunked" encoding behavior [2]. [1] http://www.w3.org/TR/cors/ [2] http://nodejs.org/api/http.html
1 parent aba505d commit 8a24a1e

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

lib/http-proxy/passes/web-incoming.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ web_o = Object.keys(web_o).map(function(pass) {
2929
*/
3030

3131
function deleteLength(req, res, options) {
32-
if(req.method === 'DELETE' && !req.headers['content-length']) {
32+
if((req.method === 'DELETE' || req.method === 'OPTIONS')
33+
&& !req.headers['content-length']) {
3334
req.headers['content-length'] = '0';
3435
}
3536
},

test/lib-http-proxy-passes-web-incoming-test.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,23 @@ var webPasses = require('../lib/http-proxy/passes/web-incoming'),
55

66
describe('lib/http-proxy/passes/web.js', function() {
77
describe('#deleteLength', function() {
8-
it('should change `content-length`', function() {
8+
it('should change `content-length` for DELETE requests', function() {
99
var stubRequest = {
1010
method: 'DELETE',
1111
headers: {}
1212
};
1313
webPasses.deleteLength(stubRequest, {}, {});
1414
expect(stubRequest.headers['content-length']).to.eql('0');
15-
})
15+
});
16+
17+
it('should change `content-length` for OPTIONS requests', function() {
18+
var stubRequest = {
19+
method: 'OPTIONS',
20+
headers: {}
21+
};
22+
webPasses.deleteLength(stubRequest, {}, {});
23+
expect(stubRequest.headers['content-length']).to.eql('0');
24+
});
1625
});
1726

1827
describe('#timeout', function() {

0 commit comments

Comments
 (0)