| Blockland Forums > Modification Help |
| Torque Webserver V2 |
| << < (3/5) > >> |
| Truce:
Whipped up a little page for testing stuff, check it out: http://trewse.us.to/ Refresh it a few times to see the different box layouts (completely randomized with an equally as random RGB color scheme). Clicking a box loads up a song from Youtube who's ID randomly picked from a file. Since I was lazy, I picked 16 song URLs out of recommended videos box so the genres might not be your thing, but give it a listen anywho. The songs will auto loop. That site will usually be up and down since I host it from my laptop for testing. |
| Bauklotz:
loving. --- Code: ---function createWebServer(%name,%htdocs) { if(isObject(nametoid("web_" @ %name))) return; %obj = new TCPObject() { class = webServerAPI_server; name = %name; htdocs = %htdocs; isWEBAPI = 1; }; %obj.setName("web_" @ %name); return %obj; } function TCPObject::serve(%this,%port) { %this.listen(%port); } function TCPObject::enableModule(%this,%mod) { %dir = filePath(expandFilename("./main.cs")) @ "/modules/" @ %mod @ "/"; echo(%dir); if(!isFile(%dir @ "main.cs")) return 0; exec(%dir @ "main.cs"); if(strLen(%this.mods) < 1) %this.mods = 0; %this.mod[%this.mods] = %mod; %this.mods++; } package webServers { function TCPObject::onConnectRequest(%this,%ip,%socket) { if(!%this.isWEBAPI) { Parent::onConnectRequest(%this,%ip,%socket); return; } if(isObject(%this.connection[%ip])) { if(%this.debug) echo("["@%this.name@"] duplicated_connect:" SPC %ip); %this.connection[%ip].disconnect(); %this.connection[%ip].delete(); } if(%this.debug) echo("["@%this.name@"] connect::" SPC %ip); %this.connection[%ip] = new TCPObject(webServerAPI_client,%socket) { ip = %ip; parent = %this; }; } }; activatePackage(webServers); function webServerAPI_client::handleRequestOptions(%this,%data) { deleteVariables("$_SERVER*"); deleteVariables("$_ISSET*"); deleteVariables("$_POST*"); deleteVariables("$_GET*"); $_SERVER["REMOTE_IP"] = %this.ip; $_SERVER["SERVING"] = %this; for(%i=0;%i<getLineCount(%data);%i++) { %line = getLine(%data,%i); if(firstWord(%line) $= "GET" || firstWord(%line) $= "POST") { //if(%this.parent.debug) // echo("["@%this.parent.name@"]" SPC "Handling request: "@%line); %type = getWord(%line,0); %path = getWord(%line,1); %http = getWord(%line,2); $_SERVER["RQST_TYPE"] = %type; $_SERVER["FILE_PATH"] = %path; $_SERVER["HTTP_VERSION"] = %http; } else { //if(%this.parent.debug) // echo("["@%this.parent.name@"]" SPC "Handling header: "@%line); %opt_t = getSubStr(%line,0,strPos(%line,": ")); %opt_v = getSubStr(%line,strPos(%line,": ")+2,strLen(%line)); $_SERVER[%opt_t] = %opt_v; } } if((%qPos = strPos(%path,"?")) >= 0) { %post_data = strReplace(getSubStr(%path,%qPos+1,strLen(%path)),"&","\t"); //%post_data = strReplace(getSubStr(%path,strPos(%path,"?")+1,strLen(%path)),"&","\t"); %path = getSubStr(%path,0,%qPos); for(%i=0;%i<getFieldCount(%post_data);%i++) { %ld = getField(%post_data,%i); %ps = strPos(%ld,"="); if(%ps < 0) $_ISSET[%ld] = true; else { %l_vr = getSubStr(%ld,0,%ps); %l_vl = getSubStr(%ld,%ps+1,strLen(%ld)); $_ISSET[%l_vr] = true; $_GET[%l_vr] = %l_vl; $_POST[%l_vr] = %l_vl; } } } %this.handleRequest(%path); } function webServerAPI_client::handleRequest(%this,%file) { %file = strReplace(%file,"..",""); if(getSubStr(%file,0,1) $= "/") %file = %this.parent.htdocs @ %file; else %file = %this.parent.htdocs @ "/" @ %file; if(%this.parent.debug) echo("["@%this.parent.name@"]" SPC "Sending output: "@%file); if(!isFile(%file)) { %oldFile = %file; if(isFile(%file @ "/index.html")) %file = %file @ "/index.html"; if(isFile(%file @ "/index.htm")) %file = %file @ "/index.htm"; if(isFile(%file @ "/index.tqs")) %file = %file @ "/index.tqs"; if(isFile(%file @ "index.html")) %file = %file @ "index.html"; if(isFile(%file @ "index.htm")) %file = %file @ "index.htm"; if(isFile(%file @ "index.tqs")) %file = %file @ "index.tqs"; if(%file !$= %oldFile && %this.parent.debug) echo("["@%this.parent.name@"]" SPC "File changed to: "@%file); } if(!isFile(%file)) { %exceptionCaught = true; if(!isFile(%this.parent.htdocs @ %this.parent.page[404])) %errorOccured = true; else %file = %this.parent.htdocs @ %this.parent.page[404]; %code = 404; %msg = "File Not Found"; if(%this.parent.debug) echo("["@%this.parent.name@"]" SPC "404, solution: "@(%errorOccured ? "error" : "404 page")); } if(!%exceptionCaught && isFile(filePath(%file) @ "/.htaccess") && strLen($_SERVER["Authenticate"]) < 1) { %exceptionCaught = true; %errorOccured = true; %fo = new FileObject(); %fo.openForRead(filePath(%file) @ "/.htaccess"); %realm = %fo.readLine(); %fo.close(); %fo.delete(); %code = 401; %msg = "Authorization Required"; %headAdd = "WWW-Authenticate: Basic realm=\""@%realm@"\""; } if(!%exceptionCaught && isFile(filePath(%file) @ "/.htaccess") && strLen($_SERVER["Authenticate"]) > 0) { %authType = firstWord($_SERVER["Authenticate"]); %authData = restWords($_SERVER["Authenticate"]); if(%authType !$= "Basic") { %exceptionCaught = true; if(!isFile(%this.parent.htdocs @ %this.parent.page[403])) %errorOccured = true; else %file = %this.parent.htdocs @ %this.parent.page[403]; %code = 403; %msg = "Permission Denied"; } else { %isValid = false; %fo = new FileObject(); %fo.openForRead(filePath(%file) @ "/.htaccess"); while(!%fo.isEOF()) if(%fo.readLine() $= %authData) %isValid = true; %fo.close(); %fo.delete(); if(!%isValid) { %exceptionCaught = true; if(!isFile(%this.parent.htdocs @ %this.parent.page[403])) %errorOccured = true; else %file = %this.parent.htdocs @ %this.parent.page[403]; %code = 403; %msg = "Permission Denied"; } } } if(!%errorOccured) { %fo = new FileObject(); %fo.openForRead(%file); while(!%fo.isEOF()) %filedat = %filedat @ %fo.readLine() NL ""; %filedat = trim(%filedat); %fo.close(); %fo.delete(); if(!%exceptionCaught) { %code = 200; %msg = "OK"; } } $_SERVER["FILEDATA"] = %filedat; for(%i=0;%i<%this.parent.mods;%i++) { %m = %this.parent.mod[%i] @ "_changeData"; if(isFunction(%m)) call(%m,%filedat); } if(strLen($_SERVER["FILEDATA"]) < 1) $_SERVER["FILEDATA"] = "No Content"; %header = %header @ "HTTP/1.1" SPC %code SPC %msg @ "\n\r"; %header = %header @ "Server: " @ %this.parent.name @ "\n\r"; %header = %header @ "Date: " @ getDateTime() @ "\n\r"; %header = %header @ (strLen(%headAdd) < 1 ? "" : %headAdd @ "\n\r"); %header = %header @ "Content-Type: text/html" @ "\n\r"; %header = %header @ "Content-Length: " @ strLen($_SERVER["FILEDATA"]) @ "\n\r"; %data = %header @ "\n\r" @ $_SERVER["FILEDATA"]; if(%this.parent.debug) { echo("["@%this.parent.name@"]" SPC "SERVER PROCESSING FINISHED, TRANSMITTING:"); echo(%data); } %this.send(%data); } function fileDataCLEAR() { $_SERVER["FILEDATA"] = ""; } function fileDataAPPEND(%str) { $_SERVER["FILEDATA"] = $_SERVER["FILEDATA"] @ %str NL ""; } function webServerAPI_client::onLine(%this,%line) { if(%this.parent.debug) echo("["@%this.parent.name@"]" SPC %this.ip SPC "sent: "@%line); if(strLen(%line) < 1) %this.handleRequestOptions(rtrim(%this.lineData)); else %this.lineData = %this.lineData @ %line NL ""; return; deleteVariables("$_SERVER*"); $_SERVER[REMOTE_IP] = %this.ip; %cmd = firstWord(%line); %arg = restWords(%line); if(%this.parent.debug) echo("["@%this.parent.name@"] recieve: "@%line); switch$ (%cmd) { case GET: %file_path = %this.parent.htdocs @ strReplace(firstWord(%arg),"..",""); %http_version = getWord(%arg,1); if(isFile(%file_path @ "index.tqs") && !isFile(%file_path)) %file_path = %file_path @ "index.tqs"; echo("["@%this.parent.name@"] "@%this.ip@" requested: "@%file_path); if(!isFile(%file_path)) { %this.send("404 File Not Found"); %this.disconnect(); %this.schedule(10,delete); return; } %this.transmitFile(%file_path); case POST: deleteVariables("$_GET*"); deleteVariables("$_POST*"); deleteVariables("$_ARG*"); %data = firstWord(%arg); %q_pos = strPos(%data,"?"); if(%q_pos >= 0) { %args = strReplace(getSubStr(%data,%q_pos+1,strLen(%data)),"&","\t"); for(%i=0;%i<getFieldCount(%args);%i++) { %tArg = getField(%args,%i); %l_pos = strPos(%tArg,"="); if(%l_pos < 0) { $_ARG[%tArg] = true; $_GET[%tArg] = true; $_POST[%tArg] = true; } else { %tArgN = getSubStr(%tArg,0,%l_pos); %tArgD = getSubStr(%tArg,%l_pos+1,strLen(%tArg)); $_ARG[%tArgN] = true; $_GET[%tArgN] = %tArgD; $_POST[%tArgN] = %tArgD; } } } %file_path = %this.parent.htdocs @ strReplace(getSubStr(%data,0,(%q_pos < 0 ? strLen(%data) : %q_pos)),"..",""); %http_version = getWord(%arg,1); if(isFile(%file_path @ "index.tqs") && !isFile(%file_path)) %file_path = %file_path @ "index.tqs"; echo("["@%this.parent.name@"] "@%this.ip@" requested: "@%file_path); if(!isFile(%file_path)) { %this.send("404 File Not Found"); %this.disconnect(); %this.schedule(10,delete); return; } %this.transmitFile(%file_path); } } function webServerAPI_client::transmitFile(%this,%f) { %read = new FileObject(); %read.openForRead(%f); while(!%read.isEOF()) { %line = %read.readLine(); if(!%inTQS) { if(%line $= "<?tqs") %inTQS = true; else %this.send(%line @ "\n\r"); } else { if(%line $= "?>") { %inTQS = false; %correct = false; eval(rtrim(%tqsCODE) SPC "%correct = true;"); if(!%correct) %this.send("A syntax error was accountered in the server-sided script files.\n\r"); %tqsCODE = ""; } else { %dat = strReplace(%line,"transmit",%this @ ".send"); %tqsCODE = %tqsCODE @ %dat NL ""; } } } %read.close(); %read.delete(); %this.disconnect(); %this.schedule(10,delete); } --- End code --- And yes, know that syntax is crappy. Did this long time ago. |
| Truce:
--- Quote from: Bauklotz on January 27, 2011, 05:12:07 AM ---loving. And yes, know that syntax is crappy. Did this long time ago. --- End quote --- How long ago exactly? |
| mctwist:
--- Quote from: Truce on January 27, 2011, 10:33:32 AM ---How long ago exactly? --- End quote --- About a month. Unless he have kept it for himself for longer. |
| Kalphiter:
Bump And 78.69.143.58 is probably DontCare4Free |
| Navigation |
| Message Index |
| Next page |
| Previous page |