Skip to content

Instantly share code, notes, and snippets.

@lazywinadmin
Forked from jakobii/HTTPServer.ps1
Created August 4, 2018 01:29

Revisions

  1. @jakobii jakobii revised this gist Jun 14, 2018. 1 changed file with 45 additions and 14 deletions.
    59 changes: 45 additions & 14 deletions Powershell_HTTP_Server.MD
    Original 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 the request url
    # Our ROUTE EXAMPLE's will use this to decide how to respond
    # 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
    # method = 'GET' and url = '/'
    # 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 ( Get-Content "C:\some\path\index.html" | Out-String )
    [string]$html = "<h1>A Powershell Webserver</h1><p>Root Route</p>"
    # 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
    # method = 'GET' and url = '/other/path'
    if ($context.Request.HttpMethod -eq 'GET' -and $context.Request.RawUrl -eq '/other/path') {
    # 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'
    # 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>"
    [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) # convert htmtl to bytes
    $buffer = [System.Text.Encoding]::UTF8.GetBytes($html)
    $context.Response.ContentLength64 = $buffer.Length
    $context.Response.OutputStream.Write($buffer, 0, $buffer.Length) #stream to broswer
    $context.Response.OutputStream.Close() # close the response
    $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...
    }
  2. @jakobii jakobii revised this gist Jun 13, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Powershell_HTTP_Server.MD
    Original 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 retuen the 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()
  3. @jakobii jakobii revised this gist Mar 6, 2018. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions Powershell_HTTP_Server.MD
    Original 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
    if ($context.Request.RawUrl -eq '/') {
    # 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
    if ($context.Request.RawUrl -eq '/other/path') {
    # 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'
  4. @jakobii jakobii revised this gist Mar 6, 2018. No changes.
  5. @jakobii jakobii revised this gist Mar 6, 2018. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions Powershell_HTTP_Server.MD
    Original 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'
    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'
    }
  6. @jakobii jakobii revised this gist Mar 6, 2018. 1 changed file with 9 additions and 11 deletions.
    20 changes: 9 additions & 11 deletions Powershell_HTTP_Server.MD
    Original 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 to terminal
    if($http.IsListening){write-host "Ready! $($http.Prefixes)" -f 'green'}
    # 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 "A request has been made from $($context.Request.UserHostAddress) for $($context.Request.Url)" -f Magenta
    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 '/some/path') {
    if ($context.Request.RawUrl -eq '/other/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
    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 :/
    ```
  7. @jakobii jakobii revised this gist Mar 6, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Powershell_HTTP_Server.MD
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@

    # This is a super simple Exmple of how to create a very basic powershell webserver
    # This is a super **SIMPLE** example of how to create a very basic powershell webserver

    ```powershell
  8. @jakobii jakobii revised this gist Mar 6, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Powershell_HTTP_Server.MD
    Original 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 respose
    $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 respose
    $context.Response.OutputStream.Close() # close the response
    }
  9. @jakobii jakobii revised this gist Mar 6, 2018. 1 changed file with 11 additions and 3 deletions.
    14 changes: 11 additions & 3 deletions Powershell_HTTP_Server.MD
    Original 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
  10. @jakobii jakobii revised this gist Mar 6, 2018. 1 changed file with 18 additions and 19 deletions.
    37 changes: 18 additions & 19 deletions Powershell_HTTP_Server.MD
    Original 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 routes will use this to decide how to respond
    # 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"
    [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
    # 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"
    [string]$content = "<h1>A Powershell Webserver</h1><p>Some Other Route</p>"
    # 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 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
    #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 :/
    ```
  11. @jakobii jakobii revised this gist Mar 6, 2018. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions Powershell_HTTP_Server.MD
    Original 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
    $listener = [System.Net.HttpListener]::new()
    $http = [System.Net.HttpListener]::new()
    # Hostname and port to listen on
    $listener.Prefixes.Add("http://localhost:8080/")
    $http.Prefixes.Add("http://localhost:8080/")
    # Start the Http Server
    $listener.Start()
    $http.Start()
    # INFINTE LOOP
    # Used to listen for requests
    while ($listener.IsListening) {
    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 = $listener.GetContext()
    $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 :/

    ```
  12. @jakobii jakobii created this gist Mar 6, 2018.
    75 changes: 75 additions & 0 deletions Powershell_HTTP_Server.MD
    Original 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 :/