Changeset 31514


Ignore:
Timestamp:
2017-03-14T18:54:14+13:00 (7 years ago)
Author:
ak19
Message:
  1. As advised by Dr Bainbridge, using open() instead of backtick operator to capture output when running wget command from servercontrol::config(). Tmpfile not created in randomly named subdir, as the tmpfile name itself is guaranteed to not have a naming clash. 2. Modifications to util in previous commit (rev 31513) were unnecessary. Putting them back. 3. Util's get-full-greenstone-url-prefix function is now also agnostic to http and https.
Location:
main/trunk/greenstone2/perllib
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/perllib/servercontrol.pm

    r31513 r31514  
    140140
    141141    my $wget_file_path = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'}, "bin", $ENV{'GSDLOS'}, "wget");
    142     my $tmpfilename = &util::get_tmp_filename(".html", 1); # random file name with html extension in timestamped-tmpdir location (2nd param=1)
    143                                                             # wherein we'll store the HTML page retrieved by wget
    144        
     142    my $tmpfilename = &util::get_tmp_filename(".html"); # random file name with html extension in tmp location in which we'll store the HTML page retrieved by wget
     143   
    145144    # https://www.gnu.org/software/wget/manual/wget.html
    146145    # output-document set to - (STDOUT), so page is streamed to STDOUT
     
    154153   
    155154    ##print STDERR "@@@@ $wgetCommand\n";
    156 
    157     my $response_content = `$wgetCommand`;
     155   
     156    my $response_content;
    158157    my $response_code = undef;
     158    #my $response_content = `$wgetCommand`; # Dr Bainbridge advises against using backticks for running a process. If capturing std output, use open():
     159    if (open(PIN, "$wgetCommand |")) {
     160        while (defined (my $perl_output_line = <PIN>)) {
     161            $response_content = $response_content . $perl_output_line;
     162        }
     163        close(PIN);
     164    } else {
     165        print STDERR "servercontrol.pm::config() failed to run $wgetCommand\n";
     166    }
     167   
     168   
    159169    my @lines = split( /\n/, $response_content );
    160170    foreach my $line (@lines) {
     
    182192        # server not running
    183193        $self->print_msg("*** Server not running. $library_url$command\n", 3);
    184         &util::rm_tmp_file($tmpfilename); # will also remove any randomly named tmp directories containing the file
     194        &FileUtils::removeFiles($tmpfilename); # get rid of the ping response's temporary html file we downloaded
    185195        return 0;
    186196    }
     
    197207        sysread(FIN, $resultstr, -s FIN);       
    198208        close(FIN);
    199         &util::rm_tmp_file($tmpfilename); # will also remove any randomly named tmp directories containing the file
     209        &FileUtils::removeFiles($tmpfilename); # get rid of the ping response's temporary html file we downloaded
    200210       
    201211       
  • main/trunk/greenstone2/perllib/util.pm

    r31513 r31514  
    276276{
    277277    my $file_ext = shift(@_) || undef;
    278     my $use_timestamped_tmp_dir = shift(@_) || 0; # else defaults to using super directory named "tmp"
    279278
    280279    my $opt_dot_file_ext = "";
     
    291290    }
    292291
    293     my $tmpdir = $use_timestamped_tmp_dir ? &util::get_timestamped_tmp_folder() : &util::determine_tmp_dir(0);
     292    my $tmpdir = &util::determine_tmp_dir(0);
    294293
    295294    my $count = 1000;
     
    387386
    388387    return $tmp_dirname;
    389 }
    390 
    391 sub rm_tmp_file
    392 {
    393     my $tmpfilename = shift(@_);
    394    
    395     my $tmpdir = &util::get_parent_folder($tmpfilename);
    396    
    397     &FileUtils::removeFiles($tmpfilename); 
    398     # if the containing folder is a randomly named temporary subdirectory, remove it
    399     # but not if it is the temporary "tmp" super directory itself.
    400     if($tmpdir !~ m@^tmp(\\|\/)*$@) {
    401         &FileUtils::removeFilesRecursive($tmpdir);
    402     }
    403388}
    404389
     
    12981283        my $host = $uri->host;
    12991284        #print STDERR "@@@@@ host: $host\n";
    1300         if($url =~ m/http:\/\/$host(\/)?$/) {
     1285        if($url =~ m/https?:\/\/$host(\/)?$/) {
    13011286            #if($url !~ m/http:\/\/$host:$portnumber(\/)?/ || $url =~ m/http:\/\/$host(\/)?$/) {
    13021287            # (if the URL does not contain the portnumber, OR if the port is implicitly 80 and)                 
     
    13381323        while (defined (my $perl_output_line = <PIN>)) {
    13391324
    1340         if($perl_output_line =~ m@http:\/\/(\S*)@) { # grab all the non-whitespace chars
    1341             $url="http://".$1;
     1325        if($perl_output_line =~ m@(https?):\/\/(\S*)@) { # grab all the non-whitespace chars
     1326            $url="$1://".$2; # preserve the http protocol #$url="http://".$1;
    13421327        }
    13431328        }
     
    13451330    } else {
    13461331        print STDERR "util::get_library_URL() failed to run $perl_command to work out library URL for $gs_mode\n";
    1347     }       
     1332    }
    13481333    }
    13491334   
Note: See TracChangeset for help on using the changeset viewer.