function Send-Text { param($Context, $Text) if (-not $Context -or -not $Context.Response) { return } $bytes = [Text.Encoding]::UTF8.GetBytes($Text) $Context.Response.ContentType = 'text/plain' $Context.Response.ContentLength64 = $bytes.Length $Context.Response.OutputStream.Write($bytes,0,$bytes.Length) $Context.Response.OutputStream.Close() } function Send-HTML { [CmdletBinding()] param( [Parameter(Mandatory = $true)][object] $Context, [Parameter(Mandatory = $true)][string] $Html ) if (-not $Context -or -not $Context.Response) { return } try { $Context.Response.Headers["Cache-Control"] = "no-store, no-cache, must-revalidate, max-age=0" $Context.Response.Headers["Pragma"] = "no-cache" $Context.Response.Headers["Expires"] = "0" } catch { } $bytes = [Text.Encoding]::UTF8.GetBytes($Html) $Context.Response.ContentType = "text/html; charset=utf-8" $Context.Response.ContentLength64 = $bytes.Length $Context.Response.OutputStream.Write($bytes, 0, $bytes.Length) $Context.Response.OutputStream.Close() } function Send-JSON { [CmdletBinding()] param( $Context, $Object ) if (-not $Context -or -not $Context.Response) { return } $json = $null try { if ($null -eq $Object) { Write-LogHybrid "Send-JSON called with `$null object; returning empty JSON array." Warning Printers -LogToEvent $json = '[]' } else { try { $json = $Object | ConvertTo-Json -Depth 8 -ErrorAction Stop } catch { Write-LogHybrid "Send-JSON serialization failed: $($_.Exception.Message); returning empty JSON array." Error Printers -LogToEvent $json = '[]' } } $json = [string]$json try { $Context.Response.Headers["Cache-Control"] = "no-store, no-cache, must-revalidate, max-age=0" $Context.Response.Headers["Pragma"] = "no-cache" $Context.Response.Headers["Expires"] = "0" } catch { } $bytes = [Text.Encoding]::UTF8.GetBytes($json) $Context.Response.ContentType = "application/json; charset=utf-8" $Context.Response.ContentLength64 = $bytes.Length $Context.Response.OutputStream.Write($bytes, 0, $bytes.Length) } catch { Write-LogHybrid "Send-JSON fatal error: $($_.Exception.Message)" Error Printers -LogToEvent try { $fallback = '[]' $bytes = [Text.Encoding]::UTF8.GetBytes($fallback) $Context.Response.ContentType = "application/json; charset=utf-8" $Context.Response.ContentLength64 = $bytes.Length $Context.Response.OutputStream.Write($bytes, 0, $bytes.Length) } catch { } } finally { try { $Context.Response.OutputStream.Close() } catch { } } }