MxScreen/Tie/Stdout.pm untested

filter: Statementstmt 0.0% 0 / 56 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 104 CC 5 SCAR 16.2
CC
5
Cov
0%
WRITE
12.0
CLOSE
6.0
DESTROY
6.0
CRAP 5.1
1 # -*- Mode: perl -*-
2 #
3 # $Id: Stdout.pm,v 0.1 2001/04/22 17:57:04 ram Exp $
4 #
5 # Copyright (c) 1998-2001, Raphael Manfredi
6 # Copyright (c) 2000-2001, Christophe Dehaudt
7 #
8 # You may redistribute only under the terms of the Artistic License,
9 # as specified in the README file that comes with the distribution.
10 #
11 # HISTORY
12 # $Log: Stdout.pm,v $
13 # Revision 0.1 2001/04/22 17:57:04 ram
14 # Baseline for first Alpha release.
15 #
16 # $EndLog$
17 #
18  
19 use strict;
20  
21 package CGI::MxScreen::Tie::Stdout;
22  
23 #
24 # The purpose of this class is to trap all STDOUT output and redirect it
25 # to /dev/null until it is time for us to generate something, i.e. after
26 # headers have been emitted. This is meant as a safety guard to ignore
27 # any buggy HTML generation before ->display is called on the target screen.
28 #
29 # CGI::MxScreen normally unties STDOUT when it is about to generate headers.
30 # However, in exceptional circumstances, we may have errors generated before
31 # STDOUT could be untied. Therefore, the about-to-be-discarded data are
32 # inspected and if we see something looking like a header, we untie ourselves
33 # automatically.
34 #
35  
36 require Tie::Handle;
37 require CGI::MxScreen::Tie::Sinkable;
38  
39 use vars qw(@ISA);
40 @ISA = qw(Tie::Handle CGI::MxScreen::Tie::Sinkable);
41  
42 use Carp::Datum;
43 use Log::Agent;
44 use Symbol;
45  
46 #
47 # (TIEHANDLE)
48 #
49 # Initial tieing.
50 #
51 sub TIEHANDLE {
52 DFEATURE my $f_;
53 my $self = bless gensym(), shift;
54 open($self, ">&STDOUT") || logdie "can't save STDOUT: $!";
55 open(STDOUT, ">/dev/null") || logdie "can't reopen STDOUT: $!";
56 return DVAL $self;
57 }
58  
59 #
60 # (WRITE)
61 #
62 # Intercept writes
63 #
64 sub WRITE {
65 DFEATURE my $f_;
66 my $self = shift;
67 my ($buf, $len, $offset) = @_;
68 return DVOID unless $len;
69  
70 #
71 # If we see "Content-Type:", or something that looks like a header,
72 # then someone is trying to reply before we were untied explicitely.
73 # Let's become transparent by untie-ing ourselves.
74 #
75 # For this to work, we assume header names will be emitted in one single
76 # "print" statement, at least.
77 #
78  
79 if ($buf =~ /^[\w-]+:/m) {
80 logtrc 'info', "header emitted, STDOUT becoming transparent";
81 untie *main::STDOUT;
82 $self->CLOSE;
83 syswrite(STDOUT, $buf, $len, $offset);
84 return DVOID;
85 }
86  
87 #
88 # Determine caller
89 #
90  
91 my ($pkg, $filename, $line);
92 my $i = 0;
93 do {
94 ($pkg, $filename, $line) = caller($i++)
95 } while ($pkg eq __PACKAGE__ || $pkg eq 'Tie::Handle');
96  
97 #
98 # Strip all trailing "\n" before logging.
99 #
100  
101 $len-- while $len && substr($buf, $offset + $len - 1, 1) =~ /^[\r\n]/;
102 logerr "STDOUT discarded (at %s, line %d): %s",
103 $filename, $line, substr($buf, $offset, $len);
104  
105 return DVOID;
106 }
107  
108 #
109 # (CLOSE)
110 #
111 # Restore orginal STDOUT
112 # NB: unties STDOUT as a side effect.
113 #
114 sub CLOSE {
115 DFEATURE my $f_;
116 my $self = shift;
117 logdie "$self already closed" unless defined fileno($self);
118 untie *STDOUT;
119 open(STDOUT, ">&=" . fileno($self)) || logdie "can't restore STDOUT: $!";
120 close $self;
121 return DVOID;
122 }
123  
124 #
125 # (DESTROY)
126 #
127 # Destructor: closes original STDOUT if not already done.
128 #
129 sub DESTROY {
130 DFEATURE my $f_;
131 my $self = shift;
132 return DVOID unless defined fileno($self);
133 $self->CLOSE;
134 return DVOID;
135 }
136  
137 1;
138  
139 =head1 NAME
140  
141 CGI::MxScreen::Tie::Stdout - Discard STDOUT output
142  
143 =head1 SYNOPSIS
144  
145 # Not meant to be used directly
146  
147 =head1 DESCRIPTION
148  
149 This class is used by C<CGI::MxScreen> to discard any STDOUT output made
150 until the C<display()> routine is called. All discarded output is logged,
151 with the origin of the call, identifying the culprit.
152  
153 Upon an unexpected reception of something that looks like an HTTP header,
154 it automatically becomes transparent and removes itself.
155  
156 =head1 AUTHOR
157  
158 Raphael Manfredi F<E<lt>Raphael_Manfredi@pobox.comE<gt>>
159  
160 =head1 SEE ALSO
161  
162 CGI::MxScreen(3).
163