File Coverage

blib/lib/Uninets/Check/Modules/HTTP.pm
Criterion Covered Total %
statement 27 65 41.5
branch 0 6 0.0
condition n/a
subroutine 9 15 60.0
pod 3 5 60.0
total 39 91 42.8


line stmt bran cond sub pod time code
1             package Uninets::Check::Modules::HTTP;
2              
3 1     1   23043 use 5.10.1;
  1         5  
  1         47  
4 1     1   7 use strict;
  1         2  
  1         49  
5 1     1   6 use warnings FATAL => 'all';
  1         9  
  1         55  
6 1     1   974 use Moo;
  1         17726  
  1         7  
7 1     1   3162 use Getopt::Long qw(GetOptionsFromArray);
  1         16322  
  1         6  
8 1     1   1084 use Mojo::UserAgent;
  1         382587  
  1         19  
9 1     1   1135 use Try::Tiny;
  1         1731  
  1         99  
10 1     1   967 use JSON;
  1         18831  
  1         6  
11 1     1   187 use Time::HiRes;
  1         2  
  1         9  
12              
13             =head1 NAME
14              
15             Uninets::Check::Modules::HTTP - Uninets::Check module to check web urls.
16              
17             =head1 VERSION
18              
19             Version 0.03
20              
21             =cut
22              
23             our $VERSION = '0.03';
24              
25              
26             =head1 SYNOPSIS
27              
28             Uninets::Check::Modules::HTTP can check return status, response time and size of web resources.
29              
30             # to show available information on parameters run
31             unicheck --info HTTP
32              
33             =cut
34              
35             sub run {
36 0     0 0   my ($self, $action, @params) = @_;
37              
38 0           $self->$action(@params);
39             }
40              
41             =head1 ACTIONS
42              
43             =head2 status
44              
45             Get the status code of a call to an URL.
46              
47             unicheck HTTP status --url example.com
48              
49             =cut
50              
51             sub status {
52 0     0 1   my ($self, @params) = @_;
53              
54 0           my $url = 'http://127.0.0.1/';
55 0           my $redirects = 3;
56 0           my $format = 'num';
57              
58 0           GetOptionsFromArray([@params],
59             'url=s' => \$url,
60             'redirects=i' => \$redirects,
61             'format=s' => \$format,
62             );
63              
64 0           my $ua = Mojo::UserAgent->new->max_redirects($redirects);
65 0           my $tx = $ua->get($url);
66 0           my $status = $tx->res->code;
67 0           my $headers = $tx->res->content->headers->{headers};
68              
69 0           $self->_return($status, $headers, $format);
70             }
71              
72             =head2 size
73              
74             Get the size of a web resource in bytes.
75              
76             unicheck HTTP size --url example.com
77              
78             =cut
79              
80             sub size {
81 0     0 1   my ($self, @params) = @_;
82              
83 0           my $url = 'http://127.0.0.1/';
84 0           my $redirects = 3;
85 0           my $format = 'num';
86              
87 0           GetOptionsFromArray([@params],
88             'url=s' => \$url,
89             'redirects=i' => \$redirects,
90             'format=s' => \$format,
91             );
92              
93 0           my $ua = Mojo::UserAgent->new->max_redirects($redirects);
94 0           my $tx = $ua->get($url);
95 0           my $status = $tx->res->content->{raw_size};
96 0           my $headers = $tx->res->content->headers->{headers};
97              
98 0 0         defined $status ? $self->_return($status, $headers, $format) : $self->_return(-1, 'Something went wrong', $format);
99             }
100              
101             =head2 time
102              
103             Get the delivery time of a web resource in milliseconds.
104              
105             unicheck HTTP time --url example.com
106              
107             =cut
108              
109             sub time {
110 0     0 1   my ($self, @params) = @_;
111              
112 0           my $url = 'http://127.0.0.1/';
113 0           my $redirects = 3;
114 0           my $format = 'num';
115              
116 0           GetOptionsFromArray([@params],
117             'url=s' => \$url,
118             'redirects=i' => \$redirects,
119             'format=s' => \$format,
120             );
121              
122 0           my $ua = Mojo::UserAgent->new->max_redirects($redirects);
123 0           my $start = Time::HiRes::gettimeofday();
124 0           my $tx = $ua->get($url);
125 0           my $end = Time::HiRes::gettimeofday();
126             # elapsed time in ms
127 0           my $time = sprintf("%.2f", ($end - $start) * 1000);
128              
129 0           my $status = $tx->res->code;
130 0 0         defined $status ? $self->_return($time, {start => $start, end => $end}, $format) : $self->_return(-1, 'Something went wrong', $format);
131             }
132              
133             sub _return {
134 0     0     my ($self, $status, $value, $format) = @_;
135              
136 0 0         return JSON->new->encode(
137             {
138             message => $value,
139             status => $status,
140             }
141             ) if $format eq 'json';
142             # default last in case some non supported format was given
143 0           return $status; # if $format eq 'num'
144             }
145              
146             sub help {
147             {
148 0     0 0   description => 'Check web server and web app status',
149             actions => {
150             status => {
151             description => 'Get status code',
152             params => {
153             '--url' => 'Default: http://127.0.0.1/',
154             '--redirects' => 'Default: 3',
155             },
156             formats => {
157             'num' => 'Returns the status code',
158             'json' => 'Returns a JSON structure',
159             },
160             default_format => 'num',
161             },
162             size => {
163             description => 'Get page size',
164             params => {
165             '--url' => 'Default: http://127.0.0.1/',
166             '--redirects' => 'Default: 3',
167             },
168             formats => {
169             'num' => 'Returns the status code',
170             'json' => 'Returns a JSON structure',
171             },
172             default_format => 'num',
173             },
174             time => {
175             description => 'Get page delivery time',
176             params => {
177             '--url' => 'Default: http://127.0.0.1/',
178             '--redirects' => 'Default: 3',
179             },
180             formats => {
181             'num' => 'Returns the status code',
182             'json' => 'Returns a JSON structure',
183             },
184             default_format => 'num',
185             },
186             },
187             }
188             }
189              
190              
191             =head1 AUTHOR
192              
193             Matthias Krull, C<< <> >>
194              
195             =head1 BUGS
196              
197             Please report any bugs or feature requests to C, or through
198             the web interface at L. I will be notified, and then you'll
199             automatically be notified of progress on your bug as I make changes.
200              
201              
202              
203              
204             =head1 SUPPORT
205              
206             You can find documentation for this module with the perldoc command.
207              
208             perldoc Uninets::Check::Modules::HTTP
209              
210              
211             You can also look for information at:
212              
213             =over 4
214              
215             =item * RT: CPAN's request tracker (report bugs here)
216              
217             L
218              
219             =item * AnnoCPAN: Annotated CPAN documentation
220              
221             L
222              
223             =item * CPAN Ratings
224              
225             L
226              
227             =item * Search CPAN
228              
229             L
230              
231             =item * Github
232              
233             L
234              
235             =back
236              
237              
238             =head1 ACKNOWLEDGEMENTS
239              
240              
241             =head1 LICENSE AND COPYRIGHT
242              
243             Copyright 2013 Matthias Krull.
244              
245             This program is free software; you can redistribute it and/or modify it
246             under the terms of the the Artistic License (2.0). You may obtain a
247             copy of the full license at:
248              
249             L
250              
251             Any use, modification, and distribution of the Standard or Modified
252             Versions is governed by this Artistic License. By using, modifying or
253             distributing the Package, you accept this license. Do not use, modify,
254             or distribute the Package, if you do not accept this license.
255              
256             If your Modified Version has been derived from a Modified Version made
257             by someone other than you, you are nevertheless required to ensure that
258             your Modified Version complies with the requirements of this license.
259              
260             This license does not grant you the right to use any trademark, service
261             mark, tradename, or logo of the Copyright Holder.
262              
263             This license includes the non-exclusive, worldwide, free-of-charge
264             patent license to make, have made, use, offer to sell, sell, import and
265             otherwise transfer the Package with respect to any patent claims
266             licensable by the Copyright Holder that are necessarily infringed by the
267             Package. If you institute patent litigation (including a cross-claim or
268             counterclaim) against any party alleging that the Package constitutes
269             direct or contributory patent infringement, then this Artistic License
270             to you shall terminate on the date that such litigation is filed.
271              
272             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
273             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
274             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
275             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
276             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
277             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
278             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
279             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
280              
281              
282             =cut
283              
284             1; # End of Uninets::Check::Modules::HTTP