Skip to content

Instantly share code, notes, and snippets.

@morgan9e
Last active March 19, 2024 05:38
Show Gist options
  • Save morgan9e/3c9e0b653508c007c811379aaf73f689 to your computer and use it in GitHub Desktop.
Save morgan9e/3c9e0b653508c007c811379aaf73f689 to your computer and use it in GitHub Desktop.
CGI to HTTP for use with `nc -e`
#!/bin/bash
while IFS=$'\n' read -r line; do
if [[ "$line" == $'\r' ]]; then
raw="${raw}\r"
break;
fi
raw="${raw}${line}\n";
done
IFS=' ' read -r rmethod rpath rver <<< "$raw";
echo -e "\033[33m${rmethod}\033[00m ${rpath}" >&2
if echo $rpath | grep -qE '^/.*\.(png|css|js|ico)'; then
if [ ! -f ${WEBROOT:-.}/${rpath#*/} ]; then
echo -ne "HTTP/1.1 404 Not Found\r\n";
else
cl=$(cat ${WEBROOT:-.}/${rpath#*/} | wc -c | awk '{print $1}');
echo -ne "HTTP/1.1 200 OK\r\nContent-length: ${cl}\r\n\r\n";
cat ${WEBROOT:-.}/${rpath#*/};
fi
else
IFS=? read -r PATH_INFO QUERY_STRING <<< $rpath;
PATH_INFO=$PATH_INFO QUERY_STRING=$QUERY_STRING ./cgit.cgi |
{
while IFS=$'\n' read -r line; do
if [[ "$line" == "" ]]; then
break;
fi
header="${header}${line}\n";
done
body="$(base64)";
header=$(echo -ne "$header" | perl -pe '
BEGIN { $prepend = 1 }
if ($. == 1) {
if (/^Status:/) {
s/^Status:/HTTP\/1.1/;
$prepend = 0;
}
}
print "HTTP/1.1 200 OK\n" if $. == 1 && $prepend;' |
awk 'BEGIN { ORS="\r\n" } { print }');
echo -ne "${header}\nContent-length: $(echo -ne "$body" | base64 -d | wc -c | awk '{print $1}')\r\n\r\n"
echo -ne "$body" | base64 -d
}
fi
@morgan9e
Copy link
Author

morgan9e commented Mar 19, 2024

nc -klp 8000 -e ./cgiwrap.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment