"use strict";

var fs = require('fs')
  , http = require('http')
  , log = require('./log')('server')
  , ProxyHandler = require('./proxy')
  , StaticHandler = require('./static')
  , url = require('url')
  , Watcher = require('./watcher')
  , WebSocketHandler = require('./websocket');


// Static file '/livereload.js' handler
function LiveReloadJsHandler() {
}

LiveReloadJsHandler.prototype = {
  handle: function(request, response) {
    if (url.parse(request.url).pathname === "/livereload.js") {
      log.debug("livereload requested");
      response.writeHead(200, { 'Content-Type': 'text/javascript' });
      response.write(fs.readFileSync(__dirname + '/../contrib/livereload.js'));
      response.end();
      return true;
    }
    return false;
  }
};

exports.LiveReloadJsHandler = LiveReloadJsHandler;


function Server(config, watcher) {
  this.config = require('./config').setDefaultValue(config);

  this.watcher = watcher || new Watcher();
}

Server.prototype = {
  init: function() {
    var self = this;

    var handlers = [
      new LiveReloadJsHandler(),
      new ProxyHandler(this.config),
      new StaticHandler(this.config)
    ];

    var server = http.createServer(function(request, response) {
      for (var i = 0; i < handlers.length; i++) {
        if (handlers[i].handle(request, response)) {
          return;
        }
      }
      response.writeHead(404);
      response.write("File not found");
      response.end();
      log.debug("404 %s", request.url);
    }).listen(this.config.port);

    this.webSocket = new WebSocketHandler(this.config, { server: server });

    log.info("Waiting on port %d...", this.config.port);
  },

  notifyFileChange: function(file) {
    var msg = JSON.stringify({
      command: 'reload',
      path: file,
      liveCSS: this.config.liveCSS,
      liveImg: this.config.liveImg
    });

    this.webSocket.send(file, msg);
  },

  watch: function() {
    this.watcher.watch(this.config.dir);

    var self = this;
    this.watcher.on('change', function(file) {
      self.notifyFileChange(file);
    });
  }
};

exports.Server = Server;


exports.startServer = function(config) {
  log.debug("startServer", config);
  var server = new Server(config || {});
  server.init();
  return server;
}

@@ -133,16 +133,43 @@
 proxy')%0A
+  , send = require('send')%0A
   , Stat
@@ -441,38 +441,29 @@
 tion(req
-uest
 , res
-ponse
 ) %7B%0A    
 if (url.
@@ -454,20 +454,27 @@
 ) %7B%0A    
-if (
+var path = 
 url.pars
@@ -478,20 +478,16 @@
 arse(req
-uest
 .url).pa
@@ -496,14 +496,32 @@
 name
- === %22
+%0A%0A    // path should be 
 /liv
@@ -534,228 +534,178 @@
 d.js
-%22) %7B
 %0A    
-  log.debug(%22livereload requested%22);%0A      response.writeHead(200, %7B 'Content-Type': 'text/javascript' %7D);%0A      response.write(fs.readFileSync(__dirname + '/../contrib/livereload.js'));%0A      response.end(
+if (path !== %22/livereload.js%22) %7B%0A      return false;%0A    %7D%0A%0A    // send /livereload.js%0A    send(req, path)%0A      .root(__dirname + '/../contrib/')%0A      .pipe(res
 );%0A
-  
     
@@ -721,32 +721,8 @@
 ue;%0A
-    %7D%0A    return false;%0A
   %7D%0A

Rewrite LiveReloadJsHandler class to use send module