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/Stanza/StartTLS.pm »
1 package DJabberd::SAXHandler;
2 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 20 ▶use base qw(XML::SAX::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 20 ▶use DJabberd::XMLElement;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 20 ▶use DJabberd::StreamStart;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 20 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 20 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
10 0 my ($class, $conn) = @_;
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 0 ▶sub set_connection {
Subroutine set_connection: not called CC=2 SCAR=17.9
Pod undocumented
25 0 my ($self, $conn) = @_;
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 0 ▶sub cleanup {
Subroutine cleanup: not called CC=1 SCAR=6.9
Pod undocumented
38 0 my $self = shift;
39 0 $self->{on_end_capture} = undef;
40 }
41  
42 0 ▶sub depth {
Subroutine depth: not called CC=1 SCAR=6.9
Pod undocumented
43 0 return $_[0]{capture_depth};
44 }
45  
46 20 ▶use constant EVT_START_ELEMENT => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 20 ▶use constant EVT_END_ELEMENT => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 20 ▶use constant EVT_CHARS => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 0 ▶sub start_element {
Subroutine start_element: not called CC=7 SCAR=40.3
Pod documented
51 0 my ($self, $data) = @_;
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 0 ▶ $self->{on_end_capture} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
89 0 my ($doc, $events) = @_;
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 0 ▶sub characters {
Subroutine characters: not called CC=2 SCAR=17.9
Pod documented
113 0 my ($self, $data) = @_;
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 0 ▶sub end_element {
Subroutine end_element: not called CC=7 SCAR=40.3
Pod documented
126 0 my ($self, $data) = @_;
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 1047 ▶sub _nodes_from_events {
Subroutine _nodes_from_events: called CC=14 SCAR=26.4
147 1047 my ($evlist, $i, $end) = @_;
148 1047 ▶ $i ||= 0;
Condition100%2/2
$i ||= 0
l!l
726321
MC/DC100%1/1
$i ||= 0
$i
Truth table100%2/2
A
A= $i
Aresult
11
00
149 1047 ▶ $end ||= scalar @$evlist;
Condition100%2/2
$end ||= scalar @$evlist
l!l
726321
MC/DC100%1/1
$end ||= scalar @$evlist
$end
Truth table100%2/2
A
A= $end
Aresult
11
00
150 1047 my $nodelist = []; # what we're returning (nodes are text or XMLElement nodes)
151  
152 1047 ▶ while ($i < $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $end) 841 1047 1888
153 841 my $ev = $evlist->[$i++];
154  
155 841 ▶ if ($ev->[0] == EVT_CHARS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ev->[0] == 3) 115 726 841
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 726 ▶ if ($ev->[0] == EVT_START_ELEMENT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ev->[0] == 1) 726 0 726
166 726 my $depth = 1;
167 726 my $start_idx = $i; # index of first potential body node
168  
169 726 ▶ while ($depth && $i < $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth and $i < $end) 2177 726 2903
Condition66%2/3
$depth and $i < $end
!ll&&!rl&&r
72602177
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 2177 my $child = $evlist->[$i++];
171  
172 2177 ▶ if ($child->[0] == EVT_START_ELEMENT) {
Branch: 2 branches
BranchTrueFalseTotal
if ($child->[0] == 1) { } 618 1559 2177
elsif ($child->[0] == 2) { } 1344 215 1559
173 618 $depth++;
174 } elsif ($child->[0] == EVT_END_ELEMENT) {
175 1344 $depth--;
176 }
177 }
178 726 ▶ die "Finished events without reaching depth 0!" if $depth;
Branch: 1 branch
BranchTrueFalseTotal
if $depth 0 726 726
179  
180 726 my $end_idx = $i - 1; # (end - start) == number of child elements
181  
182 726 my $attr_sax = $ev->[1]{Attributes};
183 726 my $attr = {};
184 726 ▶ while (my $key = each %$attr_sax) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $key = each %$attr_sax)) 934 726 1660
185 934 $attr->{$key} = $attr_sax->{$key}{Value};
186 }
187  
188 push @$nodelist, DJabberd::XMLElement->new($ev->[1]{NamespaceURI},
189 $ev->[1]{LocalName},
190 $attr,
191 _nodes_from_events($evlist, $start_idx, $end_idx),
192 undef,
193 726 $ev->[1]{Prefix});
194 726 next;
195 }
196  
197 0 die "Unknown event in stream: $ev->[0]\n";
198 }
199 1047 return $nodelist;
200 }
201  
202  
203 20 1;
« DJabberd/Connection/ServerOut.pm ↑ Summary DJabberd/Stanza/StartTLS.pm »