v-c: Re-enable https by default, make --version-check take an optional protocol argument

This commit is contained in:
Brian Fraser
2012-09-13 10:39:04 -03:00
parent c4d13f266a
commit ee338f7ceb
27 changed files with 3058 additions and 1439 deletions

View File

@@ -56,6 +56,11 @@ sub new {
return bless $self, $class;
}
my %DefaultPort = (
http => 80,
https => 443,
);
sub request {
my ($self, $method, $url, $args) = @_;
@_ == 3 || (@_ == 4 && ref $args eq 'HASH')
@@ -93,7 +98,7 @@ sub _request {
my $request = {
method => $method,
scheme => $scheme,
host_port => ($port == 80 ? $host : "$host:$port"),
host_port => ($port == $DefaultPort{$scheme} ? $host : "$host:$port"),
uri => $path_query,
headers => {},
};
@@ -158,7 +163,7 @@ sub _split_url {
my $port = do {
$host =~ s/:([0-9]*)\z// && length $1
? $1
: ($scheme eq 'http' ? 80 : undef);
: $DefaultPort{$scheme}
};
return ($scheme, $host, $port, $path_query);
@@ -194,12 +199,24 @@ sub new {
}, $class;
}
my $ssl_verify_args = {
check_cn => "when_only",
wildcards_in_alt => "anywhere",
wildcards_in_cn => "anywhere"
};
sub connect {
@_ == 4 || croak(q/Usage: $handle->connect(scheme, host, port)/);
my ($self, $scheme, $host, $port) = @_;
if ( $scheme ne 'http' ) {
croak(qq/Unsupported URL scheme '$scheme'/);
if ( $scheme eq 'https' ) {
eval "require IO::Socket::SSL"
unless exists $INC{'IO/Socket/SSL.pm'};
croak(qq/IO::Socket::SSL must be installed for https support\n/)
unless $INC{'IO/Socket/SSL.pm'};
}
elsif ( $scheme ne 'http' ) {
croak(qq/Unsupported URL scheme '$scheme'\n/);
}
$self->{fh} = 'IO::Socket::INET'->new(
@@ -213,6 +230,14 @@ sub connect {
binmode($self->{fh})
or croak(qq/Could not binmode() socket: '$!'/);
if ( $scheme eq 'https') {
IO::Socket::SSL->start_SSL($self->{fh});
ref($self->{fh}) eq 'IO::Socket::SSL'
or die(qq/SSL connection failed for $host\n/);
$self->{fh}->verify_hostname( $host, $ssl_verify_args )
or die(qq/SSL certificate not valid for $host\n/);
}
$self->{host} = $host;
$self->{port} = $port;