HTTP/Connection.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Adaptor/STREAM2HTTPConn.pm ↑ Summary HTTP/Request.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Net::IMP::HTTP::Connection;
5 1 ▶use base 'Net::IMP::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use fields qw(dispatcher pos);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Net::IMP::HTTP;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use Net::IMP;
9 use Carp 'croak';
10  
11 # just define a typical set, maybe need to be redefined in subclass
12 sub RTYPES {
13 my $factory = shift;
14 return (IMP_PASS, IMP_PREPASS, IMP_REPLACE, IMP_DENY, IMP_LOG)
15 }
16  
17 sub INTERFACE {
18 my $factory = shift;
19 my @rtx = my @rt = $factory->RTYPES;
20 push @rtx, IMP_DENY if ! grep { IMP_DENY == $_ } @rtx;
21 return (
22 [ IMP_DATA_HTTP, \@rt ],
23 [ IMP_DATA_HTTPRQ, \@rt ],
24 [ IMP_DATA_STREAM, \@rtx, 'Net::IMP::Adaptor::STREAM2HTTPConn' ],
25 );
26 }
27  
28 sub set_interface {
29 my ($factory,$interface) = @_;
30 my $newf = $factory->SUPER::set_interface($interface)
31 or return;
32 return $newf if $newf != $factory;
33  
34 # original factory, set dispatcher based on input data type
35 if ( $interface->[0] == IMP_DATA_HTTP ) {
36 $factory->{dispatcher} = {
37 IMP_DATA_HTTP_HEADER+0 => [
38 $factory->can('request_hdr'),
39 $factory->can('response_hdr'),
40 ],
41 IMP_DATA_HTTP_BODY+0 => [
42 $factory->can('request_body'),
43 $factory->can('response_body'),
44 ],
45 IMP_DATA_HTTP_CHKHDR+0 => [
46 undef,
47 $factory->can('rsp_chunk_hdr')
48 ],
49 IMP_DATA_HTTP_CHKTRAILER+0 => [
50 undef,
51 $factory->can('rsp_chunk_trailer')
52 ],
53 IMP_DATA_HTTP_DATA+0 => $factory->can('any_data'),
54 IMP_DATA_HTTP_JUNK+0 => $factory->can('junk_data')
55 }
56 } elsif ( $interface->[0] == IMP_DATA_HTTPRQ ) {
57 $factory->{dispatcher} = {
58 # HTTP request interface
59 IMP_DATA_HTTPRQ_HEADER+0 => [
60 $factory->can('request_hdr'),
61 $factory->can('response_hdr'),
62 ],
63 IMP_DATA_HTTPRQ_CONTENT+0 => [
64 $factory->can('request_body'),
65 $factory->can('response_body'),
66 ],
67 IMP_DATA_HTTPRQ_DATA+0 => $factory->can('any_data'),
68 }
69 } else {
70 die "unknown input data type $interface->[0]"
71 }
72  
73 return $factory;
74 }
75  
76 sub new_analyzer {
77 my ($factory,%args) = @_;
78 my $analyzer = $factory->SUPER::new_analyzer(%args);
79 $analyzer->{dispatcher} = $factory->{dispatcher};
80 return $analyzer;
81 }
82  
83  
84 # we can overide data to handle the types directly, but per default we
85 # dispatch to seperate methods
86 sub data {
87 my ($self,$dir,$data,$offset,$type) = @_;
88 $self->{pos}[$dir] = $offset if $offset;
89 $self->{pos}[$dir] += length($data);
90 my $disp = $self->{dispatcher};
91 my $sub = $disp->{$type+0} or croak("cannot dispatch type $type".Data::Dumper::Dumper($disp));
92 if ( ref($sub) eq 'ARRAY' ) {
93 $sub = $sub->[$dir] or croak("cannot dispatch type $type dir $dir");
94 $sub->($self,$data,$offset);
95 } else {
96 $sub->($self,$dir,$data,$offset);
97 }
98 }
99  
100 sub offset {
101 my ($self,$dir) = @_;
102 return $self->{pos}[$dir] // 0;
103 }
104  
105 ###########################################################################
106 # public interface
107 # most of these methods need to be implemented in subclass
108 ###########################################################################
109  
110 for my $subname (
111 'request_hdr', # ($self,$hdr)
112 'request_body', # ($self,$data,[$offset])
113 'response_hdr', # ($self,$hdr)
114 'response_body', # ($self,$data,[$offset])
115 'rsp_chunk_hdr', # ($self,$hdr)
116 'rsp_chunk_trailer', # ($self,$hdr)
117 'any_data', # ($self,$dir,$data,[$offset])
118 ) {
119 no strict 'refs';
120 *$subname = sub { croak("$subname needs to be implemented") }
121 }
122  
123 # by default simply ignore junk data (leading \n before message header)
124 sub junk_data {
125 my ($self,$dir,$data,$offset) = @_;
126 return
127 }
128  
129  
130  
131 1;
132 __END__
« Adaptor/STREAM2HTTPConn.pm ↑ Summary HTTP/Request.pm »