Browse Source

http: Add IncomingMessage as abstract base class of ServerReq ClientRes

v0.7.4-release
Ryan 16 years ago
parent
commit
216e6204f0
  1. 102
      src/http.js

102
src/http.js

@ -1,6 +1,29 @@
(function () { (function () {
/**
* Inherit the prototype methods from one constructor into another.
*
* The Function.prototype.inherits from lang.js rewritten as a standalone
* function (not on Function.prototype). NOTE: If this file is to be loaded
* during bootstrapping this function needs to be revritten using some native
* functions as prototype setup using normal JavaScript does not work as
* expected during bootstrapping (see mirror.js in r114903).
*
* @param {function} ctor Constructor function which needs to inherit the
* prototype
* @param {function} superCtor Constructor function to inherit prototype from
*/
function inherits(ctor, superCtor) {
var tempCtor = function(){};
tempCtor.prototype = superCtor.prototype;
ctor.super_ = superCtor.prototype;
ctor.prototype = new tempCtor();
ctor.prototype.constructor = ctor;
}
CRLF = "\r\n"; CRLF = "\r\n";
node.http.STATUS_CODES = { node.http.STATUS_CODES = {
100 : 'Continue', 100 : 'Continue',
101 : 'Switching Protocols', 101 : 'Switching Protocols',
200 : 'OK', 200 : 'OK',
@ -136,46 +159,53 @@ node.http.createServer = function (requestListener, options) {
return server; return server;
}; };
node.http.createServerRequest = function (connection) {
var req = new node.EventEmitter;
req.connection = connection;
req.method = null;
req.uri = "";
req.httpVersion = null;
req.headers = [];
req.last_was_value = false; // used internally XXX remove me
req.setBodyEncoding = function (enc) { /* Abstract base class for ServerRequest and ClientResponse. */
connection.setEncoding(enc); var IncomingMessage = function (connection) {
}; node.EventEmitter.call(this);
return req; this.connection = connection;
this.httpVersion = null;
this.headers = [];
this.last_was_value = false; // TODO: remove me.
}; };
inherits(IncomingMessage, node.EventEmitter);
// ^ IncomingMessage.prototype.setBodyEncoding = function (enc) {
// | // TODO: Find a cleaner way of doing this.
// | combine these two functions this.connection.setEncoding(enc);
// | };
// v
createClientResponse = function (client) { IncomingMessage.prototype._emitBody = function (chunk) {
var res = new node.EventEmitter; this.emit("body", [chunk]);
};
res.client = client; IncomingMessage.prototype._emitComplete = function () {
res.connection = client; this.emit("complete");
};
res.statusCode = null;
res.httpVersion = null;
res.headers = [];
res.last_was_value = false; // used internally XXX remove me
res.setBodyEncoding = function (enc) { var ServerRequest = function (connection) {
client.setEncoding(enc); IncomingMessage.call(this, connection);
};
this.uri = "";
this.method = null;
};
inherits(ServerRequest, IncomingMessage);
return res; var ClientResponse = function (connection) {
IncomingMessage.call(this, connection);
this.statusCode = null;
this.client = this.connection;
}; };
inherits(ClientResponse, IncomingMessage);
function connectionListener (connection) { function connectionListener (connection) {
// An array of responses for each connection. In pipelined connections // An array of responses for each connection. In pipelined connections
@ -194,7 +224,7 @@ function connectionListener (connection) {
var req, res; var req, res;
connection.addListener("message_begin", function () { connection.addListener("message_begin", function () {
req = new node.http.createServerRequest(connection); req = new ServerRequest(connection);
res = new node.http.ServerResponse(connection); res = new node.http.ServerResponse(connection);
}); });
@ -230,11 +260,11 @@ function connectionListener (connection) {
}); });
connection.addListener("body", function (chunk) { connection.addListener("body", function (chunk) {
req.emit("body", [chunk]); req._emitBody(chunk);
}); });
connection.addListener("message_complete", function () { connection.addListener("message_complete", function () {
req.emit("complete"); req._emitComplete()
}); });
} }
@ -386,7 +416,7 @@ node.http.createClient = function (port, host) {
client.addListener("message_begin", function () { client.addListener("message_begin", function () {
req = client.requests.shift(); req = client.requests.shift();
res = createClientResponse(client); res = new ClientResponse(client);
}); });
client.addListener("header_field", function (data) { client.addListener("header_field", function (data) {
@ -415,12 +445,12 @@ node.http.createClient = function (port, host) {
}); });
client.addListener("body", function (chunk) { client.addListener("body", function (chunk) {
res.emit("body", [chunk]); res._emitBody(chunk);
}); });
client.addListener("message_complete", function () { client.addListener("message_complete", function () {
client.close(); client.close();
res.emit("complete"); res._emitComplete();
}); });
return client; return client;

Loading…
Cancel
Save