#!/bin/sh

# test HTTPoxy mitigation

set -uex

export PATH="/sbin:/usr/sbin:$PATH"

DOCROOT="$AUTOPKGTEST_TMP/docroot"
CONF="$AUTOPKGTEST_TMP/lighttpd.conf"
mkdir -p "$DOCROOT"
cat >"$CONF" <<EOF
server.document-root = "$DOCROOT"
server.modules += ("mod_cgi")
cgi.assign = (".cgi" => "")
EOF

SCRIPT="httpoxy.cgi"
cat >"$DOCROOT/$SCRIPT" <<'EOF'
#!/bin/sh
if [ -n "$HTTP_PROXY" ] || [ -n "$HTTP_Proxy" ] || [ -n "$HTTP_proxy" ]; then
    printf "Status: 403\n\n"
else
    printf "Status: 200\n\n"
fi
EOF
chmod a+x "$DOCROOT/$SCRIPT"

# validate test configuration
lighttpd -tt -f "$CONF"

unset HTTP_PROXY
unset HTTP_Proxy
unset HTTP_proxy

OUTPUT=$(printf "GET /$SCRIPT HTTP/1.0\r\nProxy: 255.255.255.255:666\r\n\r\n" |\
         lighttpd -1 -f "$CONF" 2>/dev/null | head -1)
test "$OUTPUT" = "$(printf "HTTP/1.0 200 OK\r\n")"
