$root = "C:\Users\Administrator\Documents\Codex\2026-07-04\wo-x\outputs"
|
$prefix = "http://0.0.0.0:8088/"
|
$listener = [System.Net.HttpListener]::new()
|
$listener.Prefixes.Add($prefix)
|
$listener.Start()
|
Write-Host "Serving $root at $prefix"
|
|
function Get-ContentType($path) {
|
switch ([IO.Path]::GetExtension($path).ToLowerInvariant()) {
|
".html" { "text/html; charset=utf-8" }
|
".css" { "text/css; charset=utf-8" }
|
".js" { "application/javascript; charset=utf-8" }
|
".json" { "application/json; charset=utf-8" }
|
default { "application/octet-stream" }
|
}
|
}
|
|
while ($listener.IsListening) {
|
$context = $listener.GetContext()
|
try {
|
$path = $context.Request.Url.AbsolutePath.TrimStart("/")
|
if ([string]::IsNullOrWhiteSpace($path)) {
|
$path = "sales_crm_demo.html"
|
}
|
$path = [Uri]::UnescapeDataString($path)
|
$file = Join-Path $root $path
|
$resolvedRoot = [IO.Path]::GetFullPath($root)
|
$resolvedFile = [IO.Path]::GetFullPath($file)
|
if (-not $resolvedFile.StartsWith($resolvedRoot) -or -not (Test-Path -LiteralPath $resolvedFile -PathType Leaf)) {
|
$context.Response.StatusCode = 404
|
$bytes = [Text.Encoding]::UTF8.GetBytes("Not found")
|
} else {
|
$context.Response.StatusCode = 200
|
$context.Response.ContentType = Get-ContentType $resolvedFile
|
$bytes = [IO.File]::ReadAllBytes($resolvedFile)
|
}
|
$context.Response.ContentLength64 = $bytes.Length
|
$context.Response.OutputStream.Write($bytes, 0, $bytes.Length)
|
} catch {
|
$context.Response.StatusCode = 500
|
$bytes = [Text.Encoding]::UTF8.GetBytes("Server error")
|
$context.Response.OutputStream.Write($bytes, 0, $bytes.Length)
|
} finally {
|
$context.Response.OutputStream.Close()
|
}
|
}
|