Skip to content

Commit 62b20e9

Browse files
francois-spectreachrinza
authored andcommitted
fix: return a 415 if the call has no Content-Type in the headers
Signed-off-by: François Cabrol <[email protected]>
1 parent 68a67a8 commit 62b20e9

File tree

2 files changed

+64
-39
lines changed

2 files changed

+64
-39
lines changed

src/server.js

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -81,50 +81,56 @@ class Server extends Base {
8181
}
8282
res.end();
8383
} else if (req.method === 'POST') {
84-
res.setHeader('Content-Type', req.headers['content-type']);
85-
var chunks = [], gunzip;
86-
if (compress && req.headers['content-encoding'] === 'gzip') {
87-
gunzip = new compress.Gunzip();
88-
gunzip.init();
89-
}
90-
req.on('data', function(chunk) {
91-
if (gunzip)
92-
chunk = gunzip.inflate(chunk, 'binary');
93-
chunks.push(chunk);
94-
});
95-
req.on('end', function() {
96-
var xml = chunks.join('');
97-
var result;
98-
var error;
99-
if (gunzip) {
100-
gunzip.end();
101-
gunzip = null;
84+
if (!req.headers['content-type']) {
85+
res.statusCode = 415;
86+
res.write('The Content-Type is expected in the headers');
87+
res.end();
88+
} else {
89+
res.setHeader('Content-Type', req.headers['content-type']);
90+
var chunks = [], gunzip;
91+
if (compress && req.headers['content-encoding'] === 'gzip') {
92+
gunzip = new compress.Gunzip();
93+
gunzip.init();
10294
}
103-
try {
104-
if (typeof self.log === 'function') {
105-
self.log('received', xml);
95+
req.on('data', function(chunk) {
96+
if (gunzip)
97+
chunk = gunzip.inflate(chunk, 'binary');
98+
chunks.push(chunk);
99+
});
100+
req.on('end', function() {
101+
var xml = chunks.join('');
102+
var result;
103+
var error;
104+
if (gunzip) {
105+
gunzip.end();
106+
gunzip = null;
106107
}
107-
self._process(xml, req, function(result, statusCode) {
108-
if (statusCode) {
109-
res.statusCode = statusCode;
108+
try {
109+
if (typeof self.log === 'function') {
110+
self.log('received', xml);
110111
}
111-
res.write(result);
112+
self._process(xml, req, function(result, statusCode) {
113+
if (statusCode) {
114+
res.statusCode = statusCode;
115+
}
116+
res.write(result);
117+
res.end();
118+
if (typeof self.log === 'function') {
119+
self.log('replied', result);
120+
}
121+
});
122+
}
123+
catch (err) {
124+
error = err.stack || err;
125+
res.statusCode = 500;
126+
res.write(error);
112127
res.end();
113128
if (typeof self.log === 'function') {
114-
self.log('replied', result);
129+
self.log('error', error);
115130
}
116-
});
117-
}
118-
catch (err) {
119-
error = err.stack || err;
120-
res.statusCode = 500;
121-
res.write(error);
122-
res.end();
123-
if (typeof self.log === 'function') {
124-
self.log('error', error);
125131
}
126-
}
127-
});
132+
});
133+
}
128134
}
129135
else {
130136
res.end();

test/server-test.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ describe('SOAP Server', function() {
202202
body : '<soapenv:Envelope' +
203203
' xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"' +
204204
' xmlns:soap="http://service.applicationsnet.com/soap/">' +
205-
' <soapenv:Header/>' +
206205
' <soapenv:Body>' +
206+
' <soapenv:Header/>' +
207207
'</soapenv:Envelope>',
208208
headers: {'Content-Type': 'text/xml'}
209209
}, function(err, res, body) {
@@ -215,6 +215,25 @@ describe('SOAP Server', function() {
215215
);
216216
});
217217

218+
it('should 415 on missing Content-type header', function(done) {
219+
request.post({
220+
url: test.baseUrl + '/stockquote?wsdl',
221+
body : '<soapenv:Envelope' +
222+
' xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"' +
223+
' xmlns:soap="http://service.applicationsnet.com/soap/">' +
224+
' <soapenv:Header/>' +
225+
' <soapenv:Body>' +
226+
'</soapenv:Envelope>',
227+
headers: {}
228+
}, function(err, res, body) {
229+
assert.ok(!err);
230+
assert.equal(res.statusCode, 415);
231+
assert.equal(body, 'The Content-Type is expected in the headers');
232+
done();
233+
}
234+
);
235+
});
236+
218237
it('should server up WSDL', function(done) {
219238
request(test.baseUrl + '/stockquote?wsdl', function(err, res, body) {
220239
if (err) {
@@ -360,7 +379,7 @@ describe('SOAP Server', function() {
360379
assert.equal(0, parseFloat(result.price));
361380
done();
362381
}, {
363-
soapHeaders: {
382+
soapHeaders: {
364383
SomeToken: 123.45
365384
}
366385
});

0 commit comments

Comments
 (0)