diff options
author | Sergey Poznyakoff <gray@gnu.org> | 2020-04-06 17:29:30 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org> | 2020-04-06 17:36:23 +0300 |
commit | 3f33d0d3f0cdb8cf98a55806d181434e16420439 (patch) | |
tree | 6b0aad49571a67fb8349ff6c7dc5926b1f0f3454 | |
parent | 4f0894c3137ef7d7045909947021a8d1d7759b81 (diff) | |
download | makeredirect-3f33d0d3f0cdb8cf98a55806d181434e16420439.tar.gz makeredirect-3f33d0d3f0cdb8cf98a55806d181434e16420439.tar.bz2 |
rewrite: fix split vs. monolithic mode selection
* lib/MakeRedirect/Output.pm (getopt): Store options in
$self->{option} hash.
* lib/MakeRedirect/Output/rewrite.pm: Remove the heuristics for
determining the mode. The monolithic mode is the default.
* t/05_rewrite.t: New test.
* t/06_rewrite.t: New test.
-rw-r--r-- | lib/MakeRedirect/Output.pm | 2 | ||||
-rw-r--r-- | lib/MakeRedirect/Output/rewrite.pm | 58 | ||||
-rwxr-xr-x | makeredirect | 1 | ||||
-rw-r--r-- | t/05_rewrite.t | 36 | ||||
-rw-r--r-- | t/06_rewrite.t | 44 |
5 files changed, 111 insertions, 30 deletions
diff --git a/lib/MakeRedirect/Output.pm b/lib/MakeRedirect/Output.pm index 4f54d4a..4a180a7 100644 --- a/lib/MakeRedirect/Output.pm +++ b/lib/MakeRedirect/Output.pm @@ -120,7 +120,7 @@ sub getopt { } elsif (ref($opts{$k})) { $optdef{$k} = $opts{$k}; } else { - $optdef{$k} = \$self->{$opts{$k}}; + $optdef{$k} = \$self->{options}{$opts{$k}}; } } GetOptions(%optdef) or die "command line parsing error"; diff --git a/lib/MakeRedirect/Output/rewrite.pm b/lib/MakeRedirect/Output/rewrite.pm index a2dc370..f9ed7c8 100644 --- a/lib/MakeRedirect/Output/rewrite.pm +++ b/lib/MakeRedirect/Output/rewrite.pm @@ -12,22 +12,29 @@ sub new { local %_ = @_; my $monolithic = delete $_{monolithic}; my $self = $class->SUPER::new(%_); - $self->{monolithic} = $monolithic || (!$self->{fh} && !$self->{file}); + $self->{monolithic} = $monolithic // 1; $self } sub getopt { my $self = shift; $self->SUPER::getopt( - 'm|monolithic' => 'monolithic', - 'o|output' => 'file', + 'm|monolithic' => sub { + my ($self,undef,$val) = @_; + $self->{monolithic} = 1; + }, + 's|split' => sub { + my ($self,undef,$val) = @_; + $self->{monolithic} = 0; + }, + 'o|output=s' => sub { + my ($self,undef,$val) = @_; + $self->{file} = $val; + }, 't|temporary' => sub { shift->http_code(302) }, 'p|permanent' => sub { shift->http_code(301) }, 'indent|i=s' => sub { shift->indent($_[1]) } ); - if (!$self->{fh} && !$self->{file}) { - $self->{monolithic} = 1; - } } sub open { @@ -41,8 +48,10 @@ sub ruleset { my ($self, $host, $rules) = @_; my $fh; if (!$self->fh) { - my $filename = File::Spec->catfile($self->{file}, - ($host || 'GENERIC')) . '.conf'; + my $filename = $self->{monolithic} + ? $self->{file} + : File::Spec->catfile($self->{file}, ($host || 'GENERIC')) + . '.conf'; CORE::open($fh, '>', $filename) or croak "can't open output file $filename: $!"; $self->fh($fh); @@ -113,9 +122,10 @@ rewrite - generate mod_rewrite rules B<makeredirect> B<rewrite> [B<-i> I<N>] -[B<-mpt>] +[B<-mpst>] [B<--indent=>I<N>] [B<--monolithic>] +[B<--split>] [B<--permanent>] [B<--temporary>] I<FILE> ... @@ -127,7 +137,7 @@ B<mod_rewrite> rules. The output can be a I<monolithic> single set of rewrite rules or multiple sets of rules scattered among several output files named by the B<ServerName> they refer to. -In the I<monolithic> output, each line of the source CSV file that contains +In the I<monolithic> mode, each line of the source CSV file that contains a domain name in the column 1 produces a B<RewriteRule> directive preceded by a B<RewriteCond> that limits its scope to the given domain name only. For example, the input line @@ -147,24 +157,12 @@ produces RewriteRule "^/logout" /exit [L,R=301] -The monolithic mode is assumed in the following cases: - -=over 4 - -=item 1. The output file name is not given. - -If the B<--output> option is not supplied, the rules are printed to the -standard output. +The monolithic mode is the default. -=item 2. If the B<--monolithic> (B<-m>) option is given. - -=back - -Otherwise B<rewrite> operates in I<split> mode. In split mode, all rules -for a specific domain are grouped together and saved in a file named by the -domain name with the C<.conf> suffix. The argument to the B<--output> (B<-o>) -option supplies the name of the directory where these files will be stored. -The output directory must exist. +In I<split> mode, all rules for a specific domain are grouped together and +saved in a file named by the domain name with the C<.conf> suffix. The +argument to the B<--output> (B<-o>) option supplies the name of the directory +where these files will be stored. The output directory must exist. =head1 OPTIONS @@ -176,12 +174,16 @@ Indent each output line I<N> characters. =item B<-m>, B<--monolithic> -Enable monolithic mode. +Enable monolithic mode (default). =item B<-p>, B<--permanent> Create permanent redirects (default). +=item B<-s>, B<--split> + +Enable split mode. + =item B<-t>, B<--temporary> Create temporary redirects. diff --git a/makeredirect b/makeredirect index 18ff0df..d887a95 100755 --- a/makeredirect +++ b/makeredirect @@ -45,7 +45,6 @@ makeredirect - create URL redirect rules from CSV B<makeredirect> [B<-o> I<FILE>] [B<-s> I<N>] -[B<--indent=>I<N>] [B<--output=>I<FILE>] [B<--skip=>I<N>] I<module> diff --git a/t/05_rewrite.t b/t/05_rewrite.t new file mode 100644 index 0000000..5db0b31 --- /dev/null +++ b/t/05_rewrite.t @@ -0,0 +1,36 @@ +use lib qw(t lib); +use strict; +use warnings; +use File::Temp; +use Test::More tests => 3; + +use_ok 'MakeRedirect'; + +my $fh = File::Temp->new(); + +my $mr = new MakeRedirect('rewrite', fh => $fh); +ok($mr->read(\*DATA, filename => $0), 'read input'); +$mr->output; + +my $expect = q{RewriteRule "^/foo(/.*)?" /bar$1 [L,R=301] +RewriteCond %{HTTP_HOST} =example.com [NC] +RewriteRule "^/app(/.*)?" http://example.com$1 [L,R=301] +RewriteCond %{HTTP_HOST} =example.com [NC] +RewriteRule "^(.*)" http://example.com/static$1 [L,R=301] +RewriteCond %{HTTP_HOST} =example.org [NC] +RewriteRule "^/foo/bar(/.*)?" http://example.com/BAR$1 [L,R=301] +RewriteCond %{HTTP_HOST} =example.org [NC] +RewriteRule "^/foo(/.*)?" http://example.com/other$1 [L,R=301] +}; + +local $/ = undef; +$fh->seek(0, 0); +my $text = <$fh>; +is($text, $expect, "monolithic output"); + +__DATA__ +/foo,/bar +http://example.org/foo,http://example.com/other +http://example.org/foo/bar,http://example.com/BAR +http://example.com/app,http://example.com +http://example.com,http://example.com/static diff --git a/t/06_rewrite.t b/t/06_rewrite.t new file mode 100644 index 0000000..4dd13ed --- /dev/null +++ b/t/06_rewrite.t @@ -0,0 +1,44 @@ +use lib qw(t lib); +use strict; +use warnings; +use File::Temp; +use File::Basename; +use Test::More tests => 12; + +use_ok 'MakeRedirect'; + +my $dir = File::Temp->newdir(); +my $mr = new MakeRedirect('rewrite', file => $dir, monolithic => 0); +ok($mr->read(\*DATA, filename => $0), 'read input'); +$mr->output; + +my %files = ( + 'GENERIC.conf' => q{RewriteRule "^/foo(/.*)?" /bar$1 [L,R=301] +}, + 'example.com.conf' => q{# Host example.com +RewriteRule "^/app(/.*)?" http://example.com$1 [L,R=301] +RewriteRule "^(.*)" http://example.com/static$1 [L,R=301] +}, + 'example.org.conf' => q{# Host example.org +RewriteRule "^/foo/bar(/.*)?" http://example.com/BAR$1 [L,R=301] +RewriteRule "^/foo(/.*)?" http://example.com/other$1 [L,R=301] +}); + +is_deeply([sort grep { exists($files{$_}) } map { basename($_) } glob "$dir/*"], + [sort keys %files], + "directory listing"); + +foreach my $file (sort keys %files) { + my $name = File::Spec->catfile($dir, $file); + ok(-f $name, "$file exists") or next; + ok(open(FH, '<', $name),"$file open") or next; + local $/ = undef; + is(<FH>, $files{$file}, "$file content"); + close FH; +} +__DATA__ +/foo,/bar +http://example.org/foo,http://example.com/other +http://example.org/foo/bar,http://example.com/BAR +http://example.com/app,http://example.com +http://example.com,http://example.com/static |