App-MonM

 view release on metacpan or  search on metacpan

lib/App/MonM/Checkit/HTTP.pm  view on Meta::CPAN


Defines the timeout of HTTP request

Default: 180

=item B<URL>

    URL     https://www.example.com

Defines the URL for HTTP/HTTPS requests

Default: http://localhost

Examples:

    URL     https://user:password@www.example.com
    URL     https://www.example.com

=back

=head1 HISTORY

See C<Changes> file

=head1 TO DO

See C<TODO> file

=head1 BUGS

* none noted

=head1 SEE ALSO

L<App::MonM>

=head1 AUTHOR

Serż Minus (Sergey Lepenkov) L<https://www.serzik.com> E<lt>abalama@cpan.orgE<gt>

=head1 COPYRIGHT

Copyright (C) 1998-2022 D&D Corporation. All Rights Reserved

=head1 LICENSE

This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

See C<LICENSE> file and L<https://dev.perl.org/licenses/>

=cut

use vars qw/$VERSION/;
$VERSION = '1.01';

use Encode;
use CTK::ConfGenUtil;
use URI;
use LWP::UserAgent();
use HTTP::Request();

use App::MonM::Const qw/PROJECTNAME/;
use App::MonM::Util qw/set2attr getTimeOffset/;

use constant {
        DEFAULT_URL     => "http://localhost",
        DEFAULT_METHOD  => "GET",
        DEFAULT_TIMEOUT => 180,
    };

sub check {
    my $self = shift;
    my $type = $self->type;
    return $self->maybe::next::method() unless $type && ($type eq 'http' or $type eq 'https');

    # Init
    my $url = lvalue($self->config, 'url') || DEFAULT_URL;
    my $method = lvalue($self->config, 'method') || DEFAULT_METHOD;
    my $timeout = getTimeOffset(lvalue($self->config, 'timeout') || DEFAULT_TIMEOUT);
    my $attr = set2attr($self->config);
    my $content = lvalue($self->config, 'content') // '';
    my $proxy = lvalue($self->config, 'proxy') || "";

    # Agent
    my $uri = URI->new($url);
    my $ua = LWP::UserAgent->new(
        agent               => sprintf("%s/%s", PROJECTNAME, $VERSION),
        timeout             => $timeout,
        protocols_allowed   => ['http', 'https'],
    );
    $ua->default_header($_, value($attr, $_)) for (keys %$attr);

    # Proxy
    $ua->proxy(['http', 'https'], $proxy) if $proxy;

    # Prepare request data
    my $request = HTTP::Request->new(uc($method) => $uri);
    if ($method =~ /PUT|POST|PATCH/) {
        Encode::_utf8_on($content);
        $request->header('Content-Length' => length(Encode::encode("utf8", $content)));
        $request->content(Encode::encode("utf8", $content));
    }

    # Request
    my $response = $ua->request($request);

    # Result
    $self->status(($response->is_info || $response->is_success || $response->is_redirect) ? 1 : 0);
    $self->error($response->decoded_content // '') unless $self->status;
    $self->source(sprintf("%s %s", $method, $response->request->uri->canonical->as_string));
    $self->message($response->status_line || '');
    $self->code($response->code || 0);
    $self->content($response->decoded_content // '');

    return;
}

1;

__END__



( run in 1.072 second using v1.01-cache-2.11-cpan-524268b4103 )