forked from jsakamoto/MarkdownPresenter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpd.ps1
30 lines (23 loc) · 778 Bytes
/
httpd.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
param($Root=".", $Port=8080, $HostName="localhost")
pushd $Root
$Root = pwd
$listener = New-Object System.Net.HttpListener
$listener.Prefixes.Add("http://$HostName`:$Port/")
$listener.Start()
echo ("Start {0} at `"$Root`"" -f ($listener.Prefixes | select -f 1))
echo "Enter Ctrl + C to stop."
while ($true) {
$context = $listener.GetContext()
$url = $context.Request.Url.LocalPath.TrimStart('/')
$res = $context.Response
$path = Join-Path $Root ($url -replace "/","\")
echo $path
if ((Test-Path $path -PathType Leaf) -eq $true) {
$content = [IO.File]::ReadAllBytes($path)
$res.OutputStream.Write($content, 0, $content.Length)
}
else {
$res.StatusCode = 404
}
$res.Close()
}