Skip to content

Commit 5491004

Browse files
VanCodingtrevnorris
authored andcommittedOct 17, 2013
http: add statusMessage
Now the status message can be set via req.statusMessage = 'msg';
1 parent 2bc30f2 commit 5491004

File tree

3 files changed

+72
-6
lines changed

3 files changed

+72
-6
lines changed
 

‎doc/api/http.markdown

+16-2
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,11 @@ Indicates that the underlying connection was terminated before
258258
Sends a HTTP/1.1 100 Continue message to the client, indicating that
259259
the request body should be sent. See the ['checkContinue'][] event on `Server`.
260260

261-
### response.writeHead(statusCode, [reasonPhrase], [headers])
261+
### response.writeHead(statusCode, [statusMessage], [headers])
262262

263263
Sends a response header to the request. The status code is a 3-digit HTTP
264264
status code, like `404`. The last argument, `headers`, are the response headers.
265-
Optionally one can give a human-readable `reasonPhrase` as the second
265+
Optionally one can give a human-readable `statusMessage` as the second
266266
argument.
267267

268268
Example:
@@ -313,6 +313,20 @@ Example:
313313
After response header was sent to the client, this property indicates the
314314
status code which was sent out.
315315

316+
### response.statusMessage
317+
318+
When using implicit headers (not calling `response.writeHead()` explicitly), this property
319+
controls the status message that will be sent to the client when the headers get
320+
flushed. If this is left as `undefined` then the standard message for the status
321+
code will be used.
322+
323+
Example:
324+
325+
response.statusMessage = 'Not found';
326+
327+
After response header was sent to the client, this property indicates the
328+
status message which was sent out.
329+
316330
### response.setHeader(name, value)
317331

318332
Sets a single header value for implicit headers. If this header already exists

‎lib/_http_server.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ ServerResponse.prototype._finish = function() {
122122
exports.ServerResponse = ServerResponse;
123123

124124
ServerResponse.prototype.statusCode = 200;
125+
ServerResponse.prototype.statusMessage = undefined;
125126

126127
function onServerResponseClose() {
127128
// EventEmitter.emit makes a copy of the 'close' listeners array before
@@ -171,13 +172,14 @@ ServerResponse.prototype._implicitHeader = function() {
171172
};
172173

173174
ServerResponse.prototype.writeHead = function(statusCode) {
174-
var reasonPhrase, headers, headerIndex;
175+
var headers, headerIndex;
175176

176177
if (util.isString(arguments[1])) {
177-
reasonPhrase = arguments[1];
178+
this.statusMessage = arguments[1];
178179
headerIndex = 2;
179180
} else {
180-
reasonPhrase = STATUS_CODES[statusCode] || 'unknown';
181+
this.statusMessage =
182+
this.statusMessage || STATUS_CODES[statusCode] || 'unknown';
181183
headerIndex = 1;
182184
}
183185
this.statusCode = statusCode;
@@ -217,7 +219,7 @@ ServerResponse.prototype.writeHead = function(statusCode) {
217219
}
218220

219221
var statusLine = 'HTTP/1.1 ' + statusCode.toString() + ' ' +
220-
reasonPhrase + CRLF;
222+
this.statusMessage + CRLF;
221223

222224
if (statusCode === 204 || statusCode === 304 ||
223225
(100 <= statusCode && statusCode <= 199)) {
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var http = require('http');
25+
var net = require('net');
26+
27+
var s = http.createServer(function(req, res) {
28+
res.statusCode = 200;
29+
res.statusMessage = 'Custom Message';
30+
res.end('');
31+
});
32+
33+
s.listen(common.PORT, test);
34+
35+
36+
function test() {
37+
var bufs = [];
38+
var client = net.connect(common.PORT, function() {
39+
client.write('GET / HTTP/1.1\r\nConnection: close\r\n\r\n');
40+
});
41+
client.on('data', function(chunk) {
42+
bufs.push(chunk);
43+
});
44+
client.on('end', function() {
45+
var head = Buffer.concat(bufs).toString('binary').split('\r\n')[0];
46+
assert.equal('HTTP/1.1 200 Custom Message', head);
47+
console.log('ok');
48+
s.close();
49+
});
50+
}

0 commit comments

Comments
 (0)
Please sign in to comment.