File: sitesummary-upload

package info (click to toggle)
sitesummary 0.1.17+deb8u3
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 468 kB
  • ctags: 103
  • sloc: perl: 1,742; sh: 787; makefile: 84
file content (100 lines) | stat: -rwxr-xr-x 2,722 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/perl -w
# Written by Bill Allombert for the Debian popularity-contest project.
# This file is placed in the public domain.
# Rewritten for sitesummary by Petter Reinholdtsen

use strict;
use IO::Socket;
use Getopt::Std;
use File::Basename;

my %opts;
getopts("du:f:", \%opts);

sub usage {
    print <<"EOF";
Usage: $0 [-Cd] [-u <url>] [-f <file>]
  -d        enable debugging
  -u <url>  submit to the given URL (default localhost)
  -f <file> read popcon report from file (default stdin)
EOF
}

my ($submiturl)  = $opts{'u'} || "http://localhost/cgi-bin/sitesummary-collector.cgi";
my ($file)  = $opts{'f'} || "-";

my ($host) = $submiturl =~ m%http://([^/]+)%;

print "Unable to parse url\n" if ($opts{'d'} && ! $host);

# Configure the proxy:
my ($http_proxy,$proxy,$port,$remote);

$http_proxy=$ENV{'http_proxy'};
if (defined($http_proxy) && length($http_proxy))
{
  $http_proxy =~ m{http://([^:]*)(?::([0-9]+))?} 
        or die ("unrecognized http_proxy");
  $proxy=$1; $port=$2;
}
  
$proxy=$host unless (defined($proxy));
$port=80 unless (defined($port));

# Compress the report:
my ($str,$len);
my $encoding;
open FILE, "< $file" or die "reading from '$file'";
$encoding = "identity";
$str .= $_ while(<FILE>); 
close(FILE);
$len = length($str);

# 30 second timeout on http connections
$SIG{ALRM} = sub { die "timeout in sitesummary-upload\n" };
alarm(30);

# Connect to server
$remote = IO::Socket::INET->new(Proto => "tcp", PeerAddr => $proxy, 
                                                PeerPort => $port); 
unless ($remote) { die "cannot connect to $proxy:$port" }

my $boundary = "----------ThIs_Is_tHe_bouNdaRY_\$";

my $basefile = basename($file);

#Content-Length: $len
# text/plain; charset=utf-8
my $ORS = "\r\n"; # Use DOS line endings to make HTTP happy
my $form;
$form .= "--${boundary}$ORS";
$form .= "Content-Disposition: form-data; name=\"sitesummary\"; filename=\"$basefile\"$ORS";
$form .= "Content-Encoding: $encoding$ORS";
$form .= "Content-Type: application/octet-stream$ORS$ORS";
$form .= "$str$ORS";
$form .= "--${boundary}--$ORS";
$form .= "$ORS";

my $formlen = length($form);

#Send data
print $remote "POST $submiturl HTTP/1.1\r\n";
print $remote "User-Agent: sitesummary-upload\r\n";
print $remote "Host: $host\r\n";
print $remote "Content-Type: multipart/form-data; boundary=$boundary\r\n";
print $remote "Content-Length: $formlen\r\n";
print $remote "\r\n";
print $remote "$form";

#Get answer
my($answer)="";
while(<$remote>)
{
  $answer.=$_;
  m/SITESUMMARY HTTP-POST OK/ and last;
}
close ($remote);
#Check answer
my $status = ($answer =~ m/SITESUMMARY HTTP-POST OK/) ? 0 : 1;
print "Failed to upload, answer '$answer'\n" if $status && $opts{'d'};
exit $status;