Last active
January 24, 2018 13:50
-
-
Save kamermans/fab681a9dd8be3e4d6dd to your computer and use it in GitHub Desktop.
Dead-simple FCGI client with minimal dependencies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh -e | |
# This cgi-fcgi and Perl-based, dead simple FGCI client works out-of-the-box with just | |
# a stock Perl installation and cgi-fcgi (apt-get install libfcgi0ldbl) | |
# It was made as a simple FCGI client for health-checking FCGI daemons, for example, in | |
# a minimal Debian-based Docker image. It is tested against PHP-FPM and HHVM | |
# By: Steve Kamerman | |
# | |
# Usage: ./fcgi_get.sh <fcgi_ip>:<fcgi_port> <script_path> <document_root> [GET|HEAD|POST|PUT|DELETE] | |
# If invalid arguments are passed, exit code 1 is returned | |
# If the connection succeeds, exit code 0 is returned and the HTTP Headers are output on STDERR | |
# and the Body content is output on STDOUT. | |
# | |
# Examples: | |
# Test if connection is successful to PHP-FPM (requires /ping to be enabled) | |
# ./http_get.pl 127.0.0.1:9000 /ping / >/dev/null 2>&1 && echo "OK" || echo "Failed" | |
# Get HTTP headers only | |
# ./http_get.pl 127.0.0.1:9000 /health.php /var/www/html >/dev/null | |
# Get HTTP Body content only | |
# ./http_get.pl 127.0.0.1:9000 /health.php /var/www/html 2>/dev/null | |
# | |
FCGI="$1" | |
URI="$2" | |
DOCROOT="$3" | |
METHOD="$4" | |
if [ "x$METHOD" = "x" ]; then | |
METHOD="GET" | |
fi | |
if [ $# -lt 3 ]; then | |
echo "Usage: ./$(basename $0) <fcgi_ip>:<fcgi_port> <script_path> <document_root> [GET|HEAD|POST|PUT|DELETE]" | |
exit 2 | |
fi | |
URI=$(echo "$URI" | cut -d"?" -f1) | |
QUERY_STRING=$(echo "$URI" | cut -s -d"?" -f2) | |
# env is used to give a clean environment to FCGI | |
# perl is used to send the headers to STDERR and the body to STDOUT | |
env -i \ | |
SCRIPT_NAME="$URI" \ | |
SCRIPT_FILENAME="$DOCROOT$URI" \ | |
REQUEST_URI="$URI" \ | |
DOCUMENT_URI="$URI" \ | |
QUERY_STRING="$QUERY_STRING" \ | |
REQUEST_METHOD="$METHOD" \ | |
DOCUMENT_ROOT="$DOCROOT" \ | |
GATEWAY_INTERFACE="CGI/1.1" \ | |
REMOTE_ADDR="127.0.0.1" \ | |
HTTP_ACCEPT="*/*" \ | |
HTTP_HOST="localhost" \ | |
cgi-fcgi -bind -connect $FCGI \ | |
| perl -nle 's/\r//g; $h=1 if(!defined $h); if($h eq 1 && m/^$/){$h=0; print STDERR; next;} if($h eq 1){print STDERR;}else{print STDOUT;}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$QUERY_STRING didn't work because of loosing query data during the reassigning $URI in #35
Could you please exchange 35,36 lines order? :) I spent a hour to find the problem.