Skip to content

Commit 12d78cd

Browse files
committed
Rename sendHeader to writeHeader; allow reasonPhrase
1 parent 05d6da6 commit 12d78cd

21 files changed

+52
-31
lines changed

benchmark/http_simple.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ http.createServer(function (req, res) {
4747

4848
var content_length = body.length.toString();
4949

50-
res.sendHeader( status
50+
res.writeHeader( status
5151
, { "Content-Type": "text/plain"
5252
, "Content-Length": content_length
5353
}

benchmark/static_http_server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ for (var i = 0; i < bytes; i++) {
1616
}
1717

1818
var server = http.createServer(function (req, res) {
19-
res.sendHeader(200, {
19+
res.writeHeader(200, {
2020
"Content-Type": "text/plain",
2121
"Content-Length": body.length
2222
});

doc/api.txt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ World":
1919
var sys = require("sys"),
2020
http = require("http");
2121
http.createServer(function (request, response) {
22-
response.sendHeader(200, {"Content-Type": "text/plain"});
22+
response.writeHeader(200, {"Content-Type": "text/plain"});
2323
response.write("Hello World\n");
2424
response.close();
2525
}).listen(8000);
@@ -916,16 +916,18 @@ The +http.Connection+ object.
916916
This object is created internally by a HTTP server--not by the user. It is
917917
passed as the second parameter to the +"request"+ event.
918918

919-
+response.sendHeader(statusCode, headers)+ ::
919+
+response.writeHeader(statusCode[, reasonPhrase] , headers)+ ::
920920

921921
Sends a response header to the request. The status code is a 3-digit HTTP
922-
status code, like +404+. The second argument, +headers+, are the response headers.
922+
status code, like +404+. The last argument, +headers+, are the response headers.
923+
Optionally one can give a human-readable +reasonPhrase+ as the second
924+
argument.
923925
+
924926
Example:
925927
+
926928
----------------------------------------
927929
var body = "hello world";
928-
response.sendHeader(200, {
930+
response.writeHeader(200, {
929931
"Content-Length": body.length,
930932
"Content-Type": "text/plain"
931933
});
@@ -936,7 +938,7 @@ be called before +response.close()+ is called.
936938

937939
+response.write(chunk, encoding="ascii")+ ::
938940

939-
This method must be called after +sendHeader+ was
941+
This method must be called after +writeHeader+ was
940942
called. It sends a chunk of the response body. This method may
941943
be called multiple times to provide successive parts of the body.
942944
+
@@ -1260,7 +1262,7 @@ http.createServer(function (req, res) {
12601262
fields = {},
12611263
name, filename;
12621264
mp.addListener("error", function (er) {
1263-
res.sendHeader(400, {"content-type":"text/plain"});
1265+
res.writeHeader(400, {"content-type":"text/plain"});
12641266
res.write("You sent a bad message!\n"+er.message);
12651267
res.close();
12661268
});
@@ -1280,7 +1282,7 @@ http.createServer(function (req, res) {
12801282
});
12811283
mp.addListener("complete", function () {
12821284
var response = "You posted: \n" + sys.inspect(fields);
1283-
res.sendHeader(200, {
1285+
res.writeHeader(200, {
12841286
"content-type" : "text/plain",
12851287
"content-length" : response.length
12861288
});

doc/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
http = require('http');
4949
http.createServer(function (req, res) {
5050
setTimeout(function () {
51-
res.sendHeader(200, {'Content-Type': 'text/plain'});
51+
res.writeHeader(200, {'Content-Type': 'text/plain'});
5252
res.write('Hello World');
5353
res.close();
5454
}, 2000);

lib/http.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,31 @@ function ServerResponse (req) {
256256
sys.inherits(ServerResponse, OutgoingMessage);
257257
exports.ServerResponse = ServerResponse;
258258

259-
ServerResponse.prototype.sendHeader = function (statusCode, headers) {
260-
var reason = STATUS_CODES[statusCode] || "unknown";
261-
var status_line = "HTTP/1.1 " + statusCode.toString() + " " + reason + CRLF;
259+
260+
ServerResponse.prototype.writeHeader = function (statusCode) {
261+
var reasonPhrase, headers, headerIndex;
262+
263+
if (typeof arguments[1] == 'string') {
264+
reasonPhrase = arguments[1];
265+
headerIndex = 2;
266+
} else {
267+
reasonPhrase = STATUS_CODES[statusCode] || "unknown";
268+
headerIndex = 1;
269+
}
270+
271+
if (typeof arguments[headerIndex] == 'object') {
272+
headers = arguments[headerIndex];
273+
} else {
274+
headers = {};
275+
}
276+
277+
var status_line = "HTTP/1.1 " + statusCode.toString() + " "
278+
+ reasonPhrase + CRLF;
262279
this.sendHeaderLines(status_line, headers);
263280
};
264281

282+
// TODO eventually remove sendHeader()
283+
ServerResponse.prototype.sendHeader = ServerResponse.prototype.writeHeader;
265284

266285
function ClientRequest (method, url, headers) {
267286
OutgoingMessage.call(this);

test/mjsunit/test-byte-length.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ assert.throws(function() {
1212
});
1313
assert.throws(function() {
1414
process._byteLength(5);
15-
});
15+
});

test/mjsunit/test-http-1.0.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var server_response = "";
99
var client_got_eof = false;
1010

1111
var server = http.createServer(function (req, res) {
12-
res.sendHeader(200, {"Content-Type": "text/plain"});
12+
res.writeHeader(200, {"Content-Type": "text/plain"});
1313
res.write(body);
1414
res.close();
1515
})

test/mjsunit/test-http-cat.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ PORT = 8888;
55
var body = "exports.A = function() { return 'A';}";
66
var server = http.createServer(function (req, res) {
77
puts("got request");
8-
res.sendHeader(200, [
8+
res.writeHeader(200, [
99
["Content-Length", body.length],
1010
["Content-Type", "text/plain"]
1111
]);

test/mjsunit/test-http-chunked.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var PORT = 8888;
55
var UTF8_STRING = "Il était tué";
66

77
var server = http.createServer(function(req, res) {
8-
res.sendHeader(200, {"Content-Type": "text/plain; charset=utf8"});
8+
res.writeHeader(200, {"Content-Type": "text/plain; charset=utf8"});
99
res.write(UTF8_STRING, 'utf8');
1010
res.close();
1111
});

test/mjsunit/test-http-client-race.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var body2_s = "22222";
88

99
var server = http.createServer(function (req, res) {
1010
var body = url.parse(req.url).pathname === "/1" ? body1_s : body2_s;
11-
res.sendHeader(200, { "Content-Type": "text/plain"
11+
res.writeHeader(200, { "Content-Type": "text/plain"
1212
, "Content-Length": body.length
1313
});
1414
res.write(body);

0 commit comments

Comments
 (0)