#!/usr/bin/perl
# Copyright (c) 2001 TMAP, NOAA
# ALL RIGHTS RESERVED
#
# Please read the full copyright notice in the file COPYRIGHT
# included with this code distribution
use LWP::UserAgent;
use URI::URL;
use URI::Escape;
use strict;
use Getopt::Long;
use Data::Dumper;
my (%Opts, $Separator);
sub issueRequest {
my ($urlstr, $xml) = @_;
my $hdrs = new HTTP::Headers;
my $url = new URI::URL($urlstr);
my $req = new HTTP::Request('POST', $url, $hdrs);
$req->content_type('application/x-www-form-urlencoded');
$req->content($xml);
my $ua = new LWP::UserAgent;
my ($resp, $loc);
while(1){
$resp = $ua->request($req);
$loc = $resp->header('Location');
last if ! $loc;
$url = new URI::URL($loc);
$req = new HTTP::Request('GET', $url, $hdrs);
}
my $result = $resp->content;
print $result;
}
sub getXMLArgs {
my $str = "";
# Random number to avoid cacheing
$str = '' . rand() . '';
$str .= "long" if $Opts{l};
$str .= "$ARGV[0]" if $ARGV[0];
$str .= "$ARGV[1]" if $ARGV[1];
$str .= "$Separator";
$str;
}
sub genXML {
my $xmlHead = qq{};
my $xmlFoot = qq{};
my $xml = $xmlHead . getXMLArgs . $xmlFoot;
return uri_escape('xml=' . $xml);
}
sub usage {
print STDERR "Usage: ls.pl [-l] [-s separator] url [dataset] [variable]\n";
print STDERR " -l displays descriptive name for a dataset or variable\n";
print STDERR " and the type, units, and bounds of an axis.\n";
print STDERR " -s separator delimit fields with separator\n";
print STDERR "Example: ls.pl http://ferret.pmel.noaa.gov/las-bin/LASserver.pl\n";
exit 1;
}
use Getopt::Std;
getopts('ls:', \%Opts);
$Separator = $Opts{s};
die "-s argument can't contain '&','<', or '>'" if $Separator =~ /[\&\<\>]/;
my $requrl = shift;
if (!defined($requrl) || $#ARGV > 1){
usage;
}
issueRequest($requrl, genXML);