Permalink
Browse files

Make .htredirects work again (hacky API though)

  • Loading branch information...
1 parent 0ef3daa commit 764da1a223cff93df6748a457f7f619ec3d967cc @abh committed Apr 17, 2011
Showing with 137 additions and 118 deletions.
  1. +0 −4 TODO
  2. +6 −5 lib/Combust/App.pm
  3. +1 −1 lib/Combust/Base.pm
  4. +2 −3 lib/Combust/Control.pm
  5. +120 −0 lib/Combust/Control/Redirect.pm
  6. +6 −104 lib/Combust/Redirect.pm
  7. +2 −1 t/app/trivial/docs/live/.htredirects
View
4 TODO
@@ -2,10 +2,6 @@
In rough order of priority
==========================
-Make ApacheRouter configurions work (right now everything falls back
-to the '/' handler).
-
-
--- old stuff below ---
Make tests using the database skip_all if it can't connect to the test db
View
11 lib/Combust/App.pm
@@ -66,9 +66,12 @@ sub app {
my ($self, $env) = @_;
my $request = $self->setup_request($env);
- warn "ENV: ", pp(\$env);
+ #warn "ENV: ", pp(\$env);
- $self->rewriter->rewrite($request) if $self->rewriter;
+ {
+ my $r = $self->rewriter->rewrite($request) if $self->rewriter;
+ return $r if $r;
+ }
my $match = $request->site->router->match($request->env);
@@ -80,12 +83,10 @@ sub app {
my $controller = $match->{controller}->new(request => $request);
- warn "calling controller!";
-
my $r = $controller->run($match->{action} || 'render');
use Data::Dump qw(pp);
- warn "RETURN: ", pp($r);
+ # warn "RETURN: ", pp($r);
return $r;
}
View
2 lib/Combust/Base.pm
@@ -5,7 +5,7 @@ use Combust::Config;
my $config = Combust::Config->new();
has 'request' => (
- is => 'ro',
+ is => 'rw',
required => 0,
);
View
5 lib/Combust/Control.pm
@@ -316,7 +316,7 @@ sub send_output {
$self->request->response->status(200) unless $self->request->response->status;
-$self->request->response->content(ref $output
+ $self->request->response->content(ref $output
&& reftype($output) eq "GLOB" ? $output : [$output]);
my $response_ref = $self->request->response->finalize;
@@ -355,8 +355,7 @@ EOH
# don't care for that feature anyway).
$self->post_process( $data );
- $self->send_output( $data, 'text/html' );
- return DONE;
+ return $self->send_output( $data, 'text/html' );
}
sub cookies {
View
120 lib/Combust/Control/Redirect.pm
@@ -0,0 +1,120 @@
+package Combust::Control::Redirect;
+use Moose;
+extends 'Combust::Control';
+use Combust::Constant qw(DECLINED OK);
+
+my $map = {};
+
+sub redirect_reload {
+ my ($self, $file) = @_;
+
+ warn "Checking file $file";
+
+ my $mtime = (stat($file))[9];
+ unless ($mtime) {
+ #warn "could not find file: $file";
+ delete $map->{$file};
+ return;
+ }
+
+ #warn "mtime: $mtime, last update: ", $map->{$file}->{update};
+
+ return if $map->{$file}->{update}
+ and $map->{$file}->{update} > $mtime;
+
+ $map->{$file}->{update} = time;
+
+ #warn "reloading $file";
+
+ my $site_rules = [];
+ if (open MAP, $file) {
+ while (<MAP>) {
+ #warn ":: $_\n";
+ next unless (my ($regexp, $url, $option) = $_ =~ m/(\S+)\s+(\S+)(?:\s*(\S+))?/);
+ $regexp =~ s/^/\^/ unless $regexp =~ m/^\^/;
+ $regexp =~ s/$/\$/ unless $regexp =~ m/\$$/;
+ $option ||= '';
+ $option = "I" if $option =~ m/^int/i;
+ $option = "P" if $option =~ m/^per/i;
+ $option = "" unless $option =~ m/^[IP]$/;
+ #warn "regexp: $regexp => $url";
+ $regexp = qr/$regexp/;
+ push @{$site_rules}, [$regexp, $url, $option];
+ }
+ close MAP;
+ }
+ else {
+ warn "Could not open url map file $file: $!";
+ }
+ $map->{$file}->{rules} = $site_rules;
+}
+
+my $stat_check = 0;
+my %files;
+
+sub rewrite {
+ my ($self, $request) = @_;
+
+ $self->request($request);
+
+ warn "redirecting!?!";
+
+ my $site = $request->site;
+ my $uri = $request->uri;
+
+ warn join " / ", "REDIRECT CHECK FOR $site", $uri;
+
+ my $path = $self->get_include_path($request);
+ return unless $path and $path->[0];
+
+ my $file;
+
+ if (time - 30 > $stat_check) {
+ %files = ();
+ $stat_check = time;
+ }
+
+ while (1) {
+ my $dir = shift @$path;
+ last unless $dir;
+ $file = "$dir/.htredirects";
+ my $exists = defined $files{$file}
+ ? $files{$file}
+ : $files{$file} = -e $file || 0;
+ last if $exists;
+ }
+
+ #warn "FILE: $file";
+
+ $self->redirect_reload($file);
+ my $conf = $map->{$file} ? $map->{$file}->{rules} : undef;
+
+ #warn Data::Dumper->Dump([\$conf],[qw(conf)]);
+
+ return unless $conf and ref $conf eq "ARRAY";
+
+ for my $c (@$conf) {
+ warn "matching $uri to $c->[0]";
+ if (my @n = ($uri =~ m/$c->[0]/)) {
+ warn "matched";
+ my $url = eval qq["$c->[1]"];
+ warn "URLMAP ERROR: $c->[1]: $@" and next if $@;
+ warn "URL: [$url]";
+ next unless $url;
+ if ($c->[2] eq "I") {
+ warn "rewriting to $url";
+ $request->env->{PATH_INFO} = $url;
+ }
+ else {
+ return $self->redirect($url, $c->[2] eq "P" ? 1 : 0);
+ }
+ }
+ else {
+ warn "no match";
+ }
+ }
+
+}
+
+
+1;
View
110 lib/Combust/Redirect.pm
@@ -1,111 +1,13 @@
package Combust::Redirect;
use Moose::Role;
-# extends 'Combust::Base';
-use Combust::Constant qw(DECLINED OK);
+use Combust::Control::Redirect;
-my $map = {};
-sub redirect_reload {
- my ($self, $file) = @_;
-
- #warn "Checking file $file";
-
- my $mtime = (stat($file))[9];
- unless ($mtime) {
- #warn "could not find file: $file";
- delete $map->{$file};
- return;
- }
-
- #warn "mtime: $mtime, last update: ", $map->{$file}->{update};
-
- return if $map->{$file}->{update}
- and $map->{$file}->{update} > $mtime;
-
- $map->{$file}->{update} = time;
-
- #warn "reloading $file";
-
- my $site_rules = [];
- if (open MAP, $file) {
- while (<MAP>) {
- #warn ":: $_\n";
- next unless (my ($regexp, $url, $option) = $_ =~ m/(\S+)\s+(\S+)(?:\s*(\S+))?/);
- $regexp =~ s/^/\^/ unless $regexp =~ m/^\^/;
- $regexp =~ s/$/\$/ unless $regexp =~ m/\$$/;
- $option ||= '';
- $option = "I" if $option =~ m/^int/i;
- $option = "P" if $option =~ m/^per/i;
- $option = "" unless $option =~ m/^[IP]$/;
- #warn "regexp: $regexp => $url";
- $regexp = qr/$regexp/;
- push @{$site_rules}, [$regexp, $url, $option];
- }
- close MAP;
- }
- else {
- warn "Could not open url map file $file: $!";
- }
- $map->{$file}->{rules} = $site_rules;
-}
-
-my $stat_check = 0;
-my %files;
-
-sub rewrite {
- my ($self, $request) = @_;
-
- my $site = $request->site;
- my $uri = $request->uri;
-
- #warn join " / ", "REDIRECT CHECK FOR $site", $uri;
-
- my $path = $self->get_include_path($request);
- return unless $path and $path->[0];
-
- my $file;
-
- if (time - 30 > $stat_check) {
- %files = ();
- $stat_check = time;
- }
-
- while (1) {
- my $dir = shift @$path;
- last unless $dir;
- $file = "$dir/.htredirects";
- my $exists = defined $files{$file}
- ? $files{$file}
- : $files{$file} = -e $file || 0;
- last if $exists;
- }
-
- #warn "FILE: $file";
-
- $self->redirect_reload($file);
- my $conf = $map->{$file} ? $map->{$file}->{rules} : undef;
-
- #warn Data::Dumper->Dump([\$conf],[qw(conf)]);
-
- return unless $conf and ref $conf eq "ARRAY";
-
- for my $c (@$conf) {
- #warn "matching $uri to $c->[0]";
- if (my @n = ($uri =~ m/$c->[0]/)) {
- my $url = eval qq["$c->[1]"];
- warn "URLMAP ERROR: $c->[1]: $@" and next if $@;
- next unless $url;
- if ($c->[2] eq "I") {
- warn "rewriting to $url";
- $request->env->{PATH_INFO} = $url;
- }
- else {
- return $self->redirect($url, $c->[2] eq "P" ? 1 : 0);
- }
- }
- }
-
-}
+after 'BUILD' => sub {
+ my ($self, $params) = @_;
+ my $rewriter = Combust::Control::Redirect->new();
+ $self->rewriter($rewriter);
+};
1;
View
3 t/app/trivial/docs/live/.htredirects
@@ -1 +1,2 @@
-/not_this /two
+/four /two
+/five /two I

0 comments on commit 764da1a

Please sign in to comment.