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