Skip to content

Instantly share code, notes, and snippets.

@isaacs
Created November 9, 2012 18:39
Show Gist options
  • 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);
@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