/
PodeServer.ps1
329 lines (286 loc) · 10.8 KB
/
PodeServer.ps1
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
function Start-PodeSocketServer
{
param (
[switch]
$Browse
)
# setup the callback for sockets
$PodeContext.Server.Sockets.Ssl.Callback = Get-PodeSocketCertifcateCallback
# setup any inbuilt middleware
$inbuilt_middleware = @(
(Get-PodeAccessMiddleware),
(Get-PodeLimitMiddleware),
(Get-PodePublicMiddleware),
(Get-PodeRouteValidateMiddleware),
(Get-PodeBodyMiddleware),
(Get-PodeCookieMiddleware)
)
$PodeContext.Server.Middleware = ($inbuilt_middleware + $PodeContext.Server.Middleware)
# work out which endpoints to listen on
$endpoints = @()
@(Get-PodeEndpoints -Type Http) | ForEach-Object {
# get the ip address
$_ip = [string]($_.Address)
$_ip = (Get-PodeIPAddressesForHostname -Hostname $_ip -Type All | Select-Object -First 1)
$_ip = (Get-PodeIPAddress $_ip)
# add endpoint to list
$endpoints += @{
Address = $_ip
Port = $_.Port
Certificate = $_.Certificate.Raw
HostName = $_.Url
}
}
try
{
# register endpoints on the listener
$endpoints | ForEach-Object {
$PodeContext.Server.Sockets.Listeners += (Initialize-PodeSocketListenerEndpoint `
-Type Sockets `
-Address $_.Address `
-Port $_.Port `
-Certificate $_.Certificate)
}
}
catch {
$_ | Write-PodeErrorLog
$_.Exception | Write-PodeErrorLog -CheckInnerException
Close-PodeSocketListener -Type Sockets
throw $_.Exception
}
# script for listening out for incoming requests
$listenScript = {
param (
[Parameter(Mandatory=$true)]
[int]
$ThreadId
)
try
{
Start-PodeSocketListener -Listeners $PodeContext.Server.Sockets.Listeners
[System.Threading.Thread]::CurrentThread.IsBackground = $true
[System.Threading.Thread]::CurrentThread.Priority = [System.Threading.ThreadPriority]::Lowest
while (!$PodeContext.Tokens.Cancellation.IsCancellationRequested)
{
Wait-PodeTask ([System.Threading.Tasks.Task]::Delay(60))
}
}
catch [System.OperationCanceledException] {}
catch {
$_ | Write-PodeErrorLog
$_.Exception | Write-PodeErrorLog -CheckInnerException
throw $_.Exception
}
}
# start the runspace for listening on x-number of threads
1..$PodeContext.Threads.Web | ForEach-Object {
Add-PodeRunspace -Type 'Main' -ScriptBlock $listenScript `
-Parameters @{ 'ThreadId' = $_ }
}
# script to keep web server listening until cancelled
$waitScript = {
try {
while (!$PodeContext.Tokens.Cancellation.IsCancellationRequested) {
Start-Sleep -Seconds 1
}
}
catch [System.OperationCanceledException] {}
catch {
$_ | Write-PodeErrorLog
$_.Exception | Write-PodeErrorLog -CheckInnerException
throw $_.Exception
}
finally {
Close-PodeSocketListener -Type Sockets
}
}
Add-PodeRunspace -Type 'Main' -ScriptBlock $waitScript
# browse to the first endpoint, if flagged
if ($Browse) {
Start-Process $endpoints[0].HostName
}
return @($endpoints.HostName)
}
function Invoke-PodeSocketHandler
{
param(
[Parameter(Mandatory)]
[hashtable]
$Context
)
try
{
# reset with basic event data
$WebEvent = @{
OnEnd = @()
Auth = @{}
Response = @{
Headers = @{}
ContentLength64 = 0
ContentType = $null
OutputStream = New-Object -TypeName System.IO.MemoryStream
StatusCode = 200
StatusDescription = 'OK'
}
Request = @{}
Lockable = $PodeContext.Lockable
Path = $null
Method = $null
Query = $null
Protocol = $Context.Protocol
Endpoint = $null
ContentType = $null
ErrorType = $null
Cookies = @{}
PendingCookies = @{}
Parameters = $null
Data = $null
Files = $null
Streamed = $true
Timestamp = [datetime]::UtcNow
}
# set pode in server response header
Set-PodeServerHeader
# make the stream (use an ssl stream if we have a cert)
$stream = [System.Net.Sockets.NetworkStream]::new($Context.Socket, $true)
if ($null -ne $Context.Certificate) {
try {
$stream = [System.Net.Security.SslStream]::new($stream, $false, $PodeContext.Server.Sockets.Ssl.Callback)
$stream.AuthenticateAsServer($Context.Certificate, $true, $PodeContext.Server.Sockets.Ssl.Protocols, $false)
}
catch {
# immediately close http connections
Close-PodeSocket -Socket $Context.Socket -Shutdown
return
}
}
# read the request headers - prepare for the dodgest of hacks ever. I apologise profusely.
try {
$bytes = New-Object byte[] 0
$Context.Socket.Receive($bytes) | Out-Null
}
catch {
$err = [System.Net.Http.HttpRequestException]::new()
$err.Data.Add('PodeStatusCode', 408)
throw $err
}
$bytes = New-Object byte[] $Context.Socket.Available
(Wait-PodeTask -Task $stream.ReadAsync($bytes, 0, $Context.Socket.Available)) | Out-Null
$req_info = Get-PodeServerRequestDetails -Bytes $bytes -Protocol $Context.Protocol
# set the rest of the event data
$WebEvent.Request = @{
Body = @{
Value = $req_info.Body
Bytes = $req_info.RawBody
}
Headers = $req_info.Headers
Url = $req_info.Uri
UrlReferrer = $req_info.Headers['Referer']
UserAgent = $req_info.Headers['User-Agent']
HttpMethod = $req_info.Method
RemoteEndPoint = $Context.Socket.RemoteEndPoint
Protocol = $req_info.Protocol
ProtocolVersion = ($req_info.Protocol -isplit '/')[1]
ContentEncoding = (Get-PodeEncodingFromContentType -ContentType $req_info.Headers['Content-Type'])
}
$WebEvent.Path = $req_info.Uri.AbsolutePath
$WebEvent.Method = $req_info.Method.ToLowerInvariant()
$WebEvent.Endpoint = $req_info.Headers['Host']
$WebEvent.ContentType = $req_info.Headers['Content-Type']
$WebEvent.Query = [System.Web.HttpUtility]::ParseQueryString($req_info.Query)
if ($null -eq $WebEvent.Query) {
$WebEvent.Query = @{}
}
# add logging endware for post-request
Add-PodeRequestLogEndware -WebEvent $WebEvent
# invoke middleware
if ((Invoke-PodeMiddleware -WebEvent $WebEvent -Middleware $PodeContext.Server.Middleware -Route $WebEvent.Path)) {
# get the route logic
$route = Get-PodeRoute -Method $WebEvent.Method -Route $WebEvent.Path -Protocol $WebEvent.Protocol `
-Endpoint $WebEvent.Endpoint -CheckWildMethod
# invoke route and custom middleware
if ((Invoke-PodeMiddleware -WebEvent $WebEvent -Middleware $route.Middleware)) {
if ($null -ne $route.Logic) {
Invoke-PodeScriptBlock -ScriptBlock $route.Logic -Arguments (@($WebEvent) + @($route.Arguments)) -Scoped -Splat
}
}
}
}
catch [System.OperationCanceledException] {}
catch [System.Net.Http.HttpRequestException] {
$code = [int]($_.Exception.Data['PodeStatusCode'])
if ($code -le 0) {
$code = 400
}
Set-PodeResponseStatus -Code $code -Exception $_
}
catch {
$_ | Write-PodeErrorLog
$_.Exception | Write-PodeErrorLog -CheckInnerException
Set-PodeResponseStatus -Code 500 -Exception $_
}
try {
# invoke endware specifc to the current web event
$_endware = ($WebEvent.OnEnd + @($PodeContext.Server.Endware))
Invoke-PodeEndware -WebEvent $WebEvent -Endware $_endware
# write the response line
$protocol = $req_info.Protocol
if ([string]::IsNullOrWhiteSpace($protocol)) {
$protocol = 'HTTP/1.1'
}
$newLine = "`r`n"
$res_msg = "$($protocol) $($WebEvent.Response.StatusCode) $($WebEvent.Response.StatusDescription)$($newLine)"
# set response headers before adding
Set-PodeServerResponseHeaders -WebEvent $WebEvent
# write the response headers
if ($WebEvent.Response.Headers.Count -gt 0) {
foreach ($key in $WebEvent.Response.Headers.Keys) {
foreach ($value in $WebEvent.Response.Headers[$key]) {
$res_msg += "$($key): $($value)$($newLine)"
}
}
}
$res_msg += $newLine
# stream response output
$buffer = $PodeContext.Server.Encoding.GetBytes($res_msg)
Wait-PodeTask -Task $stream.WriteAsync($buffer, 0, $buffer.Length)
$WebEvent.Response.OutputStream.WriteTo($stream)
$stream.Flush()
}
catch [System.Management.Automation.MethodInvocationException] { }
finally {
# close socket stream
if ($null -ne $WebEvent.Response.OutputStream) {
Close-PodeDisposable -Disposable $WebEvent.Response.OutputStream -Close -CheckNetwork
}
Close-PodeSocket -Socket $Context.Socket -Shutdown
}
}
function Set-PodeServerResponseHeaders
{
param(
[Parameter(Mandatory=$true)]
$WebEvent
)
# add content-type
if (![string]::IsNullOrWhiteSpace($WebEvent.Response.ContentType)) {
Set-PodeHeader -Name 'Content-Type' -Value $WebEvent.Response.ContentType
}
else {
$WebEvent.Response.Headers.Remove('Content-Type')
}
# add content-length
if (($WebEvent.Response.ContentLength64 -eq 0) -and ($WebEvent.Response.OutputStream.Length -gt 0)) {
$WebEvent.Response.ContentLength64 = $WebEvent.Response.OutputStream.Length
}
if ($WebEvent.Response.ContentLength64 -gt 0) {
Set-PodeHeader -Name 'Content-Length' -Value $WebEvent.Response.ContentLength64
}
else {
$WebEvent.Response.Headers.Remove('Content-Length')
}
# add the date of the response
Set-PodeHeader -Name 'Date' -Value ([DateTime]::UtcNow.ToString("r", [CultureInfo]::InvariantCulture))
# state to close the connection (no support for keep-alive yet)
Set-PodeHeader -Name 'Connection' -Value 'close'
}