<commit_before>

/*-----------------------------------------------
  Requirements:
-----------------------------------------------*/

// System
var sys    = require("sys"),
    http   = require("http"),
    events = require("events"),
    path   = require("path");

// Local Path
require.paths.unshift(__dirname);
var Connection = require("ws/connection");

/*-----------------------------------------------
  Mixin:
-----------------------------------------------*/
var mixin = function(target, source) {
  var keys = Object.keys(source);
  var length = keys.length;
  for (var i = 0; i < length; i++) {
    var key = keys[i];
    target[key] = source[key];
  }
  return target;
};

/*-----------------------------------------------
  WebSocket Server Exports:
-----------------------------------------------*/
exports.Server = Server;
exports.createServer = function(options){
  return new Server(options || {});
}

/*-----------------------------------------------
  WebSocket Server Implementation:
-----------------------------------------------*/
function Server(options){
  events.EventEmitter.call(this);
  var ws = this;
  
  this.server = new http.Server();
    
  this.options = mixin({
    version: "draft75",//"draft75",    // string, [spec, draft75]
    origin: "*",        // string | array, any valid domain
    subprotocol: null   // string | array
  }, options || {});
  
  this.count = 0;
  
  this.server.addListener("listening", function(){
    ws.emit("listening");
  });
  
  this.server.addListener("upgrade", function(){
    handleUpgradeRequest.apply(ws, arguments);
  });
};

sys.inherits(Server, events.EventEmitter);

Server.prototype.broadcast = function(data){
  throw new Error("WebSocketServer.broadcast is not yet implemented.");
};

Server.prototype.listen = function(){
  this.server.listen.apply(this.server, arguments);
};

Server.prototype.destroy = function(){
  throw new Error("WebSocketServer.destroy is not yet implemented.");
};

Server.prototype.end = function(){
  throw new Error("WebSocketServer.end is not yet implemented.");
};

/*-----------------------------------------------
  Specific receivers of Events
-----------------------------------------------*/

function handleUpgradeRequest(req, socket, upgradeHead){
  if(req.method == "GET" && "upgrade" in req.headers && "connection" in req.headers &&
     req.headers.upgrade.toLowerCase() == "websocket" && req.headers.connection.toLowerCase() == "upgrade"
  ){
    new Connection(this, req, socket, upgradeHead);
  } else {
    socket.end();
  }
};<commit_msg>Patching a few things I left out when moving away from inheriting from http.Server<commit_after>

/*-----------------------------------------------
  Requirements:
-----------------------------------------------*/

// System
var sys    = require("sys"),
    http   = require("http"),
    events = require("events"),
    path   = require("path");

// Local Path
require.paths.unshift(__dirname);
var Connection = require("ws/connection");

/*-----------------------------------------------
  Mixin:
-----------------------------------------------*/
var mixin = function(target, source) {
  var keys = Object.keys(source);
  var length = keys.length;
  for (var i = 0; i < length; i++) {
    var key = keys[i];
    target[key] = source[key];
  }
  return target;
};

/*-----------------------------------------------
  WebSocket Server Exports:
-----------------------------------------------*/
exports.Server = Server;
exports.createServer = function(options){
  return new Server(options || {});
}

/*-----------------------------------------------
  WebSocket Server Implementation:
-----------------------------------------------*/
function Server(options){
  events.EventEmitter.call(this);
  var ws = this;
  
  this.server = new http.Server();
    
  this.options = mixin({
    version: "draft75",//"draft75",    // string, [spec, draft75]
    origin: "*",        // string | array, any valid domain
    subprotocol: null   // string | array
  }, options || {});
  
  this.count = 0;
  
  this.server.addListener("listening", function(){
    ws.emit("listening");
  });
  
  this.server.addListener("request", function(req, res){
    ws.emit("request", req, res);
  });
  
  this.server.addListener("stream", function(stream){
    ws.emit("stream", stream);
  });
  
  this.server.addListener("close", function(errno){
    ws.emit("close", errno);
  });
  
  this.server.addListener("clientError", function(e){
    ws.emit("clientError", e);
  });
  
  this.server.addListener("upgrade", function(){
    handleUpgradeRequest.apply(ws, arguments);
  });
};

sys.inherits(Server, events.EventEmitter);

Server.prototype.broadcast = function(data){
  throw new Error("WebSocketServer.broadcast is not yet implemented.");
};

Server.prototype.listen = function(){
  this.server.listen.apply(this.server, arguments);
};

Server.prototype.end = function(){
  this.server.end();
};

/*-----------------------------------------------
  Specific receivers of Events
-----------------------------------------------*/

function handleUpgradeRequest(req, socket, upgradeHead){
  if(req.method == "GET" && "upgrade" in req.headers && "connection" in req.headers &&
     req.headers.upgrade.toLowerCase() == "websocket" && req.headers.connection.toLowerCase() == "upgrade"
  ){
    new Connection(this, req, socket, upgradeHead);
  } else {
    socket.end();
  }
};
{'fraction_non_alphanumeric': 0.3108980269223677, 'fraction_numerical': 0.0029503964595242486, 'mean_word_length': 1.8383045525902668, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1288572', 'n_tokens_mistral': 1451, 'n_tokens_neox': 1427, 'n_words': 320}