File Coverage

blib/lib/POE/Component/FastCGI/Request.pm
Criterion Covered Total %
statement 55 73 75.3
branch 15 26 57.6
condition 4 6 66.6
subroutine 10 13 76.9
pod 6 6 100.0
total 90 124 72.5


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