diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2014-10-10 19:27:46 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2014-10-10 19:27:46 +0300 |
commit | 8ff3e5093c0ce807460cbc6b7d212622a112825d (patch) | |
tree | ef38cbbbfc90cf2a463afb4dcdfe37471c436b3e /whoseip | |
parent | 16ee098b02aa7a799c632d004f35278a83aec1df (diff) | |
download | dnstools-8ff3e5093c0ce807460cbc6b7d212622a112825d.tar.gz dnstools-8ff3e5093c0ce807460cbc6b7d212622a112825d.tar.bz2 |
whoseip: implement fmt= (Fast)CGI parameter
Diffstat (limited to 'whoseip')
-rw-r--r-- | whoseip/whoseip.pl | 46 |
1 files changed, 31 insertions, 15 deletions
diff --git a/whoseip/whoseip.pl b/whoseip/whoseip.pl index 51ea369..ca6ce17 100644 --- a/whoseip/whoseip.pl +++ b/whoseip/whoseip.pl @@ -563,16 +563,27 @@ sub docgi { if ($env->{QUERY_STRING} =~ /^$ipv4rx$/) { $term = $env->{QUERY_STRING}; } else { - my @res = grep(/^ip=$ipv4rx$/, split(/\&/, $env->{QUERY_STRING})); - if ($#res == 0) { - $term = $res[0]; - $term =~ s/ip=//; - } + my %q = map { /(.+?)=(.*)/ ? ($1 => $2) : ($1 => 1); } + split(/\&/, $env->{QUERY_STRING}); + if (defined($q{fmt})) { + if (defined($fmtab{$q{fmt}})) { + if ($fmtab{$q{fmt}} =~ /^Content-Type:/) { + $fmt = $fmtab{$q{fmt}}; + } else { + %res = (status => 'BAD', diag => 'invalid format') + } + } else { + %res = (status => 'BAD', diag => 'format undefined'); + } + } + $term = $q{ip} if defined($q{ip}); } - if (defined($term)) { - %res = serve($term); - } else { - %res = (status => 'BAD', diag => 'search term invalid or missing'); + unless (defined($res{status})) { + if (defined($term)) { + %res = serve($term); + } else { + %res = (status => 'BAD', diag => 'search term invalid or missing'); + } } format_out($fmt, %res); } @@ -656,7 +667,7 @@ if ($fastcgi) { $output_format = $fmtab{cgi} unless defined($output_format); my $req = FCGI::Request(); - while($req->Accept() >= 0) { + while ($req->Accept() >= 0) { docgi($output_format, $req->GetEnvironment()); } } elsif ($ENV{GATEWAY_INTERFACE} =~ m#CGI/\d+\.\d+#) { @@ -710,14 +721,20 @@ B<whoiseip> For each IP address, B<whoseip> returns the country it is located in (a ISO 3166-1 code), the network it belongs to and the number of addresses -in this network. +in the network. The program can operate in several modes. If the program name ends in B<.fcgi> the B<Fast CGI> mode is enabled. This mode is also enabled if the command line option B<--fastcgi> is given without arguments, or if the program name ends in one of the -suffixes supplied in its argument (a whitespace-separated list). +suffixes supplied in the argument to this option (a whitespace-separated +list). In this mode, the the IP address to look for is taken from the +parameter B<ip>. Additional parameter B<fmt> can be used to supply the +name of the desired output format. The format must be either one of +the built-in formats, or must be defined using the B<--define-format> +option (see below). As a shortcut, the invocation command line containing +an IP alone is also recognized. Otherwise, when one or more IP addresses are given in the command line, B<whoseip> prints the data for each of them on the standard output. @@ -725,9 +742,8 @@ This is B<command line> mode. If called without arguments, the program checks if the environment variable B<GATEWAY_INTERFACE> is defined and contains B<CGI/I<V>> (where I<V> is the -version number). If so, it assumes B<CGI> mode. In this mode, the IP -address to query for is taken from the environment variable QUERY_STRING. -It can contain either IP address alone, or prefixed with B<ip=>. +version number). If so, it assumes B<CGI> mode. In this mode the command +line is parsed the same way as in B<Fast CGI> mode. If B<GATEWAY_INTERFACE> is not set, the program reads IP addresses from input (one per line) and prints replies for each of them. This is B<inetd |