File Coverage

blib/lib/Dancer/Plugin/ProxyPath/Proxy.pm
Criterion Covered Total %
statement 47 48 97.9
branch 12 16 75.0
condition 6 10 60.0
subroutine 8 8 100.0
pod 4 4 100.0
total 77 86 89.5


line stmt bran cond sub pod time code
1             package Dancer::Plugin::ProxyPath::Proxy;
2              
3 3     3   32198 use warnings;
  3         6  
  3         98  
4 3     3   17 use strict;
  3         5  
  3         89  
5 3     3   15 use Carp;
  3         11  
  3         243  
6 3     3   1033 use URI;
  3         7864  
  3         1459  
7              
8             =head1 NAME
9              
10             Dancer::Plugin::ProxyPath::Proxy - Provides user-perspective paths
11              
12             =head1 VERSION
13              
14             Version 0.03
15              
16             =cut
17              
18             our $VERSION = '0.03';
19              
20             =head1 SYNOPSIS
21              
22             This object provides the method uri_for to provide
23             user perspective paths. See L
24              
25             use Dancer::Plugin::ProxyPath;
26              
27             my $external_path = proxy->uri_for("/path/to/elsewhere");
28             # http://public.server.com/dancer-app/path/to/elsewhere
29             ...
30              
31             # and in your templates: (assuming a passed variable $background)
32              
33            
34            
35              
36             If no proxy information is found, proxy->path and proxy->uri for will
37             return the same paths as request->path an request->uri_for, making it work
38             in development as well.
39              
40             =head1 METHODS
41              
42             =head2 uri_for( path, parameters )
43              
44             Returns a fully qualified url for a path, as seen by the user.
45              
46             =cut
47              
48             my $base_header = "request-base";
49              
50             sub uri_for {
51 8     8 1 3267 my $self = shift;
52 8   66     29 my $destination = shift || Dancer::request->path;
53 8   100     150 my $parameters = shift || {};
54 8 50       22 ref $parameters eq 'HASH' or croak q/Usage: proxy->uri_for($path, \%parameters)/;
55 8         18 my $base = Dancer::request->header($self->{base_header});
56 8         500 my $host = Dancer::request->header("x-forwarded-host");
57 8         414 my $scheme = Dancer::request->env->{"psgi.url_scheme"};
58 8 50       400 if ($host) {
59 8         31 my $uri = URI->new;
60 8         5941 $uri->scheme($scheme);
61 8         4091 $uri->authority($host);
62 8         220 my $path = '';
63 8 50       18 $path .= $base if ($base);
64 8 100       22 unless ($self->is_absolute($destination)) {
65 2         6 $path .= Dancer::request->path;
66 2 50       126 $path .= '/' unless ($path =~ m{/$});
67             }
68 8         12 $path .= $destination;
69 8   50     34 $uri->path($path || '/');
70 8 100       207 if (%$parameters) {
71 1         9 $uri->query_form($parameters);
72             }
73 8         115 return $uri->canonical;
74             } else {
75 0         0 return Dancer::request->uri_for($destination, $parameters);
76             }
77             }
78              
79             =head2 secure_uri_for( path, parameters )
80              
81             Returns a fully qualified url for a path, as seen by the user,
82             with the scheme set to https.
83              
84             =cut
85              
86             sub secure_uri_for {
87 3     3 1 525 my $self = shift;
88 3         8 my $uri = $self->uri_for(@_);
89 3         273 $uri->scheme("https");
90 3         1413 return $uri;
91             }
92              
93             =head2 instance
94              
95             Returns a singleton instance of this class
96              
97             =cut
98              
99             my $instance;
100             sub instance {
101 6     6 1 73 my $class = shift;
102 6   33     32 my $header = shift || $base_header;
103 6 100       23 unless ($instance) {
104 2         10 $instance = bless {base_header => $header}, $class;
105             }
106 6         41 return $instance;
107             }
108              
109             =head2 is_absolute
110              
111             Determines whether the path passed to it is absolute by
112             whether or not it has a leading slash
113              
114             =cut
115              
116             sub is_absolute {
117 8     8 1 11 my $self = shift;
118 8         10 my $path = shift;
119            
120 8 100       23 if ($path =~ m{^/}) {
121 6         19 return 1;
122             };
123 2         6 return 0;
124             }
125              
126             =head1 AUTHOR
127              
128             Alex Kalderimis, C<< >>
129              
130             =head1 BUGS
131              
132             Please report any bugs or feature requests to C, or through
133             the web interface at L. I will be notified, and then you'll
134             automatically be notified of progress on your bug as I make changes.
135              
136             =head1 SUPPORT
137              
138             You can find documentation for this module with the perldoc command.
139              
140             perldoc Dancer::Plugin::ProxyPath::Proxy
141              
142              
143             You can also look for information at:
144              
145             =over 4
146              
147             =item * RT: CPAN's request tracker
148              
149             L
150              
151             =item * AnnoCPAN: Annotated CPAN documentation
152              
153             L
154              
155             =item * CPAN Ratings
156              
157             L
158              
159             =item * Search CPAN
160              
161             L
162              
163             =back
164              
165             =head1 ACKNOWLEDGEMENTS
166              
167             Dancer obviously, for being a great way to write a web-app.
168              
169             =head1 LICENSE AND COPYRIGHT
170              
171             Copyright 2011 Alex Kalderimis.
172              
173             This program is free software; you can redistribute it and/or modify it
174             under the terms of either: the GNU General Public License as published
175             by the Free Software Foundation; or the Artistic License.
176              
177             See http://dev.perl.org/licenses/ for more information.
178              
179              
180             =cut
181              
182             1; # End of Dancer::Plugin::ProxyPath::Proxy