home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 28 / amigaformatcd28.iso / -seriously_amiga- / comms / other / rxsocket / examples / hserv / main / hserv.rexx < prev    next >
OS/2 REXX Batch file  |  1998-05-09  |  8KB  |  302 lines

  1. /* */
  2.  
  3. parse arg configFile .
  4.  
  5. /***************************************************************************/
  6. /* socket is number 0 */
  7. sock=0
  8.  
  9. /***************************************************************************/
  10.  
  11. call readConfig(configFile)
  12. call Pragma("D",global.RootDir)
  13.  
  14. /***************************************************************************/
  15. /* who is it? */
  16.  
  17. if ~GetPeerName(0,"NAME")<0 then do
  18.     call ErrLog("hserv Can't get peer info (" || errno() || ")")
  19.     exit
  20. end
  21.  
  22. peer = name.addrAddr
  23. if global.HostnameLookups=="ON" then
  24.     if GetHostByAddr("HOST",name.addrAddr) then peer = host.hostName
  25.  
  26. /***************************************************************************/
  27. /* auth */
  28.  
  29. if global.Auth=="ON" then do
  30.     auth=AuthFun(name.addrAddr,80,name.addrPort)
  31.     if left(auth,4)=="-ERR" then do
  32.         parse var auth"-ERR "code
  33.         call sen "HTTP/1.0 400 Forbidden"
  34.         call sen "Content-Type: text/html"
  35.         call sen ""
  36.         call sen "<STRONG><H1>ERROR</H1></STRONG><H4><P><P>ARexxRXSWebServer error:<BR>sorry" peer ", you don't have the ident service running,<BR>or an error stopped your lookup.</BR></HTML>"
  37.         call ErrLog("hserv Can't get auth info for <" || peer || ">")
  38.         exit
  39.     end
  40.     else do
  41.         parse var auth"+OK" rp "," lp ": USERID : " sis " : " user "D"x"A"x .
  42.         userat = "<" || user || "@" || peer || ">"
  43.     end
  44. end
  45. else do
  46.     user = "unknown"
  47.     userat = "<" || peer || ">"
  48. end
  49.  
  50. /***************************************************************************/
  51. /* the service itself
  52.    most the following code is from arexxwebserver.rexx by Casey Halverson,
  53.    courtesy of the author.
  54. */
  55.  
  56. len = recv(sock,"A",256)
  57. if len<0 then do
  58.     call ErrLog("hserv Error reading (" || errno() || ")")
  59.     exit
  60. end
  61.  
  62. file=subword(a,2,1)
  63. if file="/" then file = "/"global.DocumentIndex
  64. if index(file,"//")~=0 then signal error
  65. if index(file,":")~=0 then signal error
  66. file = right(file,length(file)-1)
  67. complete = AddPart(global.DocumentDir,file)
  68.  
  69. if global.TransferLog~="OFF" then
  70.     call TransferLog("hserv Connection from" userat "- Request:" file)
  71.  
  72. if exists(complete)=0 then bad=1
  73. if lastpos(".",file)~= 0 then ext=upper(substr(file,lastpos(".",file),length(file)))
  74. else ext = ""
  75.  
  76. mime="text/html"
  77. if ext=".HTML"|ext=".HTM"|ext=".HML" then mime="text/html"
  78. if ext=".DOC"|ext=".TXT"             then mime="text/plain"
  79. if ext=".GUIDE"                      then mime="text/aguide"
  80. if ext=".GIF"                        then mime="image/gif"
  81. if ext=".JPG"|ext=".JPEG"            then mime="image/jpeg"
  82. if ext=".WAV"|ext=".AU"|ext=".8SVX"  then mime="audio/*"
  83. if ext=".MID"|ext=".MIDI"            then mime="audio/x-midi"
  84. if ext=".LHA"|ext=".LZH"|ext=".LZX"  then mime="application/octet-stream"
  85. if ext=".MOCHA"                      then mime="language/mocha"
  86. if ext=".GSM"|ext=".GSD"             then mime="audio/x-gsm"
  87. if ext=".PS"                         then mime="application/postscript"
  88.  
  89. call sen "HTTP/1.0 200 OK"
  90. call sen "Server: RexxWebServer/1.0"
  91. call sen "Date: "||date()||" "||time()
  92. call sen "Accept-ranges: bytes"
  93. /*call sen "Content-length: "||subword(statef(complete),2,1)*/
  94. call sen "Content-type: "||mime
  95.  
  96. if bad=1 then signal bad
  97.  
  98. call sen ""
  99.  
  100. if ~open("IN",complete,"R") then do
  101.     call ErrLog("hserv Unable to open" complete)
  102.     exit
  103. end
  104.  
  105. do until eof("IN")
  106.  
  107.     a = readln("IN")
  108.  
  109.     stop=0
  110.     do while a~="" & ~stop
  111.         select
  112.               when index(a,"<!CGI ")~=0 then do
  113.                 parse var a a "<!CGI " fun ">" b
  114.                 INTERPRET "res = "fun
  115.                 a = a || res || b
  116.             end
  117.  
  118.             when index(a,"<!ip>")~=0 then do
  119.                 parse var a a "<!ip>" b
  120.                 a = a || peer || b
  121.             end
  122.  
  123.             when index(a,"<!user>")~=0 then do
  124.                 parse var a a "<!user>" b
  125.                 a = a || user || b
  126.             end
  127.  
  128.             when index(a,"<!power>")~=0 then do
  129.                 parse var a a "<!power>" b
  130.                 a = a || "Powered up with rxsocket.library" || b
  131.             end
  132.  
  133.             otherwise stop = 1
  134.         end
  135.     end
  136.     call sen a
  137. end
  138. call close("IN")
  139. exit
  140.  
  141. /***************************************************************************/
  142.  
  143. bad:
  144.     call sen "HTTP/1.0 404 Not Found"
  145.     call sen "Content-Type: text/html"
  146.     call sen ""
  147.     call sen "<HTML><STRONG><H1>ERROR</H1></STRONG><H4><P><P>ARexxRXSWebServer error 404:<BR> Page does not exist<BR>FILENAME: "||file||"</HTML>"
  148.  
  149.     exit
  150.  
  151. /***************************************************************************/
  152.  
  153. error:
  154.     call sen "HTTP/1.0 400 Forbidden"
  155.     call sen "Content-Type: text/html"
  156.     call sen ""
  157.     call sen "<STRONG><H1>ERROR</H1></STRONG><H4><P><P>ARexxRXSWebServer error 400:<BR> You may not place : or double //'s in a URL<BR>FILENAME: "||file||"</HTML>"
  158.     call ErrLog("hserv <" || peer || "> Request" file)
  159.     exit
  160.  
  161. /***************************************************************************/
  162.  
  163. sen:
  164. parse arg string
  165.  
  166.     string = string || d2c(10)
  167.     res = send(sock,string)
  168.     if res~=length(string) then do
  169.         call ErrLog("hserv Error seanding (" || errno() || ")")
  170.         exit
  171.     end
  172.     return
  173.  
  174. /***************************************************************************/
  175.  
  176. readConfig: PROCEDURE EXPOSE global.
  177. parse arg file
  178.  
  179.     if ~Open("CONF",file,"R") then do
  180.         call EasyRequest("Config file" file "not found.","Http Server")
  181.         exit
  182.     end
  183.  
  184.     global.RootDir            = "miami:www"
  185.     global.DocumentDir        = "miami:www"
  186.     global.DocumentIndex    = "index.html"
  187.     global.HostnameLookups    = "OFF"
  188.     global.ErrorLog            = "SYS"
  189.     global.ErrorFile        = ""
  190.     global.TransferLog        = "SYS"
  191.     global.TransferFile        = ""
  192.     global.Auth                = "OFF"
  193.     global.Timeout             = 100
  194.  
  195.     err = 0
  196.     do i = 1 while ~eof("CONF") & (err==0)
  197.         line = ReadLn("CONF")
  198.         if line~="" & left(line,1)~="#" then do
  199.  
  200.             line = translate(line,' ','9'x)
  201.             opt = SubWord(line,1,1)
  202.             arg = SubWord(line,2,1)
  203.  
  204.             select
  205.                 when opt=="RootDir" then global.RootDir = arg
  206.  
  207.                 when opt=="DocumentDir" then global.DocumentDir = arg
  208.  
  209.                 when opt=="DocumentIndex" then global.DocumentIndex = arg
  210.  
  211.                 when opt=="HostnameLookups" then
  212.                     if arg~="ON" & arg~= "OFF" then err = 1
  213.                     else global.HostnameLookups = arg
  214.  
  215.                 when opt=="ErrorLog" then
  216.                     if arg~="ON" & arg~= "OFF" & arg~="SYS" then err = 1
  217.                     else global.ErrorLog = arg
  218.  
  219.                 when opt=="ErrorFile" then global.ErrorFile = arg
  220.  
  221.                 when opt=="TransferLog" then
  222.                     if arg~="ON" & arg~= "OFF" & arg~="SYS" then err = 1
  223.                     else global.TransferLog = arg
  224.  
  225.                 when opt=="TransferFile" then global.TransferFile = arg
  226.  
  227.                 when opt=="Auth" then
  228.                     if arg~="ON" & arg~= "OFF" then err = 1
  229.                     else global.Auth = arg
  230.  
  231.                 when opt=="Timeout" then
  232.                     if ~DataType(arg,"N") then err = 2
  233.                     else global.Timeout = arg
  234.  
  235.                 otherwise err = 3
  236.             end
  237.         end
  238.     end
  239.  
  240.     i = i-1
  241.  
  242.     select
  243.         when err==1 then do
  244.             call EasyRequest("bad option in line" i,"ARexxRXSWebServer")
  245.             exit
  246.         end
  247.  
  248.         when err==2 then do
  249.             call EasyRequest("bad number in line" i,"ARexxRXSWebServer")
  250.             exit
  251.         end
  252.  
  253.         when err==3 then do
  254.             call EasyRequest("Unknown option in line" i,"ARexxRXSWebServer")
  255.             exit
  256.         end
  257.  
  258.         otherwise nop
  259.     end
  260.  
  261.     if global.ErrorFile=="" & global.ErrorLog~="SYS" then global.ErrorLog="OFF"
  262.     if global.TransferFile=="" & global.TransferLog~="SYS" then global.TransferLog="OFF"
  263.  
  264.     call close("IN")
  265.     return
  266.  
  267. /***************************************************************************/
  268.  
  269. errLog:
  270. parse arg msg
  271.     select
  272.         when global.ErrorLog=="OFF" then return
  273.         when global.ErrorLog=="ON" then do
  274.             if ~open("LOG",AddPart(global.RootDir,global.ErrorFile),"A") then
  275.                 if ~open("LOG",AddPart(global.RootDir,global.ErrorFile),"W") then exit
  276.             call WriteLN("LOG",msg)
  277.             call close("LOG")
  278.         end
  279.         when global.ErrorLog=="SYS" then call SysLog(msg)
  280.         otherwise nop
  281.     end
  282.     return
  283.  
  284. /***************************************************************************/
  285.  
  286. transferLog:
  287. parse arg msg
  288.     select
  289.         when global.TransferLog=="OFF" then return
  290.         when global.TransferLog=="ON" then do
  291.             if ~open("LOG",AddPart(global.RootDir,global.TransferFile),"A") then
  292.                 if ~open("LOG",AddPart(global.RootDir,global.TransferFile),"W") then exit
  293.             call WriteLN("LOG",msg)
  294.             call close("LOG")
  295.         end
  296.         when global.TransferLog=="SYS" then call SysLog(msg)
  297.         otherwise nop
  298.     end
  299.     return
  300.  
  301. /***************************************************************************/
  302.