home *** CD-ROM | disk | FTP | other *** search
/ PC World 2000 August / PCWorld_2000-08_cd.bin / Software / TemaCD / xbasic / xbpro.exe / xb / acgibin.x < prev    next >
Text File  |  1999-12-21  |  6KB  |  193 lines

  1. '
  2. '
  3. ' ####################
  4. ' #####  PROLOG  #####
  5. ' ####################
  6. '
  7. PROGRAM "acgibin"
  8. VERSION "0.0003"
  9. '
  10. ' This program shows how to create interactive web pages, using the
  11. ' Common Gateway Interface (CGI) and XBasic as the programming tool.
  12. '
  13. ' This program reads the input data and saves it into to text variables
  14. ' commandline$ and formdata$, then builds a simple web page and sends it
  15. ' to the server.
  16. '
  17. ' This CGI must be called from a web page with a code like this:
  18. '
  19. ' <p><a href="http://default/cgi-bin/acgibin.exe?param1=val1¶m2=val2¶m3=val3">acgibin</a></p>
  20. '
  21. ' or
  22. '
  23. ' <form method="POST" action="http://default/cgi-bin/acgibin.exe?param1=val1¶m2=val2¶m3=val3">
  24. ' <p><input type="submit" value="acgibin" name="B1">.......</p>
  25. ' </form>
  26. '
  27. ' Feel free to make to copy, modify or enhance this program.
  28. ' For further information call Alberto Alva E.  email: talvae@esan.com.pe
  29. '
  30. ' this program slightly beautified by "some jerk", but not altered functionally
  31. '
  32. IMPORT "xst"
  33. IMPORT "kernel32"        ' Windows API
  34.  
  35. DECLARE FUNCTION  Entry ()
  36. DECLARE FUNCTION  Send (a$)
  37. DECLARE FUNCTION  CGIStart (pageTitle$)
  38. DECLARE FUNCTION  CGIBody ()
  39. DECLARE FUNCTION  CGIEnd (footer$)
  40. '
  41. $$STD_INPUT_HANDLE = -10
  42. $$STD_OUTPUT_HANDLE = -11
  43. '
  44. ' Standard handles declaration
  45. '
  46. XLONG #hStdIn
  47. XLONG #hStdOut
  48. '
  49. '
  50. ' ######################
  51. ' #####  Entry ()  #####
  52. ' ######################
  53. '
  54. FUNCTION  Entry ()
  55. '
  56.     pageTitle$="acgibin"
  57.     footer$="end of test"
  58. '
  59.     CGIStart (pageTitle$)
  60.     CGIBody ()
  61.     CGIEnd (footer$)
  62. END FUNCTION
  63. '
  64. '
  65. ' #####################
  66. ' #####  Send ()  #####
  67. ' #####################
  68. '
  69. FUNCTION  Send (a$)
  70.     a$ = a$ + "\r\n"
  71.     length = LEN(a$)
  72.     sent = 0
  73.     WriteFile (#hStdOut, &a$, length, &sent, 0)
  74. END FUNCTION
  75. '
  76. '
  77. ' #########################
  78. ' #####  CGIStart ()  #####
  79. ' #########################
  80. '
  81. FUNCTION  CGIStart (pageTitle$)
  82. '
  83. ' Get Standard Handles
  84. '
  85.     #hStdIn = GetStdHandle($$STD_INPUT_HANDLE)
  86.     #hStdOut = GetStdHandle($$STD_OUTPUT_HANDLE)
  87. '
  88. ' Get Enviroment variables
  89. '
  90.     Xst()
  91.     name$ = "HTTP_ACCEPT"             :    XstGetEnvironmentVariable (@name$, @#accept$)
  92.     name$ = "AUTH_TYPE"                 :    XstGetEnvironmentVariable (@name$, @#authType$)
  93.     name$ = "CONTENT_LENGTH"        :    XstGetEnvironmentVariable (@name$, @#contentLength$)
  94.     name$ = "CONTENT_TYPE"            :    XstGetEnvironmentVariable (@name$, @#contentType$)
  95.     name$ = "GATEWAY_INTERFACE"    :    XstGetEnvironmentVariable (@name$, @#gatewayInterface$)
  96.     name$ = "PATH_INFO"                    :    XstGetEnvironmentVariable (@name$, @#pathInfo$)
  97.     name$ = "PATH_TRANSLATED"        :    XstGetEnvironmentVariable (@name$, @#pathTranslated$)
  98.     name$ = "QUERY_STRING"            : XstGetEnvironmentVariable (@name$, @#queryString$)
  99.     name$ = "HTTP_REFERER"            : XstGetEnvironmentVariable (@name$, @#referer$)
  100.     name$ = "REMOTE_ADDR"                : XstGetEnvironmentVariable (@name$, @#remoteAddr$)
  101.     name$ = "REMOTE_HOST"                : XstGetEnvironmentVariable (@name$, @#remoteHost$)
  102.     name$ = "REMOTE_IDENT"            : XstGetEnvironmentVariable (@name$, @#remoteIdent$)
  103.     name$ = "REMOTE_USER"                : XstGetEnvironmentVariable (@name$, @#remoteUser$)
  104.     name$ = "REQUEST_METHOD"        :    XstGetEnvironmentVariable (@name$, @#requestMethod$)
  105.     name$ = "SCRIPT_NAME"                : XstGetEnvironmentVariable (@name$, @#scriptname$)
  106.     name$ = "SERVER_SOFTWARE"        : XstGetEnvironmentVariable (@name$, @#serverSoftware$)
  107.     name$ = "SERVER_NAME"                : XstGetEnvironmentVariable (@name$, @#servername$)
  108.     name$ = "SERVER_PORT"                : XstGetEnvironmentVariable (@name$, @#serverPort$)
  109.     name$ = "SERVER_PROTOCOL"        : XstGetEnvironmentVariable (@name$, @#serverProtocol$)
  110.     name$ = "HTTP_USER_AGENT"        : XstGetEnvironmentVariable (@name$, @#userAgent$)
  111. '
  112. ' get Input Data (formData from STDIN (if method is POST) and commandLine from #queryString$)
  113. '
  114.     length = XLONG (#contentLength$)
  115.     IF (#requestMethod$ == "POST") THEN
  116.         read = 0
  117.         buffer$ = CHR$ (0, length)
  118.         ReadFile (#hStdIn, &buffer$, length, &read, 0)
  119.         #formData$ = LEFT$ (buffer$, read)
  120.     END IF
  121.     #commandLine$= #queryString$
  122. '
  123. ' send Header
  124. '
  125.     Send ("status: 200 OK")
  126.     Send ("content-type: text/html\n\r")
  127.     Send ("<html><head><title>" + pageTitle$+ "</title></head>")
  128. END FUNCTION
  129. '
  130. '
  131. ' ########################
  132. ' #####  CGIBody ()  #####
  133. ' ########################
  134. '
  135. FUNCTION  CGIBody ()
  136. '
  137. ' The following code must be replace with your code
  138. '
  139.     Send ("<body bgcolor=\"#000000\" text=\"#FF8000\">")
  140.     Send ("<font face=\"Comic Sans MS\">")
  141.     Send ("<h1 align=\"center\">XBasic CGI Test Page</h1>")
  142.     IF #requestMethod$ = "POST" THEN
  143.         Send ("<p align=\"center\"><em>The Form Data posted is  :  "+ #formData$ + "</em></p>")
  144.     END IF
  145.     Send ("<p align=\"center\"><em>The Command Line is  :  " + #commandLine$ + " </em></p>")
  146.     Send ("<hr>")
  147.  
  148.     Send ("<p>The Principal Enviroment Variables are:</p><div align=\"center\"><center>")
  149.  
  150.     Send ("<table border=\"1\" cellpadding=\"2\">")
  151.     Send ("<tr><td align=\"center\" width=\"50%\">VARIABLE NAME</td>")
  152.     Send ("<td align=\"center\" width=\"50%\">VARIABLE VALUE</td></tr>")
  153.  
  154.     Send ("<tr><td align=\"center\" width=\"50%\">REQUEST_METHOD</td>")
  155.     Send ("<td align=\"center\" width=\"50%\">"+#requestMethod$+"</td></tr>")
  156.  
  157.     Send ("<tr><td align=\"center\" width=\"50%\">CONTENT_TYPE</td>")
  158.     Send ("<td align=\"center\" width=\"50%\">"+#contentType$+"</td></tr>")
  159.  
  160.     Send ("<tr><td align=\"center\" width=\"50%\">CONTENT_LENGTH</td>")
  161.     Send ("<td align=\"center\" width=\"50%\">"+#contentLength$+"</td></tr>")
  162.  
  163.     Send ("<tr><td align=\"center\" width=\"50%\">QUERY_STRING</td>")
  164.     Send ("<td align=\"center\" width=\"50%\">"+#queryString$+"</td></tr>")
  165.  
  166.     Send ("<tr><td align=\"center\" width=\"50%\">REMOTE_ADDR</td>")
  167.     Send ("<td align=\"center\" width=\"50%\">"+#remoteAddr$+"</td></tr>")
  168.  
  169.     Send ("</table>")
  170.     Send ("</center></div>")
  171.     Send ("</font>")
  172. END FUNCTION
  173. '
  174. '
  175. ' #######################
  176. ' #####  CGIEnd ()  #####
  177. ' #######################
  178. '
  179. FUNCTION  CGIEnd (footer$)
  180. '
  181. ' include a personalized footer here and close the web page
  182. '
  183.     Send ("<font face=\"Comic Sans MS\" font size=\"1\">")
  184.     Send ("<p></p>")
  185.     Send ("<hr>")
  186.     Send ("<p>Questions : Alberto Alva E. <a href=\"mailto:talvae@esan.com.pe\">talvae@esan.com.pe</a></p>")
  187.  
  188.     Send ("<p>"+footer$+"</p>")
  189.     Send ("</font>")
  190.     Send ("</body></html>")
  191. END FUNCTION
  192. END PROGRAM
  193.