diff --git a/apache/apache-lib.pl b/apache/apache-lib.pl index 77371dab6..19033cc3c 100755 --- a/apache/apache-lib.pl +++ b/apache/apache-lib.pl @@ -126,7 +126,6 @@ if (&read_file($site_file, \%site)) { # value - Value (possibly with spaces) # members - For type 1, a reference to the array of members # indent - Number of spaces before the name -# comment - Full text for a comment stored as a dummy directive sub parse_config_file { local($fh, @rv, $line, %dummy); @@ -147,22 +146,8 @@ foreach my $d (&get_httpd_defines()) { } while($line = <$fh>) { $line =~ s/\r|\n//g; - if ($line =~ /^(\s*)(#.*)$/) { - # Keep comments in the structure so block rewrites preserve them - local(%dir); - %dir = ('line', $_[1], - 'eline', $_[1], - 'file', $_[2], - 'type', 0, - 'name', 'dummy', - 'comment', $2); - local $indent = $1; - $indent =~ s/\t/ /g; - $dir{'indent'} = length($indent); - push(@rv, \%dir); - $_[1]++; - } - elsif ($line =~ /^\s*<\/(\S+)\s*(.*)>/) { + $line =~ s/^\s*#.*$//g; + if ($line =~ /^\s*<\/(\S+)\s*(.*)>/) { # end of a container directive. This can only happen in a # recursive call to this function $_[1]++; @@ -799,7 +784,7 @@ foreach my $dir (@$dirs) { $line+1, $file); } $dir->{'eline'} = $line; - $line++ if ($dir->{'name'} ne 'dummy' || defined($dir->{'comment'})); + $line++ if ($dir->{'name'} ne 'dummy'); } return $line; } @@ -1945,13 +1930,7 @@ sub directive_lines { my @rv; foreach my $d (@_) { - if ($d->{'name'} eq 'dummy') { - if (defined($d->{'comment'})) { - my $indent = (" " x $d->{'indent'}); - push(@rv, $indent.$d->{'comment'}); - } - next; - } + next if ($d->{'name'} eq 'dummy'); my $indent = (" " x $d->{'indent'}); if ($d->{'type'}) { push(@rv, $indent."<$d->{'name'} $d->{'value'}>"); diff --git a/apache/t/directive-lines.t b/apache/t/directive-lines.t index 152b3aacc..3b9277b22 100644 --- a/apache/t/directive-lines.t +++ b/apache/t/directive-lines.t @@ -28,16 +28,6 @@ print $fh $text; close($fh) || die "Failed to close $file: $!"; } -sub read_text -{ -my ($file) = @_; -open(my $fh, '<', $file) || die "Failed to read $file: $!"; -local $/ = undef; -my $text = <$fh>; -close($fh) || die "Failed to close $file: $!"; -return $text; -} - # Load the Apache module with an isolated Webmin configuration. write_text(File::Spec->catfile($webmin_config, 'config'), "os_type=debian-linux\n". @@ -98,48 +88,4 @@ is($directory->{'eline'}, 15, 'outer block includes the nested closing line'); is($alias->{'line'}, 16, 'following directive starts after the outer block'); is($next, 10 + scalar(@lines), 'returned line follows serialized output'); -# Parsed comments are not Apache directives, but block rewrites must retain -# their text and indentation around nested containers. -my $roundtrip = File::Spec->catfile($tmp, 'comments.conf'); -my $roundtrip_text = - "# outer comment\n". - "\n". - " # nested comment\n". - " \n". - " Require all denied\n". - " \n". - "\n"; -write_text($roundtrip, $roundtrip_text); -open(my $fh, '<', $roundtrip) || die "Failed to read $roundtrip: $!"; -my $line = 0; -my @parsed = main::parse_config_file($fh, $line, $roundtrip); -close($fh) || die "Failed to close $roundtrip: $!"; -is(join("\n", main::directive_lines(@parsed))."\n", $roundtrip_text, - 'comments survive parsing and serialization'); - -# Removing one of two parsed virtual hosts must use spans that include their -# comments, or remnants of the removed block will make Apache invalid. -my $vhosts_file = File::Spec->catfile($tmp, 'vhosts.conf'); -my $first_vhost = - "\n". - " # first comment\n". - " ServerName first.example\n". - "\n"; -my $second_vhost = - "\n". - " # second comment\n". - " ServerName second.example\n". - "\n"; -write_text($vhosts_file, $first_vhost.$second_vhost); -open($fh, '<', $vhosts_file) || die "Failed to read $vhosts_file: $!"; -$line = 0; -my @vhost_config = main::parse_config_file($fh, $line, $vhosts_file); -close($fh) || die "Failed to close $vhosts_file: $!"; -my @vhosts = main::find_directive_struct('VirtualHost', \@vhost_config); -main::save_directive_struct($vhosts[1], undef, - \@vhost_config, \@vhost_config); -main::flush_file_lines($vhosts_file); -is(read_text($vhosts_file), $first_vhost, - 'removing a block also removes all of its comments'); - done_testing();