#!/usr/bin/perl -w
use HTTP::GHTTP;
use Getopt::Long;
$|=1;
use strict;
use vars qw/$VERSION/;

$VERSION = '1.0';

my @getopt_args = qw(
        p=s  P   H=s@   u   U
        s    e      d   v
        h    V
        );

my %options;

Getopt::Long::config("noignorecase", "bundling");
unless (GetOptions(\%options, @getopt_args)) {
    usage();
}

if ($options{V}) {
    print <<EOT;
This is g-request version $VERSION

Copyright 2000, AxKit.com Ltd

EOT
}

usage() if $options{h} || !@ARGV;

$options{u} = 1 if $options{U};

unless($options{P}) {
    $options{p} ||= $ENV{http_proxy};
}

my $r = HTTP::GHTTP->new();

$r->set_header(Connection => 'close');

for my $extra_header (@{ $options{H} || [] }) {
    my ($name, $value) = split /:\s*/, $extra_header, 2;
    $r->set_header($name, $value);
}

$r->set_proxy($options{p});

my $URI = shift @ARGV;

$r->set_uri($URI);

$r->process_request();

if (!$options{d}) { print $r->get_body; }

sub usage {
    print <<EOT;
Usage: g-request [-options] <url>
    -p <proxy>    Use this as a proxy server
    -P            Don't pick up proxy settings from environment
    -H <header>   Send this HTTP header (you can specify several)
    -u            Display method and URL before any response
    -U            Display request headers (implies -u)
    -s            Display response status code
    -e            Display response headers
    -d            Do not display content
    -v            Be verbose
    -h            Print this help message
    -V            Show program version
EOT
    exit; #'
}

