forked from jsakamoto/MarkdownPresenter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpd.ps1
48 lines (41 loc) · 1.32 KB
/
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
[CmdletBinding()]
param(
[string]$Root = '.',
[int]$Port = 8080,
[string]$HostName = 'localhost'
)
$ErrorActionPreference = 'Stop'
$Here = Split-Path $MyInvocation.MyCommand.Path
$Root = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($Root)
if (![System.IO.Directory]::Exists($Root)) {Write-Error "Missing directory '$Root'."}
$listener = New-Object System.Net.HttpListener
$listener.Prefixes.Add("http://${HostName}:${Port}/")
Write-Output "Start $(@($listener.Prefixes)[0]) at $Root"
$listener.Start()
Write-Output 'Enter Ctrl-Break to stop.'
for() {
$context = $listener.GetContext()
$url = $context.Request.Url.LocalPath.TrimStart('/')
Write-Output "Getting $url"
$res = $context.Response
try {
# first try root
$path = Join-Path $Root $url
if (![System.IO.File]::Exists($path)) {
# second try here
$path = Join-Path $Here $url
if (![System.IO.File]::Exists($path)) {
Write-Output "Missing $url"
$res.StatusCode = 404
continue
}
}
Write-Output "Reading $path"
$content = [IO.File]::ReadAllBytes($path)
$res.OutputStream.Write($content, 0, $content.Length)
}
finally {
$res.Close()
}
}