Hi.

I'm working on tool to help ppl with writing cgis in rebol.

At first, I'm quite unsatisfied with built-in decode-cgi
(or decode-cgi-query - why this duplicity? These functions
are same...)

built-in decode-cgi has these two holes:

1)
If more values are of same name, only last value is saved by 
recommended arguments-reading:

>> probe make object! decode-cgi {a=1&a=2&a=3}

make object! [
    a: "3"
] 

Maybe you think that name duplicity is incorrect in relation with 
HTML specification, but it isn't :

[HTML 4.01 specs]:
...Several checkboxes in a form may share the same control name.
Thus, for example, checkboxes allow users to select several values for the
same property...

Next example of correct duplicity of names is a result of SELECT control
with MULTIPLE flag...

solution:
if there is more then one value, decode-cgi should make block
of strings after name with colon:

>> probe make object! decode-cgi {a=1&a=2&a=3}

make object! [
    a: ["1" "2" "3"]
]      

2)
Next problem is with empty names:
<INPUT type="text" name="">
I know this isn't supported by HTML spec., but it works
in practice, so I think rebol should be able to handle it
and not to do this:

>> decode-cgi "=4"
** Script Error: Expected one of: word! - not: none!.
** Where: to set-word! value  


well, here is a bit modified version of decode-cgi.      

decode-cgi: func [
    {Convert CGI argument string to a list of words and (block of) value strings.}
    args [any-string!] "Starts at first argument word"
    /local list equate value name tmp
][
    plus-to-space: func [arg /local seek chr] [
        if any [none? arg empty? arg] [return ""]
        seek: arg
        while [seek: find seek #"+"] [
            change seek #" "
            seek: next seek
        ]
        head arg
    ]
    list: make block! 8
    equate: [copy name to #"=" #"=" [copy value to #"&" #"&" | copy value to end]
        (name: either none? name [name: copy ""] [dehex plus-to-space name]
        value: either none? value [value: copy ""] [dehex plus-to-space value]
        either found? tmp: find list to-set-word name [
            if not block? first tmp: next tmp [
                change/only tmp reduce [first tmp]
            ]
            append first tmp value
        ][
            append list reduce [to-set-word name value]
        ])
    ]
    parse/all args [any equate]
    list
]                       

Or maybe the plus-to-space function can be replaced by 
replace/all string! "+" " "

>> probe obj: make object! decode-cgi "=4"

make object! [
    : "4"
]
>> get in obj to-word ""
== "4"    

Well, that was my two cents.

Regards,
Jan

--
Jan Strejcek
[EMAIL PROTECTED]

Reply via email to