File Coverage

blib/lib/POE/Component/FastCGI/Request.pm
Criterion Covered Total %
statement 56 74 75.6
branch 15 26 57.6
condition 1 3 33.3
subroutine 11 14 78.5
pod 6 6 100.0
total 89 123 72.3


line stmt bran cond sub pod time code
1             package POE::Component::FastCGI::Request;
2             BEGIN {
3 2     2   21077 $POE::Component::FastCGI::Request::VERSION = '0.19';
4             }
5              
6 2     2   16 use strict;
  2         5  
  2         266  
7              
8 2     2   2170 use CGI::Util qw(unescape);
  2         37427  
  2         190  
9 2     2   3907 use HTTP::Headers;
  2         24253  
  2         99  
10 2     2   26 use base qw/HTTP::Request/;
  2         5  
  2         2581  
11              
12 2     2   91047 use POE::Component::FastCGI::Response; # for make_response
  2         10  
  2         2042  
13              
14             sub new {
15 2     2 1 1697 my($class, $client, $sessionid, $id, $cgi, $query) = @_;
16 2 100       14 my $host = defined $cgi->{HTTP_HOST} ? $cgi->{HTTP_HOST} :
17             $cgi->{SERVER_NAME};
18              
19 2         5 my $self = $class->SUPER::new(
20             $cgi->{REQUEST_METHOD},
21             "http" . (defined $cgi->{HTTPS} and $cgi->{HTTPS} ? "s" : "") .
22             "://$host" . $cgi->{REQUEST_URI},
23             # Convert CGI style headers back into HTTP style
24             HTTP::Headers->new(
25             map {
26 2   33     33 my $p = $_;
27 2         10 s/^HTTP_//;
28 2         4 s/_/-/g;
29 2         25 ucfirst(lc $_) => delete $cgi->{$p};
30             } grep /^HTTP_/, keys %$cgi
31             ),
32             $query
33             );
34              
35 2         17597 $self->{client} = $client;
36 2         7 $self->{sessionid} = $sessionid;
37 2         5 $self->{requestid} = $id;
38 2         8 $self->{env} = $cgi;
39              
40 2         9 return $self;
41             }
42              
43             sub DESTROY {
44 0     0   0 my $self = shift;
45 0 0       0 if(not exists $self->{_res}) {
46 0         0 warn __PACKAGE__ . " object destroyed without sending response";
47             }
48             }
49              
50             sub make_response {
51 2     2 1 3479 my($self, $response) = @_;
52              
53 2 50       10 if(not defined $response) {
54 2         26 $response = POE::Component::FastCGI::Response->new(
55             $self->{client},
56             $self->{requestid},
57             );
58 2         7 $self->{_res} = $response;
59 2         14 $response->request($self);
60 2         36 return $response;
61             }
62              
63 0 0       0 if(not $response->isa("POE::Component::FastCGI::Response")) {
64 0         0 bless $response, "POE::Component::FastCGI::Response";
65             }
66              
67 0         0 $response->{client} = $self->{client};
68 0         0 $response->{requestid} = $self->{requestid};
69 0         0 $response->request($self);
70 0         0 $self->{_res} = $response;
71              
72 0         0 return $response;
73             }
74              
75             sub error {
76 0     0 1 0 my($self, $code, $text) = @_;
77 0         0 warn "Error $code: $text\n";
78 0         0 $self->make_response->error($code, $text);
79             }
80              
81             sub env {
82 0     0 1 0 my($self, $env) = @_;
83 0 0       0 if(exists $self->{env}->{$env}) {
84 0         0 return $self->{env}->{$env};
85             }
86 0         0 return undef;
87             }
88              
89             sub query {
90 3     3 1 2826 my($self, $param) = @_;
91              
92 3 100       14 if(not exists $self->{_query}) {
93 2 100       12 if($self->method eq 'GET') {
94 1         22 $self->{_query} = _parse(\$self->{env}->{QUERY_STRING});
95             }else{
96 1         19 $self->{_query} = _parse($self->content_ref);
97             }
98             }
99              
100 3 100       21 if(not defined $param) {
    100          
101 1         6 return $self->{_query};
102             }elsif(exists $self->{_query}->{$param}) {
103 1         11 return $self->{_query}->{$param};
104             }
105 1         5 return undef;
106             }
107              
108             sub cookie {
109 1     1 1 2185 my($self, $name) = @_;
110              
111 1 50       7 if(not exists $self->{_cookie}) {
112 1 50       10 return undef unless defined $self->header("Cookie");
113 1         64 $self->{_cookie} = _parse(\$self->header("Cookie"));
114             }
115              
116 1 50       5 return $self->{_cookie} if not defined $name;
117              
118 1 50       144 return $self->{_cookie}->{$name} if exists $self->{_cookie}->{$name};
119              
120 0         0 return undef;
121             }
122              
123             sub _parse {
124 3     3   53 my $string = shift;
125 3         7 my $res = {};
126 3         3131 for(split /[;&] ?/, $$string) {
127 4         24 my($n, $v) = split /=/, $_, 2;
128 4         18 $v = unescape($v);
129 4         152 $res->{$n} = $v;
130             }
131 3         13 return $res;
132             }
133              
134             1;
135              
136             =head1 NAME
137              
138             POE::Component::FastCGI::Request - PoCo::FastCGI HTTP Request class
139              
140             =head1 SYNOPSIS
141              
142             use POE::Component::FastCGI::Request;
143             my $response = POE::Component::FastCGI::Response->new($client, $id,
144             $cgi, $query);
145              
146             =head1 DESCRIPTION
147              
148             Objects of this class are generally created by L,
149              
150             C is a subclass of L
151             so inherits all of its methods. The includes C for reading
152             headers.
153              
154             It also wraps the enviroment variables found in FastCGI requests, so
155             information such as the client's IP address and the server software
156             in use is available.
157              
158             =over 4
159              
160             =item $request = POE::Component::FastCGI::Request->new($client, $id, $cgi, $query)
161              
162             Creates a new C object. This deletes values
163             from C<$cgi> while converting it into a L object.
164             It also assumes $cgi contains certain CGI variables. This generally should
165             not be used directly, POE::Component::FastCGI creates these objects for you.
166              
167             =item $response = $request->make_response([$response])
168              
169             Makes a response object for this request or if the optional parameter is
170             provided turns a normal HTTP::Response object into a
171             POE::Component::FastCGI::Response object that is linked to this request.
172              
173             =item $request->error($code[, $text])
174              
175             Sends a HTTP error back to the user.
176              
177             =item $request->env($name)
178              
179             Gets the specified variable out of the CGI environment.
180              
181             eg:
182             $request->env("REMOTE_ADDR");
183              
184             =item $request->query([$name])
185              
186             Gets the value of name from the query (GET or POST data).
187             Without a parameter returns a hash reference containing all
188             the query data.
189              
190             =item $request->cookie([$name])
191              
192             Gets the value of the cookie with name from the request.
193             Without a parameter returns a hash reference containing all
194             the cookie data.
195              
196             =back
197              
198             =head1 AUTHOR
199              
200             Copyright 2005, David Leadbeater L. All rights reserved.
201              
202             This library is free software; you can redistribute it and/or modify
203             it under the same terms as Perl itself.
204              
205             =head1 BUGS
206              
207             Please let me know.
208              
209             =head1 SEE ALSO
210              
211             L, L,
212             L, L.
213              
214             =cut