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