Skip to content

Instantly share code, notes, and snippets.

@isaacs isaacs/http.js
Created Nov 9, 2012

Embed
What would you like to do?
Sketch for new http.Server impl
// http.js
var net = require('net');
var util = require('util');
exports.Server = Server;
function Server(handler, options) {
if (!(this instanceof Server))
return new Server(handler, options);
if (typeof handler === 'object') {
options = handler;
handler = null;
}
if (typeof handler === 'function')
this.on('request', handler);
// only option that matters is { allowHalfOpen: true }
// no one actually uses that for http, but more could be
// added in the future.
net.Server.call(this, options);
this.on('connection', function(socket) {
// http.Parser is a writable stream that uses the HTTPParser
// binding, and emits 'request' objects that are readable streams
// This also gives us another object that we can pass around which
// has a reference to the HTTPParser binding objects, so that we can
// avoid leaks by never having an HTTPParser link to anything else.
var parser = new Parser('request');
parser.on('request', function(request) {
// http.Response is mostly a PassThrough transform stream,
// but manages response headers/trailers as well, and drops
// all the written data for requests that don't get a response body
var response = new http.Response();
response.pipe(socket);
request.server = response.server = this;
request.response = response;
response.request = request;
request.connection = response.connection = socket;
request.socket = response.socket = socket;
this.emit('request', request, response);
}.bind(this));
// incoming data is fed into the parser
socket.pipe(parser);
// when the socket disconnects, free the parser binding
socket.on('close', function() {
parser.close();
});
}).listen(80);
}
util.inherits(Server, net.Server);
@isaacs

This comment has been minimized.

Copy link
Owner Author

commented Nov 9, 2012

All the capitalized Class names will be exported. Haven't figured out how to manage errors, or even begun looking at the client yet.

@brianc

This comment has been minimized.

Copy link

commented Nov 9, 2012

any thought to splitting the various classes into their own files? I'm not pedantic about 1 class per file, but sometimes it helps when trying to initially grok a codebase on where to begin looking. Then again http is a pretty straightforward file name

@isaacs

This comment has been minimized.

Copy link
Owner Author

commented Nov 9, 2012

If they are split out, they'll have "internal-ish" names, like _http_parser.js and such, so that they don't collide with any existing npm packages. But they'll probably just stay in http.js for now.

@stephenhandley

This comment has been minimized.

Copy link

commented Nov 9, 2012

i'm confused by the duplication here

request.connection = response.connection = socket;
request.socket = response.socket = socket;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.