Skip to content

Commit

Permalink
Fix HTTPS by using TLS::Client and fix HTTP/1.1 compat by adding Host…
Browse files Browse the repository at this point in the history
… header
  • Loading branch information
jelu committed May 23, 2016
1 parent 045736f commit 31f5988
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 2 deletions.
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ lib/Lim/RPC/Protocol/XMLRPC.pm
lib/Lim/RPC/Protocols.pm
lib/Lim/RPC/Server.pm
lib/Lim/RPC/TLS.pm
lib/Lim/RPC/TLS/Client.pm
lib/Lim/RPC/Transport.pm
lib/Lim/RPC/Transport/HTTP.pm
lib/Lim/RPC/Transport/HTTPS.pm
Expand Down
2 changes: 2 additions & 0 deletions debian/liblim-common-perl.install
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
usr/share/man/man3/Lim::Component.3pm
usr/share/man/man3/Lim::RPC::Value::Collection.3pm
usr/share/man/man3/Lim::RPC::TLS.3pm
usr/share/man/man3/Lim::RPC::TLS::Client.3pm
usr/share/man/man3/Lim::RPC::Call.3pm
usr/share/man/man3/Lim::RPC::Value.3pm
usr/share/man/man3/Lim::Plugins.3pm
Expand All @@ -25,6 +26,7 @@ usr/share/perl5/Lim/RPC/Transport/Clients.pm
usr/share/perl5/Lim/RPC/Protocol.pm
usr/share/perl5/Lim/RPC/Protocols.pm
usr/share/perl5/Lim/RPC/TLS.pm
usr/share/perl5/Lim/RPC/TLS/Client.pm
usr/share/perl5/Lim/RPC/Value/Collection.pm
usr/share/perl5/Lim/RPC/Call.pm
usr/share/perl5/Lim/RPC/Value.pm
Expand Down
2 changes: 2 additions & 0 deletions epel/perl-Lim.spec
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ rm -rf $RPM_BUILD_ROOT
%{_mandir}/man3/Lim::Component.3*
%{_mandir}/man3/Lim::RPC::Value::Collection.3*
%{_mandir}/man3/Lim::RPC::TLS.3*
%{_mandir}/man3/Lim::RPC::TLS::Client.3*
%{_mandir}/man3/Lim::RPC::Call.3*
%{_mandir}/man3/Lim::RPC::Value.3*
%{_mandir}/man3/Lim::Plugins.3*
Expand All @@ -275,6 +276,7 @@ rm -rf $RPM_BUILD_ROOT
%{perl_vendorlib}/Lim/RPC/Protocol.pm
%{perl_vendorlib}/Lim/RPC/Protocols.pm
%{perl_vendorlib}/Lim/RPC/TLS.pm
%{perl_vendorlib}/Lim/RPC/TLS/Client.pm
%{perl_vendorlib}/Lim/RPC/Value/Collection.pm
%{perl_vendorlib}/Lim/RPC/Call.pm
%{perl_vendorlib}/Lim/RPC/Value.pm
Expand Down
124 changes: 124 additions & 0 deletions lib/Lim/RPC/TLS/Client.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package Lim::RPC::TLS::Client;

use common::sense;
use Carp;

use Log::Log4perl ();
use Scalar::Util qw(weaken);

use AnyEvent::TLS ();
use Net::SSLeay ();

use Lim ();

=encoding utf8
=head1 NAME
...
=head1 VERSION
See L<Lim> for version.
=cut

our $VERSION = $Lim::VERSION;
our $INSTANCE;

=head1 SYNOPSIS
...
=head1 SUBROUTINES/METHODS
=head2 new
=cut

sub new {
my $this = shift;
my $class = ref($this) || $this;
my $self = {
logger => Log::Log4perl->get_logger($class),
};
bless $self, $class;
weaken($self->{logger});

eval {
$self->{tls_ctx} = AnyEvent::TLS->new(%{Lim::Config->{rpc}->{tls}});
};
if ($@) {
Lim::OBJ_DEBUG and $self->{logger}->debug('Unable to initialize TLS context, will not use TLS/SSL: ', $@);
$self->{tls_ctx} = undef;
}

Lim::OBJ_DEBUG and $self->{logger}->debug('new ', __PACKAGE__, ' ', $self);
$self;
}

sub DESTROY {
my ($self) = @_;
Lim::OBJ_DEBUG and $self->{logger}->debug('destroy ', __PACKAGE__, ' ', $self);
}

END {
undef($INSTANCE);
}

=head2 instance
=cut

sub instance {
$INSTANCE ||= Lim::RPC::TLS::Client->new;
}

=head2 tls_ctx
=cut

sub tls_ctx {
$_[0]->{tls_ctx};
}

=head1 AUTHOR
Jerry Lundström, C<< <lundstrom.jerry at gmail.com> >>
=head1 BUGS
Please report any bugs or feature requests to L<https://github.com/jelu/lim/issues>.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Lim
You can also look for information at:
=over 4
=item * Lim issue tracker (report bugs here)
L<https://github.com/jelu/lim/issues>
=back
=head1 ACKNOWLEDGEMENTS
=head1 LICENSE AND COPYRIGHT
Copyright 2012-2013 Jerry Lundström.
This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
=cut

1; # End of Lim::RPC::TLS::Client
5 changes: 3 additions & 2 deletions lib/Lim/RPC/Transport/Client/HTTP.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use HTTP::Status qw(:constants);

use Lim ();
use Lim::Error ();
use Lim::RPC::TLS ();
use Lim::RPC::TLS::Client ();
use Lim::Util ();

use base qw(Lim::RPC::Transport::Client);
Expand Down Expand Up @@ -56,7 +56,7 @@ sub MAX_RESPONSE_LEN (){ 8 * 1024 * 1024 }
sub Init {
my ($self) = @_;

if ($self->isa('Lim::RPC::Transport::Client::HTTPS') and !defined Lim::RPC::TLS->instance->tls_ctx) {
if ($self->isa('Lim::RPC::Transport::Client::HTTPS') and !defined Lim::RPC::TLS::Client->instance->tls_ctx) {
confess 'using HTTPS but can not create TLS context';
}
}
Expand Down Expand Up @@ -97,6 +97,7 @@ sub request {
}
$self->{request} = $args{request};
$self->{request}->protocol('HTTP/1.1');
$self->{request}->header( 'Host' => $self->{host} );

Lim::Util::resolve_host $self->{host}, $self->{port}, sub {
my ($host, $port) = @_;
Expand Down

0 comments on commit 31f5988

Please sign in to comment.