Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- This creates a web server on port 8080
- It servers up output from this script.
- example: http://localhost:8080/hello
- This does not server files or directories
- #>
- $routes = @{
- "/hello" = { return '<html><body>Hello world!</body></html>' }
- }
- $url = 'http://localhost:8080/'
- $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
- $route = $routes.Get_Item($requestUrl.LocalPath)
- if ($route -eq $null)
- {
- $response.StatusCode = 404
- }
- else
- {
- $content = & $route
- $buffer = [System.Text.Encoding]::UTF8.GetBytes($content)
- $response.ContentLength64 = $buffer.Length
- $response.OutputStream.Write($buffer, 0, $buffer.Length)
- }
- $response.Close()
- $responseStatus = $response.StatusCode
- Write-Host "< $responseStatus"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement