HTTP/Host.pm untested

filter: Statementstmt 0.0% 0 / 85 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 169 CC 15 SCAR 51.0
CC
15
Cov
0%
parse
240.0
empty
2.0
valid_ip_octet
2.0
CRAP 164.3
1 # HTTP Host; implements hostname and IP address standards
2 #
3 # subclass of HTTP:Part; see that for interface requirements
4 #
5  
6 package IDS::DataSource::HTTP::Host;
7  
8 use strict;
9 use warnings;
10 use Carp qw(carp confess);
11 use base qw(IDS::DataSource::HTTP::Part);
12 use IDS::Utils qw(split_value);
13  
14 $IDS::DataSource::HTTP::Host::VERSION = "1.0";
15  
16 sub empty {
17 my $self = shift;
18 undef $self->{"data"}, $self->{"tokens"};
19 }
20  
21 sub parse {
22 my $self = shift;
23 my $hostname = $self->{"data"}; # convenience
24 my (@tokens, $valid, $reason, $hostpat);
25  
26 $self->mesg(1, *parse{PACKAGE} . "::parse: host '$hostname'");
27  
28 $reason = "";
29 if ($hostname =~ /^[\.\d]+$/) {
30 $self->mesg(2, *parse{PACKAGE} . "::parse: IP addr");
31 if (${$self->{"params"}}{"recognize_hostnames"}) {
32 if ($hostname =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/) {
33 $valid = 1;
34 } else {
35 $reason = "IP addr has bad form.";
36 $valid = 0;
37 }
38  
39 if ($valid && !(valid_ip_octet($1) && valid_ip_octet($2) &&
40 valid_ip_octet($3) && valid_ip_octet($4))) {
41 $valid = 0;
42 $reason = " IP addr has bad octet.";
43 }
44  
45 if ($valid && ${$self->{"params"}}{"lookup_hosts"}) {
46 my @results = gethostbyaddr($hostname, "AF_INET");
47 if ($#results <= 3) {
48 $valid = 0;
49 $reason = "IP addr fails DNS lookup.";
50 }
51 }
52 if ($valid) {
53 push @tokens, "Valid IP address";
54 } else {
55 my $pmsg = *parse{PACKAGE} . "::parse: In " .
56 ${$self->{"params"}}{"source"} .
57 " invalid IP address '$hostname': $reason\n";
58 $self->warn($pmsg, \@tokens, "!Invalid IP address: $reason");
59 }
60 } else {
61 #push @tokens, split_value("IP addr part", '\.', $hostname);
62 push @tokens, "IP addr: $hostname";
63 }
64 } else {
65 $self->mesg(2, *parse{PACKAGE} . "::parse: hostname");
66 if (${$self->{"params"}}{"recognize_hostnames"}) {
67 # hostname parsing from RFC 952, 1034, 1123
68 # <label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]
69 $hostpat = qr/([A-Za-z0-9][-A-Za-z0-9]*[A-Za-z0-9]*)/;
70 if (($hostname =~ /^($hostpat)(\.$hostpat)*\.?$/)
71 && $1 !~ /^\d+$/
72 && length($hostname) <= 255) {
73 $valid = 1;
74 } elsif ($hostname eq "unknown") {
75 ### Non-standard, but some proxies use this.
76 push @tokens, "Non-standard hostname 'unknown'";
77 $valid = 1;
78 } elsif ($hostname =~ /^([\w.]+)(, [\w.]+)+$/) {
79 ### hostname list, nonstandard but common
80 ### Note that \w above means we accept _ in a hostname---illegal
81 my @hosts = split /, /, $hostname;
82 for my $host (@hosts) {
83 my $sub = new IDS::DataSource::HTTP::Host($self->{"params"}, $host);
84 push @tokens, $sub->tokens;
85 }
86 $valid = 1; # not necessarily true, since children may have had invalid hosts; we
87 # count on them to do their own reporting of problems.
88 } else {
89 $valid = 0;
90 $reason = "Hostname does not match pattern";
91 }
92  
93 if ($valid && ${$self->{"params"}}{"lookup_hosts"}) {
94 my @results = gethostbyname($hostname);
95 if ($#results <= 3) {
96 $valid = 0;
97 $reason = "Hostname fails DNS lookup";
98 }
99 }
100 if ($valid) {
101 push @tokens, "Valid hostname";
102 } else {
103 my $pmsg = *parse{PACKAGE} . "::parse: In " .
104 ${$self->{"params"}}{"source"} .
105 " invalid hostname '$hostname': $reason\n";
106 $self->warn($pmsg, \@tokens, "!Invalid hostname: $reason");
107 }
108 } else {
109 #push @tokens, split_value("Host part", '\.', $hostname);
110 push @tokens, "Hostname: $hostname";
111 }
112 }
113  
114 $self->mesg(2, *parse{PACKAGE} . "::parse: tokens\n ",
115 "\n ", \@tokens);
116 $self->{"tokens"} = \@tokens;
117 }
118  
119 sub valid_ip_octet {
120 my $octet = shift;
121 defined($octet) or confess "missing octet in valid_ip_octet";
122 return $octet >= 0 && $octet <= 255;
123 }
124  
125 # accessor functions not provided by the superclass
126  
127 =head1 AUTHOR INFORMATION
128  
129 Copyright 2005-2007, Kenneth Ingham. All rights reserved.
130  
131 This library is free software; you can redistribute it and/or modify
132 it under the same terms as Perl itself.
133  
134 Address bug reports and comments to: ids_test at i-pi.com. When sending
135 bug reports, please provide the versions of IDS::Test.pm, IDS::Algorithm.pm,
136 IDS::DataSource.pm, the version of Perl, and the name and version of the
137 operating system you are using. Since Kenneth is a PhD student, the
138 speed of the reponse depends on how the research is proceeding.
139  
140 =head1 BUGS
141  
142 Please report them.
143  
144 =head1 SEE ALSO
145  
146 L<IDS::Algorithm>, L<IDS::DataSource>
147  
148 =cut
149  
150 1;