1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
#!/usr/bin/perl -w
# Simple command line web client.
# Initially loosely based on examples from the perlipc manpage.
#
# Copyright (C) 2009 Michael Adam <obnox@samba.org>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, see <http://www.gnu.org/licenses/>.
use strict;
use IO::Socket;
use Getopt::Long;
use Pod::Usage;
my $EOL = "\015\012";
my $VERSION = "0.1";
my $NAME = "Tinyproxy-Web-Client";
my $user_agent = "$NAME/$VERSION";
my $user_agent_header = "User-Agent: $user_agent$EOL";
my $http_version = "1.0";
my $method = "GET";
my $dry_run = 0;
my $help = 0;
my $entity = undef;
my $default_port = "80";
my $port = $default_port;
sub process_options() {
my $result = GetOptions("help|?" => \$help,
"http-version=s" => \$http_version,
"method=s" => \$method,
"dry-run" => \$dry_run,
"entity=s" => \$entity);
die "Error reading cmdline options! $!" unless $result;
pod2usage(1) if $help;
# some post-processing:
}
sub build_request($$$$$$)
{
my ( $host, $port, $version, $method, $document, $entity ) = @_;
my $request = "";
$method = uc($method);
if ($version eq '0.9') {
if ($method ne 'GET') {
die "invalid method '$method'";
}
$request = "$method $document$EOL";
} elsif ($version eq '1.0') {
$request = "$method $document HTTP/$version$EOL"
. $user_agent_header;
} elsif ($version eq '1.1') {
$request = "$method $document HTTP/$version$EOL"
. "Host: $host" . (($port and ($port ne $default_port))?":$port":"") . "$EOL"
. $user_agent_header
. "Connection: close$EOL";
} else {
die "invalid version '$version'";
}
$request .= $EOL;
if ($entity) {
$request .= $entity;
}
return $request;
}
# main
process_options();
unless (@ARGV > 1) {
pod2usage(1);
}
my $hostarg = shift(@ARGV);
my $host = $hostarg;
if ($host =~ /^([^:]+):(.*)/) {
$port = $2;
$host = $1;
}
foreach my $document (@ARGV) {
my $request = build_request($host, $port, $http_version, $method, $document, $entity);
if ($dry_run) {
print $request;
exit(0);
}
my $remote = IO::Socket::INET->new(
Proto => "tcp",
PeerAddr => $host,
PeerPort => $port,
);
unless ($remote) {
die "cannot connect to http daemon on $host (port $port)";
}
$remote->autoflush(1);
print $remote $request;
while (<$remote>) {
print;
}
close $remote;
}
exit(0);
__END__
=head1 webclient.pl
A simple WEB client written in perl.
=head1 SYNOPSIS
webclient.pl [options] host[:port] document [document ...]
=head1 OPTIONS
=over 8
=item B<--help>
Print a brief help message and exit.
=item B<--http-version>
Specify the HTTP protocol version to use (0.9, 1.0, 1.1). Default is 1.0.
=item B<--method>
Specify the HTTP request method ('GET', 'CONNECT', ...). Default is 'GET'.
=item B<--entity>
Add the provided string as entity (i.e. body) to the request.
=item B<--dry-run>
Don't actually connect to the server but print the request that would be sent.
=back
=head1 DESCRIPTION
This is a basic web client. It permits to send http request messages to
web servers or web proxy servers. The result is printed as is to standard output,
including headers. This is meant as a tool for diagnosing and testing
web servers and proxy servers.
=head1 COPYRIGHT
Copyright (C) 2009 Michael Adam <obnox@samba.org>
This program is distributed under the terms of the GNU General Public License
version 2 or above. See the COPYING file for additional information.
=cut
|