Rename sendHeader to writeHeader; allow reasonPhrase
This commit is contained in:
parent
05d6da6c4a
commit
12d78cd1df
@ -47,7 +47,7 @@ http.createServer(function (req, res) {
|
|||||||
|
|
||||||
var content_length = body.length.toString();
|
var content_length = body.length.toString();
|
||||||
|
|
||||||
res.sendHeader( status
|
res.writeHeader( status
|
||||||
, { "Content-Type": "text/plain"
|
, { "Content-Type": "text/plain"
|
||||||
, "Content-Length": content_length
|
, "Content-Length": content_length
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ for (var i = 0; i < bytes; i++) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var server = http.createServer(function (req, res) {
|
var server = http.createServer(function (req, res) {
|
||||||
res.sendHeader(200, {
|
res.writeHeader(200, {
|
||||||
"Content-Type": "text/plain",
|
"Content-Type": "text/plain",
|
||||||
"Content-Length": body.length
|
"Content-Length": body.length
|
||||||
});
|
});
|
||||||
|
16
doc/api.txt
16
doc/api.txt
@ -19,7 +19,7 @@ World":
|
|||||||
var sys = require("sys"),
|
var sys = require("sys"),
|
||||||
http = require("http");
|
http = require("http");
|
||||||
http.createServer(function (request, response) {
|
http.createServer(function (request, response) {
|
||||||
response.sendHeader(200, {"Content-Type": "text/plain"});
|
response.writeHeader(200, {"Content-Type": "text/plain"});
|
||||||
response.write("Hello World\n");
|
response.write("Hello World\n");
|
||||||
response.close();
|
response.close();
|
||||||
}).listen(8000);
|
}).listen(8000);
|
||||||
@ -916,16 +916,18 @@ The +http.Connection+ object.
|
|||||||
This object is created internally by a HTTP server--not by the user. It is
|
This object is created internally by a HTTP server--not by the user. It is
|
||||||
passed as the second parameter to the +"request"+ event.
|
passed as the second parameter to the +"request"+ event.
|
||||||
|
|
||||||
+response.sendHeader(statusCode, headers)+ ::
|
+response.writeHeader(statusCode[, reasonPhrase] , headers)+ ::
|
||||||
|
|
||||||
Sends a response header to the request. The status code is a 3-digit HTTP
|
Sends a response header to the request. The status code is a 3-digit HTTP
|
||||||
status code, like +404+. The second argument, +headers+, are the response headers.
|
status code, like +404+. The last argument, +headers+, are the response headers.
|
||||||
|
Optionally one can give a human-readable +reasonPhrase+ as the second
|
||||||
|
argument.
|
||||||
+
|
+
|
||||||
Example:
|
Example:
|
||||||
+
|
+
|
||||||
----------------------------------------
|
----------------------------------------
|
||||||
var body = "hello world";
|
var body = "hello world";
|
||||||
response.sendHeader(200, {
|
response.writeHeader(200, {
|
||||||
"Content-Length": body.length,
|
"Content-Length": body.length,
|
||||||
"Content-Type": "text/plain"
|
"Content-Type": "text/plain"
|
||||||
});
|
});
|
||||||
@ -936,7 +938,7 @@ be called before +response.close()+ is called.
|
|||||||
|
|
||||||
+response.write(chunk, encoding="ascii")+ ::
|
+response.write(chunk, encoding="ascii")+ ::
|
||||||
|
|
||||||
This method must be called after +sendHeader+ was
|
This method must be called after +writeHeader+ was
|
||||||
called. It sends a chunk of the response body. This method may
|
called. It sends a chunk of the response body. This method may
|
||||||
be called multiple times to provide successive parts of the body.
|
be called multiple times to provide successive parts of the body.
|
||||||
+
|
+
|
||||||
@ -1260,7 +1262,7 @@ http.createServer(function (req, res) {
|
|||||||
fields = {},
|
fields = {},
|
||||||
name, filename;
|
name, filename;
|
||||||
mp.addListener("error", function (er) {
|
mp.addListener("error", function (er) {
|
||||||
res.sendHeader(400, {"content-type":"text/plain"});
|
res.writeHeader(400, {"content-type":"text/plain"});
|
||||||
res.write("You sent a bad message!\n"+er.message);
|
res.write("You sent a bad message!\n"+er.message);
|
||||||
res.close();
|
res.close();
|
||||||
});
|
});
|
||||||
@ -1280,7 +1282,7 @@ http.createServer(function (req, res) {
|
|||||||
});
|
});
|
||||||
mp.addListener("complete", function () {
|
mp.addListener("complete", function () {
|
||||||
var response = "You posted: \n" + sys.inspect(fields);
|
var response = "You posted: \n" + sys.inspect(fields);
|
||||||
res.sendHeader(200, {
|
res.writeHeader(200, {
|
||||||
"content-type" : "text/plain",
|
"content-type" : "text/plain",
|
||||||
"content-length" : response.length
|
"content-length" : response.length
|
||||||
});
|
});
|
||||||
|
@ -48,7 +48,7 @@ var sys = require('sys'),
|
|||||||
http = require('http');
|
http = require('http');
|
||||||
http.createServer(function (req, res) {
|
http.createServer(function (req, res) {
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
res.sendHeader(200, {'Content-Type': 'text/plain'});
|
res.writeHeader(200, {'Content-Type': 'text/plain'});
|
||||||
res.write('Hello World');
|
res.write('Hello World');
|
||||||
res.close();
|
res.close();
|
||||||
}, 2000);
|
}, 2000);
|
||||||
|
25
lib/http.js
25
lib/http.js
@ -256,12 +256,31 @@ function ServerResponse (req) {
|
|||||||
sys.inherits(ServerResponse, OutgoingMessage);
|
sys.inherits(ServerResponse, OutgoingMessage);
|
||||||
exports.ServerResponse = ServerResponse;
|
exports.ServerResponse = ServerResponse;
|
||||||
|
|
||||||
ServerResponse.prototype.sendHeader = function (statusCode, headers) {
|
|
||||||
var reason = STATUS_CODES[statusCode] || "unknown";
|
ServerResponse.prototype.writeHeader = function (statusCode) {
|
||||||
var status_line = "HTTP/1.1 " + statusCode.toString() + " " + reason + CRLF;
|
var reasonPhrase, headers, headerIndex;
|
||||||
|
|
||||||
|
if (typeof arguments[1] == 'string') {
|
||||||
|
reasonPhrase = arguments[1];
|
||||||
|
headerIndex = 2;
|
||||||
|
} else {
|
||||||
|
reasonPhrase = STATUS_CODES[statusCode] || "unknown";
|
||||||
|
headerIndex = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof arguments[headerIndex] == 'object') {
|
||||||
|
headers = arguments[headerIndex];
|
||||||
|
} else {
|
||||||
|
headers = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
var status_line = "HTTP/1.1 " + statusCode.toString() + " "
|
||||||
|
+ reasonPhrase + CRLF;
|
||||||
this.sendHeaderLines(status_line, headers);
|
this.sendHeaderLines(status_line, headers);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// TODO eventually remove sendHeader()
|
||||||
|
ServerResponse.prototype.sendHeader = ServerResponse.prototype.writeHeader;
|
||||||
|
|
||||||
function ClientRequest (method, url, headers) {
|
function ClientRequest (method, url, headers) {
|
||||||
OutgoingMessage.call(this);
|
OutgoingMessage.call(this);
|
||||||
|
@ -12,4 +12,4 @@ assert.throws(function() {
|
|||||||
});
|
});
|
||||||
assert.throws(function() {
|
assert.throws(function() {
|
||||||
process._byteLength(5);
|
process._byteLength(5);
|
||||||
});
|
});
|
||||||
|
@ -9,7 +9,7 @@ var server_response = "";
|
|||||||
var client_got_eof = false;
|
var client_got_eof = false;
|
||||||
|
|
||||||
var server = http.createServer(function (req, res) {
|
var server = http.createServer(function (req, res) {
|
||||||
res.sendHeader(200, {"Content-Type": "text/plain"});
|
res.writeHeader(200, {"Content-Type": "text/plain"});
|
||||||
res.write(body);
|
res.write(body);
|
||||||
res.close();
|
res.close();
|
||||||
})
|
})
|
||||||
|
@ -5,7 +5,7 @@ PORT = 8888;
|
|||||||
var body = "exports.A = function() { return 'A';}";
|
var body = "exports.A = function() { return 'A';}";
|
||||||
var server = http.createServer(function (req, res) {
|
var server = http.createServer(function (req, res) {
|
||||||
puts("got request");
|
puts("got request");
|
||||||
res.sendHeader(200, [
|
res.writeHeader(200, [
|
||||||
["Content-Length", body.length],
|
["Content-Length", body.length],
|
||||||
["Content-Type", "text/plain"]
|
["Content-Type", "text/plain"]
|
||||||
]);
|
]);
|
||||||
|
@ -5,7 +5,7 @@ var PORT = 8888;
|
|||||||
var UTF8_STRING = "Il était tué";
|
var UTF8_STRING = "Il était tué";
|
||||||
|
|
||||||
var server = http.createServer(function(req, res) {
|
var server = http.createServer(function(req, res) {
|
||||||
res.sendHeader(200, {"Content-Type": "text/plain; charset=utf8"});
|
res.writeHeader(200, {"Content-Type": "text/plain; charset=utf8"});
|
||||||
res.write(UTF8_STRING, 'utf8');
|
res.write(UTF8_STRING, 'utf8');
|
||||||
res.close();
|
res.close();
|
||||||
});
|
});
|
||||||
|
@ -8,7 +8,7 @@ var body2_s = "22222";
|
|||||||
|
|
||||||
var server = http.createServer(function (req, res) {
|
var server = http.createServer(function (req, res) {
|
||||||
var body = url.parse(req.url).pathname === "/1" ? body1_s : body2_s;
|
var body = url.parse(req.url).pathname === "/1" ? body1_s : body2_s;
|
||||||
res.sendHeader(200, { "Content-Type": "text/plain"
|
res.writeHeader(200, { "Content-Type": "text/plain"
|
||||||
, "Content-Length": body.length
|
, "Content-Length": body.length
|
||||||
});
|
});
|
||||||
res.write(body);
|
res.write(body);
|
||||||
|
@ -18,7 +18,7 @@ var server = http.createServer(function(req, res) {
|
|||||||
req.addListener('end', function () {
|
req.addListener('end', function () {
|
||||||
server_req_complete = true;
|
server_req_complete = true;
|
||||||
puts("request complete from server");
|
puts("request complete from server");
|
||||||
res.sendHeader(200, {'Content-Type': 'text/plain'});
|
res.writeHeader(200, {'Content-Type': 'text/plain'});
|
||||||
res.write('hello\n');
|
res.write('hello\n');
|
||||||
res.close();
|
res.close();
|
||||||
});
|
});
|
||||||
|
@ -13,7 +13,7 @@ nrequests_expected = 1;
|
|||||||
var s = http.createServer(function (req, res) {
|
var s = http.createServer(function (req, res) {
|
||||||
puts("req: " + JSON.stringify(url.parse(req.url)));
|
puts("req: " + JSON.stringify(url.parse(req.url)));
|
||||||
|
|
||||||
res.sendHeader(200, {"Content-Type": "text/plain"});
|
res.writeHeader(200, {"Content-Type": "text/plain"});
|
||||||
res.write("Hello World");
|
res.write("Hello World");
|
||||||
res.close();
|
res.close();
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ var BACKEND_PORT = 8870;
|
|||||||
|
|
||||||
var backend = http.createServer(function (req, res) {
|
var backend = http.createServer(function (req, res) {
|
||||||
// debug("backend");
|
// debug("backend");
|
||||||
res.sendHeader(200, {"content-type": "text/plain"});
|
res.writeHeader(200, {"content-type": "text/plain"});
|
||||||
res.write("hello world\n");
|
res.write("hello world\n");
|
||||||
res.close();
|
res.close();
|
||||||
});
|
});
|
||||||
@ -19,7 +19,7 @@ var proxy = http.createServer(function (req, res) {
|
|||||||
debug("proxy req headers: " + JSON.stringify(req.headers));
|
debug("proxy req headers: " + JSON.stringify(req.headers));
|
||||||
var proxy_req = proxy_client.request(url.parse(req.url).pathname);
|
var proxy_req = proxy_client.request(url.parse(req.url).pathname);
|
||||||
proxy_req.addListener('response', function(proxy_res) {
|
proxy_req.addListener('response', function(proxy_res) {
|
||||||
res.sendHeader(proxy_res.statusCode, proxy_res.headers);
|
res.writeHeader(proxy_res.statusCode, proxy_res.headers);
|
||||||
proxy_res.addListener("data", function(chunk) {
|
proxy_res.addListener("data", function(chunk) {
|
||||||
res.write(chunk);
|
res.write(chunk);
|
||||||
});
|
});
|
||||||
|
@ -38,7 +38,7 @@ http.createServer(function (req, res) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
res.sendHeader(200, {"Content-Type": "text/plain"});
|
res.writeHeader(200, {"Content-Type": "text/plain"});
|
||||||
res.write(url.parse(req.url).pathname);
|
res.write(url.parse(req.url).pathname);
|
||||||
res.close();
|
res.close();
|
||||||
}, 1);
|
}, 1);
|
||||||
|
@ -52,7 +52,7 @@ var http_server=http.createServer(function (req, res) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
req.addListener('end', function () {
|
req.addListener('end', function () {
|
||||||
res.sendHeader(200, {"Content-Type": "text/plain"});
|
res.writeHeader(200, {"Content-Type": "text/plain"});
|
||||||
res.write("The path was " + url.parse(req.url).pathname);
|
res.write("The path was " + url.parse(req.url).pathname);
|
||||||
res.close();
|
res.close();
|
||||||
responses_sent += 1;
|
responses_sent += 1;
|
||||||
|
@ -24,7 +24,7 @@ var client_got_eof = false;
|
|||||||
var connection_was_closed = false;
|
var connection_was_closed = false;
|
||||||
|
|
||||||
var server = http.createServer(function (req, res) {
|
var server = http.createServer(function (req, res) {
|
||||||
res.sendHeader(200, {"Content-Type": "text/plain"});
|
res.writeHeader(200, {"Content-Type": "text/plain"});
|
||||||
res.write("hello ");
|
res.write("hello ");
|
||||||
res.write("world\n");
|
res.write("world\n");
|
||||||
res.close();
|
res.close();
|
||||||
|
@ -28,7 +28,7 @@ http.createServer(function (req, res) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
req.addListener('end', function () {
|
req.addListener('end', function () {
|
||||||
res.sendHeader(200, {"Content-Type": "text/plain"});
|
res.writeHeader(200, {"Content-Type": "text/plain"});
|
||||||
res.write("The path was " + url.parse(req.url).pathname);
|
res.write("The path was " + url.parse(req.url).pathname);
|
||||||
res.close();
|
res.close();
|
||||||
responses_sent += 1;
|
responses_sent += 1;
|
||||||
|
@ -6,7 +6,7 @@ PORT = 8891;
|
|||||||
|
|
||||||
body = "hello world\n";
|
body = "hello world\n";
|
||||||
server = http.createServer(function (req, res) {
|
server = http.createServer(function (req, res) {
|
||||||
res.sendHeader(200, {
|
res.writeHeader(200, {
|
||||||
"Content-Length": body.length,
|
"Content-Length": body.length,
|
||||||
"Content-Type": "text/plain",
|
"Content-Type": "text/plain",
|
||||||
});
|
});
|
||||||
|
@ -77,12 +77,12 @@ var server = http.createServer(function (req, res) {
|
|||||||
}
|
}
|
||||||
mp.addListener("error", function (er) {
|
mp.addListener("error", function (er) {
|
||||||
sys.puts("!! error occurred");
|
sys.puts("!! error occurred");
|
||||||
res.sendHeader(400, {});
|
res.writeHeader(400, {});
|
||||||
res.write("bad");
|
res.write("bad");
|
||||||
res.close();
|
res.close();
|
||||||
});
|
});
|
||||||
mp.addListener("complete", function () {
|
mp.addListener("complete", function () {
|
||||||
res.sendHeader(200, {});
|
res.writeHeader(200, {});
|
||||||
res.write("ok");
|
res.write("ok");
|
||||||
res.close();
|
res.close();
|
||||||
});
|
});
|
||||||
|
@ -11,7 +11,7 @@ var server = http.createServer(function(req, res) {
|
|||||||
'return '+JSON.stringify(url.parse(req.url).pathname)+';'+
|
'return '+JSON.stringify(url.parse(req.url).pathname)+';'+
|
||||||
'};';
|
'};';
|
||||||
|
|
||||||
res.sendHeader(200, {'Content-Type': 'text/javascript'});
|
res.writeHeader(200, {'Content-Type': 'text/javascript'});
|
||||||
res.write(body);
|
res.write(body);
|
||||||
res.close();
|
res.close();
|
||||||
});
|
});
|
||||||
|
@ -2,4 +2,4 @@ process.mixin(require('./common'));
|
|||||||
|
|
||||||
var fixture = path.join(__dirname, "fixtures/x.txt");
|
var fixture = path.join(__dirname, "fixtures/x.txt");
|
||||||
|
|
||||||
assert.equal("xyz\n", fs.readFileSync(fixture));
|
assert.equal("xyz\n", fs.readFileSync(fixture));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user