Skip to content

Instantly share code, notes, and snippets.

@rvizx
Forked from zhilich/SimpleHttpServer.ps1
Created July 26, 2022 15:33
Show Gist options
  • Save rvizx/90aa7a32f4be9b472580a58f1729a1a8 to your computer and use it in GitHub Desktop.
Save rvizx/90aa7a32f4be9b472580a58f1729a1a8 to your computer and use it in GitHub Desktop.
Simple Http Server in PowerShell
function Load-Packages
{
param ([string] $directory = 'Packages')
$assemblies = Get-ChildItem $directory -Recurse -Filter '*.dll' | Select -Expand FullName
foreach ($assembly in $assemblies) { [System.Reflection.Assembly]::LoadFrom($assembly) }
}
Load-Packages
$url = 'http://*:443/'
$listener = New-Object System.Net.HttpListener
$listener.Prefixes.Add($url)
$listener.Start()
Write-Host "Listening at $url..."
while ($listener.IsListening)
{
$context = $listener.GetContext()
$requestUrl = $context.Request.Url
$response = $context.Response
Write-Host ''
Write-Host "> $requestUrl"
$localPath = $requestUrl.LocalPath
$buffer = [System.Text.Encoding]::UTF8.GetBytes('<html><body>Hello world!</body></html>')
$response.ContentLength64 = $buffer.Length
$response.OutputStream.Write($buffer, 0, $buffer.Length)
$response.Close()
$responseStatus = $response.StatusCode
Write-Host "< $responseStatus"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment