Skip to content

Instantly share code, notes, and snippets.

@allenday
Created June 19, 2018 07:02
Show Gist options
  • Save allenday/93c9145411cec05456fb72a0ed3eb363 to your computer and use it in GitHub Desktop.
Save allenday/93c9145411cec05456fb72a0ed3eb363 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use IO::Socket::INET;
$IO::Socket::INET::DEBUG=2;
#Credit to Gisle Aas
#http://www.farhadsaberi.com/perl/2012/01/http-post-stream-upload-file-chunked-transfer.html
my $verbose = 1;
my $server = shift;
my $url = shift;
my $port = 80;
my $src_file = shift;
open( FH, "<", "$src_file" ) || die "create: can't open src file $src_file: $!\n";
binmode FH;
my $sock;
if( !($sock = IO::Socket::INET->new(PeerAddr => $server,
PeerPort => $port,
Proto => 'tcp')) ) {
die "unable to create socket: ". &IO::Socket::INET::errstr . "\n";
}
else {
print "create: socket connected to ${server}:${port}\n" if ($verbose > 0);
}
binmode $sock;
$sock->print( "POST $url HTTP/1.1\n" );
$sock->print( "Host: ${server}:${port}\n" );
$sock->print( "Content-Type: application/octet-stream\n" );
$sock->print( "farhadsaberi_com_auth: ". $auth_string ."\n" );
$sock->print( "Any_Other_Header: Some more Header info here for ya ... \n" );
$sock->print( "Transfer-Encoding: chunked\n\n" );
my $filebuf;
while ( (my $bytes = read( FH, $filebuf, 8192 )) > 0 ) {
my $hex = sprintf( "%X", $bytes );
unless ( $sock->print($hex) ) {
warn "Error printing to socket " . &IO::Socket::INET::errstr . "\n";
return 0;
}
$sock->print( "\r\n" );
unless ( $sock->print( $filebuf ) ) {
warn "Error printing to socket " . &IO::Socket::INET::errstr . "\n";
return 0;
}
$sock->print( "\r\n" );
}
$sock->print( "0\r\n" ) || return 0;
$sock->print( "\r\n" ) || return 0;
my @buf = $sock->getlines();
close( FH );
$sock->close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment