Skip to content

Commit

Permalink
Bug fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
splatteredbits committed Feb 18, 2023
1 parent 9ebe381 commit a5faf0a
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 13 deletions.
88 changes: 81 additions & 7 deletions Tests/Import-W3CLog.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ BeforeAll {
Describe 'Import-W3CLog' {
BeforeEach {
$script:result = $null
Get-ChildItem -Path $TestDrive -File | Remove-Item
}

It 'should parse a log file' {
Expand All @@ -77,7 +78,7 @@ Describe 'Import-W3CLog' {
1996-01-01 10:48:05 195.52.225.44 - WEB1 192.166.0.24 GET /styles/style1.css - 200 586 249 0 HTTP/1.0 Mozilla/4.0+[en]+(WinNT;+I) WEBTRENDS_ID=195.52.225.44-100386000.29188902 -
1996-01-01 10:48:05 195.52.225.44 - WEB1 192.166.0.24 GET /graphics/atremote/remote.jpg - 200 12367 301 656 HTTP/1.0 Mozilla/4.0+[en]+(WinNT;+I) WEBTRENDS_ID=195.52.225.44-100386000.29188902 http://webtrends.sample.com/wt_f2.htm
1996-01-01 10:48:05 195.52.225.44 - WEB1 192.166.0.24 GET /graphics/backg/backg1.gif - 200 448 313 0 HTTP/1.0 Mozilla/4.0+[en]+(WinNT;+I) WEBTRENDS_ID=195.52.225.44-100386000.29188902 http://webtrends.sample.com/loganalyzer/info.htm
'@
'@
WhenImporting 'log1.log'

ThenImported @{
Expand Down Expand Up @@ -184,7 +185,7 @@ Describe 'Import-W3CLog' {
# Ignore me
#Fields: cs(Referer) cs(Cookie) cs(User-Agent) cs-version time-taken cs-bytes sc-bytes sc-status cs-uri-query cs-uri-stem cs-method s-ip s-computername cs-username c-ip time date
http://www.webtrends.com/def_f1.htm WEBTRENDS_ID=195.52.225.44-100386000.29188902 Mozilla/4.0+[en]+(WinNT;+I) HTTP/1.0 0 303 3960 200 - /loganalyzer/info.htm GET 192.166.0.24 WEB1 - 195.52.225.44 10:48:02 1996-01-01
'@
'@
WhenImporting 'log2.log'

ThenImported @{
Expand Down Expand Up @@ -231,23 +232,23 @@ http://www.webtrends.com/def_f1.htm WEBTRENDS_ID=195.52.225.44-100386000.2918890
GivenLogFile 'log1.log' -WithContent @'
#Fields: date time c-ip cs-username s-computername s-ip cs-method cs-uri-stem cs-uri-query sc-status sc-bytes cs-bytes time-taken cs-version cs(User-Agent) cs(Cookie) cs(Referer)
1996-01-01 10:48:02 195.52.225.44 - WEB1 192.166.0.24 GET /default.htm - 200 1703 279 0 HTTP/1.0 Mozilla/4.0+[en]+(WinNT;+I) - http://www.webtrends.com/def_f1.htm
'@
'@
GivenLogFile 'log2.log' -WithContent @'
#Fields: date time c-ip cs-username s-computername s-ip cs-method cs-uri-stem cs-uri-query sc-status sc-bytes cs-bytes time-taken cs-version cs(User-Agent) cs(Cookie) cs(Referer)
1996-01-01 10:48:02 195.52.225.44 - WEB1 192.166.0.24 GET /loganalyzer/info.htm - 200 3960 303 0 HTTP/1.0 Mozilla/4.0+[en]+(WinNT;+I) WEBTRENDS_ID=195.52.225.44-100386000.29188902 http://www.webtrends.com/def_f1.htm
'@
'@
GivenLogFile 'log3.log' -WithContent @'
#Fields: date time c-ip cs-username s-computername s-ip cs-method cs-uri-stem cs-uri-query sc-status sc-bytes cs-bytes time-taken cs-version cs(User-Agent) cs(Cookie) cs(Referer)
1996-01-01 10:48:05 195.52.225.44 - WEB1 192.166.0.24 GET /styles/style1.css - 200 586 249 0 HTTP/1.0 Mozilla/4.0+[en]+(WinNT;+I) WEBTRENDS_ID=195.52.225.44-100386000.29188902 -
'@
'@
GivenLogFile 'log4.log' -WithContent @'
#Fields: date time c-ip cs-username s-computername s-ip cs-method cs-uri-stem cs-uri-query sc-status sc-bytes cs-bytes time-taken cs-version cs(User-Agent) cs(Cookie) cs(Referer)
1996-01-01 10:48:05 195.52.225.44 - WEB1 192.166.0.24 GET /graphics/atremote/remote.jpg - 200 12367 301 656 HTTP/1.0 Mozilla/4.0+[en]+(WinNT;+I) WEBTRENDS_ID=195.52.225.44-100386000.29188902 http://webtrends.sample.com/wt_f2.htm
'@
'@
GivenLogFile 'log5.log' -WithContent @'
#Fields: date time c-ip cs-username s-computername s-ip cs-method cs-uri-stem cs-uri-query sc-status sc-bytes cs-bytes time-taken cs-version cs(User-Agent) cs(Cookie) cs(Referer)
1996-01-01 10:48:05 195.52.225.44 - WEB1 192.166.0.24 GET /graphics/backg/backg1.gif - 200 448 313 0 HTTP/1.0 Mozilla/4.0+[en]+(WinNT;+I) WEBTRENDS_ID=195.52.225.44-100386000.29188902 http://webtrends.sample.com/loganalyzer/info.htm
'@
'@
$script:result = Get-ChildItem -Path $TestDrive -Filter '*.log' | Import-W3CLog

ThenImported @{
Expand Down Expand Up @@ -345,4 +346,77 @@ http://www.webtrends.com/def_f1.htm WEBTRENDS_ID=195.52.225.44-100386000.2918890
Url = 'http://192.166.0.24/graphics/backg/backg1.gif';
}
}

It 'should parse OPTIONS request' {
GivenLogFile 'options.log' -WithContent @'
#Fields: date time s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs(User-Agent) cs(Referer) sc-status sc-substatus sc-win32-status time-taken
2023-02-17 02:27:27 10.32.40.215 OPTIONS * - 8001 - 10.32.30.220 Mozilla/4.0+(compatible;+MSIE+8.0;+Windows+NT+5.1;+Trident/4.0) - 200 0 64 0
'@
WhenImporting 'options.log'
ThenImported @{
Date = [DateTime]'2023-02-17';
Time = [TimeSpan]'02:27:27';
DateTime = [DateTime]'2023-02-17 02:27:27';
ServerIP = [ipaddress]'10.32.40.215';
Method = [Net.Http.HttpMethod]::New('OPTIONS');
Stem = '*';
Port = 8001;
ClientIP = [ipaddress]'10.32.30.220';
UserAgent = 'Mozilla/4.0+(compatible;+MSIE+8.0;+Windows+NT+5.1;+Trident/4.0)';
Status = [Net.HttpStatusCode]200;
Substatus = 0;
Win32Status = 64;
TimeTaken = [TimeSpan]::Zero;
Url = [Uri]'http://10.32.40.215/*';
}
}

It 'should parse Win32 status' {
GivenLogFile 'win32status.log' -WithContent @'
#Fields: date time s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs(User-Agent) cs(Referer) sc-status sc-substatus sc-win32-status time-taken
2023-02-17 19:50:32 10.32.40.215 GET /stem/file - 80 - 10.32.30.9 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/109.0.0.0+Safari/537.36 http://example.com/vdir/ 401 1 2148074248 0
'@
WhenImporting 'win32status.log'
ThenImported @{
Date = [DateTime]'2023-02-17';
Time = [TimeSpan]'19:50:32';
DateTime = [DateTime]'2023-02-17 19:50:32';
ServerIP = '10.32.40.215';
Method = [Net.Http.HttpMethod]::New('GET');
Stem = '/stem/file';
Port = 80;
ClientIP = '10.32.30.9';
UserAgent = 'Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/109.0.0.0+Safari/537.36';
Referer = 'http://example.com/vdir/';
Status = [Net.HttpStatusCode]401;
Substatus = 1;
Win32Status = 2148074248;
TimeTaken = [TimeSpan]::Zero;
Url = [Uri]'http://10.32.40.215/stem/file';
}
}

It 'should parse IPv6 addresses' {
GivenLogFile 'ipv6.log' -WithContent @'
#Fields: date time s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs(User-Agent) cs(Referer) sc-status sc-substatus sc-win32-status time-taken
2023-02-03 21:46:09 ::1 HEAD / - 80 - ::1 Mozilla/5.0+(Windows+NT;+Windows+NT+6.3;+en-US)+WindowsPowerShell/5.1.14409.1029 - 302 0 0 31
'@
WhenImporting 'ipv6.log'
ThenImported @{
Date = [DateTime]'2023-02-03';
Time = [TimeSpan]'21:46:09';
DateTime = [DateTime]'2023-02-03 21:46:09';
ServerIP = [ipaddress]'::1';
Method = [Net.Http.HttpMethod]::New('HEAD')
Stem = '/';
Port = 80;
ClientIP = [ipaddress]'::1';
UserAgent = 'Mozilla/5.0+(Windows+NT;+Windows+NT+6.3;+en-US)+WindowsPowerShell/5.1.14409.1029';
Status = [Net.HttpStatusCode]302;
Substatus = 0;
Win32Status = 0;
TimeTaken = [TimeSpan]::New(0, 0, 0, 0, 31);
Url = [Uri]'http://example.com/';
}
}
}
43 changes: 39 additions & 4 deletions W3CLogs/Functions/Import-W3CLog.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,21 @@ function Import-W3CLog
return
}

Write-Verbose "$($Path | Resolve-Path -Relative)"
$displayPath = $Path | Resolve-Path -Relative
if ($displayPath.StartsWith('..'))
{
$displayPath = $Path
}

Write-Verbose $displayPath

$fields = @()

$lineNum = 0
foreach ($line in (Get-Content -Path $Path))
{
$lineNum += 1

if (-not $line)
{
continue
Expand All @@ -60,6 +70,8 @@ function Import-W3CLog
continue
}

$errMsgPrefix = "$($displayPath) line $($lineNum): "

$entry = [W3CLogs.LogEntry]::New()

[String[]]$values = $line.Split(' ')
Expand Down Expand Up @@ -90,7 +102,15 @@ function Import-W3CLog
$value = [TimeSpan]::New(0, 0, 0, 0, $value)
}

$entry.$propertyName = $value
try
{
$entry.$propertyName = $value
}
catch
{
$msg = "$($errMsgPrefix)Failed to convert $($fieldName) value ""$($value)"": $($_)"
Write-Error $msg -ErrorAction $ErrorActionPreference
}
}

$entry.DateTime = $entry.Date + $entry.Time
Expand All @@ -100,7 +120,7 @@ function Import-W3CLog
{
$hostname = $entry.Host
}
elseif ($entry.ServerIP)
elseif ($entry.ServerIP -and $entry.ServerIP.AddressFamily -eq [Net.Sockets.AddressFamily]::InterNetwork)
{
$hostname = $entry.ServerIP.IPAddressToString
}
Expand All @@ -111,7 +131,22 @@ function Import-W3CLog
$queryString = "?$($entry.Query)"
}

$entry.Url = [Uri]::New("http://$($hostname)$($entry.Stem)$($queryString)")
$stem = $entry.Stem
if (-not $stem.StartsWith('/'))
{
$stem = "/$($stem)"
}

$url = "http://$($hostname)$($stem)$($queryString)"
try
{
$entry.Url = [Uri]::New($url)
}
catch
{
$msg = "$($errMsgPrefix)Failed to convert ""$($url)"" to a [Uri] object: $($_)"
Write-Error $msg -ErrorAction $ErrorActionPreference
}

$entry | Write-Output
}
Expand Down
4 changes: 2 additions & 2 deletions W3CLogs/src/LogEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public sealed class LogEntry
public string Query { get; set; }
public HttpStatusCode Status { get; set; }
public int Substatus { get; set; }
public int Win32SStatus { get; set; }
public uint Win32Status { get; set; }
public ulong BytesSent { get; set; }
public ulong BytesReceived { get; set; }
public TimeSpan TimeTaken { get; set; }
Expand Down Expand Up @@ -50,7 +50,7 @@ public override bool Equals(object obj)
this.Query == entry.Query &&
this.Status == entry.Status &&
this.Substatus == entry.Substatus &&
this.Win32SStatus == entry.Win32SStatus &&
this.Win32Status == entry.Win32Status &&
this.BytesSent == entry.BytesSent &&
this.BytesReceived == entry.BytesReceived &&
this.TimeTaken == entry.TimeTaken &&
Expand Down

0 comments on commit a5faf0a

Please sign in to comment.