Perl Client to Upload files to a Web Server

This beast is hard to find: It's Perl code for a client that uploads a file to a web server. It's not a CGI (like 99.9999% of things you'll find on the subject). Very useful indeed - thanks to the Perl Monks for that one.

(By the way, if you have no idea what I'm talking about, don't worry)
Here's the code (stolen from Perl Monks), of course, these days you can use Perl library functions to simplify the whole socket code and HTTP handling. But still, you get the idea...

use MIME::Base64 ();

# put some error checking here eventually
$user = $ARGV[0];
$pass = $ARGV[1];
$file = $ARGV[2];

open(OUT,"|/usr/local/openssl/bin/openssl s_client -v -connect www:443") or die "Failure\n";
binmode OUT;
$bound = "---------------------------2218759766176618872063491442";

# print out header stuff
print OUT "POST /cgi-bin/save HTTP/1.0\n";
print OUT "Referer: https://www/upload.html\n";
print OUT "Connection: Keep-Alive\n";
print OUT "User-Agent: Amelindinator/0.1 [en] (amelinda\\n";
print OUT "Host:\n";
print OUT "Content-type: multipart/form-data; boundary=$bound\n";
print OUT "Content-Length: ";

# calculate content length
open(FILE, "$file") or die "$!";
binmode FILE; # I'm not sure I need this.
local($/) = undef;# slurp
$mime = MIME::Base64::encode();

$len = (length $mime) + (length $user) + (length $pass);
$len += 514;# this is stuff that is constant

# print rest of stuff.
print OUT "$len\n\n";
print OUT "--$bound\n";
print OUT "Content-Disposition: form-data; name=\"user\"\n\n";
print OUT "$user\n";
print OUT "--$bound\n";
print OUT "Content-Disposition: form-data; name=\"pass\"\n\n";
print OUT "$pass\n";
print OUT "--$bound\n";
print OUT "Content-Disposition: form-data; name=\"FILE\"; filename=\"$file\"\n";
print OUT "Content-Transfer-Encoding: BASE64\n\n";
print OUT $mime; # print out the mimencoded bit here
print OUT "--$bound\n";
print OUT "Content-Disposition: form-data; name=\"SUBMIT\"\n\n";
print OUT "Submit\n";
print OUT "--$bound--\n";

# close the stream, silly!

Submitted by coofercat on Sun, 2003-07-27 01:26