-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
125 additions
and
120 deletions.
There are no files selected for viewing
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<# | ||
.SYNOPSIS | ||
Extracts the resource name from the URL | ||
.DESCRIPTION | ||
Takes "/time/month?SomeQuery=1" and returns "time" i.e the first node in the URL path | ||
#> | ||
function Get-ResourceFromURL { | ||
Param( | ||
[Parameter(Mandatory=$true)] | ||
[string]$RawURL | ||
) | ||
Write-Verbose "Get-ResourceFromURL| RawURL: $RawURL" | ||
|
||
$Resource = (($RawURL -split "\?")[0] -split "/")[1] # element 0 is empty because of leading "/" | ||
Write-Verbose "Get-ResourceFromURL| Extracted resource: $Resource" | ||
|
||
$VerificationRegex = '^(?:[a-zA-Z0-9]+|favicon.ico)$' | ||
if (-not ($Resource -match $VerificationRegex)){ | ||
Write-Verbose "Get-ResourceFromURL| Resource name does not match the verification RegEx: $VerificationRegex" | ||
throw "Resource identifier contains invalid characters" | ||
} | ||
$Resource | ||
} |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<# | ||
.SYNOPSIS | ||
Receives HTTP request body | ||
#> | ||
function Receive-Request { | ||
Param( | ||
[Parameter(Mandatory,ValueFromPipelineByPropertyName)] | ||
[System.Net.HttpListenerRequest]$Request | ||
) | ||
$Output = "" | ||
|
||
$Size = $Request.ContentLength64 + 1 | ||
|
||
$buffer = New-Object byte[] $Size | ||
do { | ||
$count = $Request.InputStream.Read($buffer, 0, $Size) | ||
Write-Verbose "Receive-Request | Received $count bytes" | ||
$Output += $Request.ContentEncoding.GetString($buffer, 0, $count) | ||
} until($count -lt $Size) | ||
|
||
$Request.InputStream.Close() | ||
$Output | ||
} |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<# | ||
.SYNOPSIS | ||
Writes the response and closes Response object | ||
#> | ||
function Send-Response { | ||
Param( | ||
[Parameter(Position=0, Mandatory=$true)] | ||
[System.Net.HttpListenerResponse]$Response, | ||
|
||
[Parameter(Position=1, Mandatory=$true)] | ||
[int]$StatusCode = 200, | ||
|
||
[Parameter(Position=2, ValueFromPipeline=$true)] | ||
$Content = "" | ||
) | ||
# Seems like we need to set the status code first before we write data. Otherwise 200 is set by default | ||
$Response.StatusCode = $StatusCode | ||
|
||
Write-Verbose "Send-ResponseNew| Content type is $($Content.GetType().ToString())" | ||
switch($Content.GetType().ToString()){ | ||
"System.Object[]" { $buffer = $Content } | ||
default { $buffer = [System.Text.Encoding]::UTF8.GetBytes($Content) } | ||
} | ||
|
||
$Response.ContentLength64 = $buffer.Length | ||
$Response.OutputStream.Write($buffer, 0, $buffer.Length) | ||
$Response.Close() | ||
} |