root/trunk/lib/Mungo/Response.pm

Revision 13, 6.4 kB (checked in by jesus, 7 years ago)

fixes #6

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 IO::Handle;
9 use Mungo::Arbiter::Response;
10 use Mungo::Response::Trap;
11 use Mungo::Cookie;
12 use Mungo::Utils;
13 use HTML::Entities;
14 our $AUTOLOAD;
15
16 my $one_true_buffer = '';
17
18 sub new {
19   my $class = shift;
20   my $parent = shift;
21   my $r = $parent->{'Apache::Request'};
22   my $singleton = $r->pnotes(__PACKAGE__);
23   return $singleton if ($singleton);
24   my %core_data = (
25     'Apache::Request' => $r,
26     'ContentType' => $r->dir_config('MungoContentType') || $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     use Data::Dumper;
133     print "Error in Include($subject):<br />\n";
134     my $href = $@;
135     eval {
136       my $pkg = $href->{callstack}->[0]->[0];
137       my $preamble = eval "\$${pkg}::Mungo_preamble;";
138       my $postamble = eval "\$${pkg}::Mungo_postamble;";
139       my $contents = eval "\$${pkg}::Mungo_contents;";
140       print "<pre class=\"error\">$href->{error}</pre><br />\n";
141
142       unless($contents) {
143         my $filename = $href->{callstack}->[0]->[1];
144         if(open(FILE, "<$filename")) {
145           local $/ = undef;
146           $$contents = <FILE>;
147           close(FILE);
148         }
149       }
150
151       if($contents) {
152         if($self->{'Apache::Request'}->dir_config('Debug')) {
153           print Mungo::Utils::pretty_print_code($preamble, $contents, $postamble, $href->{callstack}->[0]->[2]);
154         }
155       } else {
156         print '<pre>'.Dumper($@).'</pre>';
157       }
158     };
159     if($@) {
160       # Oh, dear lord this is bad.  We'd died trying to print out death.
161       print '<pre>'.Dumper($@).'</pre>';
162     }
163     return undef;
164   }
165   return $rv;
166 }
167 sub TrapInclude {
168   my $self = shift;
169   my $output;
170   my $handle = \do { local *HANDLE };
171   tie *{$handle}, 'Mungo::Response::Trap', \$output;
172   push @{$self->{'IO_stack'}}, select(*{$handle});
173   eval {
174     $self->Include(@_);
175   };
176   untie *{$handle} if tied *{$handle};
177   select(pop @{$self->{'IO_stack'}});
178   die $@ if $@;
179   return $output;
180 }
181 sub End {
182   shift->Flush();
183   eval { goto MUNGO_HANDLER_FINISH; };
184 }
185 sub Flush {
186   my $self = shift;
187   # Flush doesn't apply unless we're immediately above STDOUT
188   return if(scalar(@{$self->{'IO_stack'}}) > 1);
189   unless($self->{'__OUTPUT_STARTED__'}) {
190     $self->send_http_header;
191     $self->{'__OUTPUT_STARTED__'} = 1;
192   }
193   $self->{'IO_stack'}->[-1]->print($one_true_buffer);
194   $one_true_buffer = '';
195 }
196
197 sub AUTOLOAD {
198   my $self = shift;
199   my $name = $AUTOLOAD;
200   $name =~ s/.*://;   # strip fully-qualified portion
201   die __PACKAGE__." does not implement $name";
202 }
203
204 sub TIEHANDLE {
205   my $class = shift;
206   my $self = shift;
207   return $self;
208 }
209 sub PRINT {
210   my $self = shift;
211   my $output = shift;
212   if(scalar(@{$self->{'IO_stack'}}) == 1) {
213     # Buffering a just-in-time headers only applies if we
214     # immediately above STDOUT
215     if($self->{Buffer}) {
216       $one_true_buffer .= $output;
217       return;
218     }
219     unless($self->{'__OUTPUT_STARTED__'}) {
220       $self->{'__OUTPUT_STARTED__'} = 1;
221       $self->send_http_header;
222     }
223   }
224   $self->{'IO_stack'}->[-1]->print($output);
225 }
226 sub PRINTF {
227   my $self = shift;
228   if(scalar(@{$self->{'IO_stack'}}) == 1) {
229     # Buffering a just-in-time headers only applies if we
230     # immediately above STDOUT
231     if($self->{Buffer}) {
232       $one_true_buffer .= sprintf(@_);
233       return;
234     }
235     unless($self->{'__OUTPUT_STARTED__'}) {
236       $self->{'__OUTPUT_STARTED__'} = 1;
237       $self->send_http_header;
238     }
239   }
240   $self->{'IO_stack'}->[-1]->printf(@_);
241 }
242 sub CLOSE {
243   my $self = shift;
244   $self->{Buffer} = 0;
245 }
246 sub UNTIE { }
247
248 1;
Note: See TracBrowser for help on using the browser.