Update src/router.ps1

This commit is contained in:
2026-01-24 23:55:02 -05:00
parent 72283998ed
commit c100bd618c

View File

@@ -4,49 +4,47 @@ function Invoke-TasksCompleted {
Send-Text $Context "Tasks completion acknowledged." Send-Text $Context "Tasks completion acknowledged."
} }
function Dispatch-Request { function Dispatch-Request {
param($Context) 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) { 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 return
} }
# Normalize path try {
$path = $Context.Request.Url.AbsolutePath.TrimStart('/') $path = $Context.Request.Url.AbsolutePath.TrimStart('/')
switch -Regex ($path) { switch -Regex ($path) {
'^$' { '^$' {
$html = Get-UIHtml -Page 'onboard' $html = Get-UIHtml -Page 'onboard'
return Send-HTML $Context $html Send-HTML $Context $html
return
} }
'^samy\.js$' { '^samy\.js$' {
return Send-RemoteAsset -Context $Context -Url $Script:SamyJsUrl -ContentType 'application/javascript; charset=utf-8' Send-RemoteAsset -Context $Context -Url $Script:SamyJsUrl -ContentType 'application/javascript; charset=utf-8'
return
} }
'^samy\.css$' { '^samy\.css$' {
return Send-RemoteAsset -Context $Context -Url $Script:SamyCssUrl -ContentType 'text/css; charset=utf-8' Send-RemoteAsset -Context $Context -Url $Script:SamyCssUrl -ContentType 'text/css; charset=utf-8'
return
} }
'^SVS_logo\.svg$' { '^SVS_logo\.svg$' {
return Send-RemoteAsset -Context $Context -Url $Script:SamyTopLogoUrl -ContentType 'image/svg+xml' Send-RemoteAsset -Context $Context -Url $Script:SamyTopLogoUrl -ContentType 'image/svg+xml'
return
} }
'^SAMY\.png$' { '^SAMY\.png$' {
return Send-RemoteAsset -Context $Context -Url $Script:SamyBgLogoUrl -ContentType 'image/png' Send-RemoteAsset -Context $Context -Url $Script:SamyBgLogoUrl -ContentType 'image/png'
return
} }
'^SVS_Favicon\.ico$' { '^SVS_Favicon\.ico$' {
return Send-RemoteAsset -Context $Context -Url $Script:SamyFaviconUrl -ContentType 'image/x-icon' Send-RemoteAsset -Context $Context -Url $Script:SamyFaviconUrl -ContentType 'image/x-icon'
return
} }
} }
@@ -63,7 +61,6 @@ function Dispatch-Request {
if ($Context.Request.HttpMethod -eq 'POST' -and $path -eq 'getprinters') { Invoke-GetPrinters $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 'installprinters'){ Invoke-InstallPrinters $Context; return }
# Only locally-generated pages (root is handled above)
if ($path -in @('onboard', 'offboard', 'devices')) { if ($path -in @('onboard', 'offboard', 'devices')) {
$html = Get-UIHtml -Page $path $html = Get-UIHtml -Page $path
Send-HTML $Context $html Send-HTML $Context $html
@@ -94,3 +91,19 @@ function Dispatch-Request {
$Context.Response.StatusCode = 404 $Context.Response.StatusCode = 404
Send-Text $Context '404 - Not Found' Send-Text $Context '404 - Not Found'
} }
catch {
# Always return something so clients don't hang
try {
$Context.Response.StatusCode = 500
$msg = "Dispatch error: $($_.Exception.Message)"
$bytes = [Text.Encoding]::UTF8.GetBytes($msg)
$Context.Response.ContentType = 'text/plain; charset=utf-8'
$Context.Response.ContentLength64 = $bytes.Length
$Context.Response.OutputStream.Write($bytes, 0, $bytes.Length)
} catch { }
}
finally {
try { $Context.Response.OutputStream.Close() } catch { }
try { $Context.Response.Close() } catch { }
}
}