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