Fix body predicate bug/back compat issue
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Response.pm
1 package Catalyst::Response;
2
3 use Moose;
4 use HTTP::Headers;
5
6 with 'MooseX::Emulate::Class::Accessor::Fast';
7
8 has cookies   => (is => 'rw', default => sub { {} });
9 has body      => (is => 'rw', default => undef);
10 sub has_body { defined($_[0]->body) }
11
12 has location  => (is => 'rw');
13 has status    => (is => 'rw', default => 200);
14 has finalized_headers => (is => 'rw', default => 0);
15 has headers   => (
16   is      => 'rw',
17   handles => [qw(content_encoding content_length content_type header)],
18   default => sub { HTTP::Headers->new() },
19   required => 1,
20   lazy => 1,
21 );
22 has _context => (
23   is => 'rw',
24   weak_ref => 1,
25   handles => ['write'],
26   clearer => '_clear_context',
27 );
28
29 sub output { shift->body(@_) }
30
31 sub code   { shift->status(@_) }
32
33 no Moose;
34
35 =head1 NAME
36
37 Catalyst::Response - stores output responding to the current client request
38
39 =head1 SYNOPSIS
40
41     $res = $c->response;
42     $res->body;
43     $res->code;
44     $res->content_encoding;
45     $res->content_length;
46     $res->content_type;
47     $res->cookies;
48     $res->header;
49     $res->headers;
50     $res->output;
51     $res->redirect;
52     $res->status;
53     $res->write;
54
55 =head1 DESCRIPTION
56
57 This is the Catalyst Response class, which provides methods for responding to
58 the current client request. The appropriate L<Catalyst::Engine> for your environment
59 will turn the Catalyst::Response into a HTTP Response and return it to the client.
60
61 =head1 METHODS
62
63 =head2 $res->body( $text | $fh | $iohandle_object )
64
65     $c->response->body('Catalyst rocks!');
66
67 Sets or returns the output (text or binary data). If you are returning a large body,
68 you might want to use a L<IO::Handle> type of object (Something that implements the read method
69 in the same fashion), or a filehandle GLOB. Catalyst
70 will write it piece by piece into the response.
71
72 =head2 $res->has_body
73
74 Predicate which returns true when a body has been set.
75
76 =head2 $res->code
77
78 Alias for $res->status.
79
80 =head2 $res->content_encoding
81
82 Shortcut for $res->headers->content_encoding.
83
84 =head2 $res->content_length
85
86 Shortcut for $res->headers->content_length.
87
88 =head2 $res->content_type
89
90 Shortcut for $res->headers->content_type.
91
92 This value is typically set by your view or plugin. For example,
93 L<Catalyst::Plugin::Static::Simple> will guess the mime type based on the file
94 it found, while L<Catalyst::View::TT> defaults to C<text/html>.
95
96 =head2 $res->cookies
97
98 Returns a reference to a hash containing cookies to be set. The keys of the
99 hash are the cookies' names, and their corresponding values are hash
100 references used to construct a L<CGI::Simple::Cookie> object.
101
102     $c->response->cookies->{foo} = { value => '123' };
103
104 The keys of the hash reference on the right correspond to the L<CGI::Simple::Cookie>
105 parameters of the same name, except they are used without a leading dash.
106 Possible parameters are:
107
108 =over
109
110 =item value
111
112 =item expires
113
114 =item domain
115
116 =item path
117
118 =item secure
119
120 =item httponly
121
122 =back
123
124 =head2 $res->header
125
126 Shortcut for $res->headers->header.
127
128 =head2 $res->headers
129
130 Returns an L<HTTP::Headers> object, which can be used to set headers.
131
132     $c->response->headers->header( 'X-Catalyst' => $Catalyst::VERSION );
133
134 =head2 $res->output
135
136 Alias for $res->body.
137
138 =head2 $res->redirect( $url, $status )
139
140 Causes the response to redirect to the specified URL. The default status is
141 C<302>.
142
143     $c->response->redirect( 'http://slashdot.org' );
144     $c->response->redirect( 'http://slashdot.org', 307 );
145
146 This is a convenience method that sets the Location header to the
147 redirect destination, and then sets the response status.  You will
148 want to C< return > or C<< $c->detach() >> to interrupt the normal
149 processing flow if you want the redirect to occur straight away.
150
151 =cut
152
153 sub redirect {
154     my $self = shift;
155
156     if (@_) {
157         my $location = shift;
158         my $status   = shift || 302;
159
160         $self->location($location);
161         $self->status($status);
162     }
163
164     return $self->location;
165 }
166
167 =head2 $res->location
168
169 Sets or returns the HTTP 'Location'.
170
171 =head2 $res->status
172
173 Sets or returns the HTTP status.
174
175     $c->response->status(404);
176
177 $res->code is an alias for this, to match HTTP::Response->code.
178
179 =head2 $res->write( $data )
180
181 Writes $data to the output stream.
182
183 =head2 meta
184
185 Provided by Moose
186
187 =head2 $res->print( @data )
188
189 Prints @data to the output stream, separated by $,.  This lets you pass
190 the response object to functions that want to write to an L<IO::Handle>.
191
192 =cut
193
194 sub print {
195     my $self = shift;
196     my $data = shift;
197
198     defined $self->write($data) or return;
199
200     for (@_) {
201         defined $self->write($,) or return;
202         defined $self->write($_) or return;
203     }
204
205     return 1;
206 }
207
208 =head1 AUTHORS
209
210 Catalyst Contributors, see Catalyst.pm
211
212 =head1 COPYRIGHT
213
214 This library is free software. You can redistribute it and/or modify
215 it under the same terms as Perl itself.
216
217 =cut
218
219 __PACKAGE__->meta->make_immutable;
220
221 1;