[REBOL] Re: Decode CGI with checkboxes
From: g:santilli:tiscalinet:it at: 18-Sep-2000 19:59
Hello [rebol--keithdevens--com]!
On 17-Set-00, you wrote:
r> Hi, just wondering how you deal with checkboxes when doing
r> CGI.
The CGI decoder built in YARWeS worked in this case. Let me see if
I can dig it back...
Hmm... perhaps it's a bit complicated, anyway:
insert-no-dups: func [
list [any-block!]
word [word!]
value [any-type!]
/local pos
] [
either found? pos: find head list to-set-word :word [
either list? second pos [
append second pos value
] [
change/only next pos to-list reduce [second pos value]
]
] [
append list reduce [to-set-word :word value]
]
head list
]
uchar: [unreserved | escape]
unreserved: make bitset! #{000000009277FF03FEFFFFFFFFFFFF7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF}
escape: [#"%" 2 HEX]
HEX: charset "0123456789ABCDEFabcdef"
query-allowed: charset ";/?:@+"
last-query-name: none
last-query-value: none
last-query-object: make list! []
namevalue: [
copy last-query-name some [uchar | query-allowed] #"="
copy last-query-value some [uchar | query-allowed] [#"&" | CRLF | end]
(insert-no-dups last-query-object to-word decode last-query-name decode last-query-value)
]
query-rule: [(last-query-object: make list! []) some namevalue end]
decoded-string: copy ""
last-string: none
last-hex: none
filter: func [str] [either none? str [""] [str]]
decode-rule: [[
some [copy last-string to #"%" skip copy last-hex 2 HEX (
append decoded-string join filter last-string hex2char last-hex
)] | none
] copy last-string to end (append decoded-string filter last-string)
]
decode: func [
string [any-string!]
] [
decoded-string: copy ""
string: translate copy string #"+" #" "
if not parse/all string decode-rule [
throw make error! reduce [
'user 'message
"Decode error"
"Error decoding url-encoded string"
string
]
]
decoded-string
]
parse-query: func [string [string!]] [
parse/all string query-rule
make object! to-block head last-query-object
]
I just did some cut&paste, so probably it won't work. Anyway,
you could even continue using DECODE-CGI:
>> blk: decode-cgi "name=value1&name=value2&name=value3"
== [name: "value1" name: "value2" name: "value3"]
>> fixed: []
== []
>> foreach [name value] blk [
[ either pos: find fixed :name [
[ pos/2: compose [(pos/2) (value)]
[ ] [
[ insert insert fixed :name value
[ ]
[ ]
== [name: ["value1" "value2" "value3"]]
HTH,
Gabriele.
--
Gabriele Santilli <[giesse--writeme--com]> - Amigan - REBOL programmer
Amiga Group Italia sez. L'Aquila -- http://www.amyresource.it/AGI/