Fixed POD to have one-line summary
[catagits/Catalyst-TraitFor-Request-ProxyBase.git] / lib / Catalyst / TraitFor / Request / ProxyBase.pm
1 package Catalyst::TraitFor::Request::ProxyBase;
2 use Moose::Role;
3 use URI ();
4 use namespace::autoclean;
5
6 our $VERSION = '0.000001';
7
8 requires qw/
9     base
10     secure
11 /;
12
13 around 'base' => sub {
14     my ($orig, $self, @args) = @_;
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
24 around '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);
30 };
31
32 1;
33
34 __END__
35
36 =head1 NAME
37
38 Catalyst::TraitFor::Request::ProxyBase - Replace request base with value passed by HTTP proxy
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
55     __PACKAGE__->setup;
56
57 =head1 DESCRIPTION
58
59 This module is a L<Moose::Role> which allows you more flexibility in your
60 application's deployment configurations when deployed behind a proxy.
61
62 The problem is that there is no standard way for a proxy to tell a backend
63 server what the original URI for the request was, or if the request was
64 initially SSL. (Yes, I do know about C<< X-Forwarded-Host >>, but they don't
65 do enough)
66
67 This creates an issue for someone wanting to deploy the same cluster of
68 application servers behind various URI endpoints.
69
70 Using this module, the request base (C<< $c->req->base >>)
71 is replaced with the contents of the C<< X-Request-Base >> header,
72 which 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
78 This value will then be used as the base for uris constructed by
79 C<< $c->uri_for >>.
80
81 =head1 REQUIRED METHODS
82
83 =over
84
85 =item base
86
87 =item secure
88
89 =back
90
91 =head1 WRAPPED METHODS
92
93 =over
94
95 =item base
96
97 =item secure
98
99 =back
100
101 =head1 BUGS
102
103 Probably. Patches welcome, please fork from:
104
105     http://github.com/bobtfish/catalyst-traitfor-request-proxybase
106
107 and send a pull request.
108
109 =head1 AUTHOR
110
111 Tomas Doran (t0m) C<< <bobtfish@bobtfish.net> >>
112
113 =head1 COPYRIGHT
114
115 This module is Copyright (c) 2009 Tomas Doran and is licensed under the same
116 terms as perl itself.
117
118 =cut
119