test script/form for CGI
[1/2] from: t_degrav::rhrk::uni-kl::de at: 16-Nov-2000 1:34
Hi there,
I noticed lots of people here using Rebol for CGI scripts, so maybe some
of you might find the following script and a examplary HTML form helpful.
All others hopefully don't mind me sending those here.
The script can handle both GET and POST requests and shows all information
in system/options/cgi and all the sent parameters via the returned HTML
page. Receiving large POST request could be improved.
Making tese raised one question:
Did anyone ever bother to alter 'decode-cgi-query so that it is able to
handle multiple <option>s selected in a form?
If not, I might try to do so as soon as I have the time...
Tom
__________
CGI script %cgi-pars.r
(longest line is 76 chars, hopefully no wrapping through mailing this)
REBOL [
Title: "CGI Parameter Viewer"
Date: 13-Nov-2000
Name: 'CGI-Parameter-Viewer
Version: 1.0.0
File: %cgi-pars.r
Author: "Tom De Grave"
Email: [t_degrav--rhrk--uni-kl--de]
Tabs: 2
Language: 'English
Purpose: {
Shows all determined parameters when called via CGI.
}
Comment: {
nothing special yet
}
History: [
1.0.0 [13-Nov-2000 "first script version (after some experimenting)" "TD"]
]
Example: {
}
]
; send HTML header & table with info from system/options/cgi
print [{Content-Type: text/html
<html>
<head>
<title>CGI Parameters</title>
</head>
<body>
<h2>CGI Parameters</h2>
<table border="0">
<tr>
<td><b>server-software:</b></td>
<td>} system/options/cgi/server-software {</td>
</tr>
<tr>
<td><b>server-name:</b></td>
<td>} system/options/cgi/server-name {</td>
</tr>
<tr>
<td><b>gateway-interface:</b></td>
<td>} system/options/cgi/gateway-interface {</td>
</tr>
<tr>
<td><b>server-protocol:</b></td>
<td>} system/options/cgi/server-protocol {</td>
</tr>
<tr>
<td><b>server-port:</b></td>
<td>} system/options/cgi/server-port {</td>
</tr>
<tr>
<td><b>request method:</b></td>
<td>} system/options/cgi/request-method {</td>
</tr>
<tr>
<td><b>path-info:</b></td>
<td>} system/options/cgi/path-info {</td>
</tr>
<tr>
<td><b>path-translated:</b></td>
<td>} system/options/cgi/path-translated {</td>
</tr>
<tr>
<td><b>script-name:</b></td>
<td>} system/options/cgi/script-name {</td>
</tr>
<tr>
<td><b>query-string:</b></td>
<td>} system/options/cgi/query-string {</td>
</tr>
<tr>
<td><b>remote-host:</b></td>
<td>} system/options/cgi/remote-host {</td>
</tr>
<tr>
<td><b>remote-addr:</b></td>
<td>} system/options/cgi/remote-addr {</td>
</tr>
<tr>
<td><b>auth-type:</b></td>
<td>} system/options/cgi/auth-type {</td>
</tr>
<tr>
<td><b>remote-user:</b></td>
<td>} system/options/cgi/remote-user {</td>
</tr>
<tr>
<td><b>remote-ident:</b></td>
<td>} system/options/cgi/remote-ident {</td>
</tr>
<tr>
<td><b>Content-Type:</b></td>
<td>} system/options/cgi/Content-Type {</td>
</tr>
<tr>
<td><b>content-length:</b></td>
<td>} system/options/cgi/content-length {</td>
</tr>
<tr>
<td> </td>
<td></td>
</tr>
<tr>
<td><b>other-headers:</b></td>
<td></td>
</tr>}
] : print
; <tr>
; <td><b>other-headers:</b></td>
; <td>} system/options/cgi/other-headers {</td>
; </tr>
; send table rows with key/value pairs from system/options/cgi/other-headers
foreach [key value] system/options/cgi/other-headers [
print [{ <tr>
<td><b> } key {</b></td>
<td>} value {</td>
</tr>}] ; print
] ; foreach
; GET method: send table rows with key/value pairs from request
if system/options/cgi/request-method = "GET" [
if system/options/cgi/query-string <> none [
query: make object! decode-cgi-query system/options/cgi/query-string
print { <tr>
<td> </td>
<td></td>
</tr>
<tr>
<td><b>GET parameters:</b></td>
<td></td>
</tr>} ; print
foreach key next first query [
print [{ <tr>
<td><b> } key {</b></td>
<td>} get in query key {</td>
</tr>}] ; print
] ; foreach
] ; if
] ; if
print {</table>^/}
; GET method: send data received by request
if system/options/cgi/request-method = "POST" [
print {<br />
<b>POST Parameters:</b><br />}
; handling of content-length schould be changed for large files
read-io
system/ports/input
data: make string! to-integer system/options/cgi/content-length
to-integer system/options/cgi/content-length
print [{<pre>^/} data {</pre>^/}]
print {<br>}
] ; if
; send HTML footer
print {
</body>
</html>}
__________
examplary HTML form calling the above script after changing the action
paramter of the form tag
(maybe some line wrapping as it contains rather long lines, hope it's
recognizable or maybe doens't matter at all)
<html>
<head>
<title>Testing CGI Paramter Script</title>
</head>
<body>
<h1><font face="Arial,Helvetica">Testing CGI Paramter Script</font></h1>
<p>
This page demontrates the use of the GET and POST method for calling CGI
scripts. Not all input types are included, but this should show most relevant
funtions. On the server side the script <code>cgi-pars.r</code> is
called for both methods.
</p>
<hr />
<h3>GET method</h3>
<!-- change URL for CGI script here!!! -->
<form action="http://www. ... /cgi-bin/cgi-pars.r" method="GET"><br />
<table border="0">
<tr>
<td valign="top">Checkbox: </td>
<td><input type="checkbox" name="checkbox1" checked /> Checkbox 1<br />
<input type="checkbox" name="checkbox2" /> Checkbox 2<br />
<input type="checkbox" name="checkbox3" /> Checkbox 3</td>
</tr>
<tr>
<td>Hidden:</td>
<td>well... not visible, but right here ;-)
<input type="hidden" name="hidden" value="hidden Text" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" maxlength="12" size="12" /></td>
</tr>
<tr>
<td valign="top">Radio:</td>
<td> <input type="radio" name="radio" value="radio button 1" checked />
Radio Button 1<br />
<input type="radio" name="radio" value="radio button 2" />
Radio Button 2<br />
<input type="radio" name="radio" value="radio button 3" />
Radio Button 3</td>
</tr>
<tr>
<td>Text input:</td>
<td><input type="text" name="text" maxlength="52" size="52"></td>
</tr>
<tr>
<td valign="top">Select:</td>
<td valign="top">
Try selecting more than one item here and closely look at the result!
:-(<br />
<select name="select" multiple size="5" />
<option />Select 1
<option />Select 2
<option />Select 3
<option />Select 4
<option />Select 5
<option />Select 6
<option />Select 7
<option />Select 8
<option />Select 9
<option />Select 10
</select>
</td>
</tr>
<tr>
<td valign="top">Textarea:</td>
<td><textarea name="textarea" cols="50" rows="5" wrap="hard">Multiline text
goes here...</textarea></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Send"> <input type="reset"
value="Reset"></td>
</tr>
</table>
</form>
<hr />
<h3>POST method</h3>
<!-- change URL for CGI script here!!! -->
<form action="http://www. ... /cgi-bin/cgi-pars.r" method="POST"
enctype="multipart/form-data">
<table border="0">
<tr>
<td valign="top">Checkbox: </td>
<td><input type="checkbox" name="checkbox1" checked /> Checkbox 1<br />
<input type="checkbox" name="checkbox2" /> Checkbox 2<br />
<input type="checkbox" name="checkbox3" /> Checkbox 3</td>
</tr>
<tr>
<td>File:</td>
<td><input type=file size=38 name=file></td>
</tr>
<tr>
<td>Hidden:</td>
<td>well... not visible, but right here ;-)
<input type="hidden" name="hidden" value="hidden Text" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" maxlength="12" size="12" /></td>
</tr>
<tr>
<td valign="top">Radio:</td>
<td> <input type="radio" name="radio" value="radio button 1" checked />
Radio Button 1<br />
<input type="radio" name="radio" value="radio button 2" />
Radio Button 2<br />
<input type="radio" name="radio" value="radio button 3" />
Radio Button 3</td>
</tr>
<tr>
<td>Text input:</td>
<td><input type="text" name="text" maxlength="52" size="52"></td>
</tr>
<tr>
<td valign="top">Select:</td>
<td><select name="select" multiple size="5" />
<option />Select 1
<option />Select 2
<option />Select 3
<option />Select 4
<option />Select 5
<option />Select 6
<option />Select 7
<option />Select 8
<option />Select 9
<option />Select 10
</select></td>
</tr>
<tr>
<td valign="top">Textarea:</td>
<td><textarea name="textarea" cols="50" rows="5" wrap="hard">Multiline text
goes here...</textarea></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Send"> <input type="reset"
value="Reset"></td>
</tr>
</table>
</form>
<hr />
</body>
</html>
[2/2] from: tim:johnsons-web at: 15-Nov-2000 18:48
Great Stuff Tom!
To answer some of your other questions, take a look
at my article and code at the rebolforces page.
http://www.rebolforces.com/
I've bypassed decode-cgi for my own reasons.
The code is pretty crude and has some redundancies
and patches, but may give you some other ideas.
-Tim
Tom De Grave wrote: