[LON-CAPA-cvs] cvs: loncom /lonnet/perl londns.pm lonnet.pm

raeburn raeburn at source.lon-capa.org
Sun Jun 6 19:14:18 EDT 2021


raeburn		Sun Jun  6 23:14:18 2021 EDT

  Modified files:              
    /loncom/lonnet/perl	londns.pm lonnet.pm 
  Log:
  - Read from the filesystem instead of using LWP request when current server
    is an "authoritative" server in hosts.tab, to prevent delays on Apache 
    reload for standalone machines. 
  
  
Index: loncom/lonnet/perl/londns.pm
diff -u loncom/lonnet/perl/londns.pm:1.6 loncom/lonnet/perl/londns.pm:1.7
--- loncom/lonnet/perl/londns.pm:1.6	Mon Jul 30 14:39:51 2018
+++ loncom/lonnet/perl/londns.pm	Sun Jun  6 23:14:18 2021
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # A debugging harness.
 #
-# $Id: londns.pm,v 1.6 2018/07/30 14:39:51 raeburn Exp $
+# $Id: londns.pm,v 1.7 2021/06/06 23:14:18 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -41,6 +41,7 @@
     }
     if (open(my $config,"<","$dir/$file")) {
         my $contents = join('',<$config>);
+        close($config);
         $r->content_type($type);
         $r->send_http_header;
         return OK if $r->header_only;
@@ -63,18 +64,15 @@
     }
 
     my $command = (split('/',$r->uri))[3];
-    my $dir = $Apache::lonnet::perlvar{'lonTabDir'};
-    if ($command eq 'hosts') {
-	return &serve_file($r,$dir,'dns_hosts.tab','loncapa/hosts');
-    } elsif ($command eq 'domain') {
-	return &serve_file($r,$dir,'dns_domain.tab','loncapa/domain');
-    } elsif ($command eq 'checksums') {
-        my $version = (split('/',$r->uri))[4];
-        return &serve_file($r,$dir,"dns_checksums/$version.tab",'loncapa/versions');
-    } elsif ($command eq 'loncapaCRL') {
-        $dir = $Apache::lonnet::perlvar{'lonCertificateDirectory'};
-        return &serve_file($r,$dir,$Apache::lonnet::perlvar{'lonnetCertRevocationList'},
-                           'application/x-pem-file');
+    my ($dir,$file) = &Apache::lonnet::parse_getdns_url($command,$r->uri);
+    my %types = (
+                    hosts      => 'loncapa/hosts',
+                    domain     => 'loncapa/domain',
+                    checksums  => 'loncapa/versions',
+                    loncapaCRL => 'application/x-pem-file',
+                );
+    if (exists($types{$command})) {
+        return &serve_file($r,$dir,$file,$types{$command});
     }
     return FORBIDDEN;
 }
Index: loncom/lonnet/perl/lonnet.pm
diff -u loncom/lonnet/perl/lonnet.pm:1.1455 loncom/lonnet/perl/lonnet.pm:1.1456
--- loncom/lonnet/perl/lonnet.pm:1.1455	Tue May 11 17:51:23 2021
+++ loncom/lonnet/perl/lonnet.pm	Sun Jun  6 23:14:18 2021
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # TCP networking package
 #
-# $Id: lonnet.pm,v 1.1455 2021/05/11 17:51:23 raeburn Exp $
+# $Id: lonnet.pm,v 1.1456 2021/06/06 23:14:18 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -97,6 +97,7 @@
 use Math::Random;
 use File::MMagic;
 use Net::CIDR;
+use Sys::Hostname::FQDN();
 use LONCAPA qw(:DEFAULT :match);
 use LONCAPA::Configuration;
 use LONCAPA::lonmetadata;
@@ -8572,7 +8573,6 @@
                    my ($start,$end) = split(/\./,$env{$envkey});
                    next unless (($now >= $start) && (!$end || $end > $now));
                }
-               
                my $expiretime=600;
                if ($env{'request.role'} eq $roleid) {
 		  $expiretime=120;
@@ -14680,14 +14680,33 @@
     }
     while (%alldns) {
 	my ($dns) = sort { $b cmp $a } keys(%alldns);
-	my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
-        my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar,30,0);
-        delete($alldns{$dns});
-	next if ($response->is_error());
+        my ($contents, at content);
+        if ($dns eq Sys::Hostname::FQDN::fqdn()) {
+            my $command = (split('/',$url))[3];
+            my ($dir,$file) = &parse_getdns_url($command,$url);
+            delete($alldns{$dns});
+            next if (($dir eq '') || ($file eq ''));
+            if (open(my $config,'<',"$dir/$file")) {
+                @content = <$config>);
+                close($config);
+            }
+            if ($url eq '/adm/dns/loncapaCRL') {
+                $contents = join('', at content);
+            }
+        } else {
+	    my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
+            my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar,30,0);
+            delete($alldns{$dns});
+	    next if ($response->is_error());
+            if ($url eq '/adm/dns/loncapaCRL') {
+                $contents = $response->content;
+            } else {
+                @content = split("\n",$response->content);
+            }
+        }
         if ($url eq '/adm/dns/loncapaCRL') {
-            return &$func($response);
+            return &$func($contents);
         } else {
-	    my @content = split("\n",$response->content);
 	    unless ($nocache) {
 	        &do_cache_new('dns',$url,\@content,30*24*60*60);
 	    }
@@ -14775,14 +14794,14 @@
 }
 
 sub save_crl_pem {
-    my ($response) = @_;
+    my ($content) = @_;
     my ($msg,$hadchanges);
-    if (ref($response)) {
+    if ($content ne '') {
         my $now = time;
         my $lonca = $perlvar{'lonCertificateDirectory'}.'/'.$perlvar{'lonnetCertificateAuthority'};
         my $tmpcrl = $tmpdir.'/'.$perlvar{'lonnetCertRevocationList'}.'_'.$now.'.'.$$.'.tmp';
         if (open(my $fh,'>',"$tmpcrl")) {
-            print $fh $response->content;
+            print $fh $content;
             close($fh);
             if (-e $lonca) {
                 if (open(PIPE,"openssl crl -in $tmpcrl -inform pem -CAfile $lonca -noout 2>&1 |")) {
@@ -14843,6 +14862,24 @@
     return ($msg,$hadchanges);
 }
 
+sub parse_getdns_url {
+    my ($command,$url) = @_;
+    my $dir = $perlvar{'lonTabDir'};
+    my $file;
+    if ($command eq 'hosts') {
+        $file = 'dns_hosts.tab';
+    } elsif ($command eq 'domain') {
+        $file = 'dns_domain.tab';
+    } elsif ($command eq 'checksums') {
+        my $version = (split('/',$url))[4];
+        $file = "dns_checksums/$version.tab",
+    } elsif ($command eq 'loncapaCRL') {
+        $dir = $perlvar{'lonCertificateDirectory'};
+        $file = $perlvar{'lonnetCertRevocationList'};
+    }
+    return ($dir,$file);
+}
+
 # ------------------------------------------------------------ Read domain file
 {
     my $loaded;




More information about the LON-CAPA-cvs mailing list