root/trunk/lib/Mungo/Response.pm

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

Require 'PerlSetVar? Debug 1' to get nice (and revealing) debugging output, refs #3

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 HTML::Entities;
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') || $r->content_type || 'text/html',
26     # We don't set buffer here, we set it after it has been tied.
27     # 'Buffer' => $r->dir_config('MungoBuffer') || 0,
28     'Buffer' => 0,
29     'CacheControl' => $r->dir_config('MungoCacheControl') || 'private',
30     'Charset' => $r->dir_config('MungoCharset') || undef,
31     'Status' => 200,
32     'Mungo' => $parent,
33     'CookieClass' => $r->dir_config('MungoCookieClass') || 'Mungo::Cookie',
34     'Cookies' => undef, # placeholder for visibility
35   );
36   my %data;
37   $singleton = bless \%data, $class;
38   tie %data, 'Mungo::Arbiter::Response', $singleton, \%core_data;
39   $singleton->{Buffer} = $r->dir_config('MungoBuffer') || 0;
40   $r->pnotes(__PACKAGE__, $singleton);
41   return $singleton;
42 }
43
44 sub DESTROY {
45   my $self = shift;
46   $self->cleanse();
47 }
48
49 sub cleanse {
50   my $self = shift;
51   if(ref $self->{'IO_stack'} eq 'ARRAY') {
52     while (@{$self->{'IO_stack'}}) {
53       my $fh = pop @{$self->{'IO_stack'}};
54       close(select($fh));
55     }
56   }
57   delete $self->{$_} for keys %$self;
58   untie %$self if tied %$self;
59 }
60
61 sub send_http_header {
62   my $self = shift;
63   my $r = $self->{'Apache::Request'};
64   return if($self->{'__HEADERS_SENT__'});
65   $self->{'__HEADERS_SENT__'} = 1;
66   if($self->{CacheControl} eq 'no-cache') {
67     $r->no_cache(1);
68   }
69   else {
70     $r->header_out('Cache-Control', $self->{CacheControl});
71   }
72   $self->{Cookies}->inject_headers($r);
73   $r->status($self->{Status});
74   $r->send_http_header($self->{ContentType});
75 }
76
77 sub start {
78   my $self = shift;
79   return if(exists $self->{'IO_stack'} &&
80             scalar(@{$self->{'IO_stack'}}) > 0);
81   $self->{'IO_stack'} = [];
82   tie *DIRECT, ref $self, $self;
83   push @{$self->{'IO_stack'}}, select(DIRECT);
84 }
85
86 sub finish {
87   my $self = shift;
88   # Unbuffer outselves, this will actually induce a flush
89   $self->{Buffer} = 0;
90   untie *DIRECT if tied *DIRECT;
91   return unless(exists $self->{'IO_stack'});
92   my $fh = $self->{'IO_stack'}->[0];
93   delete $self->{'IO_stack'};
94   die __PACKAGE__." IO stack of wrong depth" if(scalar(@{$self->{'IO_stack'}}) != 1);
95 }
96
97 sub AddHeader {
98   my $self = shift;
99   my $r = $self->{'Apache::Request'};
100   die "Headers already sent." if($self->{'__HEADERS_SENT__'});
101   $r->header_out(@_);
102 }
103 sub Cookies {
104   my $self = shift;
105   die "Headers already sent." if($self->{'__HEADERS_SENT__'});
106   my $cookie = $self->{'Cookies'};
107   $cookie->__set(@_);
108 }
109 sub Redirect {
110   my $self = shift;
111   my $url = shift;
112   die "Cannot redirect, headers already sent\n" if($self->{'__HEADERS_SENT__'});
113   $self->{Status} = shift || 302;
114   $self->{'Apache::Request'}->header_out('Location', $url);
115   $self->send_http_header();
116   $self->End();
117 }
118 sub Include {
119   my $self = shift;
120   my $subject = shift;
121   my $rv;
122   eval {
123     if(ref $subject) {
124       $rv = $self->{'Mungo'}->include_mem($subject, @_);
125     }
126     else {
127       $rv = $self->{'Mungo'}->include_file($subject, @_);
128     }
129   };
130   if($@) {
131     use Data::Dumper;
132     print "Error in Include($subject):<br />\n";
133     my $href = $@;
134     eval {
135       my $pkg = $href->{callstack}->[0]->[0];
136       my $preamble = eval "\$${pkg}::Mungo_preamble;";
137       my $postamble = eval "\$${pkg}::Mungo_postamble;";
138       my $contents = eval "\$${pkg}::Mungo_contents;";
139       print "<pre class=\"error\">$href->{error}</pre><br />\n";
140
141       unless($contents) {
142         my $filename = $href->{callstack}->[0]->[1];
143         if(open(FILE, "<$filename")) {
144           local $/ = undef;
145           $$contents = <FILE>;
146           close(FILE);
147         }
148       }
149
150       if($contents) {
151         if($self->{'Apache::Request'}->dir_config('Debug')) {
152           print Mungo::pretty_print_code($preamble, $contents, $postamble, $href->{callstack}->[0]->[2]);
153         }
154       } else {
155         print '<pre>'.Dumper($@).'</pre>';
156       }
157     };
158     if($@) {
159       # Oh, dear lord this is bad.  We'd died trying to print out death.
160       print '<pre>'.Dumper($@).'</pre>';
161     }
162     return undef;
163   }
164   return $rv;
165 }
166 sub TrapInclude {
167   my $self = shift;
168   my $output;
169   my $handle = \do { local *HANDLE };
170   tie *{$handle}, 'Mungo::Response::Trap', \$output;
171   push @{$self->{'IO_stack'}}, select(*{$handle});
172   eval {
173     $self->Include(@_);
174   };
175   untie *{$handle} if tied *{$handle};
176   select(pop @{$self->{'IO_stack'}});
177   die $@ if $@;
178   return $output;
179 }
180 sub End {
181   shift->Flush();
182   eval { goto MUNGO_HANDLER_FINISH; };
183 }
184 sub Flush {
185   my $self = shift;
186   # Flush doesn't apply unless we're immediately above STDOUT
187   return if(scalar(@{$self->{'IO_stack'}}) > 1);
188   unless($self->{'__OUTPUT_STARTED__'}) {
189     $self->send_http_header;
190     $self->{'__OUTPUT_STARTED__'} = 1;
191   }
192   $self->{'IO_stack'}->[-1]->print($one_true_buffer);
193   $one_true_buffer = '';
194 }
195
196 sub AUTOLOAD {
197   my $self = shift;
198   my $name = $AUTOLOAD;
199   $name =~ s/.*://;   # strip fully-qualified portion
200   die __PACKAGE__." AUTOLOAD($name)" unless(ref $self);
201   return undef;
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.