var fs = require('fs');
////////////////////////////////////////////////////////////////////////////
// CONTENT

function Content(path) {
    this._path = path;
    this._body = '';

    // TODO:
    this._status = 200;
    this._header = {'Content-Type': 'text/plain'};

    return this;
}

Content.prototype.readAll = function(onsuccess, onerror) {
    var self = this;
    self._onsuccess = onsuccess;
    self._onerror = onerror;
    var reader = fs.createReadStream(self._path);
    reader.on('error', function(e) { onerror(); });
    reader.on('end', function() { self._onend(); });
    reader.on('data', function(data) { self._ondata(data); });
}

Content.prototype._ondata = function(data) {
    // TODO: redirect to the response object for speed.
    this._body += data;
}

Content.prototype._onend = function() {
    // TODO: split and expand template.
    this._onsuccess(this._status, this._header, this._body);
}

////////////////////////////////////////////////////////////////////////////
// CONTEXT

function Context() {
    this.htdocsDir = './htdocs';
    return this;
}

Context.prototype.handle = function(req, res) {
    var path = this._dispatch(req, res);
    if (path) {
        this._render(path, req, res);
    } else {
        res.writeHead(404, {'Context-Type': 'text/html'});
        res.end('<h1>404 Not found</h1>');
    }
}

Context.prototype._dispatch = function(req, res) {
    // TODO: choice correct object by path.
    return './htdocs/_folder';
}

Context.prototype._render = function(path, req, res) {
    var content = new Content(path);
    content.readAll(function(status, header, body) {
        res.writeHead(status, header, body);
        res.write(body);
        res.end();
    }, function() {
        res.writeHead(500, {'Content-Type': 'text/html'});
        res.end('<h1>500 Internal server error</h1>');
    });
}

////////////////////////////////////////////////////////////////////////////
// MODULE EXPORTS

module.exports = Context;
// vim:set ts=8 sts=4 sw=4 tw=0 et:

@@ -174,110 +174,89 @@
 is._
-body = '';%0D%0A%0D%0A    // TODO:%0D%0A    this._status = 200;%0D%0A    this._header = %7B'Content-Type': 'text/plain'%7D
+onsuccessCallback = null;%0D%0A    this._onerrorCallback = null;%0D%0A    this._body = ''
 ;%0D%0A%0D
@@ -380,16 +380,24 @@
 nsuccess
+Callback
  = onsuc
@@ -424,19 +424,16 @@
 rror
- = onerror;
+Callback
 %0D%0A  
@@ -522,16 +522,22 @@
 ) %7B 
+self._
 onerror(
 ); %7D
@@ -532,16 +532,17 @@
 onerror(
+e
 ); %7D);%0D%0A
@@ -717,20 +717,21 @@
 %0A    // 
-TODO
+FIXME
 : redire
@@ -894,58 +894,356 @@
 is._
-onsuccess(this._status, this._header, this._body);
+status = 200;%0D%0A    this._header = %7B'Content-Type': 'text/plain'%7D;%0D%0A    if (this._onsuccessCallback) %7B%0D%0A        this._onsuccessCallback(this._status, this._header, this._body);%0D%0A    %7D%0D%0A%7D%0D%0A%0D%0AContent.prototype._onerror = function(exception) %7B%0D%0A    if (this._onerrorCallback) %7B%0D%0A        // FIXME: log exception.%0D%0A        this._onerrorCallback();%0D%0A    %7D
 %0D%0A%7D%0D

fix handlers