diff --git a/src/router.ps1 b/src/router.ps1 index 67a5b1d..66a42e7 100644 --- a/src/router.ps1 +++ b/src/router.ps1 @@ -4,48 +4,51 @@ function Invoke-TasksCompleted { Send-Text $Context "Tasks completion acknowledged." } + function Dispatch-Request { param($Context) + # Guard against null contexts (prevents "null-valued expression" crashes) + if ($null -eq $Context -or $null -eq $Context.Request -or $null -eq $Context.Request.Url) { + try { + if ($Context -and $Context.Response) { + $Context.Response.StatusCode = 400 + $Context.Response.Close() + } + } catch { } + return + } + # Normalize path $path = $Context.Request.Url.AbsolutePath.TrimStart('/') - # Normalize path -$path = $Context.Request.Url.AbsolutePath.TrimStart('/') + switch -Regex ($path) { -switch -Regex ($path) { + '^$' { + return Send-RemoteAsset -Context $Context -Url $Script:SamyHtmlUrl -ContentType 'text/html; charset=utf-8' + } - '^$' { - # / - return Send-RemoteAsset -Context $Context -Url $Script:SamyHtmlUrl -ContentType 'text/html; charset=utf-8' + '^samy\.js$' { + return Send-RemoteAsset -Context $Context -Url $Script:SamyJsUrl -ContentType 'application/javascript; charset=utf-8' + } + + '^samy\.css$' { + return Send-RemoteAsset -Context $Context -Url $Script:SamyCssUrl -ContentType 'text/css; charset=utf-8' + } + + '^SVS_logo\.svg$' { + return Send-RemoteAsset -Context $Context -Url $Script:SamyTopLogoUrl -ContentType 'image/svg+xml' + } + + '^SAMY\.png$' { + return Send-RemoteAsset -Context $Context -Url $Script:SamyBgLogoUrl -ContentType 'image/png' + } + + '^SVS_Favicon\.ico$' { + return Send-RemoteAsset -Context $Context -Url $Script:SamyFaviconUrl -ContentType 'image/x-icon' + } } - '^samy\.js$' { - return Send-RemoteAsset -Context $Context -Url $Script:SamyJsUrl -ContentType 'application/javascript; charset=utf-8' - } - - '^samy\.css$' { - return Send-RemoteAsset -Context $Context -Url $Script:SamyCssUrl -ContentType 'text/css; charset=utf-8' - } - - '^SVS_logo\.svg$' { - return Send-RemoteAsset -Context $Context -Url $Script:SamyTopLogoUrl -ContentType 'image/svg+xml' - } - - '^SAMY\.png$' { - return Send-RemoteAsset -Context $Context -Url $Script:SamyBgLogoUrl -ContentType 'image/png' - } - - '^SVS_Favicon\.ico$' { - return Send-RemoteAsset -Context $Context -Url $Script:SamyFaviconUrl -ContentType 'image/x-icon' - } - - default { - # Continue into your existing API/task routing - } -} - - if ($path -eq 'quit') { Write-LogHybrid "Shutdown requested" "Info" "Server" -LogToEvent Send-Text $Context "Server shutting down." @@ -53,41 +56,21 @@ switch -Regex ($path) { return } - if ($Context.Request.HttpMethod -eq 'POST' -and $path -eq 'tasksCompleted') { - Invoke-TasksCompleted $Context - return - } + if ($Context.Request.HttpMethod -eq 'POST' -and $path -eq 'tasksCompleted') { Invoke-TasksCompleted $Context; return } + if ($Context.Request.HttpMethod -eq 'POST' -and $path -eq 'getpw') { Invoke-FetchSites $Context; return } + if ($Context.Request.HttpMethod -eq 'POST' -and $path -eq 'renameComputer') { Invoke-RenameComputer $Context; return } + if ($Context.Request.HttpMethod -eq 'POST' -and $path -eq 'getprinters') { Invoke-GetPrinters $Context; return } + if ($Context.Request.HttpMethod -eq 'POST' -and $path -eq 'installprinters'){ Invoke-InstallPrinters $Context; return } - if ($Context.Request.HttpMethod -eq 'POST' -and $path -eq 'getpw') { - Invoke-FetchSites $Context - return - } - - if ($Context.Request.HttpMethod -eq 'POST' -and $path -eq 'renameComputer') { - Invoke-RenameComputer $Context - return - } - - if ($Context.Request.HttpMethod -eq 'POST' -and $path -eq 'getprinters') { - Invoke-GetPrinters $Context - return - } - - if ($Context.Request.HttpMethod -eq 'POST' -and $path -eq 'installprinters') { - Invoke-InstallPrinters $Context - return - } - - if ($path -in @('', 'onboard', 'offboard', 'devices')) { - $page = if ($path -eq '') { 'onboard' } else { $path } - $html = Get-UIHtml -Page $page + # Only locally-generated pages (root is handled above) + if ($path -in @('onboard', 'offboard', 'devices')) { + $html = Get-UIHtml -Page $path Send-HTML $Context $html return } $task = $Global:SamyTasks | Where-Object Name -EQ $path | Select-Object -First 1 if ($task) { - $fn = Get-TaskHandlerName -Task $task if ([string]::IsNullOrWhiteSpace($fn)) { $Context.Response.StatusCode = 500 @@ -102,12 +85,8 @@ switch -Regex ($path) { return } - if ($cmd.Parameters.ContainsKey('Context')) { - & $fn -Context $Context - } - else { - & $fn - } + if ($cmd.Parameters.ContainsKey('Context')) { & $fn -Context $Context } + else { & $fn } return }