shithub: werc

Download patch

ref: 90186dc24eaf17b8f20f650a9c8ec097d1e30828
parent: 812c550f75aadbea39740d3fe37139f54139efb2
author: sl <uriel@engel.se.cat-v.org>
date: Sun Jul 19 23:59:49 EDT 2009

Add url_encode function to cgilib.rc

--- a/bin/cgilib.rc
+++ b/bin/cgilib.rc
@@ -107,6 +107,46 @@
 '
 }
 
+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
 fn set_cookie {