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