Last active
January 14, 2022 07:13
-
-
Save Inndy/7de3a5f07c2b5ce207080f3bf01fd57b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
&{ | |
$listener = New-Object Net.HttpListener; | |
$listener.Prefixes.Add("http://+:8009/"); | |
$listener.Start(); | |
while ($listener.IsListening) { | |
$ctx = $listener.GetContext(); | |
$HRes = $ctx.Response; | |
$path = (Join-Path $Pwd ($ctx.Request).RawUrl); | |
Write-Host ("Request URL: {0}" -f $ctx.Request.RawUrl) | |
if ([System.IO.Directory]::Exists($path)) { | |
if ($ctx.Request.RawUrl -match '/$') { | |
$HRes.Headers.Add("Content-Type","text/html; charset=utf-8"); | |
$html = -join (ls $path | %{ ('<a href="{0}">{0}</a><br>'+"`n") -f $_.Name }); | |
$Buf = [System.Text.Encoding]::UTF8.GetBytes($html); | |
$HRes.OutputStream.Write($Buf, 0, $Buf.Length); | |
} else { | |
$HRes.Redirect($ctx.Request.RawUrl + "/"); | |
} | |
} elseif ([System.IO.File]::Exists($path)) { | |
$HRes.Headers.Add("Content-Type","text/plain"); | |
$Buf = [System.IO.File]::ReadAllBytes($path); | |
$HRes.ContentLength64 = $Buf.Length; | |
$HRes.OutputStream.Write($Buf, 0, $Buf.Length); | |
} | |
$HRes.Close(); | |
} | |
$listener.Close(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment