line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::MonM::Checkit::HTTP; # $Id: HTTP.pm 116 2022-08-27 08:57:12Z abalama $ |
2
|
1
|
|
|
1
|
|
372
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
22
|
|
3
|
1
|
|
|
1
|
|
3
|
use utf8; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
4
|
|
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.01 |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 SYNOPSIS |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
Enable yes |
20
|
|
|
|
|
|
|
Type http |
21
|
|
|
|
|
|
|
URL http://www.example.com |
22
|
|
|
|
|
|
|
Method POST |
23
|
|
|
|
|
|
|
TimeOut 180 |
24
|
|
|
|
|
|
|
Target code |
25
|
|
|
|
|
|
|
IsTrue 200 |
26
|
|
|
|
|
|
|
Content "Blah-Blah-Blah" |
27
|
|
|
|
|
|
|
Proxy "http://http.example.com:8001" |
28
|
|
|
|
|
|
|
Set X-Foo foo |
29
|
|
|
|
|
|
|
Set X-Bar bar |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# . . . |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head1 DESCRIPTION |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
Checkit HTTP subclass |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=head2 check |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
Checkit method. |
42
|
|
|
|
|
|
|
This is backend method of L |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
Returns: |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=over 4 |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=item B |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
The HTTP response code: 1xx, 2xx, 3xx, 4xx, 5xx or 0 |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=item B |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
The response content |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=item B |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
The HTTP response status line |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=item B |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
Method and URL of request |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=item B |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
0 if error occured and if code is 4xx or 5xx |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
1 if no errors and if code is 1xx, 2xx or 3xx |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=back |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head1 CONFIGURATION DIRECTIVES |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
The basic Checkit configuration options (directives) detailed describes in L |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=over 4 |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=item B |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
Content "Content for HTTP request" |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Specifies POST/PUT/PATCH request content |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
Example: |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
Set Content-Type text/plain |
87
|
|
|
|
|
|
|
Content "Content for POST HTTP request" |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
Default: no content |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=item B |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
Method GET |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Defines the HTTP method: GET, POST, PUT, HEAD, PATCH, DELETE, and etc. |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
Default: GET |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=item B |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
Proxy http://http.example.com:8001/ |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
Defines the proxy URL for a http/https requests |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
Default: no proxy |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=item B |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
Set X-Token mdffltrtkmdffltrtk |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
Defines HTTP request headers. This directive allows you set case sensitive HTTP headers. |
112
|
|
|
|
|
|
|
There can be several such directives. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
Examples: |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
Set User-Agent "MyAgent/1.00" |
117
|
|
|
|
|
|
|
Set X-Token "mdffltrtkmdffltrtk" |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=item B |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Timeout 1m |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Defines the timeout of HTTP request |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Default: 180 |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=item B |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
URL https://www.example.com |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Defines the URL for HTTP/HTTPS requests |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Default: http://localhost |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Examples: |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
URL https://user:password@www.example.com |
139
|
|
|
|
|
|
|
URL https://www.example.com |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=back |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head1 HISTORY |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
See C file |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head1 TO DO |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
See C file |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head1 BUGS |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
* none noted |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head1 SEE ALSO |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
L |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=head1 AUTHOR |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
Serż Minus (Sergey Lepenkov) L Eabalama@cpan.orgE |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head1 COPYRIGHT |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
Copyright (C) 1998-2022 D&D Corporation. All Rights Reserved |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head1 LICENSE |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or |
170
|
|
|
|
|
|
|
modify it under the same terms as Perl itself. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
See C file and L |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=cut |
175
|
|
|
|
|
|
|
|
176
|
1
|
|
|
1
|
|
35
|
use vars qw/$VERSION/; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
44
|
|
177
|
|
|
|
|
|
|
$VERSION = '1.01'; |
178
|
|
|
|
|
|
|
|
179
|
1
|
|
|
1
|
|
5
|
use Encode; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
68
|
|
180
|
1
|
|
|
1
|
|
6
|
use CTK::ConfGenUtil; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
56
|
|
181
|
1
|
|
|
1
|
|
1075
|
use URI; |
|
1
|
|
|
|
|
3486
|
|
|
1
|
|
|
|
|
22
|
|
182
|
1
|
|
|
1
|
|
508
|
use LWP::UserAgent(); |
|
1
|
|
|
|
|
27182
|
|
|
1
|
|
|
|
|
18
|
|
183
|
1
|
|
|
1
|
|
6
|
use HTTP::Request(); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
16
|
|
184
|
|
|
|
|
|
|
|
185
|
1
|
|
|
1
|
|
4
|
use App::MonM::Const qw/PROJECTNAME/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
186
|
1
|
|
|
1
|
|
5
|
use App::MonM::Util qw/set2attr getTimeOffset/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
38
|
|
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
use constant { |
189
|
1
|
|
|
|
|
355
|
DEFAULT_URL => "http://localhost", |
190
|
|
|
|
|
|
|
DEFAULT_METHOD => "GET", |
191
|
|
|
|
|
|
|
DEFAULT_TIMEOUT => 180, |
192
|
1
|
|
|
1
|
|
5
|
}; |
|
1
|
|
|
|
|
1
|
|
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
sub check { |
195
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
196
|
0
|
|
|
|
|
|
my $type = $self->type; |
197
|
0
|
0
|
0
|
|
|
|
return $self->maybe::next::method() unless $type && ($type eq 'http' or $type eq 'https'); |
|
|
|
0
|
|
|
|
|
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
# Init |
200
|
0
|
|
0
|
|
|
|
my $url = lvalue($self->config, 'url') || DEFAULT_URL; |
201
|
0
|
|
0
|
|
|
|
my $method = lvalue($self->config, 'method') || DEFAULT_METHOD; |
202
|
0
|
|
0
|
|
|
|
my $timeout = getTimeOffset(lvalue($self->config, 'timeout') || DEFAULT_TIMEOUT); |
203
|
0
|
|
|
|
|
|
my $attr = set2attr($self->config); |
204
|
0
|
|
0
|
|
|
|
my $content = lvalue($self->config, 'content') // ''; |
205
|
0
|
|
0
|
|
|
|
my $proxy = lvalue($self->config, 'proxy') || ""; |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
# Agent |
208
|
0
|
|
|
|
|
|
my $uri = URI->new($url); |
209
|
0
|
|
|
|
|
|
my $ua = LWP::UserAgent->new( |
210
|
|
|
|
|
|
|
agent => sprintf("%s/%s", PROJECTNAME, $VERSION), |
211
|
|
|
|
|
|
|
timeout => $timeout, |
212
|
|
|
|
|
|
|
protocols_allowed => ['http', 'https'], |
213
|
|
|
|
|
|
|
); |
214
|
0
|
|
|
|
|
|
$ua->default_header($_, value($attr, $_)) for (keys %$attr); |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
# Proxy |
217
|
0
|
0
|
|
|
|
|
$ua->proxy(['http', 'https'], $proxy) if $proxy; |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
# Prepare request data |
220
|
0
|
|
|
|
|
|
my $request = HTTP::Request->new(uc($method) => $uri); |
221
|
0
|
0
|
|
|
|
|
if ($method =~ /PUT|POST|PATCH/) { |
222
|
0
|
|
|
|
|
|
Encode::_utf8_on($content); |
223
|
0
|
|
|
|
|
|
$request->header('Content-Length' => length(Encode::encode("utf8", $content))); |
224
|
0
|
|
|
|
|
|
$request->content(Encode::encode("utf8", $content)); |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
# Request |
228
|
0
|
|
|
|
|
|
my $response = $ua->request($request); |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
# Result |
231
|
0
|
0
|
0
|
|
|
|
$self->status(($response->is_info || $response->is_success || $response->is_redirect) ? 1 : 0); |
232
|
0
|
0
|
0
|
|
|
|
$self->error($response->decoded_content // '') unless $self->status; |
233
|
0
|
|
|
|
|
|
$self->source(sprintf("%s %s", $method, $response->request->uri->canonical->as_string)); |
234
|
0
|
|
0
|
|
|
|
$self->message($response->status_line || ''); |
235
|
0
|
|
0
|
|
|
|
$self->code($response->code || 0); |
236
|
0
|
|
0
|
|
|
|
$self->content($response->decoded_content // ''); |
237
|
|
|
|
|
|
|
|
238
|
0
|
|
|
|
|
|
return; |
239
|
|
|
|
|
|
|
} |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
1; |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
__END__ |