/* ----------------------------------------------------------------------------- This source file is part of OpenSpace3D For the latest info, see http://www.openspace3d.com Copyright (c) 2012 I-maginer This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA, or go to http://www.gnu.org/copyleft/lesser.txt ----------------------------------------------------------------------------- */ struct PlugWebControl = [ WCTRL_instance : PInstance, WCTRL_port : I, WCTRL_www : S, WCTRL_server : HTTPserver, WCTRL_lVar : [[S [S]] r1] ]mkPlugWebControl;; var sBaseHeader = "HTTP/1.0 200 OK\13\10Server: SCOL HTTP server\13\10";; var lMimesTypes = [nil "Content-Type: application/x-unknown"]:: ["html" "Content-Type: text/html"]:: ["jpg" "Content-Type: image/jpeg"]:: ["jpeg" "Content-Type: image/jpeg"]:: ["gif" "Content-Type: image/gif"]:: ["png" "Content-Type: image/png"]:: ["css" "Content-Type: text/css"]:: ["js" "Content-Type: text/javascript"]:: ["xml" "Content-Type: text/xml"]:: ["txt" "Content-Type: text/plain"]::nil;; fun deleteOb(inst, constr)= closeHTTPserver constr.WCTRL_server; 0;; fun makeHeader(file)= let getFileExt file -> ext in let switchstri lMimesTypes ext -> mime in strcatn sBaseHeader::mime::"\13\10\13\10"::nil ;; fun getUrlParams(url)= let strfind "?" url 0 -> pos in let if (pos != nil) then (substr url 0 pos) else url -> baseurl in let strToListSep (substr url pos + 1 1024) "&" -> params in let nil -> cparams in ( while (params != nil) do ( let hd params -> param in let strfind "=" param 0 -> ppos in let if (ppos != nil) then (substr param 0 ppos) else param -> p in let if (ppos != nil) then (substr param ppos + 1 1024) else nil -> val in ( //addLogMessage strcatn "param : "::p::" value : "::val::nil; set cparams = lcat cparams [p val]::nil; ); set params = tl params; ); [(strtrim baseurl) cparams]; );; fun cbRequest(conx, constr, req)= let hd strextr req -> l in let l->[com [url _]] in let getUrlParams url -> [baseurl params] in ( //addLogMessage strcat " com==" com; //addLogMessage strcat " url==" baseurl; if (!strcmpi com "GET") then ( let if (!strcmp baseurl "/") then "/index.html" else baseurl -> baseurl in let _getpack _checkpack (strcat constr.WCTRL_www baseurl) -> html in ( while (params != nil) do ( let hd params -> [p val] in SendPluginEvent constr.WCTRL_instance p webtostr val nil; set params = tl params; ); let 0 -> i in let sizelist constr.WCTRL_lVar -> size in while (i < size) do ( let nth_list constr.WCTRL_lVar i -> [p [val]] in set html = strreplace html (strcat "$" p) val; set i = i + 1; ); if (html == nil) then strcatn (makeHeader "/404.html")::"File not found : 404

404 file not found: "::(strcat constr.WCTRL_www baseurl)::".

"::nil else strcat (makeHeader baseurl) html; ); ) else if (!strcmpi com "POST") then { ""; } else ""; );; fun cbSaveVar(inst, from, action, param, reply, constr)= let substr action ((strlen (getPluginInstanceName inst)) + 1) (strlen action) -> action in ( let switchstri constr.WCTRL_lVar action -> vart in mutate vart <- [param]; ); 0;; fun cbSortVars(m1, m2)= let m1 -> [act1 _] in let m2 -> [act2 _] in (strcmp act1 act2) <= 0;; fun newOb(inst)= let atoi (getPluginInstanceParam inst "port") -> port in let getPluginInstanceParam inst "www" -> www in let mkPlugWebControl [inst port www nil nil] -> constr in ( let getPluginInstanceUserActions inst -> laction in let sizelist laction -> size in let 0 -> i in while i < size do ( let nth_list laction i -> act in ( set constr.WCTRL_lVar = [act [nil]]::constr.WCTRL_lVar; PluginRegisterAction inst act mkfun6 @cbSaveVar constr; ); set i = i + 1; ); set constr.WCTRL_lVar = sortlist constr.WCTRL_lVar @cbSortVars; set constr.WCTRL_lVar = revertlist constr.WCTRL_lVar; set constr.WCTRL_server = startHTTPserver _channel port @cbRequest constr; setPluginInstanceCbDel inst mkfun2 @deleteOb constr; ); 0;; fun IniPlug(file)= PlugRegister @newOb nil; setPluginEditor @dynamicedit; 0;;