Subversion Repositories camp_sysinfo_client_3

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 rodolico 1
#! /usr/bin/perl -w
2
 
3
#    Program: upload_http.pl
4
#    Copyright (C) 2016  R. W. Rodolico
5
#
6
#    Description
7
#      Script will upload file passed on STDIN to URL defined in $server
8
#      using variable name $urlVarName.
9
#      Filename may also be passed on command line
10
#      It assumes the remote server will return the exact contents which
11
#      is then used to compare to what was sent
12
#      It will return the following codes
13
#        0 - success
14
#         1 - Could not find LWP::Simple module
15
#         2 - file not passed on STDIN
16
#         3 - Contents returned by server do not match our local copy
17
#         4 - URL call returned a "not found" error
18
#         5 - a filename was passed on the cli but was not able to be opened
19
#
20
#    This program is free software: you can redistribute it and/or modify
21
#    it under the terms of the GNU General Public License as published by
22
#    the Free Software Foundation, either version 3 of the License, or
23
#    (at your option) any later version.
24
#
25
#    This program is distributed in the hope that it will be useful,
26
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
27
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28
#    GNU General Public License for more details.
29
#
30
#    You should have received a copy of the GNU General Public License
31
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
32
 
33
eval {
34
   use LWP::Simple;
35
};
36
if ( $@ ) {
37
   print STDERR "Could not load library LWP::Simple\n";
38
   exit 1; # error 1 means we could not find LWP::Simple
39
}
40
 
41
sub urlEncode {
42
   my $contents = shift;
43
   # escape report for URI
44
   eval {   # let's use URI::Escape if it is installed
45
      use URI::Escape;
46
   };
47
   if ( ! $@ ) { # we loaded URI::Escape
48
      return uri_escape( $contents );
49
   } else {
50
      # nope, URI::Escape was not installed (or died) 
51
      # so use our home grown thing
52
      # kind of a funky way to write it, but it should be very, very fast
53
      # will take the document passed and split it into array of individual 
54
      # characters -- split ( '', shift ) 
55
      # that then will go through map, which converts it to the for %xx 
56
      # required for URL encoding IF it is not alphanumeric.
57
      return join( '', 
58
            (
59
               map
60
                  { ($_ =~ m/[a-zA-Z0-9]/) ? $_ :  '%' . sprintf("%2.2x",ord( $_ ) ) } 
61
                  split ( '', $contents ) 
62
            ) 
63
         );
64
   } # if
65
}
66
 
13 rodolico 67
sub doit {
68
   my ( $parameters, $message ) = @_;
69
   my $url = $$parameters{'URL'} or die "Could not find URL to send to\n";
70
   my $varname = $$parameters{'urlVarName'} or die "Could not find varname for URL\n";
71
   if ( $url && $varname ) {
72
      my result = get( "$url?$varname=" . urlEncode( $message );
73
 
74
      if ( $result ) { # we got a response, so validate the transfer
75
         unless ( $result eq $report ) { # report returned was not one we sent
76
            print STDERR "Server did not read file correctly\n";
77
            return 3;
78
         } 
79
      } else {
80
         print STDERR "URL $server did not work\n";
81
         return 4; # crud, could not even find the URL
82
      }
83
      return 1;
1 rodolico 84
   } else {
13 rodolico 85
      print STDERR "Invalid configuration options for upload_http\n";
86
      return 2;
87
   }
1 rodolico 88
}