/
ColdWeb-0.2/
ColdWeb-0.2/root/
// ------------------------------------------------------------------
// Object

parent $root
object $daemon

var $daemon host 0
var $daemon buffer `[]
var $daemon lines []
var $daemon active 0

method initialize
    (| pass() |);
    host = "";
    buffer = `[];
    lines = [];
.

method host
    return host;
.

method set_host
    arg host;

    set_var('host, host);
.

method send
    arg what;
    var l;

    switch (type(what)) {
        case 'list:
            what = buffer_from_strings(what);
        case 'string:
            what = buffer_from_strings([what]);
    }
    echo(what);
.

method startup
    arg port;

    .log("** Starting httpd on port " + tostr(port) + " **");
    bind_port(port, this());
.

method connect
    arg host, socket;
    var d;

    d = create([$sys.starting_parent()]);
    d.initialize();
    reassign_connection(d);
    d.set_active();
    d.set_host(host);
.

method set_active
    active = 1;
.

method parse_line
    arg line;
.

method parse
    arg incoming;
    var l, line, i, t;

    catch any {
        l = buffer_to_strings(buffer_append(buffer, incoming));
        i = listlen(l);
        buffer = l[i];
        l = delete(l, i);
        lines = lines + l;
        while (lines) {
            l = lines[1];
            lines = delete(lines, 1);
            (> .process_line(l) <);
            if (!active)
                break;
        }
    } with handler {
        t = traceback();
        .set_status(500);
        .respond($parse.html_traceback(500, t));
        .close();
        .log($parse.traceback(t));
    }
    if (!active)
        .destroy();
.

method destroy
    .close();
    pass();
.

method close
    close_connection();
    active = 0;
.

method disconnect
    arg [args];

    .close();
    .destroy();
.