HTTP/Example/AddXFooHeader.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« HTTP.pm ↑ Summary HTTP/Example/BlockContentType.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::Example::AddXFooHeader;
5 1 ▶use base 'Net::IMP::HTTP::Connection';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 use fields qw(pos1);
7 use Net::IMP;
8 use Net::IMP::HTTP;
9  
10 sub RTYPES { ( IMP_PASS, IMP_PREPASS, IMP_REPLACE ) }
11 sub new_analyzer {
12 my ($factory,%args) = @_;
13 my $analyzer = $factory->SUPER::new_analyzer(%args);
14  
15 # we are not interested in request data, only response
16 # but for http we need to see requests to pair with responses
17 $analyzer->run_callback([IMP_PREPASS,0,IMP_MAXOFFSET]);
18 return $analyzer;
19 }
20  
21 # data supports IMP_DATA_HTTP and IMP_DATA_HTTPRQ interface
22 sub data {
23 my ($self,$dir,$data,$offset,$type) = @_;
24  
25 # request are handled by the pass maxoffset in new_analyzer
26 if ( $dir == 0 ) {
27 # if we speak httprq we can make the prepass to a pass
28 $self->run_callback([IMP_PASS,0,IMP_MAXOFFSET])
29 if $type == IMP_DATA_HTTPRQ_HEADER;
30 return;
31 }
32  
33 $self->{pos1} += length($data);
34 if ( $type == IMP_DATA_HTTP_HEADER
35 or $type == IMP_DATA_HTTPRQ_HEADER ) {
36 $data =~s{\n}{\nX-Foo: bar\r\n};
37 my @rv = [
38 IMP_REPLACE,
39 1,
40 $self->{pos1},
41 $data
42 ];
43 if ( $type == IMP_DATA_HTTPRQ_HEADER ) {
44 # for httprq interface we can pass until end of request (maxoffset)
45 push @rv, [ IMP_PASS,1,IMP_MAXOFFSET ];
46 }
47 $self->run_callback(@rv);
48  
49 } else {
50 # for http we might get more requests
51 # these might be chunking etc so we probably don't know their length
52 # therefore we can pass only data we get until we get the next
53 # response header
54 $self->run_callback([ IMP_PASS,1,$self->{pos1} ]);
55 }
56 }
57  
58  
59 1;
60 __END__
« HTTP.pm ↑ Summary HTTP/Example/BlockContentType.pm »