root/trunk/lib/Mungo/Response.pm

Revision 5, 5.5 kB (checked in by jesus, 7 years ago)

fixes #1

Line 
1 package Mungo::Response;
2
3 # Copyright (c) 2007 OmniTI Computer Consulting, Inc. All rights reserved.
4 # For information on licensing see:
5 #   https://labs.omniti.com/zetaback/trunk/LICENSE
6
7 use strict;
8 use Carp;
9 use IO::Handle;
10 use Mungo::Arbiter::Response;
11 use Mungo::Response::Trap;
12 use Mungo::Cookie;
13 our $AUTOLOAD;
14
15 my $one_true_buffer = '';
16
17 sub new {
18   my $class = shift;
19   my $parent = shift;
20   my $r = $parent->{'Apache::Request'};
21   my $singleton = $r->pnotes(__PACKAGE__);
22   return $singleton if ($singleton);
23   my %core_data = (
24     'Apache::Request' => $r,
25     'ContentType' => $r->dir_config('MungoContentType') ||
26                      $r->content_type || 'text/html',
27     # We don't set buffer here, we set it after it has been tied.
28     # 'Buffer' => $r->dir_config('MungoBuffer') || 0,
29     'Buffer' => 0,
30     'CacheControl' => $r->dir_config('MungoCacheControl') || 'private',
31     'Charset' => $r->dir_config('MungoCharset') || undef,
32     'Status' => 200,
33     'Mungo' => $parent,
34     'CookieClass' => $r->dir_config('MungoCookieClass') || 'Mungo::Cookie',
35     'Cookies' => undef, # placeholder for visibility
36   );
37   my %data;
38   $singleton = bless \%data, $class;
39   tie %data, 'Mungo::Arbiter::Response', $singleton, \%core_data;
40   $singleton->{Buffer} = $r->dir_config('MungoBuffer') || 0;
41   $r->pnotes(__PACKAGE__, $singleton);
42   return $singleton;
43 }
44
45 sub DESTROY {
46   my $self = shift;
47   $self->cleanse();
48 }
49
50 sub cleanse {
51   my $self = shift;
52   if(ref $self->{'IO_stack'} eq 'ARRAY') {
53     while (@{$self->{'IO_stack'}}) {
54       my $fh = pop @{$self->{'IO_stack'}};
55       close(select($fh));
56     }
57   }
58   delete $self->{$_} for keys %$self;
59   untie %$self if tied %$self;
60 }
61
62 sub send_http_header {
63   my $self = shift;
64   my $r = $self->{'Apache::Request'};
65   return if($self->{'__HEADERS_SENT__'});
66   $self->{'__HEADERS_SENT__'} = 1;
67   if($self->{CacheControl} eq 'no-cache') {
68     $r->no_cache(1);
69   }
70   else {
71     $r->header_out('Cache-Control', $self->{CacheControl});
72   }
73   $self->{Cookies}->inject_headers($r);
74   $r->status($self->{Status});
75   $r->send_http_header($self->{ContentType});
76 }
77
78 sub start {
79   my $self = shift;
80   return if(exists $self->{'IO_stack'} &&
81             scalar(@{$self->{'IO_stack'}}) > 0);
82   $self->{'IO_stack'} = [];
83   tie *DIRECT, ref $self, $self;
84   push @{$self->{'IO_stack'}}, select(DIRECT);
85 }
86
87 sub finish {
88   my $self = shift;
89   # Unbuffer outselves, this will actually induce a flush
90   $self->{Buffer} = 0;
91   untie *DIRECT if tied *DIRECT;
92   return unless(exists $self->{'IO_stack'});
93   my $fh = $self->{'IO_stack'}->[0];
94   delete $self->{'IO_stack'};
95   die __PACKAGE__." IO stack of wrong depth" if(scalar(@{$self->{'IO_stack'}}) != 1);
96 }
97
98 sub AddHeader {
99   my $self = shift;
100   my $r = $self->{'Apache::Request'};
101   die "Headers already sent." if($self->{'__HEADERS_SENT__'});
102   $r->header_out(@_);
103 }
104 sub Cookies {
105   my $self = shift;
106   die "Headers already sent." if($self->{'__HEADERS_SENT__'});
107   my $cookie = $self->{'Cookies'};
108   $cookie->__set(@_);
109 }
110 sub Redirect {
111   my $self = shift;
112   my $url = shift;
113   die "Cannot redirect, headers already sent\n" if($self->{'__HEADERS_SENT__'});
114   $self->{Status} = shift || 302;
115   $self->{'Apache::Request'}->header_out('Location', $url);
116   $self->send_http_header();
117   $self->End();
118 }
119 sub Include {
120   my $self = shift;
121   my $subject = shift;
122   my $rv;
123   eval {
124     if(ref $subject) {
125       $rv = $self->{'Mungo'}->include_mem($subject, @_);
126     }
127     else {
128       $rv = $self->{'Mungo'}->include_file($subject, @_);
129     }
130   };
131   if($@) {
132     print "<pre>Error: $@\n\n".Carp::shortmess()."\n</pre>\n";;
133     return undef;
134   }
135   return $rv;
136 }
137 sub TrapInclude {
138   my $self = shift;
139   my $output;
140   my $handle = \do { local *HANDLE };
141   tie *{$handle}, 'Mungo::Response::Trap', \$output;
142   push @{$self->{'IO_stack'}}, select(*{$handle});
143   eval {
144     $self->Include(@_);
145   };
146   untie *{$handle} if tied *{$handle};
147   select(pop @{$self->{'IO_stack'}});
148   die $@ if $@;
149   return $output;
150 }
151 sub End {
152   shift->Flush();
153   eval { goto MUNGO_HANDLER_FINISH; };
154 }
155 sub Flush {
156   my $self = shift;
157   # Flush doesn't apply unless we're immediately above STDOUT
158   return if(scalar(@{$self->{'IO_stack'}}) > 1);
159   unless($self->{'__OUTPUT_STARTED__'}) {
160     $self->send_http_header;
161     $self->{'__OUTPUT_STARTED__'} = 1;
162   }
163   $self->{'IO_stack'}->[-1]->print($one_true_buffer);
164   $one_true_buffer = '';
165 }
166
167 sub AUTOLOAD {
168   my $self = shift;
169   my $name = $AUTOLOAD;
170   $name =~ s/.*://;   # strip fully-qualified portion
171   die __PACKAGE__." AUTOLOAD($name)" unless(ref $self);
172   return undef;
173 }
174
175 sub TIEHANDLE {
176   my $class = shift;
177   my $self = shift;
178   return $self;
179 }
180 sub PRINT {
181   my $self = shift;
182   my $output = shift;
183   if(scalar(@{$self->{'IO_stack'}}) == 1) {
184     # Buffering a just-in-time headers only applies if we
185     # immediately above STDOUT
186     if($self->{Buffer}) {
187       $one_true_buffer .= $output;
188       return;
189     }
190     unless($self->{'__OUTPUT_STARTED__'}) {
191       $self->{'__OUTPUT_STARTED__'} = 1;
192       $self->send_http_header;
193     }
194   }
195   $self->{'IO_stack'}->[-1]->print($output);
196 }
197 sub PRINTF {
198   my $self = shift;
199   if(scalar(@{$self->{'IO_stack'}}) == 1) {
200     # Buffering a just-in-time headers only applies if we
201     # immediately above STDOUT
202     if($self->{Buffer}) {
203       $one_true_buffer .= sprintf(@_);
204       return;
205     }
206     unless($self->{'__OUTPUT_STARTED__'}) {
207       $self->{'__OUTPUT_STARTED__'} = 1;
208       $self->send_http_header;
209     }
210   }
211   $self->{'IO_stack'}->[-1]->printf(@_);
212 }
213 sub CLOSE {
214   my $self = shift;
215   $self->{Buffer} = 0;
216 }
217 sub UNTIE { }
218
219 1;
Note: See TracBrowser for help on using the browser.