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