changelog shortlog tags branches files raw gz bz2 help

Mercurial > hg > werc / changeset: bin/cgilib.rc: hack urlencode/urldecode for UNIX *and* Plan 9

changeset 663: 5c2c279b6320
parent 662: bd76af29cfa7
child 664: df20e7e8792a
author: sl
date: Wed, 10 Feb 2016 20:32:57 -0500
files: bin/cgilib.rc
description: bin/cgilib.rc: hack urlencode/urldecode for UNIX *and* Plan 9
     1.1--- a/bin/cgilib.rc	Wed Feb 10 20:29:27 2016 -0500
     1.2+++ b/bin/cgilib.rc	Wed Feb 10 20:32:57 2016 -0500
     1.3@@ -64,52 +64,93 @@
     1.4     END { printf "%s", buf }'
     1.5 }
     1.6 
     1.7-fn urldecode { /bin/urlencode -d }
     1.8+fn urldecode { $PLAN9/bin/urlencode -d || url_decode}	# GROSS
     1.9 
    1.10-#fn urldecode {
    1.11-#awk '
    1.12-#BEGIN {
    1.13-#    hextab ["0"] = 0; hextab ["8"] = 8;
    1.14-#    hextab ["1"] = 1; hextab ["9"] = 9;
    1.15-#    hextab ["2"] = 2; hextab ["A"] = hextab ["a"] = 10
    1.16-#    hextab ["3"] = 3; hextab ["B"] = hextab ["b"] = 11;
    1.17-#    hextab ["4"] = 4; hextab ["C"] = hextab ["c"] = 12;
    1.18-#    hextab ["5"] = 5; hextab ["D"] = hextab ["d"] = 13;
    1.19-#    hextab ["6"] = 6; hextab ["E"] = hextab ["e"] = 14;
    1.20-#    hextab ["7"] = 7; hextab ["F"] = hextab ["f"] = 15;
    1.21-#}
    1.22-#{
    1.23-#    decoded = ""
    1.24-#    i = 1
    1.25-#    len = length ($0)
    1.26-#    while ( i <= len ) {
    1.27-#        c = substr ($0, i, 1)
    1.28-#        if ( c == "%" ) {
    1.29-#            if ( i+2 <= len ) {
    1.30-#                c1 = substr ($0, i+1, 1)
    1.31-#                c2 = substr ($0, i+2, 1)
    1.32-#                if ( hextab [c1] == "" || hextab [c2] == "" ) {
    1.33-#                    print "WARNING: invalid hex encoding: %" c1 c2 | "cat >&2"
    1.34-#                } else {
    1.35-#                    code = 0 + hextab [c1] * 16 + hextab [c2] + 0
    1.36-#                    c = sprintf ("%c", code)
    1.37-#                    i = i + 2
    1.38-#                }
    1.39-#            } else {
    1.40-#                print "WARNING: invalid % encoding: " substr ($0, i, len - i)
    1.41-#            }
    1.42-#        } else if ( c == "+" ) {
    1.43-#            c = " "
    1.44-#        }
    1.45-#        decoded = decoded c
    1.46-#        ++i
    1.47-#    }
    1.48-#    printf "%s", decoded
    1.49-#}
    1.50-#'
    1.51-#}
    1.52+fn url_decode {
    1.53+awk '
    1.54+BEGIN {
    1.55+    hextab ["0"] = 0; hextab ["8"] = 8;
    1.56+    hextab ["1"] = 1; hextab ["9"] = 9;
    1.57+    hextab ["2"] = 2; hextab ["A"] = hextab ["a"] = 10
    1.58+    hextab ["3"] = 3; hextab ["B"] = hextab ["b"] = 11;
    1.59+    hextab ["4"] = 4; hextab ["C"] = hextab ["c"] = 12;
    1.60+    hextab ["5"] = 5; hextab ["D"] = hextab ["d"] = 13;
    1.61+    hextab ["6"] = 6; hextab ["E"] = hextab ["e"] = 14;
    1.62+    hextab ["7"] = 7; hextab ["F"] = hextab ["f"] = 15;
    1.63+}
    1.64+{
    1.65+    decoded = ""
    1.66+    i = 1
    1.67+    len = length ($0)
    1.68+    while ( i <= len ) {
    1.69+        c = substr ($0, i, 1)
    1.70+        if ( c == "%" ) {
    1.71+            if ( i+2 <= len ) {
    1.72+                c1 = substr ($0, i+1, 1)
    1.73+                c2 = substr ($0, i+2, 1)
    1.74+                if ( hextab [c1] == "" || hextab [c2] == "" ) {
    1.75+                    print "WARNING: invalid hex encoding: %" c1 c2 | "cat >&2"
    1.76+                } else {
    1.77+                    code = 0 + hextab [c1] * 16 + hextab [c2] + 0
    1.78+                    c = sprintf ("%c", code)
    1.79+                    i = i + 2
    1.80+                }
    1.81+            } else {
    1.82+                print "WARNING: invalid % encoding: " substr ($0, i, len - i)
    1.83+            }
    1.84+        } else if ( c == "+" ) {
    1.85+            c = " "
    1.86+        }
    1.87+        decoded = decoded c
    1.88+        ++i
    1.89+    }
    1.90+    printf "%s", decoded
    1.91+}
    1.92+'
    1.93+}
    1.94 
    1.95-fn url_encode { /bin/urlencode $* }
    1.96+fn urlencode { $PLAN9/bin/urlencode $* || url_encode }	# GROSS
    1.97+
    1.98+fn url_encode {
    1.99+    awk '
   1.100+    BEGIN {
   1.101+    # We assume an awk implementation that is just plain dumb.
   1.102+    # We will convert an character to its ASCII value with the
   1.103+    # table ord[], and produce two-digit hexadecimal output
   1.104+    # without the printf("%02X") feature.
   1.105+
   1.106+    EOL = "%0A"     # "end of line" string (encoded)
   1.107+    split ("1 2 3 4 5 6 7 8 9 A B C D E F", hextab, " ")
   1.108+    hextab [0] = 0
   1.109+    for ( i=1; i<=255; ++i ) ord [ sprintf ("%c", i) "" ] = i + 0
   1.110+    if ("'^$"EncodeEOL^'" == "yes") EncodeEOL = 1; else EncodeEOL = 0
   1.111+    }
   1.112+    {
   1.113+    encoded = ""
   1.114+    for ( i=1; i<=length ($0); ++i ) {
   1.115+        c = substr ($0, i, 1)
   1.116+        if ( c ~ /[a-zA-Z0-9.-]/ ) {
   1.117+        encoded = encoded c     # safe character
   1.118+        } else if ( c == " " ) {
   1.119+        encoded = encoded "+"   # special handling
   1.120+        } else {
   1.121+        # unsafe character, encode it as a two-digit hex-number
   1.122+        lo = ord [c] % 16
   1.123+        hi = int (ord [c] / 16);
   1.124+        encoded = encoded "%" hextab [hi] hextab [lo]
   1.125+        }
   1.126+    }
   1.127+    if ( EncodeEOL ) {
   1.128+        printf ("%s", encoded EOL)
   1.129+    } else {
   1.130+        print encoded
   1.131+    }
   1.132+    }
   1.133+    END {
   1.134+        #if ( EncodeEOL ) print ""
   1.135+    }
   1.136+' $* 
   1.137+}
   1.138 
   1.139 # Cookies
   1.140 fn set_cookie {