~kosova/+junk/tuxfamily-twiki

« back to all changes in this revision

Viewing changes to foswiki/tools/geturl.pl

  • Committer: James Michael DuPont
  • Date: 2009-07-18 19:58:49 UTC
  • Revision ID: jamesmikedupont@gmail.com-20090718195849-vgbmaht2ys791uo2
added foswiki

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl -w
 
2
#
 
3
# Simple utility to fetch an HTML page from a server
 
4
# (Utility for Foswiki - The Free and Open Source Wiki, http://foswiki.org/)
 
5
#
 
6
# Copyright (C) 1999 Jon Udell, BYTE
 
7
# Copyright (C) 2000-2007 Peter Thoeny, peter@thoeny.org
 
8
#
 
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.
 
14
#
 
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.
 
18
#
 
19
# As per the GPL, removal of this notice is prohibited.
 
20
 
 
21
use Socket;
 
22
 
 
23
if( ! $ARGV[1] ) {
 
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";
 
27
    exit 1;
 
28
}
 
29
my $host   = $ARGV[0];
 
30
my $url    = $ARGV[1];
 
31
my $port   = $ARGV[2] || "80";
 
32
my $header = $ARGV[3] || "Host: $host";
 
33
print getUrl( $host, $port, $url, $header );
 
34
 
 
35
# =========================
 
36
sub getUrl
 
37
{
 
38
    my ( $theHost, $thePort, $theUrl, $theHeader ) = @_;
 
39
    my $result = '';
 
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: $!";
 
47
    select SOCK; $| = 1;
 
48
    print SOCK $req;
 
49
    while( <SOCK> ) { $result .= $_; }
 
50
    close( SOCK )  or die "close: $!";
 
51
    select STDOUT;
 
52
    return $result;
 
53
}