Repository navigation
Expand file tree
/
Copy pathWebListener.psm1
More file actions
216 lines (191 loc) · 6.25 KB
/
Copy pathWebListener.psm1
File metadata and controls
216 lines (191 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
Class WebListener
{
[int]$HttpPort
[int]$HttpsPort
[int]$Tls11Port
[int]$TlsPort
[System.Management.Automation.Job]$Job
WebListener () { }
[String] GetStatus()
{
return $This.Job.JobStateInfo.State
}
}
[WebListener]$WebListener
function Get-WebListener
{
[CmdletBinding(ConfirmImpact = 'Low')]
[OutputType([WebListener])]
param()
process
{
return [WebListener]$Script:WebListener
}
}
function Start-WebListener
{
[CmdletBinding(ConfirmImpact = 'Low')]
[OutputType([WebListener])]
param
(
[ValidateRange(1,65535)]
[int]$HttpPort = 8083,
[ValidateRange(1,65535)]
[int]$HttpsPort = 8084,
[ValidateRange(1,65535)]
[int]$Tls11Port = 8085,
[ValidateRange(1,65535)]
[int]$TlsPort = 8086
)
process
{
$runningListener = Get-WebListener
if ($null -ne $runningListener -and $runningListener.GetStatus() -eq 'Running')
{
return $runningListener
}
$initTimeoutSeconds = 15
$appDll = 'WebListener.dll'
$serverPfx = 'ServerCert.pfx'
$serverPfxPassword = 'password'
$initCompleteMessage = 'Now listening on'
$sleepMilliseconds = 100
$serverPfxPath = Join-Path $MyInvocation.MyCommand.Module.ModuleBase $serverPfx
$Job = Start-Job {
$path = Split-Path -parent (get-command WebListener).Path -Verbose
Push-Location $path -Verbose
'appDLL: {0}' -f $using:appDll
'serverPfxPath: {0}' -f $using:serverPfxPath
'serverPfxPassword: {0}' -f $using:serverPfxPassword
'HttpPort: {0}' -f $using:HttpPort
'Https: {0}' -f $using:HttpsPort
'Tls11Port: {0}' -f $using:Tls11Port
'TlsPort: {0}' -f $using:TlsPort
$env:ASPNETCORE_ENVIRONMENT = 'Development'
dotnet $using:appDll $using:serverPfxPath $using:serverPfxPassword $using:HttpPort $using:HttpsPort $using:Tls11Port $using:TlsPort
}
$Script:WebListener = [WebListener]@{
HttpPort = $HttpPort
HttpsPort = $HttpsPort
Tls11Port = $Tls11Port
TlsPort = $TlsPort
Job = $Job
}
# Count iterations of $sleepMilliseconds instead of using system time to work around possible CI VM sleep/delays
$sleepCountRemaining = $initTimeoutSeconds * 1000 / $sleepMilliseconds
do
{
Start-Sleep -Milliseconds $sleepMilliseconds
$initStatus = $Job.ChildJobs[0].Output | Out-String
$isRunning = $initStatus -match $initCompleteMessage
$sleepCountRemaining--
}
while (-not $isRunning -and $sleepCountRemaining -gt 0)
if (-not $isRunning)
{
$jobErrors = $Job.ChildJobs[0].Error | Out-String
$jobOutput = $Job.ChildJobs[0].Output | Out-String
$jobVerbose = $Job.ChildJobs[0].Verbose | Out-String
$Job | Stop-Job
$Job | Remove-Job -Force
$message = 'WebListener did not start before the timeout was reached.{0}Errors:{0}{1}{0}Output:{0}{2}{0}Verbose:{0}{3}' -f
([System.Environment]::NewLine), $jobErrors, $jobOutput, $jobVerbose
throw $message
}
return $Script:WebListener
}
}
function Stop-WebListener
{
[CmdletBinding(ConfirmImpact = 'Low')]
[OutputType([Void])]
param()
process
{
$Script:WebListener.job | Stop-Job -PassThru | Remove-Job
$Script:WebListener = $null
}
}
function Get-WebListenerClientCertificate {
[CmdletBinding(ConfirmImpact = 'Low')]
[OutputType([System.Security.Cryptography.X509Certificates.X509Certificate2])]
param()
process {
$pfxPath = Join-Path $MyInvocation.MyCommand.Module.ModuleBase 'ClientCert.pfx'
[System.Security.Cryptography.X509Certificates.X509Certificate2]::new($pfxPath,'password')
}
}
function Get-WebListenerUrl {
[CmdletBinding()]
[OutputType([Uri])]
param (
[switch]$Https,
[ValidateSet('Default', 'Tls12', 'Tls11', 'Tls')]
[string]$SslProtocol = 'Default',
[ValidateSet(
'Auth',
'Cert',
'Compression',
'Delay',
'Delete',
'Encoding',
'Get',
'Home',
'Link',
'Multipart',
'Patch',
'Post',
'Put',
'Redirect',
'Response',
'ResponseHeaders',
'/'
)]
[String]$Test,
[String]$TestValue,
[System.Collections.IDictionary]$Query
)
process {
$runningListener = Get-WebListener
if ($null -eq $runningListener -or $runningListener.GetStatus() -ne 'Running')
{
return $null
}
$Uri = [System.UriBuilder]::new()
# Use 127.0.0.1 and not localhost due to https://github.com/dotnet/corefx/issues/24104
$Uri.Host = '127.0.0.1'
$Uri.Port = $runningListener.HttpPort
$Uri.Scheme = 'Http'
if ($Https.IsPresent)
{
switch ($SslProtocol)
{
'Tls11' { $Uri.Port = $runningListener.Tls11Port }
'Tls' { $Uri.Port = $runningListener.TlsPort }
# The base HTTPs port is configured for Tls12 only
default { $Uri.Port = $runningListener.HttpsPort }
}
$Uri.Scheme = 'Https'
}
if ($TestValue)
{
$Uri.Path = '{0}/{1}' -f $Test, $TestValue
}
else
{
$Uri.Path = $Test
}
$StringBuilder = [System.Text.StringBuilder]::new()
foreach ($key in $Query.Keys)
{
$null = $StringBuilder.Append([System.Net.WebUtility]::UrlEncode($key))
$null = $StringBuilder.Append('=')
$null = $StringBuilder.Append([System.Net.WebUtility]::UrlEncode($Query[$key].ToString()))
$null = $StringBuilder.Append('&')
}
$Uri.Query = $StringBuilder.ToString()
return [Uri]$Uri.ToString()
}
}