File Coverage

blib/lib/App/MonM/Checkit/HTTP.pm
Criterion Covered Total %
statement 33 57 57.8
branch 0 8 0.0
condition 0 22 0.0
subroutine 11 12 91.6
pod 1 1 100.0
total 45 100 45.0


line stmt bran cond sub pod time code
1             package App::MonM::Checkit::HTTP; # $Id: HTTP.pm 80 2019-07-08 10:41:47Z abalama $
2 1     1   7 use strict;
  1         1  
  1         22  
3 1     1   5 use utf8;
  1         1  
  1         3  
4              
5             =encoding utf-8
6              
7             =head1 NAME
8              
9             App::MonM::Checkit::HTTP - Checkit HTTP subclass
10              
11             =head1 VIRSION
12              
13             Version 1.00
14              
15             =head1 SYNOPSIS
16              
17            
18             Enable yes
19             Type http
20             URL http://www.example.com
21             Method GET
22             TimeOut 180
23             Target code
24             IsTrue 200
25             Content "Blah-Blah-Blah"
26             Set X-Foo foo
27             Set X-Bar bar
28              
29             # . . .
30              
31            
32              
33             =head1 DESCRIPTION
34              
35             Checkit HTTP subclass
36              
37             =head2 check
38              
39             Checkit method.
40             This is backend method of L
41              
42             Returns:
43              
44             =over 4
45              
46             =item B
47              
48             The HTTP response code: 1xx, 2xx, 3xx, 4xx, 5xx or 0
49              
50             =item B
51              
52             The response content
53              
54             =item B
55              
56             The HTTP response status line
57              
58             =item B
59              
60             Method and URL of request
61              
62             =item B
63              
64             0 if error occured and if code is 4xx or 5xx
65              
66             1 if no errors and if code is 1xx, 2xx or 3xx
67              
68             =back
69              
70             =head1 HISTORY
71              
72             See C file
73              
74             =head1 TO DO
75              
76             See C file
77              
78             =head1 BUGS
79              
80             * none noted
81              
82             =head1 SEE ALSO
83              
84             L
85              
86             =head1 AUTHOR
87              
88             Serż Minus (Sergey Lepenkov) L Eabalama@cpan.orgE
89              
90             =head1 COPYRIGHT
91              
92             Copyright (C) 1998-2019 D&D Corporation. All Rights Reserved
93              
94             =head1 LICENSE
95              
96             This program is free software; you can redistribute it and/or
97             modify it under the same terms as Perl itself.
98              
99             See C file and L
100              
101             =cut
102              
103 1     1   32 use vars qw/$VERSION/;
  1         2  
  1         35  
104             $VERSION = '1.00';
105              
106 1     1   4 use Encode;
  1         2  
  1         80  
107 1     1   6 use CTK::ConfGenUtil;
  1         1  
  1         65  
108 1     1   437 use URI;
  1         3692  
  1         23  
109 1     1   552 use LWP::UserAgent();
  1         28941  
  1         23  
110 1     1   8 use HTTP::Request();
  1         1  
  1         18  
111              
112 1     1   5 use App::MonM::Const qw/PROJECTNAME/;
  1         2  
  1         65  
113 1     1   5 use App::MonM::Util qw/set2attr/;
  1         1  
  1         44  
114              
115             use constant {
116 1         398 DEFAULT_URL => "http://localhost",
117             DEFAULT_METHOD => "GET",
118             DEFAULT_TIMEOUT => 180,
119 1     1   5 };
  1         2  
120              
121             sub check {
122 0     0 1   my $self = shift;
123 0           my $type = $self->type;
124 0 0 0       return $self->maybe::next::method() unless $type && $type eq 'http';
125              
126             # Init
127 0   0       my $url = value($self->config, 'url') || DEFAULT_URL;
128 0   0       my $method = value($self->config, 'method') || DEFAULT_METHOD;
129 0   0       my $timeout = value($self->config, 'timeout') || DEFAULT_TIMEOUT;
130 0           my $attr = set2attr($self->config);
131 0   0       my $content = value($self->config, 'content') // '';
132              
133             # Prepare request
134 0           my $uri = new URI($url);
135 0           my $ua = new LWP::UserAgent(
136             agent => sprintf("%s/%s", PROJECTNAME, $VERSION),
137             timeout => $timeout,
138             protocols_allowed => ['http', 'https'],
139             );
140 0           $ua->default_header($_, value($attr, $_)) for (keys %$attr);
141 0           my $request = new HTTP::Request(uc($method) => $uri);
142 0 0         if ($method =~ /PUT|POST|PATCH/) {
143 0           Encode::_utf8_on($content);
144 0           $request->header('Content-Length' => length(Encode::encode("utf8", $content)));
145 0           $request->content(Encode::encode("utf8", $content));
146             }
147 0           my $response = $ua->request($request);
148              
149             # Result
150 0 0 0       $self->status(($response->is_info || $response->is_success || $response->is_redirect) ? 1 : 0);
151 0 0 0       $self->error($response->decoded_content // '') unless $self->status;
152 0           $self->source(sprintf("%s %s", $method, $response->request->uri->canonical->as_string));
153 0   0       $self->message($response->status_line || '');
154 0   0       $self->code($response->code || 0);
155 0   0       $self->content($response->decoded_content // '');
156              
157 0           return;
158             }
159              
160             1;
161              
162             __END__