lib/Net/RDAP/Server.pm

filter: Statementstmt 45.1% 42 / 93 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 54.5% 12 / 22 Podpod 42.8% 3 / 7 totaltotal 36.0% 57 / 158 CC 22 SCAR 41.1
CC
22
Cov
33%
check_handlers
132.0
handle_request
110.0
set_handler
12.0
CRAP 60.8
↑ Summary lib/Net/RDAP/Server/EPPBackend.pm »
1 package Net::RDAP::Server;
2 # ABSTRACT: an RDAP server framework.
3 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Net::RDAP::Server::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Net::RDAP::Server::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use base qw(HTTP::Server::Simple::CGI);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use constant HTTP_VERSION => 'HTTP/1.1';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use vars qw($VERSION @METHODS @OBJECTS @SEARCHES @TYPES);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 $VERSION = '0.06';
17 1 @METHODS = qw(HEAD GET);
18 1 @OBJECTS = qw(domain nameserver entity ip autnum);
19 1 @SEARCHES = qw(domains nameservers entities);
20 1 @TYPES = (q{help}, @OBJECTS, @SEARCHES);
21  
22  
23 0 ▶sub set_handler {
Subroutine set_handler: not called CC=3 SCAR=24.8
Pod documented
24 0 my ($self, $method, $type, $callback) = @_;
25  
26 0 ▶ croak("Invalid method '$method'") unless ($self->method_allowed($method));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->method_allowed($method) 0 0 0
27 0 ▶ croak("Invalid type '$type'") unless ($self->type_allowed($type));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->type_allowed($type) 0 0 0
28  
29 0 $self->{_handlers}->{lc($type)}->{uc($method)} = $callback;
30 }
31  
32 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
Pod documented
33 0 my $self = shift;
34  
35 0 $self->check_handlers;
36  
37 0 $self->SUPER::run(@_);
38 }
39  
40 #
41 # This method implements the guts of this module, and implements the logic
42 # required to generate a response to an RDAP request.
43 #
44 0 ▶sub handle_request {
Subroutine handle_request: not called CC=10 SCAR=47.0
Pod documented
45 0 my ($self, $cgi) = @_;
46  
47 #
48 # Initialise request and response objects.
49 #
50 0 my $request = Net::RDAP::Server::Request->from_cgi($cgi);
51 0 my $response = Net::RDAP::Server::Response->new($request, $self);
52  
53 #
54 # Set the Server: header on all responses.
55 #
56 0 $response->header('server' => sprintf('%s/%s', ref($self), $VERSION));
57  
58 #
59 # Check the HTTP method.
60 #
61 0 ▶ if (!$self->method_allowed($request->method)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->method_allowed($request->method)) { } 0 0 0
62 0 $response->error(405, 'Bad Method');
63  
64 } else {
65 #
66 # Set the default status to 404, request handlers must override this
67 #
68 0 $response->code(404);
69 0 $response->message('Not Found');
70  
71 #
72 # Is a handler installed for this combination of type and method?
73 #
74 0 ▶ if (exists($self->{_handlers}->{$request->type})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_handlers"}{$request->type}) 0 0 0
75 0 ▶ if (!exists($self->{_handlers}->{$request->type}->{$request->method})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->{'_handlers'}{$request->type}{$request->method}) { } 0 0 0
76 0 $response->error(405, 'Bad Method');
77  
78 } else {
79 #
80 # Wrap callbacks in eval to catch exceptions so we can send a
81 # 500 response.
82 #
83 0 eval {
84 0 ▶ if (!$self->is_object($request->type)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->is_object($request->type)) { } 0 0 0
85 #
86 # Help or search request.
87 #
88 0 $self->{_handlers}->{$request->type}->{$request->method}->($response);
89  
90 } else {
91 #
92 # Object lookup.
93 #
94 0 ▶ if (!$request->object) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $request->object) { } 0 0 0
95 #
96 # Request did not specify an object.
97 #
98 0 $response->error(400, 'Bad Request');
99  
100 } else {
101 0 $self->{_handlers}->{$request->type}->{$request->method}->($response);
102  
103 }
104 }
105 };
106  
107 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
108 #
109 # Log error message to STDERR.
110 #
111 0 print STDERR $@;
112  
113 0 $response->error(500, 'Internal Server Error');
114 }
115 }
116 }
117 }
118  
119 #
120 # Ensure Content-Length header is present for responses to GET requests.
121 #
122 0 ▶ $response->header('content-length' => length($response->content)) unless (q{HEAD} eq $request->method);
Branch: 1 branch
BranchTrueFalseTotal
unless "HEAD" eq $request->method 0 0 0
123  
124 #
125 # Log to STDERR using the Combined Log Format.
126 #
127 0 ▶ print STDERR sprintf(
Condition0%0/4
$request->header('referer') || ''
l!l
00
$request->header('user-agent') || ''
l!l
00
MC/DC0%0/1
$request->header('referer') || ''
$request->header('referer')
MC/DC0%0/1
$request->header('user-agent') || ''
$request->header('user-agent')
Truth table0%0/2
A
A= $request->header('referer')
Aresult
11
00
Truth table0%0/2
A
A= $request->header('user-agent')
Aresult
11
00
128 "%s - - [%s] \"%s %s %s\" %03u %u \"%s\" \"%s\"\n",
129 $cgi->remote_addr,
130 DateTime->now->format_cldr('dd/MMM/YYYY:HH:mm:ss ZZZZZ'),
131 $request->method,
132 $request->uri->path_query,
133 HTTP_VERSION,
134 $response->code,
135 $response->header('content-length'),
136 $request->header('referer') || '',
137 $request->header('user-agent') || '',
138 );
139  
140 #
141 # Send the response.
142 #
143 0 print HTTP_VERSION.' '.$response->as_string;
144 }
145  
146 #
147 # This method returns a true value if the specified method is supported.
148 #
149 0 ▶sub method_allowed {
Subroutine method_allowed: not called CC=1 SCAR=6.9
Pod undocumented
150 0 my ($self, $method) = @_;
151 0 ▶ return any { $_ eq uc($method) } @METHODS;
Subroutine __ANON__: not called CC=1 SCAR=6.9
152 }
153  
154 #
155 # This method returns a true value if the specified query type is supported.
156 #
157 0 ▶sub type_allowed {
Subroutine type_allowed: not called CC=1 SCAR=6.9
Pod undocumented
158 0 my ($self, $type) = @_;
159 0 ▶ return any { $_ eq lc($type) } @TYPES;
Subroutine __ANON__: not called CC=1 SCAR=6.9
160 }
161  
162 #
163 # This method returns a true value if the specified query type is an object
164 # lookup.
165 #
166 0 ▶sub is_object {
Subroutine is_object: not called CC=1 SCAR=6.9
Pod undocumented
167 0 my ($self, $type) = @_;
168 0 ▶ return any { $_ eq lc($type) } @OBJECTS;
Subroutine __ANON__: not called CC=1 SCAR=6.9
169 }
170  
171 #
172 # This method is called by run() and will emit warnings if required request
173 # handlers have not been provided.
174 #
175 0 ▶sub check_handlers {
Subroutine check_handlers: not called CC=11 SCAR=48.8
Pod undocumented
176 0 my $self = shift;
177  
178 0 ▶ if (!exists($self->{_handlers}->{help})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->{'_handlers'}{'help'}) { } 0 0 0
179 0 carp("No handler(s) defined for 'help'");
180  
181 } else {
182 0 foreach my $method (@METHODS) {
183 0 ▶ carp("Missing handler for 'help' $method requests") unless (exists($self->{_handlers}->{help}->{$method}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_handlers"}{"help"}{$method} 0 0 0
184 }
185 }
186  
187 0 foreach my $type (@OBJECTS) {
188 0 ▶ if (exists($self->{_handlers}->{$type})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_handlers"}{$type}) 0 0 0
189 0 foreach my $method (@METHODS) {
190 0 ▶ carp("Missing handler for '$type' $method requests") unless (exists($self->{_handlers}->{$type}->{$method}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_handlers"}{$type}{$method} 0 0 0
191 }
192 }
193 }
194  
195 0 foreach my $type (@SEARCHES) {
196 0 ▶ if (exists($self->{_handlers}->{$type})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_handlers"}{$type}) 0 0 0
197 0 ▶ carp("Missing handler for '$type' GET requests") unless (exists($self->{_handlers}->{$type}->{q{GET}}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_handlers"}{$type}{"GET"} 0 0 0
198 }
199 }
200 }
201  
202  
203 1 1;
204  
205 __END__
↑ Summary lib/Net/RDAP/Server/EPPBackend.pm »