From f914e816092f02a4bbed779ad91a6641e7cc2122 Mon Sep 17 00:00:00 2001 From: Raghuram Subramani Date: Sun, 4 Sep 2022 05:04:04 -0400 Subject: add rooms --- support/powershell-web-server.ps1 | 111 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 support/powershell-web-server.ps1 (limited to 'support/powershell-web-server.ps1') diff --git a/support/powershell-web-server.ps1 b/support/powershell-web-server.ps1 new file mode 100644 index 0000000..f8a409b --- /dev/null +++ b/support/powershell-web-server.ps1 @@ -0,0 +1,111 @@ +# This is a super **SIMPLE** example of how to create a very basic powershell webserver +# 2019-05-18 UPDATE — Created by me and and evalued by @jakobii and the comunity. + +# Http Server +$http = [System.Net.HttpListener]::new() + +# Hostname and port to listen on +$http.Prefixes.Add("http://localhost:8080/") + +# Start the Http Server +$http.Start() + + + +# Log ready message to terminal +if ($http.IsListening) { + write-host " HTTP Server Ready! " -f 'black' -b 'gre' + write-host "now try going to $($http.Prefixes)" -f 'y' + write-host "then try going to $($http.Prefixes)other/path" -f 'y' +} + + +# INFINTE LOOP +# Used to listen for requests +while ($http.IsListening) { + + + + # Get Request Url + # When a request is made in a web browser the GetContext() method will return a request object + # Our route examples below will use the request object properties to decide how to respond + $context = $http.GetContext() + + + # ROUTE EXAMPLE 1 + # http://127.0.0.1/ + if ($context.Request.HttpMethod -eq 'GET' -and $context.Request.RawUrl -eq '/') { + + # We can log the request to the terminal + write-host "$($context.Request.UserHostAddress) => $($context.Request.Url)" -f 'mag' + + # the html/data you want to send to the browser + # you could replace this with: [string]$html = Get-Content "C:\some\path\index.html" -Raw + [string]$html = "

A Powershell Webserver

home page

" + + #resposed to the request + $buffer = [System.Text.Encoding]::UTF8.GetBytes($html) # convert htmtl to bytes + $context.Response.ContentLength64 = $buffer.Length + $context.Response.OutputStream.Write($buffer, 0, $buffer.Length) #stream to broswer + $context.Response.OutputStream.Close() # close the response + + } + + + + # ROUTE EXAMPLE 2 + # http://127.0.0.1/some/form' + if ($context.Request.HttpMethod -eq 'GET' -and $context.Request.RawUrl -eq '/some/form') { + + # We can log the request to the terminal + write-host "$($context.Request.UserHostAddress) => $($context.Request.Url)" -f 'mag' + + [string]$html = " +

A Powershell Webserver

+
+

A Basic Form

+

fullname

+ +

message

+ +
+ +
+ " + + #resposed to the request + $buffer = [System.Text.Encoding]::UTF8.GetBytes($html) + $context.Response.ContentLength64 = $buffer.Length + $context.Response.OutputStream.Write($buffer, 0, $buffer.Length) + $context.Response.OutputStream.Close() + } + + # ROUTE EXAMPLE 3 + # http://127.0.0.1/some/post' + if ($context.Request.HttpMethod -eq 'POST' -and $context.Request.RawUrl -eq '/some/post') { + + # decode the form post + # html form members need 'name' attributes as in the example! + $FormContent = [System.IO.StreamReader]::new($context.Request.InputStream).ReadToEnd() + + # We can log the request to the terminal + write-host "$($context.Request.UserHostAddress) => $($context.Request.Url)" -f 'mag' + Write-Host $FormContent -f 'Green' + + # the html/data + [string]$html = "

A Powershell Webserver

Post Successful!

" + + #resposed to the request + $buffer = [System.Text.Encoding]::UTF8.GetBytes($html) + $context.Response.ContentLength64 = $buffer.Length + $context.Response.OutputStream.Write($buffer, 0, $buffer.Length) + $context.Response.OutputStream.Close() + } + + + # powershell will continue looping and listen for new requests... + +} + +# Note: +# To end the loop you have to kill the powershell terminal. ctrl-c wont work :/ \ No newline at end of file -- cgit v1.2.3