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