bin/cgilib.rc: hack urlencode/urldecode for UNIX *and* Plan 9
This commit is contained in:
parent
a2bcdde15f
commit
2f8c4cd8e8
1 changed files with 85 additions and 44 deletions
129
bin/cgilib.rc
129
bin/cgilib.rc
|
|
@ -64,52 +64,93 @@ fn awk_buffer {
|
||||||
END { printf "%s", buf }'
|
END { printf "%s", buf }'
|
||||||
}
|
}
|
||||||
|
|
||||||
fn urldecode { /bin/urlencode -d }
|
fn urldecode { $PLAN9/bin/urlencode -d || url_decode} # GROSS
|
||||||
|
|
||||||
#fn urldecode {
|
fn url_decode {
|
||||||
#awk '
|
awk '
|
||||||
#BEGIN {
|
BEGIN {
|
||||||
# hextab ["0"] = 0; hextab ["8"] = 8;
|
hextab ["0"] = 0; hextab ["8"] = 8;
|
||||||
# hextab ["1"] = 1; hextab ["9"] = 9;
|
hextab ["1"] = 1; hextab ["9"] = 9;
|
||||||
# hextab ["2"] = 2; hextab ["A"] = hextab ["a"] = 10
|
hextab ["2"] = 2; hextab ["A"] = hextab ["a"] = 10
|
||||||
# hextab ["3"] = 3; hextab ["B"] = hextab ["b"] = 11;
|
hextab ["3"] = 3; hextab ["B"] = hextab ["b"] = 11;
|
||||||
# hextab ["4"] = 4; hextab ["C"] = hextab ["c"] = 12;
|
hextab ["4"] = 4; hextab ["C"] = hextab ["c"] = 12;
|
||||||
# hextab ["5"] = 5; hextab ["D"] = hextab ["d"] = 13;
|
hextab ["5"] = 5; hextab ["D"] = hextab ["d"] = 13;
|
||||||
# hextab ["6"] = 6; hextab ["E"] = hextab ["e"] = 14;
|
hextab ["6"] = 6; hextab ["E"] = hextab ["e"] = 14;
|
||||||
# hextab ["7"] = 7; hextab ["F"] = hextab ["f"] = 15;
|
hextab ["7"] = 7; hextab ["F"] = hextab ["f"] = 15;
|
||||||
#}
|
}
|
||||||
#{
|
{
|
||||||
# decoded = ""
|
decoded = ""
|
||||||
# i = 1
|
i = 1
|
||||||
# len = length ($0)
|
len = length ($0)
|
||||||
# while ( i <= len ) {
|
while ( i <= len ) {
|
||||||
# c = substr ($0, i, 1)
|
c = substr ($0, i, 1)
|
||||||
# if ( c == "%" ) {
|
if ( c == "%" ) {
|
||||||
# if ( i+2 <= len ) {
|
if ( i+2 <= len ) {
|
||||||
# c1 = substr ($0, i+1, 1)
|
c1 = substr ($0, i+1, 1)
|
||||||
# c2 = substr ($0, i+2, 1)
|
c2 = substr ($0, i+2, 1)
|
||||||
# if ( hextab [c1] == "" || hextab [c2] == "" ) {
|
if ( hextab [c1] == "" || hextab [c2] == "" ) {
|
||||||
# print "WARNING: invalid hex encoding: %" c1 c2 | "cat >&2"
|
print "WARNING: invalid hex encoding: %" c1 c2 | "cat >&2"
|
||||||
# } else {
|
} else {
|
||||||
# code = 0 + hextab [c1] * 16 + hextab [c2] + 0
|
code = 0 + hextab [c1] * 16 + hextab [c2] + 0
|
||||||
# c = sprintf ("%c", code)
|
c = sprintf ("%c", code)
|
||||||
# i = i + 2
|
i = i + 2
|
||||||
# }
|
}
|
||||||
# } else {
|
} else {
|
||||||
# print "WARNING: invalid % encoding: " substr ($0, i, len - i)
|
print "WARNING: invalid % encoding: " substr ($0, i, len - i)
|
||||||
# }
|
}
|
||||||
# } else if ( c == "+" ) {
|
} else if ( c == "+" ) {
|
||||||
# c = " "
|
c = " "
|
||||||
# }
|
}
|
||||||
# decoded = decoded c
|
decoded = decoded c
|
||||||
# ++i
|
++i
|
||||||
# }
|
}
|
||||||
# printf "%s", decoded
|
printf "%s", decoded
|
||||||
#}
|
}
|
||||||
#'
|
'
|
||||||
#}
|
}
|
||||||
|
|
||||||
fn url_encode { /bin/urlencode $* }
|
fn urlencode { $PLAN9/bin/urlencode $* || url_encode } # GROSS
|
||||||
|
|
||||||
|
fn url_encode {
|
||||||
|
awk '
|
||||||
|
BEGIN {
|
||||||
|
# We assume an awk implementation that is just plain dumb.
|
||||||
|
# We will convert an character to its ASCII value with the
|
||||||
|
# table ord[], and produce two-digit hexadecimal output
|
||||||
|
# without the printf("%02X") feature.
|
||||||
|
|
||||||
|
EOL = "%0A" # "end of line" string (encoded)
|
||||||
|
split ("1 2 3 4 5 6 7 8 9 A B C D E F", hextab, " ")
|
||||||
|
hextab [0] = 0
|
||||||
|
for ( i=1; i<=255; ++i ) ord [ sprintf ("%c", i) "" ] = i + 0
|
||||||
|
if ("'^$"EncodeEOL^'" == "yes") EncodeEOL = 1; else EncodeEOL = 0
|
||||||
|
}
|
||||||
|
{
|
||||||
|
encoded = ""
|
||||||
|
for ( i=1; i<=length ($0); ++i ) {
|
||||||
|
c = substr ($0, i, 1)
|
||||||
|
if ( c ~ /[a-zA-Z0-9.-]/ ) {
|
||||||
|
encoded = encoded c # safe character
|
||||||
|
} else if ( c == " " ) {
|
||||||
|
encoded = encoded "+" # special handling
|
||||||
|
} else {
|
||||||
|
# unsafe character, encode it as a two-digit hex-number
|
||||||
|
lo = ord [c] % 16
|
||||||
|
hi = int (ord [c] / 16);
|
||||||
|
encoded = encoded "%" hextab [hi] hextab [lo]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ( EncodeEOL ) {
|
||||||
|
printf ("%s", encoded EOL)
|
||||||
|
} else {
|
||||||
|
print encoded
|
||||||
|
}
|
||||||
|
}
|
||||||
|
END {
|
||||||
|
#if ( EncodeEOL ) print ""
|
||||||
|
}
|
||||||
|
' $*
|
||||||
|
}
|
||||||
|
|
||||||
# Cookies
|
# Cookies
|
||||||
fn set_cookie {
|
fn set_cookie {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue