Update src/router.ps1
This commit is contained in:
111
src/router.ps1
111
src/router.ps1
@@ -4,48 +4,51 @@ 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) {
|
||||||
|
try {
|
||||||
|
if ($Context -and $Context.Response) {
|
||||||
|
$Context.Response.StatusCode = 400
|
||||||
|
$Context.Response.Close()
|
||||||
|
}
|
||||||
|
} catch { }
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
# Normalize path
|
# Normalize path
|
||||||
$path = $Context.Request.Url.AbsolutePath.TrimStart('/')
|
$path = $Context.Request.Url.AbsolutePath.TrimStart('/')
|
||||||
|
|
||||||
# Normalize path
|
switch -Regex ($path) {
|
||||||
$path = $Context.Request.Url.AbsolutePath.TrimStart('/')
|
|
||||||
|
|
||||||
switch -Regex ($path) {
|
'^$' {
|
||||||
|
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'
|
||||||
return Send-RemoteAsset -Context $Context -Url $Script:SamyHtmlUrl -ContentType 'text/html; 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') {
|
if ($path -eq 'quit') {
|
||||||
Write-LogHybrid "Shutdown requested" "Info" "Server" -LogToEvent
|
Write-LogHybrid "Shutdown requested" "Info" "Server" -LogToEvent
|
||||||
Send-Text $Context "Server shutting down."
|
Send-Text $Context "Server shutting down."
|
||||||
@@ -53,41 +56,21 @@ switch -Regex ($path) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($Context.Request.HttpMethod -eq 'POST' -and $path -eq 'tasksCompleted') {
|
if ($Context.Request.HttpMethod -eq 'POST' -and $path -eq 'tasksCompleted') { Invoke-TasksCompleted $Context; return }
|
||||||
Invoke-TasksCompleted $Context
|
if ($Context.Request.HttpMethod -eq 'POST' -and $path -eq 'getpw') { Invoke-FetchSites $Context; return }
|
||||||
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') {
|
# Only locally-generated pages (root is handled above)
|
||||||
Invoke-FetchSites $Context
|
if ($path -in @('onboard', 'offboard', 'devices')) {
|
||||||
return
|
$html = Get-UIHtml -Page $path
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
Send-HTML $Context $html
|
Send-HTML $Context $html
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
$task = $Global:SamyTasks | Where-Object Name -EQ $path | Select-Object -First 1
|
$task = $Global:SamyTasks | Where-Object Name -EQ $path | Select-Object -First 1
|
||||||
if ($task) {
|
if ($task) {
|
||||||
|
|
||||||
$fn = Get-TaskHandlerName -Task $task
|
$fn = Get-TaskHandlerName -Task $task
|
||||||
if ([string]::IsNullOrWhiteSpace($fn)) {
|
if ([string]::IsNullOrWhiteSpace($fn)) {
|
||||||
$Context.Response.StatusCode = 500
|
$Context.Response.StatusCode = 500
|
||||||
@@ -102,12 +85,8 @@ switch -Regex ($path) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($cmd.Parameters.ContainsKey('Context')) {
|
if ($cmd.Parameters.ContainsKey('Context')) { & $fn -Context $Context }
|
||||||
& $fn -Context $Context
|
else { & $fn }
|
||||||
}
|
|
||||||
else {
|
|
||||||
& $fn
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user