-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ps1
More file actions
38 lines (27 loc) · 1.33 KB
/
server.ps1
File metadata and controls
38 lines (27 loc) · 1.33 KB
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
$http = [System.Net.HttpListener]::new()
$http.Prefixes.Add("http://localhost:8080/")
$http.Start()
if ($http.IsListening) {
Write-Host "HTTP Server Ready! " -f Black -b Green
Write-Host "now trying to $($http.Prefixes)" -f 'y'
Write-Host "then try going to $($http.Prefixes)other/path" -f 'y'
}
while ($http.IsListening){
$context = $http.GetContext()
if ($context.Request.HttpMethod -eq 'GET' -and $context.Request.RawUrl -eq '/'){
Write-Host "$($context.Request.UserHostAddress) => $($context.Request.Url)" -f 'mag'
[string]$html = Get-Content ".\index.html" -Raw
$buffer = [System.Text.Encoding]::UTF8.GetBytes($html)
$context.Response.ContentLength64 = $buffer.Length
$context.Response.OutputStream.Write($buffer, 0, $buffer.Length)
$context.Response.OutputStream.Close()
}
if ($context.Request.HttpMethod -eq 'GET' -and $context.Request.RawUrl -ne '/'){
Write-Host "$($context.Request.UserHostAddress) => $($context.Request.Url)" -f 'mag'
[string]$html = Get-Content ".\$($context.Request.RawUrl)" -Raw
$buffer = [System.Text.Encoding]::UTF8.GetBytes($html)
$context.Response.ContentLength64 = $buffer.Length
$context.Response.OutputStream.Write($buffer, 0, $buffer.Length)
$context.Response.OutputStream.Close()
}
}