59 lines
1.9 KiB
PowerShell
59 lines
1.9 KiB
PowerShell
param(
|
|
[string]$Dir = ".",
|
|
[int]$Port = 8080
|
|
)
|
|
|
|
$prefix = "http://localhost:$Port/"
|
|
$listener = New-Object System.Net.HttpListener
|
|
$listener.Prefixes.Add($prefix)
|
|
$listener.Start()
|
|
|
|
Write-Host "Server laeuft auf $prefix" -ForegroundColor Green
|
|
Write-Host "Druecke STRG+C zum Beenden."
|
|
Write-Host ""
|
|
|
|
try {
|
|
while ($listener.IsListening) {
|
|
$ctx = $listener.GetContext()
|
|
|
|
try {
|
|
$path = $ctx.Request.Url.AbsolutePath
|
|
if ($path -eq "/") { $path = "/index.html" }
|
|
|
|
$rel = $path.TrimStart("/")
|
|
$file = Join-Path (Resolve-Path $Dir) $rel
|
|
|
|
if ((Test-Path $file) -and -not (Get-Item $file -ErrorAction SilentlyContinue).PSIsContainer) {
|
|
$bytes = [System.IO.File]::ReadAllBytes($file)
|
|
$ext = [System.IO.Path]::GetExtension($file).ToLower()
|
|
$mime = switch ($ext) {
|
|
".html" { "text/html; charset=utf-8" }
|
|
".htm" { "text/html; charset=utf-8" }
|
|
".css" { "text/css" }
|
|
".js" { "application/javascript" }
|
|
".json" { "application/json" }
|
|
".png" { "image/png" }
|
|
".jpg" { "image/jpeg" }
|
|
".jpeg" { "image/jpeg" }
|
|
".gif" { "image/gif" }
|
|
".svg" { "image/svg+xml" }
|
|
".ico" { "image/x-icon" }
|
|
".woff2"{ "font/woff2" }
|
|
default { "application/octet-stream" }
|
|
}
|
|
$ctx.Response.ContentType = $mime
|
|
$ctx.Response.OutputStream.Write($bytes, 0, $bytes.Length)
|
|
}
|
|
else {
|
|
$ctx.Response.StatusCode = 404
|
|
}
|
|
}
|
|
finally {
|
|
$ctx.Response.OutputStream.Close()
|
|
}
|
|
}
|
|
}
|
|
finally {
|
|
$listener.Stop()
|
|
}
|