File Coverage

lib/Egg/Request.pm
Criterion Covered Total %
statement 48 164 29.2
branch 0 48 0.0
condition 0 63 0.0
subroutine 16 47 34.0
pod 1 1 100.0
total 65 323 20.1


line stmt bran cond sub pod time code
1             package Egg::Request;
2             #
3             # Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
4             #
5             # $Id: Request.pm 337 2008-05-14 12:30:09Z lushe $
6             #
7 1     1   577 use strict;
  1         3  
  1         42  
8 1     1   7 use warnings;
  1         3  
  1         154  
9              
10             our $VERSION= '3.02';
11              
12             our $MP_VERSION= 0;
13              
14 0     0 1   sub mp_version { $MP_VERSION }
15              
16             {
17             my $r_class;
18             sub _import {
19 0     0     my($e)= @_;
20 0           $r_class= Egg::Request::handler->_import($e);
21 1     1   6 no warnings 'redefine';
  1         3  
  1         167  
22 0   0 0     *request= sub { $_[0]->{request} ||= $r_class->new(@_) };
  0            
23 0           *req= \&request;
24 0           $e->next::method;
25             }
26             sub _setup_comp {
27 0     0     my $e= shift;
28 0           $r_class->_setup_request($e, @_);
29 0           $e->next::method(@_);
30             }
31             };
32              
33             package Egg::Request::handler;
34 1     1   6 use strict;
  1         3  
  1         30  
35 1     1   15 use warnings;
  1         2  
  1         29  
36 1     1   3656 use CGI::Cookie;
  1         9730  
  1         17  
37 1     1   38 use CGI::Util qw/ unescape /;
  1         4  
  1         59  
38 1     1   7 use base qw/ Egg::Base /;
  1         2  
  1         105  
39 1     1   7 use Carp qw/ croak /;
  1         4  
  1         88  
40              
41             __PACKAGE__->mk_accessors(qw/ r path /);
42              
43             {
44 1     1   20 no strict 'refs'; ## no critic
  1         4  
  1         47  
45 1     1   6 no warnings 'redefine';
  1         3  
  1         569  
46             for ( qw{ REMOTE_USER SCRIPT_NAME
47             REQUEST_URI PATH_INFO HTTP_REFERER HTTP_ACCEPT_ENCODING },
48             [qw{ REMOTE_ADDR 127.0.0.1 }], [qw{ REQUEST_METHOD GET }],
49             [qw{ SERVER_NAME localhost }], [qw{ SERVER_SOFTWARE cmdline }],
50             [qw{ SERVER_PROTOCOL HTTP/1.1 }], [qw{ HTTP_USER_AGENT local }],
51             [qw{ SERVER_PORT 80 }] ) {
52             my($key, $accessor, $default)=
53             ref($_) ? ($_->[0], lc($_->[0]), $_->[1]): ($_, lc($_), "");
54 0 0   0     *{__PACKAGE__."::$accessor"}= sub { $ENV{$key} || $default };
55             }
56             };
57              
58             *agent = \&http_user_agent; *user_agent = \&http_user_agent;
59             *protocol = \&server_protocol; *user = \&remote_user;
60             *method = \&request_method; *port = \&server_port;
61             *addr = \&remote_addr; *address = \&remote_addr;
62             *referer = \&http_referer; *url = \&uri;
63             *accept_encoding = \&http_accept_encoding;
64             *mp_version = \&Egg::Request::mp_version;
65              
66 0 0 0 0     sub host { $ENV{HTTP_HOST} || $ENV{SERVER_NAME} || 'localhost' }
67 0 0 0 0     sub args { $ENV{QUERY_STRING} || $ENV{REDIRECT_QUERY_STRING} || "" }
68              
69             sub _import {
70 0     0     my($class, $e)= @_;
71             my $r_class= $e->global->{request_class}=
72 0   0       $ENV{ uc($e->project_name). '_REQUEST_CLASS'} || do {
73             ($ENV{MOD_PERL} and ModPerl::VersionUtil->require) ? do {
74             my $mp_util= 'ModPerl::VersionUtil';
75             $MP_VERSION= $mp_util->mp_version;
76             $MP_VERSION > 2 ? 'Egg::Request::Apache::MP20' :
77             $mp_util->is_mp2 ? 'Egg::Request::Apache::MP20' :
78             $mp_util->is_mp19 ? 'Egg::Request::Apache::MP19' :
79             $mp_util->is_mp1 ? 'Egg::Request::Apache::MP13' :
80             do {
81             $MP_VERSION= 0;
82             warn qq{ Unsupported mod_perl v$MP_VERSION };
83             'Egg::Request::CGI';
84             };
85             }: 'Egg::Request::CGI';
86             };
87 0 0         $r_class->require or die $@;
88 1     1   6 no warnings 'redefine';
  1         2  
  1         93  
89 0           *params= $r_class->can('parameters');
90 0           $r_class->_init_handler($e);
91 0           $r_class;
92             }
93             sub _init_handler {
94 0     0     my($class, $e)= @_;
95 1     1   6 no strict 'refs'; ## no critic.
  1         3  
  1         130  
96 1     1   7 no warnings 'redefine';
  1         2  
  1         270  
97 0   0       *{$e->namespace. '::handler'}=
  0            
98             $e->can('run') || die q{ $e->run is not found. };
99             }
100             sub is_get {
101 0 0   0     $_[0]->method=~m{^GET}i ? 1: 0;
102             }
103             sub is_post {
104 0 0   0     $_[0]->method=~m{^POST}i ? 1: 0;
105             }
106             sub is_head {
107 0 0   0     $_[0]->method=~m{^HEAD}i ? 1: 0;
108             }
109             sub _setup_request {
110 0     0     my($class, $e)= @_;
111 0           my $pname= $e->project_name;
112 1     1   6 no strict 'refs'; ## no critic.
  1         2  
  1         32  
113 1     1   6 no warnings 'redefine';
  1         1  
  1         1660  
114 0 0         if (my $max= $e->config->{max_snip_deep}) {
115 0           *{"${pname}::___request_max_snip_deep"}= sub {
116 0     0     my($egg, $snip)= @_;
117 0 0         $egg->finished('403 Forbidden') if $max < scalar(@$snip);
118 0           };
119             } else {
120 0     0     *{"${pname}::___request_max_snip_deep"}= sub { };
  0            
  0            
121             }
122 0 0         if (my $regexp= $e->config->{request_path_trim}) {
123 0           *{"${pname}::___request_path_trim"}= sub {
124 0     0     my($egg, $path)= @_;
125 0           $$path=~s{$regexp} [];
126 0           };
127             } else {
128 0     0     *{"${pname}::___request_path_trim"}= sub { };
  0            
  0            
129             }
130 0           @_;
131             }
132             sub new {
133 0     0     my($class, $e)= @_;
134 0           my $req= bless { e=> $e }, $class;
135 0           my $path;
136 0 0         if ($ENV{REDIRECT_URI}) {
137 0   0       $path= $ENV{PATH_INFO} || $ENV{REDIRECT_URI} || '/';
138             } else {
139 0   0       $path = $ENV{SCRIPT_NAME} || "";
140 0           $path =~s{/+$} [];
141 0 0         $path.= $ENV{PATH_INFO} if $ENV{PATH_INFO};
142             }
143 0           $e->___request_path_trim(\$path);
144 0 0         $req->path( $path=~m{^/} ? $path: "/$path" );
145             # Request parts are generated.
146 0           $path=~s{\s+} []g; $path=~s{^/+} []; $path=~s{/+$} [];
  0            
  0            
147 0           $e->___request_max_snip_deep( $e->snip([ split /\/+/, $path ]) );
148 0           $e->debug_out("# + Request Path : /$path");
149 0           $req;
150             }
151             sub parameters {
152 0     0     my($req)= @_;
153 0   0       $req->{parameters} ||= do {
154 0           my $r= $req->r;
155 0           my %params;
156 0           $params{$_}= $r->param($_) for $r->param;
157 0           \%params;
158             };
159             }
160             sub snip {
161 0     0     shift->e->snip(@_);
162             }
163             sub cookies {
164 0     0     my($req)= @_;
165 0 0 0       $req->{cookies} ||= do { fetch CGI::Cookie || {} };
  0            
166             }
167             sub cookie {
168 0     0     my $req= shift;
169 0           my $cookie= $req->cookies;
170 0 0         return keys %$cookie if @_== 0;
171 0 0 0       ($_[0] && exists($cookie->{$_[0]})) ? $cookie->{$_[0]}: undef;
172             }
173             sub cookie_value {
174 0     0     my $req= shift;
175 0   0       my $key= shift || return "";
176 0   0       my $cookie= $req->cookies->{$key} || return "";
177 0 0         $cookie->value || "";
178             }
179             sub cookie_more {
180 0     0     my $req= shift;
181 0   0       my $key= shift || croak 'I want cookie key.';
182 0 0         my $val= defined($_[0]) ? $_[0]: croak 'I want cookie value.';
183 0 0         if (ref($val) ne 'ARRAY') {
184 0           my @tmp= map{ unescape($_) }(split( /[&;]/, $val. '&dmy'));
  0            
185 0           pop @tmp;
186 0           $val= \@tmp;
187             }
188 0           $req->cookies->{$key}= CGI::Cookie->new( -name=> $key, -value=> $val );
189             }
190             sub secure {
191 0 0 0 0     $_[0]->{secure} ||= (
      0        
192             ($ENV{HTTPS} && lc($ENV{HTTPS}) eq 'on')
193             || ($ENV{SERVER_PORT} && $ENV{SERVER_PORT}== 443)
194             ) ? 1: 0;
195             }
196             sub scheme {
197 0 0 0 0     $_[0]->{scheme} ||= $_[0]->secure ? 'https': 'http';
198             }
199             sub uri {
200 0     0     my($req)= @_;
201 0   0       $req->{uri} ||= do {
202 0           require URI;
203 0           my $uri = URI->new;
204 0           my $path= $req->path; $path=~s{^/} [];
  0            
205 0           $uri->scheme($req->scheme);
206 0           $uri->host($req->host);
207 0           $uri->port($req->port);
208 0           $uri->path($path);
209 0 0         $ENV{QUERY_STRING} and $uri->query($ENV{QUERY_STRING});
210 0           $uri->canonical;
211             };
212             }
213             sub remote_host {
214 0     0     my($req)= @_;
215 0   0       $req->{remote_host} ||= do {
216 0 0 0       $ENV{REMOTE_HOST}
217             || gethostbyaddr(pack("C*", split(/\./, $req->remote_addr)), 2)
218             || $req->remote_addr;
219             };
220             }
221             sub host_name {
222 0     0     my($req)= @_;
223 0   0       $req->{host_name} ||= do {
224 0           my $host= $req->host;
225 0           $host=~s{\:\d+$} [];
226 0           $host;
227             };
228             }
229             sub output {
230 0     0     my $req = shift;
231 0   0       my $header= shift || \"";
232 0   0       my $body = shift || \"";
233 0   0       CORE::print STDOUT $$header, ($$body || "");
234             }
235             sub result {
236 0     0     my($req)= @_;
237 0   0       my $code= $req->e->response->status || return 0;
238 0 0         $code== 200 ? 0: $code;
239             }
240              
241             1;
242              
243             __END__
244              
245             =head1 NAME
246              
247             Egg::Request - WEB request processing for Egg.
248              
249             =head1 SYNOPSIS
250              
251             # The object is acquired.
252             my $req= $e->request;
253            
254             # The query data is acquired specifying the name.
255             my $data= $req->param('query_name');
256            
257             # The mass of the query data is obtained.
258             my $params= $req->params;
259            
260             # Passing the requested place is obtained.
261             my $path= $req->path
262            
263             # Cookie is acquired specifying the name.
264             my $cookie= $req->cookie('cookie_name');
265            
266             # The mass of Cookie is acquired.
267             my $cookies= $req->cookies;
268            
269             # The content of cookie is acquired. $cookie-E<gt>value is done at the same time.
270             my $cookie_value= $req->cookie_value('cookie_name');
271              
272             =head1 DESCRIPTION
273              
274             The WEB request processing for the Egg framework is done.
275              
276             If mod_perl can be used, it is composed by L<Egg::Request::Apache> system class
277             though this module is usually composed in the shape succeeded to to L<Egg::Request::CGI>.
278              
279             Please set environment variable [PROJECT_NAME]_REQUEST_CLASS when you use another
280             request class.
281             Please look at the source of 'bin/dispatch.fcgi' about the use example.
282             After L<Egg::Request::FastCGI> is set to the environment variable in the BEGIN
283             block, it starts.
284              
285             =head1 METHODS
286              
287             The main body of this module is built into the component of the project.
288              
289             =head2 request
290              
291             The handler object of this module is returned.
292              
293             =over 4
294              
295             =item * Alias = req
296              
297             =back
298              
299             =head1 HANDLER METHODS
300              
301             =head2 new
302              
303             Constructor. It is not necessary to call from the application.
304              
305             my $req= $e->request;
306              
307             =head2 r
308              
309             The object that is basic of the processing of this module is returned.
310             In a word, the object of CGI.pm or L<Apache::Request> is restored.
311              
312             =head2 mp_version
313              
314             The version of mod_perl is returned.
315              
316             0 returns whenever it is not composed of the Egg::Apache system.
317              
318             =head2 is_get
319              
320             When the request method is GET, true is restored.
321              
322             =head2 is_post
323              
324             When the request method is POST, true is restored.
325              
326             =head2 is_head
327              
328             The request method returns and GET and POST return HEAD request and considering
329             true when not is.
330              
331             Please use 'request_method' or 'Method' when you want to check the request method in detail.
332              
333             =head2 parameters
334              
335             The mass of request query is returned by the HASH reference.
336              
337             $e->parameters->{'query_name'};
338              
339             =over 4
340              
341             =item * Alias = params
342              
343             =back
344              
345             =head2 param ([KEY], [VALUE])
346              
347             When the argument is omitted, the list of the key to the request query is
348             returned.
349              
350             When KEY is given, the content of the corresponding request query is returned.
351              
352             When VALUE is given, the request query is set.
353              
354             my @key_list= $req->param;
355            
356             my $hoge= $req->param('hoge');
357            
358             $req->param( hoge => 'boo' );
359              
360             =head2 snip
361              
362             It relays it to $e-E<gt>snip. Please look at the document of L<Egg::Util>.
363              
364             =head2 cookies
365              
366             The data received with cookie is returned.
367             The object of L<CGI::Cookie> is restored.
368              
369             while (my($key, $value)= each %{$req->cookies}) {
370             $cookie{$key}= $value->value;
371             }
372              
373             =head2 cookie ([KEY])
374              
375             The content of cookie specified with KEY is acquired.
376              
377             It is necessary to use the value method further for obtaining data.
378              
379             my $gao= $req->cookie('gao')->value;
380              
381             This method doesn't support the set of the value.
382              
383             Cookie returned with the response header must be used and set must use the cookie
384             method of L<Egg::Response>.
385              
386             =head2 cookie_value ([KEY])
387              
388             Cookie specified with KEY is returned and the result of receipt value is
389             returned.
390              
391             my $gao= $req->cookie_value('gao');
392              
393             =head2 path
394              
395             Passing information on the requested place is returned.
396              
397             =head2 remote_user
398              
399             The content of environment variable REMOTE_USER is returned.
400              
401             =over 4
402              
403             =item * Alias = user
404              
405             =back
406              
407             =head2 script_name
408              
409             The content of environment variable SCRIPT_NAME is returned.
410              
411             =head2 request_uri
412              
413             The content of environment variable REQUEST_URI is returned.
414              
415             =head2 path_info
416              
417             The content of environment variable PATH_INFO is returned.
418              
419             =head2 http_referer
420              
421             The content of environment variable HTTP_REFERER is returned.
422              
423             =over 4
424              
425             =item * Alias = referer
426              
427             =back
428              
429             =head2 http_accept_encoding
430              
431             The content of environment variable HTTP_ACCEPT_ENCODING is returned.
432              
433             =over 4
434              
435             =item * Alias = accept_encoding
436              
437             =back
438              
439             =head2 remote_addr
440              
441             The content of environment variable REMOTE_ADDR is returned.
442              
443             Default is '127.0.0.1'.
444              
445             =over 4
446              
447             =item * Alias = addr
448              
449             =back
450              
451             =head2 request_method
452              
453             The content of environment variable REQUEST_METHOD is returned.
454              
455             Default is GET.
456              
457             =over 4
458              
459             =item * Alias = method
460              
461             =back
462              
463             =head2 server_name
464              
465             The content of environment variable SERVER_NAME is returned.
466              
467             Default is localhost.
468              
469             =head2 server_software
470              
471             The content of environment variable SERVER_SOFTWARE is returned.
472              
473             Default is cmdline.
474              
475             =head2 server_protocol
476              
477             The content of environment variable SERVER_PROTOCOL is returned.
478              
479             Default is 'HTTP/1.1'.
480              
481             =over 4
482              
483             =item * Alias = protocol
484              
485             =back
486              
487             =head2 http_user_agent
488              
489             The content of environment variable HTTP_USER_AGENT is returned.
490              
491             Default is local.
492              
493             =over 4
494              
495             =item * Alias = agent, user_agent
496              
497             =back
498              
499             =head2 server_port
500              
501             The content of environment variable SERVER_PORT is returned.
502              
503             Default is 80.
504              
505             =over 4
506              
507             =item * Alias = port
508              
509             =back
510              
511             =head2 secure
512              
513             True is returned if the request is due to SSL.
514              
515             =head2 scheme
516              
517             The URI scheme when requesting it is returned.
518              
519             =head2 uri
520              
521             It composes of information that receives requested URI again and it returns it.
522              
523             =over 4
524              
525             =item * Alias = url
526              
527             =back
528              
529             =head2 remote_host
530              
531             Host information on the requested client is returned. Remote_addr is returned
532             when not obtaining it.
533              
534             =head2 host
535              
536             Host information on the WEB server under operation is returned.
537              
538             =head2 host_name
539              
540             When the port number is included in the content of host, the value in which it
541             is excluded is returned.
542              
543             =head2 output ([HEDER_SCALAR_REF], [BODY_SCALAR_REF])
544              
545             The received content is output to STDOUT.
546              
547             =head2 result
548              
549             The response status is returned from L<Egg::Response> as a receipt result code.
550              
551             When the response status is undefined or 200, 0 is always returned.
552              
553             =head2 SEE ALSO
554              
555             L<Egg::Release>
556             L<Egg::Request::CGI>,
557             L<Egg::Request::FastCGI>,
558             L<Egg::Request::Apache>,
559             L<Egg::Base>,
560             L<ModPerl::VersionUtil>,
561             L<URI>
562              
563             =head2 AUTHOR
564              
565             Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
566              
567             =head2 COPYRIGHT AND LICENSE
568              
569             Copyright (C) 2008 Bee Flag, Corp. E<lt>L<http://egg.bomcity.com/>E<gt>.
570              
571             This library is free software; you can redistribute it and/or modify
572             it under the same terms as Perl itself, either Perl version 5.8.6 or,
573             at your option, any later version of Perl 5 you may have available.
574              
575             =cut
576