Skip to content

Commit

Permalink
deprecated custom listen values in Mojo::Server::Daemon->listen in fa…
Browse files Browse the repository at this point in the history
…vor of URLs, this change also affects Morbo and Hypnotoad
  • Loading branch information
kraih committed Mar 1, 2012
1 parent 5af93ee commit edf9d4d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 31 deletions.
5 changes: 5 additions & 0 deletions Changes
@@ -1,6 +1,11 @@
This file documents the revision history for Perl extension Mojolicious.

2.56 2012-03-01 00:00:00
- Deprecated custom listen values in Mojo::Server::Daemon->listen in
favor of URLs, this change also affects Morbo and Hypnotoad.
https://*:3000:/x/server.crt:/y/server.key:/z/ca.crt
becomes
https://*:3000?cert=/x/server.crt&key=/y/server.key&ca=/z/ca.crt
- Removed experimental status from patch function in
Mojolicious::Lite.
- Removed experimental status from patch method in
Expand Down
54 changes: 29 additions & 25 deletions lib/Mojo/Server/Daemon.pm
Expand Up @@ -3,6 +3,7 @@ use Mojo::Base 'Mojo::Server';

use Carp 'croak';
use Mojo::IOLoop;
use Mojo::URL;
use POSIX;
use Scalar::Util 'weaken';
use Sys::Hostname;
Expand All @@ -20,19 +21,6 @@ has ioloop => sub { Mojo::IOLoop->singleton };
has max_clients => 1000;
has max_requests => 25;

my $LISTEN_RE = qr|
^
(http(?:s)?)\:// # Scheme
(.+) # Address
\:(\d+) # Port
(?:
\:(.*?) # Certificate
\:(.*?) # Key
(?:\:(.+)?)? # Certificate Authority
)?
$
|x;

sub DESTROY {
my $self = shift;
return unless my $loop = $self->ioloop;
Expand Down Expand Up @@ -204,17 +192,32 @@ sub _listen {
return unless $listen;

# Check listen value
croak qq/Invalid listen value "$listen"/ unless $listen =~ $LISTEN_RE;
my $options = {};
my $url = Mojo::URL->new($listen);
my $query = $url->query;

# DEPRECATED in Leaf Fluttering In Wind!
my ($address, $port, $cert, $key, $ca);
if ($listen =~ qr|//(.+)\:(\d+)\:(.*?)\:(.*?)(?:\:(.+)?)?$|) {
warn "Custom listen values are DEPRECATED in favor of URLs!\n";
($address, $port, $cert, $key, $ca) = ($1, $2, $3, $4, $5);
}

else {
$address = $url->host;
$port = $url->port;
$cert = $query->param('cert');
$key = $query->param('key');
$ca = $query->param('ca');
}

# Options
my $options = {port => $port};
my $tls;
$tls = $options->{tls} = 1 if $1 eq 'https';
$options->{address} = $2 if $2 ne '*';
$options->{port} = $3;
$options->{tls_cert} = $4 if $4;
$options->{tls_key} = $5 if $5;
$options->{tls_ca} = $6 if $6;

# Listen backlog size
$tls = $options->{tls} = 1 if $url->scheme eq 'https';
$options->{address} = $address if $address ne '*';
$options->{tls_cert} = $cert if $cert;
$options->{tls_key} = $key if $key;
$options->{tls_ca} = $ca if $ca;
my $backlog = $self->backlog;
$options->{backlog} = $backlog if $backlog;

Expand Down Expand Up @@ -420,10 +423,11 @@ List of one or more locations to listen on, defaults to C<http://*:3000>.
$daemon->listen(['http://*:3000', 'https://*:4000']);
# Use a custom certificate and key
$daemon->listen(['https://*:3000:/x/server.crt:/y/server.key']);
$daemon->listen(['https://*:3000?cert=/x/server.crt&key=/y/server.key']);
# Or even a custom certificate authority
$daemon->listen(['https://*:3000:/x/server.crt:/y/server.key:/z/ca.crt']);
$daemon->listen(
['https://*:3000?cert=/x/server.crt&key=/y/server.key&ca=/z/ca.crt']);
=head2 C<max_clients>
Expand Down
9 changes: 6 additions & 3 deletions t/mojo/user_agent_tls.t
Expand Up @@ -31,9 +31,12 @@ get '/' => {text => 'works!'};
# Web server with valid certificates
my $daemon =
Mojo::Server::Daemon->new(app => app, ioloop => Mojo::IOLoop->singleton);
my $port = Mojo::IOLoop->new->generate_port;
my $listen = "https://*:$port"
. ':t/mojo/certs/server.crt:t/mojo/certs/server.key:t/mojo/certs/ca.crt';
my $port = Mojo::IOLoop->new->generate_port;
my $listen =
"https://*:$port"
. '?cert=t/mojo/certs/server.crt'
. '&key=t/mojo/certs/server.key'
. '&ca=t/mojo/certs/ca.crt';
$daemon->listen([$listen])->start;

# No certificate
Expand Down
9 changes: 6 additions & 3 deletions t/mojo/websocket_proxy_tls.t
Expand Up @@ -62,9 +62,12 @@ websocket '/test' => sub {
# Web server with valid certificates
my $daemon =
Mojo::Server::Daemon->new(app => app, ioloop => Mojo::IOLoop->singleton);
my $port = Mojo::IOLoop->new->generate_port;
my $listen = "https://*:$port"
. ':t/mojo/certs/server.crt:t/mojo/certs/server.key:t/mojo/certs/ca.crt';
my $port = Mojo::IOLoop->new->generate_port;
my $listen =
"https://*:$port"
. '?cert=t/mojo/certs/server.crt'
. '&key=t/mojo/certs/server.key'
. '&ca=t/mojo/certs/ca.crt';
$daemon->listen([$listen])->start;

# Connect proxy server for testing
Expand Down

0 comments on commit edf9d4d

Please sign in to comment.