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