root/trunk/lib/Mungo/Response.pm

Revision 14, 6.5 kB (checked in by jesus, 7 years ago)

fixes #7

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