lib/SOAP/Clean/Misc.pm untested

filter: Statementstmt 0.0% 0 / 105 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 192 CC 9 SCAR 26.5
CC
9
Cov
0%
request
42.0
assert
12.0
backtrace
6.0
CRAP 14.1
1 # Copyright (c) 2003, Cornell University
2 # See the file COPYING for the status of this software
3  
4 package SOAP::Clean::Misc;
5  
6 use strict;
7 use warnings;
8  
9 BEGIN {
10 use Exporter ();
11 our (@ISA, @EXPORT);
12  
13 @ISA = qw(Exporter);
14 @EXPORT = qw(
15 &assert
16 &backtrace
17 &my_cgifile_handler
18 &escape_HTML
19 );
20 }
21  
22 sub backtrace {
23 my $result = "";
24 for (my $i=0; ; $i++) {
25 my ($package, $filename, $line) = caller $i;
26 if ( !defined($package) ) {
27 return $result;
28 }
29 $result .= $package." ".$filename." ".$line."\n";
30 }
31 }
32  
33 sub assert {
34 my ($x,$msg) = @_;
35 if (defined($x) && $x) { return $x; }
36 die ((defined($msg) ? $msg : "Assertion failed.")
37 ." Backtrace follows.\n".backtrace());
38 }
39  
40 sub escape_HTML {
41 my ($str) = @_;
42 $str =~ s/</&lt;/g;
43 $str =~ s/>/&gt;/g;
44 return $str;
45 }
46 ########################################################################
47  
48 #########################################################################
49 # A better file transport method for LWP.
50 #
51 # This one causes
52 #########################################################################
53  
54 package my_cgifile_handler;
55  
56 use strict;
57 use warnings;
58  
59 use File::Temp qw/ :POSIX /;
60 use File::Basename;
61  
62 use vars qw(@ISA);
63  
64 require LWP::Protocol::file;
65 @ISA=qw(LWP::Protocol::file);
66  
67 sub request {
68 my($self, $request, $proxy, $arg, $size) = @_;
69  
70 LWP::Debug::trace('()');
71  
72 my $url = $request->url;
73  
74 # input and output temporary files
75 my $in_name = tmpnam();
76 my $out_name = tmpnam();
77 my $err_name = tmpnam();
78 # Generate the command to call the cgi script. CGI scripts read the
79 # headers of the request from environment variables. So, we need to
80 # set those.
81 my $env = "";
82 # Set the SERVER_ variables
83 $env .= sprintf "SERVER_PROTOCOL=CGIFILE ";
84 # REQUEST_METHOD={PUT,GET,...}
85 $env .= sprintf "REQUEST_METHOD=%s ",$request->method;
86 $env .= sprintf "REQUEST_URI=%s ",$url->path;
87 # if URL was ...?xxx, the QUERY_STRING=xxx
88 if ($url->query) {
89 $env .= sprintf "QUERY_STRING=%s ",quotemeta($url->query);
90 }
91 # Add the rest of the headers as environment variables, after
92 # converting "Some-Header:" to "HTTP_SOME_HEADER".
93 $request->headers->scan(sub {
94 my ($k,$v) = @_;
95 $k =~ tr/a-z/A-Z/;
96 $k =~ tr/-/_/;
97 $env .= sprintf "HTTP_%s=%s ",
98 $k,quotemeta($v);
99 });
100 # Now, the command.
101 my $cmd .= sprintf "cd %s ; %s ./%s < %s > %s 2> %s",
102 dirname($url->path), $env, basename($url->path),
103 $in_name, $out_name, $err_name;
104  
105 # The input file must contains the content of the request.
106 open F, ">$in_name" || assert(0);
107 print F $request->content;
108 print F "\n";
109 close F || assert(0);
110  
111 # Run the command.
112 my $status = system($cmd);
113  
114 my $response;
115 if ($status == 0) {
116  
117 $response = new HTTP::Response(&HTTP::Status::RC_OK);
118  
119 # The CGI script prints the response to stdout. Headers are
120 # followed by a blank line, then the content of the response
121 # appears.
122 open F, "<$out_name" || assert(0);
123 my $seen_break = 0;
124 while (<F>) {
125 $_ =~ s/\r//g;
126 if ( $seen_break ) {
127 $response->add_content($_);
128 } elsif ( $_ =~ /^$/ ) {
129 $seen_break = 1;
130 } else {
131 ($_ =~ /^(\S+)\s*:\s*(.*)/) || assert(0);
132 $response->header($1,$2);
133 if ( $1 eq "Status" ) {
134 my $status_text = $2;
135 ($status_text =~ /^([0-9]+)\s*(.*)/) || assert(0);
136 $response->code($1);
137 $response->message($2);
138 }
139 }
140 }
141 close F || assert(0);
142  
143 } else {
144  
145 # The CGI script failed. Return code 500 and stderr.
146 $response = new HTTP::Response(&HTTP::Status::RC_INTERNAL_SERVER_ERROR);
147 $response->header("Content-Type","text/plain");
148 open F, "<$err_name" || assert(0);
149 while (<F>) {
150 $response->add_content($_);
151 }
152 close F || assert(0);
153 }
154  
155 unlink($in_name, $out_name, $err_name);
156 return $response;
157 }
158  
159 ########################################################################
160  
161 package SOAP::Clean::Misc::Object;
162  
163 # fixmebad
164  
165 # Inheritance
166 our @ISA = qw();
167  
168 sub new {
169 my $proto = shift;
170 my $class = ref($proto) || $proto;
171 my $self = {};
172 bless $self, $class;
173 $self->initialize(@_);
174 return $self;
175 }
176  
177 # "Virtual" methods
178 # $self->_print("message");
179  
180 ########################################################################
181  
182 1;