Rev 7 | Rev 14 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
#! /usr/bin/perl -w
# Program: upload_http.pl
# Copyright (C) 2016 R. W. Rodolico
#
# Description
# Script will upload file passed on STDIN to URL defined in $server
# using variable name $urlVarName.
# Filename may also be passed on command line
# It assumes the remote server will return the exact contents which
# is then used to compare to what was sent
# It will return the following codes
# 0 - success
# 1 - Could not find LWP::Simple module
# 2 - file not passed on STDIN
# 3 - Contents returned by server do not match our local copy
# 4 - URL call returned a "not found" error
# 5 - a filename was passed on the cli but was not able to be opened
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
eval {
use LWP::Simple;
};
if ( $@ ) {
print STDERR "Could not load library LWP::Simple\n";
exit 1; # error 1 means we could not find LWP::Simple
}
sub urlEncode {
my $contents = shift;
# escape report for URI
eval { # let's use URI::Escape if it is installed
use URI::Escape;
};
if ( ! $@ ) { # we loaded URI::Escape
return uri_escape( $contents );
} else {
# nope, URI::Escape was not installed (or died)
# so use our home grown thing
# kind of a funky way to write it, but it should be very, very fast
# will take the document passed and split it into array of individual
# characters -- split ( '', shift )
# that then will go through map, which converts it to the for %xx
# required for URL encoding IF it is not alphanumeric.
return join( '',
(
map
{ ($_ =~ m/[a-zA-Z0-9]/) ? $_ : '%' . sprintf("%2.2x",ord( $_ ) ) }
split ( '', $contents )
)
);
} # if
}
sub doit {
my ( $parameters, $message ) = @_;
my $url = $$parameters{'URL'} or die "Could not find URL to send to\n";
my $varname = $$parameters{'urlVarName'} or die "Could not find varname for URL\n";
if ( $url && $varname ) {
my result = get( "$url?$varname=" . urlEncode( $message );
if ( $result ) { # we got a response, so validate the transfer
unless ( $result eq $report ) { # report returned was not one we sent
print STDERR "Server did not read file correctly\n";
return 3;
}
} else {
print STDERR "URL $server did not work\n";
return 4; # crud, could not even find the URL
}
return 1;
} else {
print STDERR "Invalid configuration options for upload_http\n";
return 2;
}
}