2009-02-01 12:06:51 -07:00
|
|
|
# Useful CGI stuff
|
2008-10-13 17:59:10 -06:00
|
|
|
|
2008-09-26 02:35:12 -06:00
|
|
|
fn dprint { echo $* >[1=2] }
|
2009-02-01 12:06:51 -07:00
|
|
|
fn dprintv { { for(v in $*) { echo -n $v^'#'^$#$v^'=' $$v '; ' }; echo } >[1=2] }
|
2008-09-26 02:35:12 -06:00
|
|
|
|
2008-09-26 06:23:19 -06:00
|
|
|
fn escape_html { sed 's/&/\&/g; s/</\</g; s/>/\>/g' $* }
|
|
|
|
|
2009-01-25 05:56:53 -07:00
|
|
|
fn http_redirect {
|
2009-03-03 06:15:46 -07:00
|
|
|
if(~ $1 http://* https://*)
|
2009-01-30 08:13:06 -07:00
|
|
|
t=$1
|
|
|
|
if not if(~ $1 /*)
|
|
|
|
t=$"base_url^$1
|
|
|
|
if not
|
|
|
|
t=$"base_url^$"req_path^$1
|
2009-01-25 05:56:53 -07:00
|
|
|
echo 'Status: '^$2^'
|
2009-01-30 08:13:06 -07:00
|
|
|
Location: '^$t^'
|
2008-09-26 02:35:12 -06:00
|
|
|
|
|
|
|
'
|
|
|
|
exit
|
|
|
|
}
|
2009-01-25 05:56:53 -07:00
|
|
|
fn perm_redirect { http_redirect $1 '301 Moved Permanantly' }
|
|
|
|
fn post_redirect { http_redirect $1 '303 See Other' }
|
2008-09-26 02:35:12 -06:00
|
|
|
|
2009-01-10 20:02:07 -07:00
|
|
|
|
2009-01-22 06:21:40 -07:00
|
|
|
# Note: should check if content type is application/x-www-form-urlencoded?
|
2009-02-18 02:26:19 -07:00
|
|
|
# Should compare with http://www.shelldorado.com/scripts/cmds/urlgetopt.txt
|
2009-01-22 06:21:40 -07:00
|
|
|
fn load_post_args {
|
|
|
|
if(~ $REQUEST_METHOD POST && ~ $#post_args 0) {
|
2008-10-17 19:26:54 -06:00
|
|
|
ifs='&
|
|
|
|
' for(pair in `{cat}) {
|
2009-01-22 06:21:40 -07:00
|
|
|
ifs='=' { pair=`{echo -n $pair} }
|
|
|
|
n='post_arg_'^`{echo $pair(1)|tr -cd 'a-zA-Z0-9_'}
|
|
|
|
post_args=( $post_args $n )
|
2009-01-25 05:56:53 -07:00
|
|
|
ifs=() { $n=`{echo -n $pair(2)|urldecode|tr -d '
'} }
|
2008-10-17 19:26:54 -06:00
|
|
|
}
|
2009-01-22 06:21:40 -07:00
|
|
|
pair=()
|
2008-10-17 19:26:54 -06:00
|
|
|
}
|
2009-01-22 06:21:40 -07:00
|
|
|
if not
|
|
|
|
status='No POST or post args already loaded'
|
|
|
|
}
|
2009-01-22 06:24:48 -07:00
|
|
|
# Status is () if at least one arg is found. DEPRECATED: access vars directly.
|
2009-01-22 06:21:40 -07:00
|
|
|
fn get_post_args {
|
|
|
|
load_post_args
|
|
|
|
_status='No post arg matches'
|
|
|
|
for(n in $*) {
|
|
|
|
v=post_arg_$n
|
|
|
|
if(! ~ $#$v 0) {
|
|
|
|
$n=$$v
|
|
|
|
_status=()
|
2008-10-17 19:26:54 -06:00
|
|
|
}
|
2008-09-26 02:35:12 -06:00
|
|
|
}
|
2009-01-10 20:02:07 -07:00
|
|
|
status=$_status
|
|
|
|
}
|
2008-09-26 02:35:12 -06:00
|
|
|
|
2009-01-08 18:06:41 -07:00
|
|
|
# This seems slightly improve performance, but might depend on httpd buffering behavior.
|
2008-09-26 02:35:12 -06:00
|
|
|
fn awk_buffer {
|
|
|
|
awk '{
|
|
|
|
buf = buf $0"\n"
|
2009-01-08 18:06:41 -07:00
|
|
|
if(length(buf) > 1400) {
|
2008-09-26 02:35:12 -06:00
|
|
|
printf "%s", buf
|
|
|
|
buf = ""
|
|
|
|
}
|
|
|
|
}
|
2008-12-19 22:32:37 -07:00
|
|
|
END { printf "%s", buf }'
|
2008-09-26 02:35:12 -06:00
|
|
|
}
|
|
|
|
|
2008-09-26 06:23:19 -06:00
|
|
|
fn urldecode {
|
|
|
|
awk '
|
|
|
|
BEGIN {
|
|
|
|
hextab ["0"] = 0; hextab ["8"] = 8;
|
|
|
|
hextab ["1"] = 1; hextab ["9"] = 9;
|
|
|
|
hextab ["2"] = 2; hextab ["A"] = hextab ["a"] = 10
|
|
|
|
hextab ["3"] = 3; hextab ["B"] = hextab ["b"] = 11;
|
|
|
|
hextab ["4"] = 4; hextab ["C"] = hextab ["c"] = 12;
|
|
|
|
hextab ["5"] = 5; hextab ["D"] = hextab ["d"] = 13;
|
|
|
|
hextab ["6"] = 6; hextab ["E"] = hextab ["e"] = 14;
|
|
|
|
hextab ["7"] = 7; hextab ["F"] = hextab ["f"] = 15;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
decoded = ""
|
2008-12-19 22:32:37 -07:00
|
|
|
i = 1
|
2008-09-26 06:23:19 -06:00
|
|
|
len = length ($0)
|
|
|
|
while ( i <= len ) {
|
|
|
|
c = substr ($0, i, 1)
|
|
|
|
if ( c == "%" ) {
|
|
|
|
if ( i+2 <= len ) {
|
|
|
|
c1 = substr ($0, i+1, 1)
|
|
|
|
c2 = substr ($0, i+2, 1)
|
|
|
|
if ( hextab [c1] == "" || hextab [c2] == "" ) {
|
|
|
|
print "WARNING: invalid hex encoding: %" c1 c2 | "cat >&2"
|
|
|
|
} else {
|
|
|
|
code = 0 + hextab [c1] * 16 + hextab [c2] + 0
|
|
|
|
c = sprintf ("%c", code)
|
|
|
|
i = i + 2
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
print "WARNING: invalid % encoding: " substr ($0, i, len - i)
|
|
|
|
}
|
|
|
|
} else if ( c == "+" ) {
|
|
|
|
c = " "
|
|
|
|
}
|
|
|
|
decoded = decoded c
|
|
|
|
++i
|
|
|
|
}
|
2009-01-22 06:21:40 -07:00
|
|
|
printf "%s", decoded
|
2008-09-26 06:23:19 -06:00
|
|
|
}
|
|
|
|
'
|
2008-09-26 02:35:12 -06:00
|
|
|
}
|
2008-10-13 17:59:10 -06:00
|
|
|
|
2008-11-25 20:50:03 -07:00
|
|
|
|
2008-10-17 19:26:54 -06:00
|
|
|
# Cookies
|
|
|
|
fn set_cookie {
|
|
|
|
# TODO: should check input values more carefully
|
2008-12-19 22:32:37 -07:00
|
|
|
name=$1
|
|
|
|
val=$2
|
|
|
|
extraHttpHeaders=( $extraHttpHeaders 'Set-cookie: '^$"name^'='^$"val^'; path=/;' )
|
2008-10-17 19:26:54 -06:00
|
|
|
}
|
|
|
|
fn get_cookie {
|
2009-03-03 13:23:51 -07:00
|
|
|
ifs=';' { co=`{echo $HTTP_COOKIE} }
|
2008-10-17 19:26:54 -06:00
|
|
|
|
2009-01-16 19:43:05 -07:00
|
|
|
# XXX: we might be adding a trailing new line?
|
|
|
|
# The ' ?' is needed to deal with '; ' inter-cookie delimiter
|
|
|
|
{ for(c in $co) echo $c } | sed -n 's/^ ?'$1'=//p'
|
2008-10-17 19:26:54 -06:00
|
|
|
}
|
|
|
|
|
2009-02-01 12:06:51 -07:00
|
|
|
|
|
|
|
fn static_file {
|
|
|
|
echo 'Content-Type: '`{select_mime $1}
|
|
|
|
echo
|
|
|
|
cat $1
|
|
|
|
exit
|
|
|
|
}
|
|
|
|
|
2009-01-10 20:02:07 -07:00
|
|
|
fn select_mime {
|
|
|
|
m='text/plain'
|
|
|
|
if(~ $1 *.css)
|
|
|
|
m='text/css'
|
|
|
|
if not if(~ $1 *.ico)
|
|
|
|
m='image/x-icon'
|
|
|
|
if not if(~ $1 *.png)
|
|
|
|
m='image/png'
|
|
|
|
if not if(~ $1 *.jpg *.jpeg)
|
|
|
|
m='image/jpeg'
|
|
|
|
if not if(~ $1 *.gif)
|
|
|
|
m='image/gif'
|
|
|
|
if not if(~ $1 *.pdf)
|
|
|
|
m='application/pdf'
|
|
|
|
echo $m
|
|
|
|
}
|
2008-10-22 01:18:55 -06:00
|
|
|
|
2009-01-08 08:46:00 -07:00
|
|
|
##############################################
|
|
|
|
# Generic rc programming helpers
|
|
|
|
|
2009-02-01 12:06:51 -07:00
|
|
|
# Manage nested lists
|
2009-01-08 08:46:00 -07:00
|
|
|
fn ll_add {
|
2009-01-08 08:48:09 -07:00
|
|
|
_l=$1^_^$#$1
|
|
|
|
$_l=$*(2-)
|
|
|
|
$1=( $$1 $_l )
|
2009-01-08 08:46:00 -07:00
|
|
|
}
|
|
|
|
|
2009-02-01 12:06:51 -07:00
|
|
|
NEW_LINE='
|
|
|
|
'
|
2008-10-22 01:18:55 -06:00
|
|
|
|
2009-02-13 16:18:34 -07:00
|
|
|
# crop_text [max_lenght [ellipsis]]
|
|
|
|
# TODO: Option to crop only at word-delimiters.
|
2009-02-01 12:06:51 -07:00
|
|
|
fn crop_text {
|
2009-02-13 16:18:34 -07:00
|
|
|
m=512
|
|
|
|
e='...'
|
|
|
|
if(! ~ $#1 0)
|
|
|
|
m=$1
|
|
|
|
if(! ~ $#2 0)
|
|
|
|
e=$2
|
|
|
|
|
|
|
|
awk -v 'max='^$"m -v 'ellipsis='$e '
|
2009-02-01 12:06:51 -07:00
|
|
|
{
|
|
|
|
nc += 1 + length;
|
|
|
|
if(nc > max) {
|
2009-02-13 16:18:34 -07:00
|
|
|
print substr($0, 1, nc - max) " " ellipsis
|
2009-02-01 12:06:51 -07:00
|
|
|
exit
|
2008-10-22 01:18:55 -06:00
|
|
|
}
|
2009-02-01 12:06:51 -07:00
|
|
|
print
|
|
|
|
}'
|
2009-01-16 19:43:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|