Skip to content

Commit

Permalink
Allow @ (at sign) when parsing remote user format string.
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronweeden authored and kassner committed Dec 12, 2023
1 parent e772595 commit ab4fd24
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/LogParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final class LogParser
'\%\{(local|canonical|remote)\}p' => '(?P<\\1Port>\d+)',
'%r' => '(?P<request>(?:(?:[A-Z]+) .+? HTTP/(1\.0|1\.1|2\.0))|-|)',
'%t' => '\[(?P<time>\d{2}/(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/\d{4}:\d{2}:\d{2}:\d{2} (?:-|\+)\d{4})\]',
'%u' => '(?P<user>(?:-|[\w\-\.]+))',
'%u' => '(?P<user>(?:-|[\w\-\.@]+))',
'%U' => '(?P<URL>.+?)',
'%v' => '(?P<serverName>([a-zA-Z0-9]+)([a-z0-9.-]*))',
'%V' => '(?P<canonicalServerName>([a-zA-Z0-9]+)([a-z0-9.-]*))',
Expand Down
71 changes: 71 additions & 0 deletions tests/Format/UserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Kassner\LogParser\Tests\Format;

use Kassner\LogParser\LogParser;

/**
* @format %u
*
* @description The remote user
*/
class UserTest extends \PHPUnit\Framework\TestCase
{
protected $parser;

protected function setUp(): void
{
$this->parser = new LogParser();
$this->parser->setFormat('%u');
}

protected function tearDown(): void
{
$this->parser = null;
}

/**
* @dataProvider successProvider
*/
public function testSuccess($line)
{
$entry = $this->parser->parse($line);
$this->assertEquals($line, $entry->user);
}

/**
* @dataProvider invalidProvider
*/
public function testInvalid($line)
{
$this->expectException(\Kassner\LogParser\FormatException::class);
$this->parser->parse($line);
}

public function successProvider()
{
return [
['-'],
['.'],
['@'],
['abc'],
['abc_def'],
['abc-def'],
['abc.def'],
['abc.def-ghi'],
['abc-def@ghi.jkl'],
];
}

public function invalidProvider()
{
return [
['abc '],
[' '],
['!'],
['a:b'],
['<abc>'],
['a/b'],
];
}
}

0 comments on commit ab4fd24

Please sign in to comment.