2c63ac733cd223aa5d7f32ba8004380e1c75702a
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Response.pm
1 package Catalyst::Response;
2
3 use Moose;
4 use HTTP::Headers;
5
6 has cookies   => (is => 'rw', default => sub { {} });
7 has body      => (is => 'rw', default => '');
8 has location  => (is => 'rw');
9 has status    => (is => 'rw', default => 200);
10 has finalized_headers => (is => 'rw', default => 0);
11 has headers   => (
12   is      => 'rw',
13   handles => [qw(content_encoding content_length content_type header)],
14   default => sub { HTTP::Headers->new() },
15   required => 1,
16   lazy => 1,
17 );
18 has _context => (
19   is => 'rw',
20   weak_ref => 1,
21 );
22
23 sub output { shift->body(@_) }
24
25 no Moose;
26
27 =head1 NAME
28
29 Catalyst::Response - stores output responding to the current client request
30
31 =head1 SYNOPSIS
32
33     $res = $c->response;
34     $res->body;
35     $res->content_encoding;
36     $res->content_length;
37     $res->content_type;
38     $res->cookies;
39     $res->header;
40     $res->headers;
41     $res->output;
42     $res->redirect;
43     $res->status;
44     $res->write;
45
46 =head1 DESCRIPTION
47
48 This is the Catalyst Response class, which provides methods for responding to
49 the current client request. The appropriate L<Catalyst::Engine> for your environment
50 will turn the Catalyst::Response into a HTTP Response and return it to the client.
51
52 =head1 METHODS
53
54 =head2 $res->body(<$text|$fh|$iofh_object)
55
56     $c->response->body('Catalyst rocks!');
57
58 Sets or returns the output (text or binary data). If you are returning a large body,
59 you might want to use a L<IO::FileHandle> type of object (Something that implements the read method
60 in the same fashion), or a filehandle GLOB. Catalyst
61 will write it piece by piece into the response.
62
63 =head2 $res->content_encoding
64
65 Shortcut for $res->headers->content_encoding.
66
67 =head2 $res->content_length
68
69 Shortcut for $res->headers->content_length.
70
71 =head2 $res->content_type
72
73 Shortcut for $res->headers->content_type.
74
75 This value is typically set by your view or plugin. For example,
76 L<Catalyst::Plugin::Static::Simple> will guess the mime type based on the file
77 it found, while L<Catalyst::View::TT> defaults to C<text/html>.
78
79 =head2 $res->cookies
80
81 Returns a reference to a hash containing cookies to be set. The keys of the
82 hash are the cookies' names, and their corresponding values are hash
83 references used to construct a L<CGI::Cookie> object.
84
85     $c->response->cookies->{foo} = { value => '123' };
86
87 The keys of the hash reference on the right correspond to the L<CGI::Cookie>
88 parameters of the same name, except they are used without a leading dash.
89 Possible parameters are:
90
91 =over 
92
93 =item value
94
95 =item expires
96
97 =item domain
98
99 =item path
100
101 =item secure
102
103 =back
104
105 =head2 $res->header
106
107 Shortcut for $res->headers->header.
108
109 =head2 $res->headers
110
111 Returns an L<HTTP::Headers> object, which can be used to set headers.
112
113     $c->response->headers->header( 'X-Catalyst' => $Catalyst::VERSION );
114
115 =head2 $res->output
116
117 Alias for $res->body.
118
119 =head2 $res->redirect( $url, $status )
120
121 Causes the response to redirect to the specified URL.
122
123     $c->response->redirect( 'http://slashdot.org' );
124     $c->response->redirect( 'http://slashdot.org', 307 );
125
126 =cut
127
128 sub redirect {
129     my $self = shift;
130
131     if (@_) {
132         my $location = shift;
133         my $status   = shift || 302;
134
135         $self->location($location);
136         $self->status($status);
137     }
138
139     return $self->location;
140 }
141
142 =head2 $res->location
143
144 Sets or returns the HTTP 'Location'.
145
146 =head2 $res->status
147
148 Sets or returns the HTTP status.
149
150     $c->response->status(404);
151     
152 =head2 $res->write( $data )
153
154 Writes $data to the output stream.
155
156 =cut
157
158 sub write { shift->_context->write(@_); }
159
160 =head2 meta
161
162 Provided by Moose
163
164 =head1 AUTHORS
165
166 Sebastian Riedel, C<sri@cpan.org>
167
168 Marcus Ramberg, C<mramberg@cpan.org>
169
170 =head1 COPYRIGHT
171
172 This program is free software, you can redistribute it and/or modify 
173 it under the same terms as Perl itself.
174
175 =cut
176
177 __PACKAGE__->meta->make_immutable;
178
179 1;