Update src/router.ps1

This commit is contained in:
2026-01-24 23:07:34 -05:00
parent 60e92ac9dc
commit 0c6555a5d3

View File

@@ -4,11 +4,20 @@ function Invoke-TasksCompleted {
Send-Text $Context "Tasks completion acknowledged."
}
function Dispatch-Request {
param($Context)
# Normalize path
$path = $Context.Request.Url.AbsolutePath.TrimStart('/')
# 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('/')
@@ -16,7 +25,6 @@ $path = $Context.Request.Url.AbsolutePath.TrimStart('/')
switch -Regex ($path) {
'^$' {
# /
return Send-RemoteAsset -Context $Context -Url $Script:SamyHtmlUrl -ContentType 'text/html; charset=utf-8'
}
@@ -39,12 +47,7 @@ switch -Regex ($path) {
'^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
@@ -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
}