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