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."
}
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
try {
$path = $Context.Request.Url.AbsolutePath.TrimStart('/')
switch -Regex ($path) {
'^$' {
$html = Get-UIHtml -Page 'onboard'
return Send-HTML $Context $html
Send-HTML $Context $html
return
}
'^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$' {
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$' {
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$' {
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$' {
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 'installprinters'){ Invoke-InstallPrinters $Context; return }
# Only locally-generated pages (root is handled above)
if ($path -in @('onboard', 'offboard', 'devices')) {
$html = Get-UIHtml -Page $path
Send-HTML $Context $html
@@ -93,4 +90,20 @@ function Dispatch-Request {
$Context.Response.StatusCode = 404
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 { }
}
}