Daemon/LWPutil.pm untested

filter: Statementstmt 0.0% 0 / 124 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 36 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 226 SCAR 54.8
CC
15
Cov
0%
lwp_make_response
30.0
lwp_run_request
20.0
lwp_handle_connection
12.0
CRAP 240.0
1 # Copyrights 2007-2018 by [Mark Overmeer <markov@cpan.org>].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.02.
5 # This code is part of distribution XML-Compile-SOAP-Daemon. Meta-POD
6 # processed with OODoc into POD and HTML manual-pages. See README.md
7 # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8  
9 package XML::Compile::SOAP::Daemon::LWPutil;
10 use vars '$VERSION';
11 $VERSION = '3.14';
12  
13 use parent 'Exporter';
14  
15 use warnings;
16 use strict;
17  
18  
19 our @EXPORT = qw(
20 lwp_action_from_header
21 lwp_add_header
22 lwp_handle_connection
23 lwp_make_response
24 lwp_run_request
25 lwp_wsdl_response
26 lwp_socket_init
27 lwp_http11_connection
28 );
29  
30 use Log::Report 'xml-compile-soap-daemon';
31 use XML::Compile::SOAP::Util ':daemon';
32 use LWP;
33 use HTTP::Status qw/RC_OK RC_METHOD_NOT_ALLOWED RC_NOT_ACCEPTABLE/;
34  
35 sub lwp_add_header($$@);
36 sub lwp_handle_connection($@);
37 sub lwp_run_request($$;$$);
38 sub lwp_make_response($$$$;$);
39 sub lwp_action_from_header($);
40  
41  
42  
43 our @default_headers;
44 BEGIN
45 { foreach my $pkg (qw/XML::Compile XML::Compile::SOAP
46 XML::Compile::SOAP::Daemon XML::LibXML LWP/)
47 { no strict 'refs';
48 my $version = ${"${pkg}::VERSION"} || 'undef';
49 (my $field = "X-$pkg-Version") =~ s/\:\:/-/g;
50 push @default_headers, $field => $version;
51 }
52 }
53  
54 sub lwp_add_header($$@) { push @default_headers, @_ }
55  
56  
57 my $wsdl_response;
58 sub lwp_wsdl_response(;$$)
59 { @_ or return $wsdl_response;
60  
61 my ($file, $ft) = @_;
62 $file && !ref $file
63 or return $wsdl_response = $file;
64  
65 local *SRC;
66 open SRC, '<:raw', $file
67 or fault __x"cannot read wsdl file {file}", file => $file;
68 local $/;
69 my $spec = <SRC>;
70 close SRC;
71  
72 $ft ||= 'application/wsdl+xml';
73 $wsdl_response = HTTP::Response->new
74 ( RC_OK, "WSDL specification"
75 , [ @default_headers
76 , "Content-Type" => "$ft; charset=utf-8"
77 ]
78 , $spec
79 );
80 }
81
82  
83 sub lwp_handle_connection($@)
84 { my ($connection, %args) = @_;
85 my $expires = $args{expires};
86 my $maxmsgs = $args{maxmsgs};
87 my $reqbonus = $args{reqbonus};
88 my $postproc = $args{postprocess};
89  
90 local $SIG{ALRM} = sub { die "timeout\n" };
91  
92 my $timeleft;
93 while(($timeleft = $expires - time) > 0.01)
94 { alarm $timeleft if $timeleft;
95 my $request = $connection->get_request;
96 alarm 0;
97 $request or last;
98  
99 my $response = lwp_run_request $request, $args{handler}
100 , $connection, $postproc;
101  
102 $connection->force_last_request if $maxmsgs==1;
103 $connection->send_response($response);
104  
105 --$maxmsgs or last;
106 $expires += $reqbonus;
107 }
108 }
109  
110  
111 sub lwp_run_request($$;$$)
112 { my ($request, $handler, $connection, $postproc) = @_;
113  
114 # my $client = $connection->peerhost;
115 return $wsdl_response
116 if $wsdl_response
117 && $request->method eq 'GET'
118 && uc($request->uri->query || '') eq 'WSDL';
119  
120 if($request->method !~ m/^(?:M-)?POST/ )
121 { return lwp_make_response $request
122 , RC_METHOD_NOT_ALLOWED
123 , 'only POST or M-POST'
124 , "attempt to connect via ".$request->method;
125 }
126  
127 my $media = $request->content_type || 'text/plain';
128 $media =~ m{[/+]xml$}i
129 or return lwp_make_response $request
130 , RC_NOT_ACCEPTABLE
131 , 'required is XML'
132 , "content-type seems to be $media, must be some XML";
133  
134 my $action = lwp_action_from_header $request;
135 my $ct = $request->header('Content-Type');
136 my $charset = $ct =~ m/\;\s*type\=(["']?)([\w-]*)\1/ ? $2: 'utf-8';
137 my $xmlin = $request->decoded_content(charset => $charset, ref => 1);
138  
139 my ($status, $status_msg, $xml)
140 = $handler->($xmlin, $request, $action);
141  
142 lwp_make_response $request, $status, $status_msg, $xml, $postproc;
143 }
144  
145  
146 sub lwp_make_response($$$$;$)
147 { my ($request, $status, $msg, $body, $postproc) = @_;
148  
149 my $response = HTTP::Response->new($status, $msg);
150 $response->header(@default_headers);
151 $response->protocol($request->protocol); # match request's
152  
153 my $s;
154 if(UNIVERSAL::isa($body, 'XML::LibXML::Document'))
155 { $s = $body->toString($status == RC_OK ? 0 : 1);
156 $response->header('Content-Type' => 'text/xml; charset=utf-8');
157 }
158 else
159 { $s = "[$status] $body";
160 $response->header(Content_Type => 'text/plain');
161 }
162  
163 $postproc->($request, $response, $status, \$s)
164 if $postproc;
165  
166 $response->content_ref(\$s);
167 { use bytes; $response->header('Content-Length' => length $s); }
168  
169 if(substr($request->method, 0, 2) eq 'M-')
170 { # HTTP extension framework. More needed?
171 $response->header(Ext => '');
172 }
173  
174 $response;
175 }
176  
177  
178 sub lwp_action_from_header($)
179 { my ($request) = @_;
180  
181 my $action;
182 if($request->method eq 'POST')
183 { $action = $request->header('SOAPAction');
184 }
185 elsif($request->method eq 'M-POST')
186 { # Microsofts HTTP Extension Framework
187 my $http_ext_id = '"' . MSEXT . '"';
188 my $man = first { m/\Q$http_ext_id\E/ } $request->header('Man');
189 defined $man or return undef;
190  
191 $man =~ m/\;\s*ns\=(\d+)/ or return undef;
192 $action = $request->header("$1-SOAPAction");
193 }
194 else
195 { return undef;
196 }
197  
198 defined $action or return;
199  
200 $action =~ s/["'\s]//g; # often wrong blanks and quotes
201 $action;
202 }
203  
204  
205 sub lwp_socket_init($)
206 { my $socket = shift;
207 my $http11_impl = $socket->isa('IO::Socket::SSL')
208 ? 'HTTP::Daemon::SSL' : 'HTTP::Daemon';
209  
210 eval "require $http11_impl";
211 error $@ if $@;
212 }
213  
214  
215 sub lwp_http11_connection($$)
216 { my ($daemon, $client) = @_;
217 my $http11_impl = $client->isa('IO::Socket::SSL')
218 ? 'HTTP::Daemon::ClientConn::SSL' : 'HTTP::Daemon::ClientConn';
219  
220 # Ugly hack: hijack the HTTP11 implementation of HTTP::Daemon
221 my $connection = bless $client, $http11_impl;
222 ${*$connection}{httpd_daemon} = $daemon;
223 $connection;
224 }
225  
226  
227 #------------------------------
228  
229 1;