linux - How to read an HTTP header value in a CGI Bash script -
i have cgi bash script hw.sh
located on server running nginx. script called following command:
curl -s -d - -h "range: bytes=1-8" -h "connection: close" -x http://server_ip/cgi-bin/hw.sh?path=/some/path/to/data/
i need fetch value of range
request header within hw.sh
. please me figure out how can achieved.
http headers passed cgi programs environment variables. not case.
generally, way access header values in cgi program depends on how web server processes headers. headers may modified, or removed. example, in nginx configuration, possible pass value of range
header via custom fastcgi parameter:
fastcgi_param range $http_range;
it case environment variable called range
, depends on implementation of protocol driver. headers may dropped due server configuration. example, following apache 2 configuration drops range header when there more 5 ranges:
setenvif range (,.*?){5,} bad-range=1 requestheader unset range env=bad-range
thus, impossible predict name of environment variable, , whether available in cgi program.
however, if header is available in script, available via environment variable. can find exact name examining output of env
command.
the following should work default settings apache2:
#!/bin/bash - printf '%s\n\n' 'content-type: text/html' printf '>>> %s <<< \n' "$http_range" exit 0
sample output
'http://apache-test.local/cgi-bin/test.sh?sdfdsf' http/1.1 200 ok date: thu, 10 nov 2016 08:17:23 gmt server: apache connection: close transfer-encoding: chunked content-type: text/html >>> bytes=1-8 <<<
Comments
Post a Comment