Subversion Repositories camp_sysinfo_client_3

Rev

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
my $urlVarName = 'report';
34
my $server = 'http://10.111.110.72/test/index.php';
35
 
36
eval {
37
   use LWP::Simple;
38
};
39
if ( $@ ) {
40
   print STDERR "Could not load library LWP::Simple\n";
41
   exit 1; # error 1 means we could not find LWP::Simple
42
}
43
 
44
sub urlEncode {
45
   my $contents = shift;
46
   # escape report for URI
47
   eval {   # let's use URI::Escape if it is installed
48
      use URI::Escape;
49
   };
50
   if ( ! $@ ) { # we loaded URI::Escape
51
      return uri_escape( $contents );
52
   } else {
53
      # nope, URI::Escape was not installed (or died) 
54
      # so use our home grown thing
55
      # kind of a funky way to write it, but it should be very, very fast
56
      # will take the document passed and split it into array of individual 
57
      # characters -- split ( '', shift ) 
58
      # that then will go through map, which converts it to the for %xx 
59
      # required for URL encoding IF it is not alphanumeric.
60
      return join( '', 
61
            (
62
               map
63
                  { ($_ =~ m/[a-zA-Z0-9]/) ? $_ :  '%' . sprintf("%2.2x",ord( $_ ) ) } 
64
                  split ( '', $contents ) 
65
            ) 
66
         );
67
   } # if
68
}
69
 
70
# figure out where we are reading from
71
if ( $filename = shift ) { # they passed a file name on the command line
72
   if ( open DATA, $filename ) {
73
      $report = do {local $/=undef; <DATA>}; # join( '',<DATA> )
74
      close DATA;
75
   } else {
76
      print "Unable to open $filename: $!\n" ; 
77
      exit 5; 
78
   };
79
} else {
80
   $report = do {local $/=undef; <>}; #join( '', <> );
81
}
82
 
83
unless ( $report ) { # Could not be read
84
   print STDERR "Could not read report\n";
85
   exit 2;
86
}
87
 
88
# send it to the server and get the result
89
my $result = get( "$server?$urlVarName=" . urlEncode( $report ) );
90
 
91
if ( $result ) { # we got a response, so validate the transfer
92
   unless ( $result eq $report ) { # report returned was not one we sent
93
      print STDERR "Server did not read file correctly\n";
94
      exit 3;
95
   } 
96
} else {
97
   print STDERR "URL $server did not work\n";
98
   exit 4; # crud, could not even find the URL
99
}
100
 
101
print $result; # only used for debuggin
102
 
103
print STDERR "Ok\n";
104
exit 0; # all good