Skip to content

Instantly share code, notes, and snippets.

@isaacs
Created November 9, 2012 18:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacs/4047419 to your computer and use it in GitHub Desktop.
Save isaacs/4047419 to your computer and use it in GitHub Desktop.
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
Copy link
Author

isaacs 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
Copy link

brianc 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
Copy link
Author

isaacs 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
Copy link

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