3
# Simple utility to fetch an HTML page from a server
4
# (Utility for Foswiki - The Free and Open Source Wiki, http://foswiki.org/)
6
# Copyright (C) 1999 Jon Udell, BYTE
7
# Copyright (C) 2000-2007 Peter Thoeny, peter@thoeny.org
9
# This program is free software; you can redistribute it and/or
10
# modify it under the terms of the GNU General Public License
11
# as published by the Free Software Foundation; either version 2
12
# of the License, or (at your option) any later version. For
13
# more details read LICENSE in the root of this distribution.
15
# This program is distributed in the hope that it will be useful,
16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19
# As per the GPL, removal of this notice is prohibited.
24
print "Usage: geturl <host> <path> [<port> [<header>]]\n";
25
print "Example: geturl some.domain /some/dir/file.html 80\n";
26
print "will get: http://some.domain:80/some/dir/file.html\n";
31
my $port = $ARGV[2] || "80";
32
my $header = $ARGV[3] || "Host: $host";
33
print getUrl( $host, $port, $url, $header );
35
# =========================
38
my ( $theHost, $thePort, $theUrl, $theHeader ) = @_;
40
my $req = "GET $theUrl HTTP/1.0\r\n$theHeader\r\nUser-Agent: TWikigeturl.pl\r\n\r\n";
41
my ( $iaddr, $paddr, $proto );
42
$iaddr = inet_aton( $theHost );
43
$paddr = sockaddr_in( $thePort, $iaddr );
44
$proto = getprotobyname( 'tcp' );
45
socket( SOCK, PF_INET, SOCK_STREAM, $proto ) or die "socket: $!";
46
connect( SOCK, $paddr ) or die "connect: $!";
49
while( <SOCK> ) { $result .= $_; }
50
close( SOCK ) or die "close: $!";