-
Notifications
You must be signed in to change notification settings - Fork 0
/
server
executable file
·84 lines (68 loc) · 1.76 KB
/
server
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#! env bash
while getopts "n:c:e:" OPT; do
case $OPT in
n) name=$OPTARG ;;
c) capture_file=$OPTARG ;;
e) handler=$OPTARG ;;
esac
done
shift $((OPTIND-1))
[[ -z $name ]] && echo "Must provide -n <name> option" >&2 && exit 1
function log {
# Log to syslog since stdout is used for response content
logger -t xinetd.webhook-${name} -p user.info "$@" &>/dev/null
}
capture_file=${capture_file:-/tmp/webhook-${name}.http}
function read-http {
# Read in the full HTTP request
# We look for the Content-Length because the client
# may not send an EOF on the input so we have to be
# precise about how we read from stdin.
while IFS=$'\r\n' read -r line; do
echo -en "$line\r\n"
case $line in
Content-Length:*) content_length=$(cut -d ' ' -f 2 <<<$line);;
"") break;;
esac
done
if [[ -n $content_length ]]; then
read -n $content_length -r data
echo -n "$data"
fi
}
function reply-http {
# Send a 200 OK reply to the client
local data
read -r -d '' data
# Reply with correct headers and body
if [[ -n $data ]]; then
cat <<EOF
HTTP/1.1 $*
Content-Type: text/plain
Content-Length: ${#data}
$data
EOF
else
cat <<EOF
HTTP/1.1 $*
EOF
fi
}
function reply-http-success {
reply-http "200 OK"
}
function reply-http-error {
reply-http "500 Internal Server Error"
}
# Capture the request to a file
mkdir -p $(dirname $capture_file)
read-http > $capture_file
log "Saved request to $capture_file"
# Respond to HTTP request
if [[ -n $handler ]]; then
log "Sending request to $handler"
$handler <$capture_file | reply-http-success
else
log "Sending generic 200 OK response"
echo "" | reply-http-success
fi