Revisions
-
jakobii revised this gist
Jun 14, 2018 . 1 changed file with 45 additions and 14 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -28,21 +28,21 @@ 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 = "<h1>A Powershell Webserver</h1><p>home page</p>" #resposed to the request $buffer = [System.Text.Encoding]::UTF8.GetBytes($html) # convert htmtl to bytes @@ -55,24 +55,55 @@ while ($http.IsListening) { # 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 = " <h1>A Powershell Webserver</h1> <form action='/some/post' method='post'> <p>A Basic Form</p> <p>fullname</p> <input type='text' name='fullname'> <p>message</p> <textarea rows='4' cols='50' name='message'></textarea> <br> <input type='submit' value='Submit'> </form> " #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 = "<h1>A Powershell Webserver</h1><p>Post Successful!</p>" #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... } -
jakobii revised this gist
Jun 13, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -28,7 +28,7 @@ while ($http.IsListening) { # Get Request Url # When a request is made in a web browser the GetContext() method will return the request url # Our ROUTE EXAMPLE's will use this to decide how to respond $context = $http.GetContext() -
jakobii revised this gist
Mar 6, 2018 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -21,7 +21,6 @@ if ($http.IsListening) { } # INFINTE LOOP # Used to listen for requests while ($http.IsListening) { @@ -34,9 +33,9 @@ while ($http.IsListening) { $context = $http.GetContext() # ROUTE EXAMPLE 1 # method = 'GET' and url = '/' 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' @@ -56,7 +55,8 @@ while ($http.IsListening) { # ROUTE EXAMPLE 2 # method = 'GET' and url = '/other/path' if ($context.Request.HttpMethod -eq 'GET' -and $context.Request.RawUrl -eq '/other/path') { # We can log the request to the terminal write-host "$($context.Request.UserHostAddress) => $($context.Request.Url)" -f 'mag' -
jakobii revised this gist
Mar 6, 2018 . No changes.There are no files selected for viewing
-
jakobii revised this gist
Mar 6, 2018 . 1 changed file with 5 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,9 +14,11 @@ $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' } -
jakobii revised this gist
Mar 6, 2018 . 1 changed file with 9 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,6 @@ # This is a super **SIMPLE** example of how to create a very basic powershell webserver ```powershell # Http Server $http = [System.Net.HttpListener]::new() @@ -12,18 +11,20 @@ $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 retuen the request url @@ -36,7 +37,7 @@ while ($http.IsListening) { if ($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 ( Get-Content "C:\some\path\index.html" | Out-String ) @@ -53,10 +54,10 @@ while ($http.IsListening) { # ROUTE EXAMPLE 2 if ($context.Request.RawUrl -eq '/other/path') { # 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 ( Get-Content "C:\some\path\index.html" | Out-String ) @@ -70,13 +71,10 @@ while ($http.IsListening) { } # 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 :/ ``` -
jakobii revised this gist
Mar 6, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ # This is a super **SIMPLE** example of how to create a very basic powershell webserver ```powershell -
jakobii revised this gist
Mar 6, 2018 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -46,7 +46,7 @@ while ($http.IsListening) { $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 } @@ -66,7 +66,7 @@ while ($http.IsListening) { $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 } -
jakobii revised this gist
Mar 6, 2018 . 1 changed file with 11 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,6 +2,7 @@ # This is a super simple Exmple of how to create a very basic powershell webserver ```powershell # Http Server $http = [System.Net.HttpListener]::new() @@ -11,29 +12,35 @@ $http.Prefixes.Add("http://localhost:8080/") # Start the Http Server $http.Start() # Log to terminal if($http.IsListening){write-host "Ready! $($http.Prefixes)" -f 'green'} # 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 retuen the request url # Our ROUTE EXAMPLE's will use this to decide how to respond $context = $http.GetContext() # ROUTE EXAMPLE 1 if ($context.Request.RawUrl -eq '/') { # We can log the request to the terminal write-host "A request has been made from $($context.Request.UserHostAddress) for $($context.Request.Url)" -f Magenta # the html/data you want to send to the browser # you could replace this with ( Get-Content "C:\some\path\index.html" | Out-String ) [string]$html = "<h1>A Powershell Webserver</h1><p>Root Route</p>" #resposed to the request $buffer = [System.Text.Encoding]::UTF8.GetBytes($html) # convert htmtl to bytes @@ -48,12 +55,13 @@ while ($http.IsListening) { # ROUTE EXAMPLE 2 if ($context.Request.RawUrl -eq '/some/path') { # We can log the request to the terminal write-host "A request has been made from $($context.Request.UserHostAddress) for $($context.Request.Url)" -f Magenta # the html/data you want to send to the browser # you could replace this with ( Get-Content "C:\some\path\index.html" | Out-String ) [string]$html = "<h1>A Powershell Webserver</h1><p>Some Other Route</p>" #resposed to the request $buffer = [System.Text.Encoding]::UTF8.GetBytes($html) # convert htmtl to bytes $context.Response.ContentLength64 = $buffer.Length -
jakobii revised this gist
Mar 6, 2018 . 1 changed file with 18 additions and 19 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -22,7 +22,7 @@ while ($http.IsListening) { # Get Request Url # When a request is made in a web browser the GetContext() method will retuen the request url # Our ROUTE EXAMPLE's will use this to decide how to respond $context = $http.GetContext() @@ -31,16 +31,15 @@ while ($http.IsListening) { # the html/data you want to send to the browser # you could replace this with ( Get-Content "C:\some\path\index.html" | Out-String ) [string]$html = "<h1>A Powershell Webserver</h1><p>Root Route</p>" #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 respose } @@ -51,16 +50,15 @@ while ($http.IsListening) { # the html/data you want to send to the browser # you could replace this with ( Get-Content "C:\some\path\index.html" | Out-String ) [string]$html = "<h1>A Powershell Webserver</h1><p>Some Other Route</p>" #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 respose } @@ -72,4 +70,5 @@ while ($http.IsListening) { # Note: # To end the loop you have to kill the powershell terminal. ctrl-c wont work :/ ``` -
jakobii revised this gist
Mar 6, 2018 . 1 changed file with 7 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,29 +1,29 @@ # This is a super simple Exmple of how to create a very basic powershell webserver ```powershell # 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() # 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 retuen the request url # Our routes will use this to decide how to respond $context = $http.GetContext() # ROUTE EXAMPLE 1 @@ -72,4 +72,4 @@ while ($listener.IsListening) { # Note: # To end the loop you have to kill the powershell terminal. ctrl-c wont work :/ ``` -
jakobii created this gist
Mar 6, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,75 @@ # This is a super simple Exmple of how to create a very basic powershell webserver # Http Server $listener = [System.Net.HttpListener]::new() # Hostname and port to listen on $listener.Prefixes.Add("http://localhost:8080/") # Start the Http Server $listener.Start() # INFINTE LOOP # Used to listen for requests while ($listener.IsListening) { # Get Request Url # When a request is made in a web browser the GetContext() method will retuen the request url # Our routes will use this to decide how to respond $context = $listener.GetContext() # ROUTE EXAMPLE 1 if ($context.Request.RawUrl -eq '/') { # the html/data you want to send to the browser # you could replace this with Get-Content "C:\some\path\index.html" [string]$content = "<h1>A Powershell Webserver</h1><p>Root Route</p>" #resposed to the request and $response = $context.Response $buffer = [System.Text.Encoding]::UTF8.GetBytes($content) # convert htmtl to bytes $response.ContentLength64 = $buffer.Length $response.OutputStream.Write($buffer, 0, $buffer.Length) #stream to broswer $response.OutputStream.Close() # close the respose } # ROUTE EXAMPLE 2 if ($context.Request.RawUrl -eq '/some/path') { # the html/data you want to send to the browser # you could replace this with Get-Content "C:\some\path\index.html" [string]$content = "<h1>A Powershell Webserver</h1><p>Some Other Route</p>" #resposed to the request and $response = $context.Response $buffer = [System.Text.Encoding]::UTF8.GetBytes($content) # convert htmtl to bytes $response.ContentLength64 = $buffer.Length $response.OutputStream.Write($buffer, 0, $buffer.Length) #stream to broswer $response.OutputStream.Close() # close the respose } # 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 :/