add Catalyst::Response->code
[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 =back
119
120 =head2 $res->header
121
122 Shortcut for $res->headers->header.
123
124 =head2 $res->headers
125
126 Returns an L<HTTP::Headers> object, which can be used to set headers.
127
128     $c->response->headers->header( 'X-Catalyst' => $Catalyst::VERSION );
129
130 =head2 $res->output
131
132 Alias for $res->body.
133
134 =head2 $res->redirect( $url, $status )
135
136 Causes the response to redirect to the specified URL. The default status is
137 C<302>.
138
139     $c->response->redirect( 'http://slashdot.org' );
140     $c->response->redirect( 'http://slashdot.org', 307 );
141
142 This is a convenience method that sets the Location header to the
143 redirect destination, and then sets the response status.  You will
144 want to C< return; > or C< $c->detach() > to interrupt the normal
145 processing flow if you want the redirect to occur straight away.
146
147 =cut
148
149 sub redirect {
150     my $self = shift;
151
152     if (@_) {
153         my $location = shift;
154         my $status   = shift || 302;
155
156         $self->location($location);
157         $self->status($status);
158     }
159
160     return $self->location;
161 }
162
163 =head2 $res->location
164
165 Sets or returns the HTTP 'Location'.
166
167 =head2 $res->status
168
169 Sets or returns the HTTP status.
170
171     $c->response->status(404);
172
173 $res->code is an alias for this, to match HTTP::Response->code.
174     
175 =head2 $res->write( $data )
176
177 Writes $data to the output stream.
178
179 =head2 meta
180
181 Provided by Moose
182
183 =head2 $res->print( @data )
184
185 Prints @data to the output stream, separated by $,.  This lets you pass
186 the response object to functions that want to write to an L<IO::Handle>.
187
188 =cut
189
190 sub print {
191     my $self = shift;
192     my $data = shift;
193
194     defined $self->write($data) or return;
195
196     for (@_) {
197         defined $self->write($,) or return;
198         defined $self->write($_) or return;
199     }
200     
201     return 1;
202 }
203
204 =head1 AUTHORS
205
206 Catalyst Contributors, see Catalyst.pm
207
208 =head1 COPYRIGHT
209
210 This program is free software, you can redistribute it and/or modify 
211 it under the same terms as Perl itself.
212
213 =cut
214
215 __PACKAGE__->meta->make_immutable;
216
217 1;