Actually try to use the code in anger, fix issues
[catagits/Catalyst-TraitFor-Request-ProxyBase.git] / lib / Catalyst / TraitFor / Request / ProxyBase.pm
CommitLineData
1640822e 1package Catalyst::TraitFor::Request::ProxyBase;
30e98937 2use Moose::Role;
f03ff44b 3use URI ();
30e98937 4use namespace::autoclean;
5
9bdb832c 6our $VERSION = '0.000002';
f03ff44b 7
8requires qw/
9 base
10 secure
11/;
30e98937 12
13around 'base' => sub {
14 my ($orig, $self, @args) = @_;
9bdb832c 15 if (my $base = $self->header('X-Request-Base')) {
16 $base .= '/' unless $base =~ m|/$|;
17 @args = (URI->new($base));
f03ff44b 18 }
19 $self->$orig(@args);
20};
21
22around 'secure' => sub {
23 my ($orig, $self, @args) = @_;
24 if (my $base = $self->header('X-Request-Base')) {
25 return URI->new($base)->scheme eq 'http' ? 0 : 1;
26 }
27 $self->$orig(@args);
30e98937 28};
29
301;
31
32__END__
1640822e 33
34=head1 NAME
35
cb930a44 36Catalyst::TraitFor::Request::ProxyBase -
37
38=head1 SYNOPSIS
39
40 package MyApp;
41 use Moose;
42 use namespace::autoclean;
43
44 use Catalyst;
45 use CatalystX::RoleApplicator;
46
47 extends 'Catalyst';
48
49 __PACKAGE__->apply_request_class_roles(qw/
50 Catalyst::TraitFor::Request::ProxyBase
51 /);
52
cb930a44 53 __PACKAGE__->setup;
54
55=head1 DESCRIPTION
56
57This module is a L<Moose::Role> which allows you more flexibility in your
58application's deployment configurations when deployed behind a proxy.
59
60The problem is that there is no standard way for a proxy to tell a backend
61server what the original URI for the request was, or if the request was
62initially SSL. (Yes, I do know about C<< X-Forwarded-Host >>, but they don't
63do enough)
64
65This creates an issue for someone wanting to deploy the same cluster of
66application servers behind various URI endpoints.
67
f03ff44b 68Using this module, the request base (C<< $c->req->base >>)
cb930a44 69is replaced with the contents of the C<< X-Request-Base >> header,
70which is expected to be a full URI, for example:
71
72 http://example.com
73 https://example.com
74 http://other.example.com:81/foo/bar/yourapp
75
76This value will then be used as the base for uris constructed by
77C<< $c->uri_for >>.
78
79=head1 REQUIRED METHODS
80
81=over
82
83=item base
84
f03ff44b 85=item secure
86
cb930a44 87=back
88
89=head1 WRAPPED METHODS
90
f03ff44b 91=over
92
93=item base
cb930a44 94
f03ff44b 95=item secure
96
97=back
cb930a44 98
99=head1 BUGS
100
101Probably. Patches welcome, please fork from:
102
103 http://github.com/bobtfish/catalyst-traitfor-request-proxybase
104
105and send a pull request.
106
107=head1 AUTHOR
108
109Tomas Doran (t0m) C<< <bobtfish@bobtfish.net> >>
110
111=head1 COPYRIGHT
112
113This module is Copyright (c) 2009 Tomas Doran and is licensed under the same
114terms as perl itself.
1640822e 115
116=cut
117