HTTP/Example/LogFormData.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 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« HTTP/Example/BlockContentType.pm ↑ Summary HTTP/Example/SaveResponse.pm »
1 # sample IMP plugin to log formulare data
2 # e.q query_string and POST data
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 package Net::IMP::HTTP::Example::LogFormData;
7 1 ▶use base 'Net::IMP::HTTP::Connection';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 no if $] >= 5.017011, warnings => 'experimental::smartmatch';
9  
10 use fields (
11 'rqbuf', # buffered data from request
12 'req', # HTTP::Request object for request header
13 'info', # collection of infos for logging after request end
14 'btype', # content type from request body, eg.
15 # application/x-www-form-urlencoded or multipart/form-data
16 );
17  
18 use Net::IMP qw(:DEFAULT :log); # constants
19 require HTTP::Request;
20 use Net::IMP::Debug;
21  
22 sub RTYPES {
23 # we don't change anything but need to analyze, so we can PREPASS
24 # everything initially until Inf and later upgrade it to PASS
25 # because we are only interested in request header and body, data
26 # from server can be passed from the beginning
27 return (
28 IMP_PREPASS,
29 IMP_PASS,
30 IMP_DENY, # on parsing errors
31 IMP_LOG, # somewhere to log the info about form data
32 );
33 }
34  
35 sub new_analyzer {
36 my ($class,%args) = @_;
37 my $self = $class->SUPER::new_analyzer(%args);
38 $self->run_callback(
39 # prepass all from request
40 [ IMP_PREPASS,0,IMP_MAXOFFSET ],
41 # we don't even need to look at response
42 [ IMP_PASS,1,IMP_MAXOFFSET ],
43 );
44 return $self;
45 }
46  
47  
48 sub request_hdr {
49 my ($self,$hdr) = @_;
50 my $req = $self->{req} = HTTP::Request->parse($hdr) or do {
51 # failed to parse
52 $self->run_callback(
53 [ IMP_DENY,0,"failed to parse request header" ]);
54 return;
55 };
56  
57 $self->{rqbuf} = '';
58 $self->{info} = undef;
59 $self->{btype} = undef;
60  
61 if ( my @qp = $req->uri->query_form ) {
62 #debug("got query_string @qp");
63 my @param;
64 for(my $i=0;$i<@qp;$i+=2 ) {
65 push @param,[ $qp[$i], $qp[$i+1] ];
66 }
67 $self->{info}{'header.query_string'} = \@param
68 }
69  
70 my $ct = $req->content_type;
71 if ( $ct && $req->method eq 'POST' and
72 $ct ~~ ['application/x-www-form-urlencoded','multipart/form-data']
73 ){
74 #debug("got content-type $ct");
75 $self->{btype} = $ct;
76 } else {
77 # no need to analyze further
78 my $len = $req->content_length // 0;
79 #debug("no or no interesting body");
80 $self->_log_formdata() if $self->{info};
81 $self->{rqbuf} = ''; # throw away
82 $self->run_callback( [ IMP_PASS,0, $self->offset(0) + $len ]);
83 }
84 }
85  
86 sub request_body {
87 my ($self,$data,$offset) = @_;
88 $offset and die "gaps should not happen";
89  
90 if (( $data//'') eq '' ) {
91 # eof
92 # parse body if necessary
93 #debug("eof on $dir");
94 if ( ! $self->{btype} ) {
95 } elsif ( $self->{btype} eq 'application/x-www-form-urlencoded' ) {
96 my @param;
97 for( split( /\&/,$self->{rqbuf}) ) {
98 my ($k,$v) = split('=',$_,2);
99 for($k,$v) {
100 defined($_) or next;
101 s{\+}{ }g;
102 s{%([\da-fA-F]{2})}{ chr(hex($1)) }esg;
103 }
104 push @param,[$k,$v];
105 }
106 $self->{info}{'body.urlencoded'} = \@param;
107
108 } elsif ( $self->{btype} eq 'multipart/form-data' ) {
109 my (undef,$boundary) = $self->{req}->header('content-type')
110 =~m{;\s*boundary=(\"?)([^";,]+)\1}i;
111 if ( ! $boundary ) {
112 $self->run_callback([
113 IMP_DENY,0,
114 "missing boundary for multipart/form-data"
115 ]);
116 }
117 # we might use MIME:: heere, but this would be yet another non-CORE
118 # dependency :(
119 # this is quick and dirty and we just skip param on errors, but
120 # this is just a demo!
121 my @param;
122 for my $part ( split(
123 m{^--\Q$boundary\E(?:--)?\r?\n}m,
124 $self->{rqbuf} )) {
125 $part =~m{\A(.*?(\r?\n))\2(.*)}s or next;
126 my ($hdr,$v) = ($1,$3);
127 my ($cd) = $hdr =~m{^Content-Disposition:[ \t]*(.*(?:\r?\n[ \t].*)*)}mi
128 or do {
129 debug("no content-disposition in multipart header: $hdr");
130 next;
131 };
132 $cd =~s{\r?\n}{}g;
133 my $name = $cd =~m{;\s*name=(?:\"([^\"]+)\"|([^\s\";]+))} && ($1||$2);
134 $name or do {
135 debug("no name in content-disposition in multipart header: $hdr");
136 next;
137 };
138 my $fname = $cd =~m{;\s*filename=(?:\"([^\"]+)\"|([^\s\";]+))} && ($1||$2);
139 $v =~s{\r?\n\Z}{};
140 $v = "UPLOAD:$fname (".length($v)." bytes)" if $fname; # don't display content of file
141 push @param, [$name,$v];
142 }
143 $self->{info}{'body.multipart'} = \@param;
144 } else {
145 # should not happen, we set btype only if we can handle the type
146 die "unhandled POST content-type $self->{btype}"
147 }
148 $self->_log_formdata();
149  
150 } elsif ( $self->{btype} ) {
151 # add to buf to analyze later
152 $self->{rqbuf} .= $data;
153 }
154 }
155  
156 # these should not be reached
157 sub response_hdr {}
158 sub response_body {}
159 sub any_data {}
160 sub chunk_header {}
161 sub chunk_trailer {}
162  
163 sub _log_formdata {
164 my $self = shift;
165 my $info = $self->{info} or return;
166 # report form information if any, preferable as YAML, but fall back to
167 # Data::Dumper, which is in core
168 my $text;
169 if ( eval { require YAML } ) {
170 $text = YAML::Dump($info)
171 } elsif ( eval { require YAML::Tiny } ) {
172 $text = YAML::Tiny::Dump($info)
173 } elsif ( eval { require Data::Dumper }) {
174 $text = Data::Dumper->new([$info])->Terse(1)->Dump;
175 } else {
176 # Data::Dumper is perl core!
177 die "WTF, not even Data::Dumper is installed?";
178 }
179 $self->run_callback([ IMP_LOG,0,0,0,IMP_LOG_INFO,$text ]);
180 $self->{info} = undef;
181 }
182  
183 __END__
« HTTP/Example/BlockContentType.pm ↑ Summary HTTP/Example/SaveResponse.pm »