Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search

[REBOL] Re: Decode-CGI

From: andreas:bolka:gmx at: 1-Jun-2002 14:27

Saturday, June 1, 2002, 4:03:14 AM, Andrew wrote:
> I've noticed there's a problem with Rebol's standard decode-cgi > function.
> Note that 'plus-to-space is created inside 'decode-cgi each time > it's used. It can also be easily replaced with 'replace/all.
actually plus-to-space is a scaled down version of replace/all (have a look at source replace). however, in the sense of reuse, using replace is ok ;)
> Also, CRLF aren't converted to Rebol's newline.
this is most likely an application level problem - normal CRLFs should not pass up to decode-cgi, only CRLFs which are hex-encoded. and i think those should be passed thru to the app. HOWEVER, there are two more important problems (imho): 1. decode-cgi is not able to properly parse "empty params" like name2 in "name1=val1&name2&name3=val3"
>> decode-cgi "name1=val1&name2&name3=val3"
== [name1: "val1" name2&name3: "val3"] 2. decode-cgi requires post-processing when it parses a string containing multiple parameters with the same name: name=val1&name=val2
>> decode-cgi "name=val1&name=val2"
== [name: "val1" name: "val2"] this list is quite ok, but it does not make sense to 'make object! this or even 'do it. naturally, multiple parameters with the same name map to a list, imho:
>> decode-cgi "name=val1&name=val2"
== [name: ["val1" "val2"]] so here is another patched decode-cgi :) [also submitted to feedback] ; --- snip --- decode-cgi: func [ {Converts CGI argument string to a list of words and value strings.} args [any-string!] "Starts at first argument word" /local list equate value name name-chars val plus-to-space ][ add-nv: func [ list name value /local val-ptr ] [ name: to-set-word name value: either none? value [ copy "" ] [ form dehex (replace/all value "+" " ") ] either none? val-ptr: find list name [ append list compose [ (name) (value) ] ] [ idx: index? next val-ptr poke list idx compose [ (pick list idx) (value) ] ] ] list: make block! 8 name-chars: complement charset "&=" equate: [ copy name some name-chars value] value: [ "=" value | "&" (add-nv list name "") | [ copy val to "&" "&" | copy val to end ] (add-nv list name val) ] parse/all args [some equate | none] list ] ; --- snap --- -- Best regards, Andreas mailto:[andreas--bolka--gmx--net]