Newbie Rebol (CGI) questions
[1/4] from: kpeters:mvinc at: 15-Apr-2004 22:07
Hi All ~
From the web tutorials I have doctored the script below together which
leaves me with three questions:
1) Why do I receive an error if i omit the print line immediately after
the REBOL [] header?
2) How can I insert LFs in the line " emit [ mold var mold
value ]" to make the mail more readable?
3) How can i do the set-net stuff permanently?
Thanks again for any help!
Kai
#! /usr/local/bin/rebol -cs
REBOL []
print "Content-type: text/html^/"
response: make string! 2000
emit: func [data] [repend response data]
read-cgi: func [
/local data buffer
][
switch system/options/cgi/request-method [
"POST" [
data: make string! 1020
buffer: make string! 16380
while [positive? read-io system/ports/input buffer 16380][
append data buffer
clear buffer
]
]
"GET" [data: system/options/cgi/query-string]
]
data
]
foreach [var value] decode-cgi read-cgi [
emit [ mold var mold value ]
]
set-net[ [kpeters--somedom--net] smtp.telus.net ]
header: make system/standard/email [
To: [[kpeters--somedom--net]]
From: [[kpeters--somedom--com]]
Subject: "Message with a header"
Organization: "Super Org"
]
send/header [kpeters--somedom--net] response header
[2/4] from: petr:krenzelok:trz:cz at: 16-Apr-2004 8:08
Kai Peters wrote:
>Hi All ~
>
> From the web tutorials I have doctored the script below together which
>leaves me with three questions:
>
>1) Why do I receive an error if i omit the print line immediately after
>the REBOL [] header?
>
you should not receive an error? I don't understand exactly what do you
mean though. As for cgi mode, you have to print that "Content/type....."
line for your browser to be set to right mode ...
>2) How can I insert LFs in the line " emit [ mold var mold
>value ]" to make the mail more readable?
>
easily .... put newline there .... rejoin [value newline value newline]
>3) How can i do the set-net stuff permanently?
>
into user.r? I just don't remember exactly, where is rebol looking for
it. It will be either your cgi-bin dir, or rebol executable location dir
.....
-pekr-
[3/4] from: Gary::Jones::usap::gov at: 17-Apr-2004 7:28
Hi, Kai,
From: Kai Peters
KP> From the web tutorials I have doctored the script below
KP> together which leaves me with three questions:
KP> 1) Why do I receive an error if i omit the print line
KP> immediately after the REBOL [] header?
The web HTTP protocol requires that the web server return a designation for the type
of content that is to be sent, and that this designation shall be followed by a blank
line before any content is actually sent. The CGI script may be designed to handle form
input, but in a way of speaking, its "responsibility" is to return something to the browser.
Hence the "Content-type: text/html" which is the usual html web type. As an aside,
notice that this line ends with a escaped newline character "^/". The print command
supplies one newline and a second must be supplied inorder for there the be a "blank
line" following this line.
KP> 2) How can I insert LFs in the line
KP> " emit [ mold var mold value ]"
KP> to make the mail more readable?
This "emit" function is one of the language designer's favorite methods for forming large
strings from static text, variables, etc. However, I suspect it may be causing confusion
for you where the answer might otherwise be obvious. Using a more linear method makes
it more obvious how to manage the formatting of strings:
;create a decent size string in order to save
; time having to "grow" it later
response: make string! 2000
; skip code for brevity
;...
foreach [var value] decode-cgi read-cgi [
append response var
append response "="
append response value
append response newline
]
which can be shortened to:
response: make string! 2000
;...
foreach [var value] decode-cgi read-cgi [
append response reduce [var "=" value newline]
]
Since the variables are in a block, REBOL needs to know to substitute their values, which
is the purpose of the 'reduce command. This can be further shortened to:
response: make string! 2000
;...
foreach [var value] decode-cgi read-cgi [
repend response [var "=" value newline]
]
in which the "reduction" occurs within the repend command. At this juncture, you will
more readily recognize the emit function.
KP> 3) How can i do the set-net stuff permanently?
Place a user.r file with the requisite 'set-net command in the executable directory.
This essentially just reads the same exact command in before presenting REBOL's command
line.
By the way, before sending your email through the CGI interface, you may wish to add
two things. First add a first line that will then become the subject for the email message,
like:
response: rejoin ["[somedom.com form] sent: " now newline response]
This makes it easy to spot mail that comes from your website form system. Second, send
the user some feedback that the information was taken and that they can expect a response
back, like:
....
header: make system/standard/email [
To: [[kpeters--somedom--net]]
From: [[kpeters--somedom--com]]
Subject: "Message with a header"
Organization: "Super Org"
]
either error? [send/header [kpeters--somedom--net] response header][
web-response: {There has been an error in processing your
information. While the error has been logged, you may wish to
resubmit this information if you have not heard back within 48 hours.}
write/append %/path/to/web/error/log/log.txt response
][
web-response: {Thank you for submitting this information.
Your response will be processed, and you should receive
an answer within 48 hours.}
]
print web-response
Hope this helps demystify the REBOL way of managing CGI form data.
--Scott Jones
[4/4] from: Gary::Jones::usap::gov at: 17-Apr-2004 7:32
Re: Newbie Rebol (CGI) questions - correction
Forgot the 'try command:
....
header: make system/standard/email [
To: [[kpeters--somedom--net]]
From: [[kpeters--somedom--com]]
Subject: "Message with a header"
Organization: "Super Org"
]
either error? try [send/header [kpeters--somedom--net] response header][
web-response: {There has been an error in processing your
information. While the error has been logged, you may wish to
resubmit this information if you have not heard back within 48 hours.}
write/append %/path/to/web/error/log/log.txt response
][
web-response: {Thank you for submitting this information.
Your response will be processed, and you should receive
an answer within 48 hours.}
]
print web-response
--Scott Jones