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