File Coverage

blib/lib/POE/Component/FastCGI/Response.pm
Criterion Covered Total %
statement 19 67 28.3
branch 1 16 6.2
condition n/a
subroutine 6 17 35.2
pod 8 8 100.0
total 34 108 31.4


line stmt bran cond sub pod time code
1             package POE::Component::FastCGI::Response;
2             BEGIN {
3 3     3   103798 $POE::Component::FastCGI::Response::VERSION = '0.19';
4             }
5              
6 3     3   22 use strict;
  3         7  
  3         118  
7 3     3   19 use base qw/HTTP::Response/;
  3         12  
  3         5248  
8 3     3   78186 use bytes;
  3         28  
  3         22  
9              
10 3     3   3642 use POE::Kernel;
  3         189413  
  3         34  
11              
12             sub new {
13 2     2 1 7 my($class, $client, $id, $code, @response) = @_;
14 2 50       8 $code = 200 unless defined $code;
15              
16 2         24 my $response = $class->SUPER::new($code, @response);
17              
18 2         157 $response->{client} = $client;
19 2         5 $response->{requestid} = $id;
20              
21 2         7 return $response;
22             }
23              
24             sub DESTROY {
25 0     0     my($self) = @_;
26 0           $self->close;
27             }
28              
29             sub streaming {
30 0     0 1   my($self, $streaming) = @_;
31 0 0         if(defined $streaming) {
32 0           $self->{streaming} = $streaming;
33             }else{
34 0           return $self->{streaming};
35             }
36             }
37              
38             sub closed {
39 0     0 1   my($self, $callback) = @_;
40 0 0         if(defined $callback) {
    0          
41 0           $self->{closed} = $callback;
42             }elsif(defined $self->{closed}) {
43 0           $self->{closed}->($self);
44             }
45             }
46              
47             # Write and send call put() on the wheel. It is imperative that we
48             # do this from the wheel-owners session. Else we might register event
49             # handlers in the wrong sessions. For example, when we register the
50             # FlushedEvent event handler, that would be registered on the wrong
51             # session, and the wheel would never be closed properly.
52             sub send {
53 0     0 1   my($self) = @_;
54 0           $poe_kernel->call($self->request->{sessionid},
55             'w_send', $self);
56             }
57              
58             sub write {
59 0     0 1   my($self, $out) = @_;
60 0           $poe_kernel->call($self->request->{sessionid},
61             'w_write', $self, $out);
62 0           return 1;
63             }
64              
65             sub close {
66 0     0 1   my($self, $out) = @_;
67 0 0         return unless defined $self->{client};
68 0           $poe_kernel->call($self->request->{sessionid},
69             'w_close', $self, $out);
70             }
71              
72              
73             sub _send {
74 0     0     my($self) = @_;
75              
76             # Adapted from POE::Filter::HTTPD
77 0           my $status_line = "Status: " . $self->code;
78              
79             # Use network newlines, and be sure not to mangle newlines in the
80             # response's content.
81              
82 0           $self->header( "Content-Length" => length($self->content) );
83 0           my @headers;
84 0           push @headers, $status_line;
85 0           push @headers, $self->headers_as_string("\x0D\x0A");
86              
87 0           my $filter = $self->{client}->get_input_filter();
88 0           my $keepconn = $filter->{conn}->[$filter->{requestid}]->{keepconn};
89              
90 0           $self->{client}->put({
91             requestid => $self->{requestid},
92             close => !$keepconn,
93             content => join("\x0D\x0A", @headers, "") . $self->content
94             });
95              
96             ### FCGI_KEEP_CONN: disconnect after request if NOT set:
97 0 0         if($keepconn == 0) {
98 0           $self->{client}->event( FlushedEvent => "client_shutdown" );
99             }
100              
101             # Kill circular ref & delete wheel reference
102 0           $self->request->{_res} = 0;
103 0           delete $self->{client};
104 0           return 1;
105             }
106              
107             sub _write {
108 0     0     my($self, $out) = @_;
109 0           $self->{client}->put({requestid => $self->{requestid}, content => $out});
110             }
111              
112             sub _close {
113 0     0     my($self, $out) = @_;
114 0           $self->{client}->put({
115             requestid => $self->{requestid},
116             close => 1,
117             content => ""
118             });
119              
120             # Kill circular ref & delete wheel reference
121 0           $self->request->{_res} = 0;
122 0           delete $self->{client};
123 0           return 1;
124             }
125              
126             sub redirect {
127 0     0 1   my($self, $url, $uri) = @_;
128 0 0         $url = defined $self->request
129             ? URI->new_abs($url, $self->request->uri)
130             : $url;
131              
132 0           $self->code(302);
133 0           $self->header(Location => $url);
134             }
135              
136             sub error {
137 0     0 1   my($self, $code, $text) = @_;
138 0           $self->code($code);
139 0           $self->header("Content-type" => "text/html");
140 0 0         $self->content(defined $text ? $text : $self->error_as_HTML);
141 0           $self->send;
142             }
143              
144             1;
145              
146             =head1 NAME
147              
148             POE::Component::FastCGI::Response - PoCo::FastCGI HTTP Response class
149              
150             =head1 SYNOPSIS
151              
152             use POE::Component::FastCGI::Response;
153             my $response = POE::Component::FastCGI::Response->new($client, $id,
154             200, .. HTTP::Response parameters ..);
155              
156             =head1 DESCRIPTION
157              
158             This module is generally not used directly, you should call
159             L's C method which
160             returns an object of this class.
161              
162             C is a subclass of L
163             so inherits all of its methods. The includes C for setting output
164             headers and C for setting the content.
165              
166             Therefore the following methods mostly deal with actually sending the
167             response:
168              
169             =over 4
170              
171             =item $response = POE::Component::FastCGI::Response->new($client, $id, $code)
172              
173             Creates a new C object, parameters from
174             C<$code> onwards are passed directly to L's constructor.
175              
176             =item $response->streaming
177              
178             Set and check streaming status
179              
180             =item $response->closed
181              
182             Set a callback to be called when this response is closed, mainly useful for
183             streaming.
184              
185             =item $response->send
186              
187             Sends the response object and ends the current connection.
188              
189             =item $response->write($text)
190              
191             Writes some text directly to the output stream, for use when you don't want
192             to or can't send a L object.
193              
194             =item $response->close
195              
196             Closes the output stream.
197              
198             You don't normally need to use this as the object will automatically close
199             when DESTROYed.
200              
201             =item $response->redirect($url)
202              
203             Sets the object to be a redirect to $url. You still need to call C to
204             actually send the redirect.
205              
206             =item $response->error($code, $text)
207              
208             Sends an error to the client, $code is the HTTP error code and $text is
209             the content of the page to send.
210              
211             =back
212              
213             =head1 AUTHOR
214              
215             Copyright 2005, David Leadbeater L. All rights reserved.
216              
217             This library is free software; you can redistribute it and/or modify
218             it under the same terms as Perl itself.
219              
220             =head1 BUGS
221              
222             Please let me know.
223              
224             =head1 SEE ALSO
225              
226             L, L,
227             L, L.
228              
229             =cut