DJabberd/SAXHandler.pm

filter: Statementstmt 47.5% 57 / 120 Branchbran 33.3% 16 / 48 Conditioncond 56.2% 9 / 16 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 55.5% 10 / 18 Podpod 42.8% 3 / 7 totaltotal 45.6% 100 / 219 CC 31 SCAR 31.0
CC
31
Cov
45%
start_element
56.0
end_element
56.0
_nodes_from_events
14.1
CRAP 22.2
« DJabberd/Connection/ServerOut.pm ↑ Summary DJabberd/Delivery/LocalVHosts.pm »
1 package DJabberd::SAXHandler;
2 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 17 ▶use base qw(XML::SAX::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use DJabberd::XMLElement;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use DJabberd::StreamStart;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 sub new {
10 0 ▶ my ($class, $conn) = @_;
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
11 0 my $self = $class->SUPER::new;
12  
13 0 ▶ if ($conn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conn) 0 0 0
14 0 $self->{"ds_conn"} = $conn;
15 0 weaken($self->{ds_conn});
16 }
17  
18 0 $self->{"capture_depth"} = 0; # on transition from 1 to 0, stop capturing
19 0 $self->{"on_end_capture"} = undef; # undef or $subref->($doc)
20 0 $self->{"events"} = []; # capturing events
21 0 return $self;
22 }
23  
24 sub set_connection {
25 0 ▶ my ($self, $conn) = @_;
Subroutine set_connection: not called CC=2 SCAR=17.9
Pod undocumented
26 0 $self->{ds_conn} = $conn;
27 0 ▶ if ($conn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conn) { } 0 0 0
28 0 weaken($self->{ds_conn});
29 } else {
30 # when sax handler is being put back onto the freelist...
31 0 $self->{on_end_capture} = undef;
32 }
33 }
34  
35 # called when somebody is about to destroy their reference to us, to make
36 # us clean up.
37 sub cleanup {
38 0 ▶ my $self = shift;
Subroutine cleanup: not called CC=1 SCAR=6.9
Pod undocumented
39 0 $self->{on_end_capture} = undef;
40 }
41  
42 sub depth {
43 0 ▶ return $_[0]{capture_depth};
Subroutine depth: not called CC=1 SCAR=6.9
Pod undocumented
44 }
45  
46 17 ▶use constant EVT_START_ELEMENT => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 17 ▶use constant EVT_END_ELEMENT => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 17 ▶use constant EVT_CHARS => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 sub start_element {
51 0 ▶ my ($self, $data) = @_;
Subroutine start_element: not called CC=7 SCAR=40.3
Pod documented
52 0 my $conn = $self->{ds_conn};
53  
54 # {=xml-stream}
55 0 ▶ if ($data->{NamespaceURI} eq "http://etherx.jabber.org/streams" &&
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"NamespaceURI"} eq "http://etherx.jabber.org/streams" and $data->{"LocalName"} eq "stream") 0 0 0
Condition0%0/3
$data->{"NamespaceURI"} eq "http://etherx.jabber.org/streams" and $data->{"LocalName"} eq "stream"
!ll&&!rl&&r
000
MC/DC0%0/2
$data->{"NamespaceURI"} eq "http://etherx.jabber.org/streams" and $data->{"LocalName"} eq "stream"
$data->{"NamespaceURI"} eq "http://etherx.jabber.org/streams" $data->{"LocalName"} eq "stream"
Truth table0%0/3
A and B
A= $data->{"NamespaceURI"} eq "http://etherx.jabber.org/streams"B= $data->{"LocalName"} eq "stream"
ABresult
0X0
100
111
56 $data->{LocalName} eq "stream") {
57  
58 0 my $ss = DJabberd::StreamStart->new($data);
59  
60 # when Connection.pm is prepping a new dummy root node, we legitimately
61 # get here without a connection, so we need to test for it:
62 0 ▶ $conn->on_stream_start($ss) if $conn;
Branch: 1 branch
BranchTrueFalseTotal
if $conn 0 0 0
63 0 return;
64 }
65  
66 # need a connection past this point.
67 0 ▶ return unless $conn;
Branch: 1 branch
BranchTrueFalseTotal
unless $conn 0 0 0
68  
69 # if they're not in a stream yet, bail.
70 0 ▶ unless ($conn->{in_stream}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($conn->{"in_stream"}) 0 0 0
71 0 $conn->stream_error('invalid-namespace');
72 0 return;
73 }
74  
75 0 ▶ if ($self->{capture_depth}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"capture_depth"}) 0 0 0
76 0 push @{$self->{events}}, [EVT_START_ELEMENT, $data];
77 0 $self->{capture_depth}++;
78 0 return;
79 }
80  
81 # start capturing...
82 0 $self->{"events"} = [
83 [EVT_START_ELEMENT, $data],
84 ];
85 0 $self->{capture_depth} = 1;
86  
87 0 Scalar::Util::weaken($conn);
88 $self->{on_end_capture} = sub {
89 0 ▶ my ($doc, $events) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
90 0 my $nodes = _nodes_from_events($events);
91 # {=xml-stanza}
92 0 my $t1 = Time::HiRes::time();
93 0 ▶ $conn->on_stanza_received($nodes->[0]) if $conn;
Branch: 1 branch
BranchTrueFalseTotal
if $conn 0 0 0
94 0 my $td = Time::HiRes::time() - $t1;
95  
96 # ring buffers for latency stats:
97 0 ▶ if ($td > $DJabberd::Stats::latency_log_threshold) {
Branch: 1 branch
BranchTrueFalseTotal
if ($td > $DJabberd::Stats::latency_log_threshold) 0 0 0
98 0 $DJabberd::Stats::stanza_process_latency_log[ $DJabberd::Stats::latency_log_index =
99 ($DJabberd::Stats::latency_log_index + 1)
100 % $DJabberd::Stats::latency_log_max_size
101 ] = [$td, $nodes->[0]->as_xml];
102 }
103  
104 0 $DJabberd::Stats::stanza_process_latency[ $DJabberd::Stats::latency_index =
105 ($DJabberd::Stats::latency_index + 1)
106 % $DJabberd::Stats::latency_max_size
107 ] = $td;
108 0 };
109 0 return;
110 }
111  
112 sub characters {
113 0 ▶ my ($self, $data) = @_;
Subroutine characters: not called CC=2 SCAR=17.9
Pod documented
114  
115 0 ▶ if ($self->{capture_depth}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"capture_depth"}) 0 0 0
116 0 push @{$self->{events}}, [EVT_CHARS, $data];
117 }
118  
119 # TODO: disconnect client if character data between stanzas? as
120 # long as it's not whitespace, because that's permitted as a
121 # keep-alive.
122  
123 }
124  
125 sub end_element {
126 0 ▶ my ($self, $data) = @_;
Subroutine end_element: not called CC=7 SCAR=40.3
Pod documented
127  
128 0 ▶ if ($data->{NamespaceURI} eq "http://etherx.jabber.org/streams" &&
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"NamespaceURI"} eq "http://etherx.jabber.org/streams" and $data->{"LocalName"} eq "stream") 0 0 0
Condition0%0/3
$data->{"NamespaceURI"} eq "http://etherx.jabber.org/streams" and $data->{"LocalName"} eq "stream"
!ll&&!rl&&r
000
MC/DC0%0/2
$data->{"NamespaceURI"} eq "http://etherx.jabber.org/streams" and $data->{"LocalName"} eq "stream"
$data->{"NamespaceURI"} eq "http://etherx.jabber.org/streams" $data->{"LocalName"} eq "stream"
Truth table0%0/3
A and B
A= $data->{"NamespaceURI"} eq "http://etherx.jabber.org/streams"B= $data->{"LocalName"} eq "stream"
ABresult
0X0
100
111
129 $data->{LocalName} eq "stream") {
130 0 ▶ $self->{ds_conn}->end_stream if $self->{ds_conn};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ds_conn"} 0 0 0
131 0 return;
132 }
133  
134 0 ▶ if ($self->{capture_depth}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"capture_depth"}) 0 0 0
135 0 push @{$self->{events}}, [EVT_END_ELEMENT, $data];
136 0 $self->{capture_depth}--;
137 0 ▶ return if $self->{capture_depth};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"capture_depth"} 0 0 0
138 0 my $doc = undef;
139 0 ▶ if (my $cb = $self->{on_end_capture}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $self->{"on_end_capture"}) 0 0 0
140 0 $cb->($doc, $self->{events});
141 }
142 0 return;
143 }
144 }
145  
146 sub _nodes_from_events {
147 987 ▶ my ($evlist, $i, $end) = @_;
Subroutine _nodes_from_events: called CC=14 SCAR=26.4
148 987 ▶ $i ||= 0;
Condition100%2/2
$i ||= 0
l!l
683304
MC/DC100%1/1
$i ||= 0
$i
Truth table100%2/2
A
A= $i
Aresult
11
00
149 987 ▶ $end ||= scalar @$evlist;
Condition100%2/2
$end ||= scalar @$evlist
l!l
683304
MC/DC100%1/1
$end ||= scalar @$evlist
$end
Truth table100%2/2
A
A= $end
Aresult
11
00
150 987 my $nodelist = []; # what we're returning (nodes are text or XMLElement nodes)
151  
152 987 ▶ while ($i < $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $end) 798 987 1785
153 798 my $ev = $evlist->[$i++];
154  
155 798 ▶ if ($ev->[0] == EVT_CHARS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ev->[0] == 3) 115 683 798
156 115 my $text = $ev->[1]{Data};
157 115 ▶ if (@$nodelist == 0 || ref $nodelist->[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$nodelist == 0 or ref $nodelist->[-1]) { } 103 12 115
Condition100%3/3
@$nodelist == 0 or ref $nodelist->[-1]
l!l&&r!l&&!r
881512
MC/DC100%2/2
@$nodelist == 0 or ref $nodelist->[-1]
@$nodelist == 0 ref $nodelist->[-1]
Truth table100%3/3
A or B
A= @$nodelist == 0B= ref $nodelist->[-1]
ABresult
1X1
011
000
158 103 push @$nodelist, $text;
159 } else {
160 12 $nodelist->[-1] .= $text;
161 }
162 115 next;
163 }
164  
165 683 ▶ if ($ev->[0] == EVT_START_ELEMENT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ev->[0] == 1) 683 0 683
166 683 my $depth = 1;
167 683 my $start_idx = $i; # index of first potential body node
168  
169 683 ▶ while ($depth && $i < $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth and $i < $end) 2058 683 2741
Condition66%2/3
$depth and $i < $end
!ll&&!rl&&r
68302058
MC/DC50%1/2
$depth and $i < $end
$depth $i < $end
Truth table66%2/3
A and B
A= $depthB= $i < $end
ABresult
0X0
100
111
170 2058 my $child = $evlist->[$i++];
171  
172 2058 ▶ if ($child->[0] == EVT_START_ELEMENT) {
Branch: 2 branches
BranchTrueFalseTotal
if ($child->[0] == 1) { } 580 1478 2058
elsif ($child->[0] == 2) { } 1263 215 1478
173 580 $depth++;
174 } elsif ($child->[0] == EVT_END_ELEMENT) {
175 1263 $depth--;
176 }
177 }
178 683 ▶ die "Finished events without reaching depth 0!" if $depth;
Branch: 1 branch
BranchTrueFalseTotal
if $depth 0 683 683
179  
180 683 my $end_idx = $i - 1; # (end - start) == number of child elements
181  
182 683 my $attr_sax = $ev->[1]{Attributes};
183 683 my $attr = {};
184 683 ▶ while (my $key = each %$attr_sax) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $key = each %$attr_sax)) 883 683 1566
185 883 $attr->{$key} = $attr_sax->{$key}{Value};
186 }
187  
188 push @$nodelist, DJabberd::XMLElement->new($ev->[1]{NamespaceURI},
189 $ev->[1]{LocalName},
190 683 $attr,
191 _nodes_from_events($evlist, $start_idx, $end_idx));
192 683 next;
193 }
194  
195 0 die "Unknown event in stream: $ev->[0]\n";
196 }
197 987 return $nodelist;
198 }
199  
200  
201 17 1;
« DJabberd/Connection/ServerOut.pm ↑ Summary DJabberd/Delivery/LocalVHosts.pm »