Subject: | the user data should check the REMOTE_USER environment variable |
The user field should check $ENV{REMOTE_USER} for cases that the username is not passed via
the URL. I attached a new test file and a patch file.
patch lib/Mojolicious/Plugin/AccessLog.pm.orig accesslog.diff
Subject: | 09-user.t |
#!/usr/bin/env perl
use Mojo::Base -strict;
# Disable IPv6 and libev
BEGIN {
$ENV{MOJO_NO_IPV6} = 1;
$ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll';
}
use Test::More;
use Mojo::Util qw(b64_encode);
use Mojolicious::Lite;
use Test::Mojo;
# and now disable log output written with Mojo::Log methods
app->log->unsubscribe('message');
my $b;
plugin 'AccessLog', log => sub { $b = $_[0] }, format => 'combined';
any '/:any' => sub { shift->render_text('done') };
my $t = Test::Mojo->new;
sub req_ok {
my ($method, $url, $code, $opts) = @_;
my $m = $t->can($method . '_ok')
or return fail "Cannot $method $url";
$opts = {} unless ref $opts eq 'HASH';
$opts->{user} ||= '-';
if (index($url, '@') > -1) {
($opts->{user}, $url) = split '@', $url, 2;
$opts->{Authorization} = 'Basic ' . b64_encode($opts->{user} . ':pass', '');
}
my $x = sprintf qq'^%s - %s %s "%s %s HTTP/1.1" %d %s "%s" "%s"\$',
'127\.0\.0\.1',
$opts->{user} || q{},
'\[\d{1,2}/\w{3}/\d{4}:\d{2}:\d{2}:\d{2} [\+\-]\d{4}\]',
uc($method),
quotemeta($url),
$code,
'\d+',
$opts->{Referer} ? quotemeta($opts->{Referer}) : '-',
quotemeta('Mojolicious (Perl)');
# issue request
$m->($t, $url, $opts)->status_is($code);
# check log line
chomp $b;
like $b, qr{$x}, $b;
}
req_ok(get => '/more?foo=bar&foo=baz' => 200);
req_ok(get => 'foo@/more?foo=bar&foo=baz' => 200, { user => 'foo' });
{
## default to basic auth
local $ENV{REMOTE_USER} = 'bar';
req_ok(get => 'foo@/more?foo=bar&foo=baz' => 200, { user => 'foo' });
req_ok(get => '/more?foo=bar&foo=baz' => 200, { user => 'bar' });
}
done_testing;
Subject: | accesslog.diff |
115,121d114
< my $user_cb = sub {
< my $user = (split ':', $_[3]->base->userinfo || '-:')[0];
< if ( $user eq '-' and $ENV{REMOTE_USER} ) {
< $user = $ENV{REMOTE_USER};
< }
< return _safe( $user );
< };
146c139
< u => $user_cb,
---
> u => sub { _safe((split ':', $_[3]->base->userinfo || '-:')[0]) },