File Coverage

blib/lib/XML/Compile/SOAP/Daemon/Dancer2/Handler.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 16 16 100.0


line stmt bran cond sub pod time code
1             package XML::Compile::SOAP::Daemon::Dancer2::Handler;
2 3     3   17 use warnings;
  3         3  
  3         72  
3 3     3   8 use strict;
  3         3  
  3         51  
4 3     3   8 use vars '$VERSION';
  3         2  
  3         114  
5             $VERSION = '0.1';
6              
7 3     3   369 use parent 'XML::Compile::SOAP::Daemon';
  3         202  
  3         15  
8              
9             use Log::Report 'xml-compile-soap-daemon';
10             use Encode;
11              
12              
13             use constant
14             { RC_OK => 200
15             , RC_METHOD_NOT_ALLOWED => 405
16             , RC_NOT_ACCEPTABLE => 406
17             , RC_SERVER_ERROR => 500
18             };
19              
20             #--------------------
21              
22              
23             sub init($)
24             { my ($self, $args) = @_;
25             $self->SUPER::init($args);
26             $self->_init($args);
27             $self;
28             }
29              
30             #------------------------------
31              
32             sub _init($)
33             { my ($self, $args) = @_;
34             $self->{preprocess} = $args->{preprocess};
35             $self->{postprocess} = $args->{postprocess};
36             $self;
37             }
38              
39              
40             my $parser = XML::LibXML->new;
41             sub handle($)
42             { my ($self, $dsl) = @_;
43              
44             notice __x"WSA module loaded, but not used"
45             if XML::Compile::SOAP::WSA->can('new') && !keys %{$self->{wsa_input}};
46             $self->{wsa_input_rev} = +{ reverse %{$self->{wsa_input}} };
47              
48             #return $self->sendWsdl($req)
49             #if $req->method eq 'GET' && uc($req->uri->query || '') eq 'WSDL';
50              
51             my $method = $dsl->request->method;
52             my $ct = $dsl->request->content_type || 'text/plain';
53             $ct =~ s/\;\s.*//;
54              
55             my ($rc, $msg, $err, $content, $mime);
56             if($method ne 'POST' && $method ne 'M-POST')
57             { ($rc, $msg) = (RC_METHOD_NOT_ALLOWED, 'only POST or M-POST');
58             $err = 'attempt to connect via GET';
59             }
60             elsif($ct !~ m/\bxml\b/)
61             { ($rc, $msg) = (RC_NOT_ACCEPTABLE, 'required is XML');
62             $err = 'content-type seems to be '.$ct.', must be some XML';
63             }
64             else
65             { my $charset = $dsl->request->headers->content_type_charset || 'ascii';
66             my $xmlin = try { $parser->parse_string( decode( $charset, $dsl->request->content ) ); };
67              
68             if( $@ ) {
69             ($rc, $msg, $err) = $self->faultInvalidXML($@->died);
70             } else {
71             my $version = undef;
72             $xmlin= $xmlin->documentElement
73             if $xmlin->isa('XML::LibXML::Document');
74              
75             my $local = $xmlin->localName;
76              
77             if( $local eq 'Envelope' ) {
78             my $envns = $xmlin->namespaceURI || '';
79             my $proto = XML::Compile::SOAP->fromEnvelope($envns);
80             if( $proto ) {
81             $version = $proto->version;
82             }
83             }
84             my $action = $dsl->request->header('SOAPAction') || $dsl->request->header('Action') || $dsl->request->header('action') || '';
85             $action =~ s/["'\s]//g; # sometimes illegal quoting and blanks "
86             ($rc, $msg, my $xmlout) = $self->process($xmlin, $dsl, $action);
87              
88             if(UNIVERSAL::isa($xmlout, 'XML::LibXML::Document'))
89             {
90             $content = $xmlout->toString($rc == RC_OK ? 0 : 1);
91             if( $version eq "SOAP11" ) {
92             $mime = 'text/xml; charset="utf-8"';
93             } else {
94             $mime = "application/soap+xml; charset=utf-8";
95             }
96             }
97             else
98             {
99             $err = $xmlout;
100             }
101             }
102             }
103              
104             if( $err ) {
105             $content = $err;# $bytes = "[$rc] $err\n";
106             $mime = 'text/plain';
107             }
108             $dsl->status( $rc );
109             $dsl->content_type( $mime );
110             $dsl->header( Warning => "199 $msg" ) if length( $msg );
111             #$dsl->content_length(length $bytes);
112             return $content;
113             }
114              
115             1;