Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make "http_host" parameter optional #36

Merged
merged 2 commits into from
Aug 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 5 additions & 31 deletions src/cgi/wwsympa.fcgi.in
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ our $session;
our $plugins;

my $loop = 0;
my ($robot, $robot_object);
my $robot;
my $ip;
my $rss;
my $ajax;
Expand Down Expand Up @@ -1034,7 +1034,6 @@ while ($query = new_loop()) {
undef $param;
undef $list;
undef $robot;
undef $robot_object;
undef $ip;
undef $rss;
undef $ajax;
Expand Down Expand Up @@ -1095,40 +1094,15 @@ while ($query = new_loop()) {
$log->syslog('debug3', 'Changed Param: %s', $k);
}

## Parse CGI parameters
# &CGI::ReadParse();

# Determin robot.
# N.B. As of 6.2.15, the http_host parameter will match with the host name
# and path locally detected by server. If remotely detected host name
# and / or path should be differ, the proxy must adjust them.
my $server_name = Sympa::Tools::WWW::get_server_name() || '';
if ($server_name
and my $hostmap = $Conf::Conf{'robot_by_http_host'}->{$server_name}) {
my $selected_robot;
my $selected_path = '';

my $request_uri = $ENV{'REQUEST_URI'} || '';
while (my ($path, $v) = each %$hostmap) {
if (0 == index $request_uri, $path) { #FIXME:percent-decode
# Longer path wins.
if (length $selected_path < length $path) {
($selected_robot, $selected_path) = ($v, $path);
}
}
}
$robot = $selected_robot;
}
$robot = $Conf::Conf{'host'} unless $robot; #FIXME:Use domain.

# Not yet implemented
### Create Robot object
#$robot_object = new Robot $robot;
$robot = Sympa::Tools::WWW::get_robot('http_host', 'wwsympa_url');

## Default robot
if ($robot eq $Conf::Conf{'host'}) { #FIXME:Use domain.
$param->{'default_robot'} = 1;
}
# Default robot.
$param->{'default_robot'} = 1
if $robot eq $Conf::Conf{'domain'};

$ip = $ENV{'REMOTE_HOST'} || $ENV{'REMOTE_ADDR'} || 'undef';

Expand Down
51 changes: 4 additions & 47 deletions src/lib/Conf.pm
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,8 @@ sub load_robots {
} else {
$param->{'config_hash'}{'robots'}{$robot} = $robot_conf;
}
_check_double_url_usage(
{'config_hash' => $param->{'config_hash'}{'robots'}{$robot}});
#_check_double_url_usage(
# {'config_hash' => $param->{'config_hash'}{'robots'}{$robot}});
}
return undef if ($exiting);
return 1;
Expand Down Expand Up @@ -2016,14 +2016,6 @@ sub _infer_server_specific_parameter_values {
};
}

## Default SOAP URL corresponds to default robot
if ($param->{'config_hash'}{'soap_url'}) {
my $url = $param->{'config_hash'}{'soap_url'};
$url =~ s/^http(s)?:\/\/(.+)$/$2/;
$param->{'config_hash'}{'robot_by_soap_url'}{$url} =
$param->{'config_hash'}{'domain'};
}

return 1;
}

Expand Down Expand Up @@ -2105,13 +2097,6 @@ sub _infer_robot_parameter_values {
$param->{'config_hash'}{'blacklist'}{$action} = 1;
}

## Create a hash to deduce robot from SOAP url
if ($param->{'config_hash'}{'soap_url'}) {
my $url = $param->{'config_hash'}{'soap_url'};
$url =~ s/^http(s)?:\/\/(.+)$/$2/;
$Conf{'robot_by_soap_url'}{$url} =
$param->{'config_hash'}{'robot_name'};
}
# Hack because multi valued parameters are not available for Sympa 6.1.
if (defined $param->{'config_hash'}{'automatic_list_families'}) {
my @families = split ';',
Expand Down Expand Up @@ -2377,36 +2362,8 @@ sub _set_listmasters_entry {
return $number_of_valid_email;
}

sub _check_double_url_usage {
my $param = shift;
my ($host, $path);
if (Sympa::Tools::Data::smart_eq(
$param->{'config_hash'}{'http_host'},
qr/^([^\/]+)(\/.*)$/
)
) {
$param->{'config_hash'}{'http_host'} =~ qr/^([^\/]+)(\/.*)$/;
($host, $path) = ($1, $2);
} else {
($host, $path) = ($param->{'config_hash'}{'http_host'}, '/');
}

## Warn listmaster if another virtual host is defined with the same host
## +path
if (defined $Conf{'robot_by_http_host'}{$host}{$path}) {
$log->syslog(
'err',
'Two virtual hosts (%s and %s) are mapped via a single URL "%s%s"',
$Conf{'robot_by_http_host'}{$host}{$path},
$param->{'config_hash'}{'robot_name'},
$host,
$path
);
}

$Conf{'robot_by_http_host'}{$host}{$path} =
$param->{'config_hash'}{'robot_name'};
}
# No longer used.
#sub _check_double_url_usage;

sub _parse_custom_robot_parameters {
my $param = shift;
Expand Down
18 changes: 11 additions & 7 deletions src/lib/Sympa/ConfDef.pm
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ our @params = (
'file' => 'sympa.conf',
'edit' => '1',
},
{ 'name' => 'soap_url',
'optional' => '1',
'vhost' => '1',
{ 'name' => 'soap_url',
'gettext_id' => 'URL of SympaSOAP',
'vhost' => '1',
'optional' => '1',
},
{ 'name' => 'process_archive',
'default' => 'off',
Expand Down Expand Up @@ -1520,12 +1521,15 @@ our @params = (
},
# { your_lists_size: not yet implemented. }
{ 'name' => 'http_host',
'gettext_id' => 'Web domain of a virtual host',
'sample' => 'host.domain.tld',
'default' => 'host.domain.tld',
'gettext_id' => 'URL prefix of WWSympa behind proxy',
'vhost' => '1',
'edit' => '1',
'file' => 'sympa.conf',
'optional' => '1',
},
{ 'name' => 'soap_url_local',
'gettext_id' => 'URL of SympaSOAP behind proxy',
'vhost' => '1',
'optional' => '1',
},
{ 'name' => 'password_validation',
'gettext_id' =>
Expand Down
14 changes: 4 additions & 10 deletions src/lib/Sympa/SOAP/Transport.pm
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use SOAP::Transport::HTTP;

use Sympa::Session;
use Sympa::Tools::File;
use Sympa::Tools::WWW;

# 'base' pragma doesn't work here
our @ISA = qw(SOAP::Transport::HTTP::FCGI);
Expand All @@ -38,16 +39,9 @@ sub request {
my $self = shift;

if (my $request = $_[0]) {

## Select appropriate robot
if ($Conf::Conf{'robot_by_soap_url'}
{$ENV{'SERVER_NAME'} . $ENV{'SCRIPT_NAME'}}) {
$ENV{'SYMPA_ROBOT'} =
$Conf::Conf{'robot_by_soap_url'}
{$ENV{'SERVER_NAME'} . $ENV{'SCRIPT_NAME'}};
} else {
$ENV{'SYMPA_ROBOT'} = $Conf::Conf{'host'};
}
# Select appropriate robot.
$ENV{'SYMPA_ROBOT'} =
Sympa::Tools::WWW::get_robot('soap_url_local', 'soap_url');

## Empty cache of the List.pm module
Sympa::List::init_list_cache();
Expand Down
52 changes: 51 additions & 1 deletion src/lib/Sympa/Tools/WWW.pm
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ use strict;
use warnings;
use English qw(-no_match_vars);
use File::Path qw();
use URI;

use Sympa;
use Conf;
use Sympa::ConfDef;
use Sympa::Constants;
use Sympa::Language;
use Sympa::List;
use Sympa::LockedFile;
use Sympa::Log;
use Sympa::Regexps;
Expand Down Expand Up @@ -220,11 +222,59 @@ sub get_my_url {
. $original_path_info;
}

# Determine robot.
sub get_robot {
my @keys = @_;

my $request_host = _get_server_name();
my $request_path = $ENV{'REQUEST_URI'} || '';
my $robot_id;

if (defined $request_host and length $request_host) {
my $selected_path = '';
foreach my $rid (Sympa::List::get_robots()) {
my $local_url;
foreach my $key (@keys) {
$local_url = Conf::get_robot_conf($rid, $key);
last if $local_url;
}
next unless $local_url;

if ($local_url =~ m{\A[-+\w]+:}) {
;
} elsif ($local_url =~ m{\A//}) {
$local_url = 'http:' . $local_url;
} else {
$local_url = 'http://' . $local_url;
}

my $uri = URI->new($local_url);
next
unless $uri
and $uri->scheme
and grep { $uri->scheme eq $_ } qw(http https);

my $host = lc ($uri->host || '');
my $path = $uri->path || '/';
#FIXME:might need percent-decode hosts and/or paths
next
unless $request_host eq $host
and 0 == index $request_path, $path;

# The longest path wins.
($robot_id, $selected_path) = ($rid, $path)
if length $selected_path < length $path;
}
}

return (defined $robot_id) ? $robot_id : $Conf::Conf{'domain'};
}

# Old name: (part of) get_header_field() in wwsympa.fcgi.
# NOTE: As of 6.2.15, less trustworthy "X-Forwarded-Server:" request field is
# _no longer_ referred and this function returns only locally detected server
# name.
sub get_server_name {
sub _get_server_name {
my $server = $ENV{SERVER_NAME};
return undef unless defined $server and length $server;

Expand Down