Component/FastCGI/Response.pm

filter: Statementstmt 29.4% 20 / 68 Branchbran 6.2% 1 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 35.2% 6 / 17 Podpod 100% 8 / 8 totaltotal 32.1% 35 / 109 SCAR 37.7
CC
9
Cov
25%
closed
12.0
streaming
6.0
close
6.0
CRAP 43.2
« Component/FastCGI.pm ↑ Summary Component/FastCGI/Request.pm »
1 package POE::Component::FastCGI::Response;
2 BEGIN {
3 3 ▶ $POE::Component::FastCGI::Response::VERSION = '0.19';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 }
5  
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use base qw/HTTP::Response/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use POE::Kernel;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub new {
13 2 ▶ my($class, $client, $id, $code, @response) = @_;
Subroutine new: called CC=2 SCAR=7.0
Pod documented
14 2 ▶ $code = 200 unless defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 2 0 2
15  
16 2 my $response = $class->SUPER::new($code, @response);
17  
18 2 $response->{client} = $client;
19 2 $response->{requestid} = $id;
20  
21 2 return $response;
22 }
23  
24 sub DESTROY {
25 0 ▶ my($self) = @_;
Subroutine DESTROY: not called CC=1 SCAR=6.9
26 0 $self->close;
27 }
28  
29 sub streaming {
30 0 ▶ my($self, $streaming) = @_;
Subroutine streaming: not called CC=2 SCAR=17.9
Pod documented
31 0 ▶ if(defined $streaming) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $streaming) { } 0 0 0
32 0 $self->{streaming} = $streaming;
33 }else{
34 0 return $self->{streaming};
35 }
36 }
37  
38 sub closed {
39 0 ▶ my($self, $callback) = @_;
Subroutine closed: not called CC=3 SCAR=24.8
Pod documented
40 0 ▶ if(defined $callback) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $callback) { } 0 0 0
elsif (defined $self->{'closed'}) { } 0 0 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 ▶ my($self) = @_;
Subroutine send: not called CC=1 SCAR=6.9
Pod documented
54 $poe_kernel->call($self->request->{sessionid},
55 0 'w_send', $self);
56 }
57  
58 sub write {
59 0 ▶ my($self, $out) = @_;
Subroutine write: not called CC=1 SCAR=6.9
Pod documented
60 $poe_kernel->call($self->request->{sessionid},
61 0 'w_write', $self, $out);
62 0 return 1;
63 }
64  
65 sub close {
66 0 ▶ my($self, $out) = @_;
Subroutine close: not called CC=2 SCAR=17.9
Pod documented
67 0 ▶ return unless defined $self->{client};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"client"} 0 0 0
68 $poe_kernel->call($self->request->{sessionid},
69 0 'w_close', $self, $out);
70 }
71  
72  
73 sub _send {
74 0 ▶ my($self) = @_;
Subroutine _send: not called CC=2 SCAR=17.9
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 $self->{client}->put({
91 requestid => $self->{requestid},
92 0 close => !$keepconn,
93 content => join("\x0D\x0A", @headers, "") . $self->content
94 });
95  
96 ### FCGI_KEEP_CONN: disconnect after request if NOT set:
97 0 ▶ if($keepconn == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keepconn == 0) 0 0 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 ▶ my($self, $out) = @_;
Subroutine _write: not called CC=1 SCAR=6.9
109 0 $self->{client}->put({requestid => $self->{requestid}, content => $out});
110 }
111  
112 sub _close {
113 0 ▶ my($self, $out) = @_;
Subroutine _close: not called CC=1 SCAR=6.9
114 $self->{client}->put({
115 requestid => $self->{requestid},
116 0 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 ▶ my($self, $url, $uri) = @_;
Subroutine redirect: not called CC=2 SCAR=17.9
Pod documented
128 0 ▶ $url = defined $self->request
Branch: 1 branch
BranchTrueFalseTotal
defined $self->request ? : 0 0 0
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 ▶ my($self, $code, $text) = @_;
Subroutine error: not called CC=2 SCAR=17.9
Pod documented
138 0 $self->code($code);
139 0 $self->header("Content-type" => "text/html");
140 0 ▶ $self->content(defined $text ? $text : $self->error_as_HTML);
Branch: 1 branch
BranchTrueFalseTotal
defined $text ? : 0 0 0
141 0 $self->send;
142 }
143  
144 3 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<POE::Component::FastCGI::Request>'s C<make_response> method which
160 returns an object of this class.
161  
162 C<POE::Component::FastCGI::Response> is a subclass of L<HTTP::Response>
163 so inherits all of its methods. The includes C<header()> for setting output
164 headers and C<content()> 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<POE::Component::FastCGI::Response> object, parameters from
174 C<$code> onwards are passed directly to L<HTTP::Response>'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<HTTP::Response> 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<send> 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<http://dgl.cx/contact>. 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<POE::Component::FastCGI::Request>, L<HTTP::Response>,
227 L<POE::Component::FastCGI>, L<POE>.
228  
229 =cut
« Component/FastCGI.pm ↑ Summary Component/FastCGI/Request.pm »