CGI - Processing web forms
Veja também: Creating and Processing Web Forms with CGI (Tutorial)
Crie o seguinte arquivo form1.html na pasta www:
<HTML>
<TITLE>Simple Web Form</TITLE>
<BODY>
<b>Simple Web Form</b><p>
<FORM ACTION="formhandler.cgi">
<INPUT TYPE="TEXT" NAME="Field" SIZE="25"><BR>
<INPUT TYPE="SUBMIT" NAME="Submit" VALUE="Submit">
</FORM>
</BODY>
</HTML>
Agora crie e salve na mesma pasta o script formhandler.cgi:
#!www/rebol.exe -c
Rebol []
print [<HTML><PRE> mold system/options/cgi </HTML>]
Quando você escreve "My Name" no campo e aperta o botão "Submit", o seu form1.html vai chamar o formhandler.cgi, e este script vai imprimir o que o protocolo CGI passa para o Rebol e este armazena em system/options/cgi:
make object! [
server-software: "Cheyenne/1.0"
server-name: "Ungaretti"
gateway-interface: "CGI/1.1"
server-protocol: "HTTP/1.1"
server-port: "80"
request-method: "GET"
path-info: "/formhandler.cgi"
path-translated: "www\formhandler.cgi"
script-name: "/formhandler.cgi"
query-string: "Field=My+Name&Submit=Submit"
remote-host: none
remote-addr: "127.0.0.1"
auth-type: none
remote-user: none
remote-ident: none
Content-Type: none
content-length: "0"
other-headers: ["HTTP_ACCEPT" {text/html,application/xhtml+xml,application/...
]
É bom saber isso, mas o Rebol oferece uma função para decodificar o CGI, chamada decode-cgi que converte os dados brutos em um bloco de Rebol que contém palavras seguidas pelos seus valores. A informação que queremos (o conteúdo do campo) estão na variável query-string. Então, altere script formhandler.cgi como se segue:
#!www/rebol.exe -c
Rebol []
print [<HTML><PRE> decode-cgi system/options/cgi/query-string </HTML>]
O navegador agora vai mostrar :
Field My Name Submit Submit
Exemplo legal de CGI
Esta é a versão CGI do exemplo legal de RSP. Salve como coolexample.cgi na pasta www do Cheyenne. Abra com o navegador usando localhost/coolexample.cgi.
#!www/rebol.exe -c
Rebol []
; First, a not very elegant way of avoiding crashes:
either system/options/cgi/query-string = none [
system/options/cgi/query-string: ""
decoded: ""
][
decoded: second decode-cgi system/options/cgi/query-string
]
; Lets show what's in "decoded":
print {<font face="courier">}
print "decoded = " probe decoded print "<br>"
; Here we start HTML
print {
<HTML>
<br><br>
<TITLE>Cool Example</TITLE>
<BODY>
<b>Cool Example</b><p>
<FORM ACTION="coolexample.cgi">}
print {<INPUT TYPE="SUBMIT" NAME="Triangle" VALUE="Triangle"><br><br>}
if decoded = "Triangle" [
print {<svg width="120" height="120">
<polygon points="0,100 50,0 100,100"
style="fill:lime;stroke:purple;stroke-width:5;fill-rule:evenodd;" />
</svg> <br>}
]
print {<INPUT TYPE="SUBMIT" NAME="Square" VALUE="Square"><br><br>}
if decoded = "Square" [
print {<svg width="120" height="120">
<rect width="100" height="100" style="fill:rgb(0,0,255);stroke-width:10;stroke:rgb(0,0,0)" />
</svg> <br>}
]
print {<INPUT TYPE="SUBMIT" NAME="Circle" VALUE="Circle"><br><br>}
if decoded = "Circle" [
print {<svg width="120" height="120">
<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg> <br>}
]
; Now we finish HTML
print {
</FORM>
</BODY>
</HTML>}