[Popcon-commits] cvs commit to popularity-contest by pere

pere at cvs.alioth.debian.org pere at cvs.alioth.debian.org
Wed Jul 20 07:48:41 UTC 2005


Update of /cvsroot/popcon/popularity-contest
In directory haydn:/tmp/cvs-serv30578

Modified Files:
	popcon-submit.cgi 
Log Message:
Rewrite HTTP POST receiver to handle both reports in body and in
MIME-encoded upload format.


Index: popcon-submit.cgi
===================================================================
RCS file: /cvsroot/popcon/popularity-contest/popcon-submit.cgi,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- popcon-submit.cgi	6 Jul 2005 17:43:26 -0000	1.2
+++ popcon-submit.cgi	20 Jul 2005 07:48:39 -0000	1.3
@@ -1,30 +1,83 @@
 #!/usr/bin/perl -wT
+#
+# Receive HTTP post request with a file upload, uncompress it if
+# needed, and submit it as an email to the popcon collector.
+#
+# Handle three different submission methods
+#  - simple post message, where the complete body is the popcon report
+#    (used by popcon version 1.30).
+#  - mime-encoded upload with report in compressed form (used by
+#    popcon version 1.31 and newer).
+#  - mime-encoded upload with report in uncompressed form (used by
+#    ubuntu popcon).
 
 use strict;
+use CGI;
+use Compress::Zlib;
 
 my $email='survey at popcon.debian.org';
 
 $ENV{PATH}="";
 
 print "Content-Type: text/plain\n\n";
-if ($ENV{REQUEST_METHOD} ne "POST")
+if (exists $ENV{REQUEST_METHOD} && $ENV{REQUEST_METHOD} ne "POST")
 {
     print "Debian Popularity-Contest HTTP-POST submission URL\n";
     print "Visit http://popcon.debian.org/ for more info.\n";
     exit 0;
 }
-open POPCON, "|/usr/lib/sendmail -oi $email" or die "sendmail";
-open GZIP, '/bin/gzip -dc|' or die "gzip";
-close STDIN;
-open STDIN, "<&GZIP";
 
+# Extract post data, handle both simple and multipart way
+my @entry;
+if (exists $ENV{CONTENT_TYPE} && $ENV{CONTENT_TYPE} =~ m%multipart/form-data%){
+    # New method, used in popularity-contest after 1.30
+    my $query = new CGI;
+    my $fh = $query->upload("popcondata");
+    if ($fh) {
+	my $filename = $query->param("popcondata");
+	my $type = $query->uploadInfo($filename)->{'Content-Type'};
+	if ("text/plain; charset=utf-8" ne $type &&
+	    "application/octet-stream" ne $type) { # Used by ubuntu script
+	    print "Only 'text/plain; charset=utf-8' and 'application/octet-stream' is supported (not $type)!";
+	    die;
+	} else {
+	    my $encoding = $query->uploadInfo($filename)->{'Content-Encoding'};
+	    if ("x-gzip" eq $encoding || "gzip" eq $encoding) {
+		# Uncompress
+		print "Compressed ($encoding) encoding detected.\n";
+		my $data;
+		# $data = join("", <$fh>);
+		my $len = (stat($fh))[7];
+		read $fh, $data, $len;
+		$data = Compress::Zlib::memGunzip($data);
+		@entry = ($data);
+	    } else { # Pass throught
+		print "Identity encoding detected.\n";
+		@entry = <$fh>;
+	    }
+	}
+    } else {
+	print $query->cgi_error;
+	die;
+    }
+} else {
+    # Old submit method, used in popularity-contest version 1.30
+    print "Old method detected.\n";
+    open GZIP, '/bin/gzip -dc|' or die "gzip";
+    close STDIN;
+    open STDIN, "<&GZIP";
+    @entry = <GZIP>;
+}
+
+open POPCON, "|/usr/lib/sendmail -oi $email" or die "sendmail";
 print POPCON <<"EOF";
 To: $email
 Subject: popularity-contest submission
 
 EOF
-print POPCON while(<GZIP>);
+print POPCON @entry;
 close POPCON;
 
-print "Thanks for your submission to Debian Popularity-Contest\n";
+print "Thanks for your submission to Debian Popularity-Contest!\n";
+print "DEBIAN POPCON HTTP-POST OK\n";
 exit 0;




More information about the Popcon-commits mailing list