-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparam.sh
42 lines (35 loc) · 977 Bytes
/
param.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/bash
# This is a "module" meant to be sourced by cgi screpts to gain access to
# functions for handling query parameters.
urldecode() {
sed "s@+@ @g;s@%@\\\\x@g" | xargs -0 printf "%b"
}
htmlencode() {
sed "s/</\\</g;s/>/\\>/g"
}
# Gets the raw (url-encoded) value of a named parameter from the given query
# string.
get_param() {
local pname="$1"
local query="$2"
for kv in $(echo "$query" | tr "&" "\n"); do
if echo "$kv" | grep -q "$pname=.\+"; then
echo -n "$kv" | sed 's/'"$pname"'=//'
fi
done
unset kv
}
# Checks the string HTTP_COOKIE for a cookie with the given name and prints the
# value.
get_cookie() {
local name="$1"
# shellcheck disable=SC2001
for kv in $(echo "$HTTP_COOKIE" | sed 's/; /\n/g'); do
if echo "$kv" | grep -q "$name=.\+"; then
echo -n "$kv" | sed 's/'"$name"'=//'
unset kv
return 0
fi
done
unset kv
}