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