A friend of mine asked me to do a simple script that would consume IRC's (RFC 1459) pseudo BNF and map it directly to a GET request that is sent to an application server. This is what I came up within few hours.
It was a real pleasure since I haven't done a lot of www programming with Perl. I am open to all suggestions how this kind of stuff is usually done and all pointers to previous art are appreciated.
#! /usr/bin/perl use strict; use warnings; use HTTP::Lite; use English qw( -no_match_vars ); { usage() if @ARGV != 2; # Note, if you want to define the port the <domain:port> # has to contain trailing slash, eg. # http://foo.bar.com:80/ # http://foo.bar.com:80/my/remote/app my $url = shift; my $bnf = shift; my @items = split(/\s+/, $bnf); my @bnf = (); # Leave the possible 15th parameter untouched. for(my $i = 0; $i < 16; $i++ ) { push @bnf, shift @items; } # The last param may contain whitespaces etc. push @bnf, join(' ', @items); my $ret = http_get_request_from_bnf($url, @bnf); die "Server failed to serve our request '$url' : $ret" if $ret !~ /2\d{1,2}/; print "$ret\n"; } sub http_get_request_from_bnf { my $url = shift; my @bnf_items = @_; my $i = 1; my $prefix = shift @bnf_items; my $command = shift @bnf_items; my $http = new HTTP::Lite; $http->add_req_header('Prefix', $prefix); $http->add_req_header('Command', $command); foreach my $item ( @bnf_items ) { $http->add_req_header("Param$i", $item); $i++; } #use Data::Dumper; #print Dumper($http); my $request = $http->request($url) or die "Unable to create GET request : $OS_ERROR\n"; return $request; } sub usage { print<<EOL; usage: $PROGRAM_NAME <url> <Pseudo BNF command> URL must contain trailing slash and the port number can be included, e +g. http://foo.bar.com:80/ http://www.baz.com:80/my/remote/app EOL exit 1; }
|
|---|