WebDispatcher.borg

WebDispatcher.borg


{
handlers[100]:["",0];
findhandler(line)::
    {
    i:1;    
    answer:0;
    while(i<101,
        {
    h:handlers[i];
    a:strstr(line,h[1]);
    if (a = 1, {answer := h[2]; i := 100}, false);
    i := i + 1
    });
    if (is_number(answer), agentname, answer)
    };
respondto(req,line,formdata)::
    {
    req->display("

Unknown URL:

"); req->display(line); req->display("

"); req->finish() }; createformdata(line):: { pos:strstr(line,"?"); if (pos > 0, { line := substr(line,pos + 1,size(line)); s:line; aantal : 1; while({pos := strstr(s,"&")}>0, { s:substr(s,pos + 1, size(s)); aantal := aantal + 1 }); formdata[aantal]:["",""]; s := line; aantal := 1; while(size(s)>0, { row:formdata[aantal]; aantal := aantal + 1; pos := strstr(s,"="); row[1] := substr(s,1,pos - 1); s := substr(s,pos + 1, size(s)); pos := strstr(s,"&"); if (pos < 1, pos := size(s) + 1, false); row[2] := substr(s,1, pos - 1); s := substr(s,pos + 1, size(s)) }); formdata },[]) }; handlehtmlrequest(req,line):: { if (strstr(line,"/get/") = 1, { req->display(""); req->finish() }, { h:findhandler(line); formdata:createformdata(line); h->respondto(req,line,formdata) }) }; forgethandler(handler):: { i:1; while(i<101, { assoc:handlers[i]; if (assoc[1] = handler, handlers[i] := ["",0], false); i := i + 1 }) }; announcehandler(handler,agent):: { i:1; while(i<101, { assoc:handlers[i]; if (assoc[1] = "", {handlers[i] := [handler,agent]; i := 100}, false); i := i + 1 }) }; display('WebDispatcher Loaded',eoln) }