Skip to content

Commit 771c9a1

Browse files
burtonralexellis
authored andcommitted
Update to use context and response classes
Adding new class objects to be similar to other http templates These new classes allow the function to access the request and other propoerties as well as being able to modify the response as needed with some sane defaults Signed-off-by: Burton Rheutan <[email protected]>
1 parent 3b8987c commit 771c9a1

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
# Rename input for better readability
2-
$funcInput = $args
3-
$output = "Hello! Your input was: " + $funcInput
1+
Param(
2+
[Parameter(Mandatory=$true)]
3+
[FunctionContext]$fnContext,
4+
[Parameter(Mandatory=$true)]
5+
[FunctionResponse]$fnResponse
6+
)
47

5-
# Must provide an output variable for the main process to return
6-
$output
8+
$output = "Hello! Your input was: " + $fnContext.Body
9+
10+
$fnResponse.Body = $output

template/powershell-http/server.ps1

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
Add-Type -AssemblyName System.Web
22

3+
Class FunctionContext{
4+
[string] $Method;
5+
[string] $Request;
6+
[string] $Body;
7+
[System.Net.WebHeaderCollection] $Headers;
8+
[System.Collections.Specialized.NameValueCollection] $Query;
9+
[string] $Path;
10+
}
11+
12+
Class FunctionResponse{
13+
[string] $Body;
14+
[System.Net.WebHeaderCollection] $Headers;
15+
[int] $Status;
16+
}
17+
318
$listener = New-Object System.Net.HttpListener
419
$listener.Prefixes.Add("http://*:8081/")
520
$listener.Start()
@@ -14,19 +29,29 @@ do {
1429

1530
$reader = [System.IO.StreamReader]::new($reqStream, $reqEncoding)
1631

17-
$reqBody = $reader.ReadToEnd()
32+
$fnContext = [FunctionContext]::new()
33+
$fnContext.Body = $reader.ReadToEnd()
34+
$fnContext.Headers = $context.Request.Headers
35+
$fnContext.Method = $context.Request.HttpMethod
36+
$fnContext.Query = $context.Request.QueryString
37+
$fnContext.Path = $context.Request.Url.LocalPath
38+
$fnContext.Request = $context.Request
1839

40+
$fnResponse = [FunctionResponse]::new()
41+
$fnResponse.Headers = [System.Net.WebHeaderCollection]::new()
1942
try {
20-
$funcResult = . .\function\handler.ps1 $reqBody
43+
. .\function\handler.ps1 -fnContext $fnContext -fnResponse $fnResponse
2144

22-
$Content = [System.Text.Encoding]::UTF8.GetBytes($funcResult)
23-
$response.StatusCode = 200
45+
$Content = [System.Text.Encoding]::UTF8.GetBytes($fnResponse.Body)
46+
$response.StatusCode = $(If ($fnResponse.Status) {$fnResponse.Status} Else {200}) #default to 200 response if not set
2447
}
2548
catch {
26-
$Content = [System.Text.Encoding]::UTF8.GetBytes("$_.Exception.Message")
27-
$response.StatusCode = 500
49+
$responseBody = $(If ($_.Exception.Message) {$_.Exception.Message} Else {$fnResponse.Body})
50+
$Content = [System.Text.Encoding]::UTF8.GetBytes($responseBody)
51+
$response.StatusCode = $(If ($fnResponse.Status) {$fnResponse.Status} Else {500}) #default to 500 response for exceptions
2852
}
2953

54+
$response.Headers = $fnResponse.Headers
3055
$response.ContentLength64 = $Content.Length
3156
$response.OutputStream.Write($Content, 0, $Content.Length)
3257
$response.Close()

0 commit comments

Comments
 (0)