summaryrefslogtreecommitdiffstats
path: root/Bugzilla/ModPerl/Hostage.pm
blob: a3bdfac58aa01b1dc7778e32830ac1ca5c8a07a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package Bugzilla::ModPerl::Hostage;
use 5.10.1;
use strict;
use warnings;

use Apache2::Const qw(:common); ## no critic (Freenode::ModPerl)

sub _attachment_root {
    my ($base) = @_;
    return undef unless $base;
    return $base =~ m{^https?://(?:bug)?\%bugid\%\.([a-zA-Z\.-]+)}
        ? $1
        : undef;
}

sub _attachment_host_regex {
    my ($base) = @_;
    return undef unless $base;
    my $val   = $base;
    $val =~ s{^https?://}{}s;
    $val =~ s{/$}{}s;
    my $regex = quotemeta $val;
    $regex =~ s/\\\%bugid\\\%/\\d+/g;
    return qr/^$regex$/s;
}

sub handler {
    my $r = shift;
    state $urlbase               = Bugzilla->localconfig->{urlbase};
    state $urlbase_uri           = URI->new($urlbase);
    state $urlbase_host          = $urlbase_uri->host;
    state $urlbase_host_regex    = qr/^bug(\d+)\.\Q$urlbase_host\E$/;
    state $attachment_base       = Bugzilla->localconfig->{attachment_base};
    state $attachment_root       = _attachment_root($attachment_base);
    state $attachment_host_regex = _attachment_host_regex($attachment_base);

    my $hostname  = $r->hostname;
    return OK if $hostname eq $urlbase_host;

    my $path = $r->uri;
    return OK if $path eq '/__lbheartbeat__';

    if ($attachment_base && $hostname eq $attachment_root) {
        $r->headers_out->set(Location => $urlbase);
        return REDIRECT;
    }
    elsif ($attachment_base && $hostname =~ $attachment_host_regex) {
        if ($path =~ m{^/attachment\.cgi}s) {
            return OK;
        } else {
            my $new_uri = URI->new($r->unparsed_uri);
            $new_uri->scheme($urlbase_uri->scheme);
            $new_uri->host($urlbase_host);
            $r->headers_out->set(Location => $new_uri);
            return REDIRECT;
        }
    }
    elsif (my ($id) = $hostname =~ $urlbase_host_regex) {
        my $new_uri = $urlbase_uri->clone;
        $new_uri->path('/show_bug.cgi');
        $new_uri->query_form(id => $id);
        $r->headers_out->set(Location => $new_uri);
        return REDIRECT;
    }
    else {
        $r->headers_out->set(Location => $urlbase);
        return REDIRECT;
    }
}

1;