lib/MKDoc/Core/Request.pm

filter: Statementstmt 33.3% 1 / 3 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 1 / 1 Podpod n/a totaltotal 50.0% 2 / 4 CC 1 SCAR 2.6
CC
1
Cov
33%
BEGIN
1.3
CRAP 1.3
« lib/MKDoc/Core/Plugin.pm ↑ Summary lib/MKDoc/Core/Response.pm »
1 =head1 NAME
2  
3 MKDoc::Core::Request - MKDoc request object.
4  
5  
6 =head1 SUMMARY
7  
8 Just like CGI.pm, with a few additions.
9  
10 See perldoc CGI for the base CGI OO API.
11  
12 =cut
13 package MKDoc::Core::Request::CompileCGI;
14 0 ▶use CGI qw(-compile :all);
Subroutine BEGIN: called CC=1 SCAR=2.6
15  
16 package MKDoc::Core::Request;
17 use strict;
18 use warnings;
19 use base qw /CGI/;
20 use Encode;
21  
22  
23 =head1 API
24  
25 =head2 $self->instance();
26  
27 Returns the L<MKDoc::Core::Request> singleton - or creates it if necessary.
28  
29 =cut
30 sub instance
31 {
32 my $class = shift;
33 $::MKD_Request ||= $class->new();
34 return $::MKD_Request;
35 }
36  
37  
38  
39 =head2 $self->clone();
40  
41 Clones the current object and returns the copy.
42  
43 =cut
44 sub clone
45 {
46 my $self = shift;
47 return $self->new();
48 }
49  
50  
51 sub self_uri
52 {
53 my $self = shift;
54 my %opt = map { "-" . $_ => 1 } ( @_, qw /path_info query/ );
55 $opt{relative} ||= 0;
56 return $self->url (\%opt);
57 }
58  
59  
60 sub url
61 {
62 my $self = shift;
63 my $url = $self->SUPER::url (@_);
64  
65 # httpd.conf example:
66 # SetEnv MKD__URL_PORT_STRIP "80,8080"
67 # SetEnv MKD__URL_PORT_STRIP_REGEX "80\d*"
68 my $port_strip = $ENV{MKD__URL_PORT_STRIP} || '';
69 my $port_strip_regex = $ENV{MKD__URL_PORT_STRIP_REGEX} || '';
70  
71 # change commas to regex alternator
72 $port_strip =~ tr/,/|/;
73 my $port_strip_str = $port_strip || $port_strip_regex || '80';
74  
75 # assumes url always has a port specifier
76 $url =~ s/(.*?\:\/\/(?:.*?\@)?)(.*):(?:${port_strip_str})(?!\d)(.*)/$1$2$3/
77 if ($url =~ /(.*?\:\/\/(?:.*?\@)?)(.*):${port_strip_str}(?!\d)(.*)/);
78  
79 return $url;
80 }
81  
82  
83  
84 =head2 $self->param_eq ($param_name, $param_value);
85  
86 Returns TRUE if the parameter named $param_name returns
87 a value of $param_value.
88  
89 =cut
90 sub param_eq
91 {
92 my $self = shift;
93 my $param = $self->param (shift());
94 my $value = shift;
95 return unless (defined $param);
96 return unless (defined $value);
97 return $param eq $value;
98 }
99  
100  
101 sub param_checked
102 {
103 my $self = shift;
104 my $param = $self->param (@_);
105 return $param ? 'checked' : undef;
106 }
107  
108  
109 =head2 $self->param_equals ($param_name, $param_value);
110  
111 Alias for param_eq().
112  
113 =cut
114 sub param_equals
115 {
116 my $self = shift;
117 return $self->param_eq (@_);
118 }
119  
120  
121  
122 =head2 $self->path_info_eq ($value);
123  
124 Returns TRUE if $ENV{PATH_INFO} equals $value,
125 FALSE otherwise.
126  
127 =cut
128 sub path_info_eq
129 {
130 my $self = shift;
131 my $param = $self->path_info();
132 my $value = shift;
133 return unless (defined $param);
134 return unless (defined $value);
135 return $param eq $value;
136 }
137  
138  
139  
140 =head2 $self->path_info_equals ($param_name, $param_value);
141  
142 Alias for path_info_eq().
143  
144 =cut
145 sub path_info_equals
146 {
147 my $self = shift;
148 return $self->path_info_eq (@_);
149 }
150  
151  
152  
153 =head2 $self->path_info_starts_with ($value);
154  
155 Returns TRUE if $ENV{PATH_INFO} starts with $value, FALSE otherwise.
156  
157 =cut
158 sub path_info_starts_with
159 {
160 my $self = shift;
161 my $param = $self->path_info();
162 my $value = quotemeta (shift);
163 return $param =~ /^$value/;
164 }
165  
166  
167  
168 =head2 $self->method();
169  
170 Returns the current request method being used, i.e. normally HEAD, GET or POST.
171  
172 =cut
173 sub method
174 {
175 my $self = shift;
176 return $ENV{REQUEST_METHOD} || 'GET';
177 }
178  
179  
180 sub delete
181 {
182 my $self = shift;
183 while (@_) { $self->SUPER::delete (shift()) };
184 }
185  
186  
187 sub delete_all_fast
188 {
189 my $self = shift;
190 $self->{'.parameters'} = [];
191 }
192  
193  
194 =head2 $self->is_upload ($param_name);
195  
196 Returns TRUE if $param_name is an upload, FALSE otherwise.
197  
198 =cut
199 sub is_upload
200 {
201 my ($self, $param_name) = @_;
202 my @param = grep(ref && fileno($_), $self->SUPER::param ($param_name));
203 return unless @param;
204 return wantarray ? @param : $param[0];
205 }
206  
207  
208 sub param
209 {
210 my $self = shift;
211 my $key = shift || return $self->SUPER::param ();
212  
213 $self->is_upload ($key => @_) and return $self->SUPER::param ($key => @_);
214 @_ and return $self->SUPER::param ($key => @_);
215  
216 my @res = $self->SUPER::param ($key);
217 @res = map {
218 (defined $_) ? do {
219 my $res = $_;
220 my $octets = $_;
221 my $string = Encode::decode_utf8 ($octets, Encode::FB_PERLQQ);
222 $string;
223 } : undef
224 } @res;
225  
226 @res == 0 and return;
227 @res == 1 and return shift @res;
228 return wantarray ? @res : \@res;
229 }
230  
231  
232 # redirect() doesn't seem to work with CGI.pm 2.89
233 # this should fix for this particular version.
234 sub redirect
235 {
236 my $self = shift;
237 $CGI::VERSION == 2.89 ? return do {
238 my $uri = shift;
239 my $res = '';
240 $res .= "Status: 302 Moved\n";
241 $res .= "Location: $uri\n\n";
242 $res;
243 } : return $self->SUPER::redirect (@_);
244 }
245  
246  
247 1;
248  
249  
250 __END__
« lib/MKDoc/Core/Plugin.pm ↑ Summary lib/MKDoc/Core/Response.pm »