diff --git a/lib/core/tools/New-IcingaNewLine.psm1 b/lib/core/tools/New-IcingaNewLine.psm1 new file mode 100644 index 0000000..0f8d9b6 --- /dev/null +++ b/lib/core/tools/New-IcingaNewLine.psm1 @@ -0,0 +1,4 @@ +function New-IcingaNewLine() +{ + return "`r`n"; +} diff --git a/lib/web/Icinga_HTTPResponse_Enums.psm1 b/lib/web/Icinga_HTTPResponse_Enums.psm1 new file mode 100644 index 0000000..d6fd01e --- /dev/null +++ b/lib/web/Icinga_HTTPResponse_Enums.psm1 @@ -0,0 +1,36 @@ +<# + # This script will provide 'Enums' we can use within our module to + # easier access constants and to maintain a better overview of the + # entire components + #> +[hashtable]$HTTPResponseCode = @{ + 200 = 'Ok'; + 400 = 'Bad Request'; + 401 = 'Unauthorized'; + 403 = 'Forbidden'; + 404 = 'Not Found' + 500 = 'Internal Server Error'; +}; + +[hashtable]$HTTPResponseType = @{ + 'Ok' = 200; + 'Bad Request' = 400; + 'Unauthorized' = 401; + 'Forbidden' = 403; + 'Not Found' = 404; + 'Internal Server Error' = 500; +}; + +<# + # Once we defined a new enum hashtable above, simply add it to this list + # to make it available within the entire module. + # + # Example usage: + # $IcingaHTTPEnums.HTTPResponseType.Ok + #> +[hashtable]$IcingaHTTPEnums = @{ + HTTPResponseCode = $HTTPResponseCode; + HTTPResponseType = $HTTPResponseType; +} + +Export-ModuleMember -Variable @( 'IcingaHTTPEnums' ); diff --git a/lib/web/New-IcingaTCPClientRESTMessage.psm1 b/lib/web/New-IcingaTCPClientRESTMessage.psm1 new file mode 100644 index 0000000..bce7fe8 --- /dev/null +++ b/lib/web/New-IcingaTCPClientRESTMessage.psm1 @@ -0,0 +1,63 @@ +function New-IcingaTCPClientRESTMessage() +{ + param( + [Hashtable]$Headers = $null, + $ContentBody = $null, + [int]$HTTPResponse = 200, + [switch]$BasicAuth = $FALSE + ); + + [string]$ContentLength = ''; + [string]$HTMLContent = ''; + [string]$AuthHeader = ''; + + if ($null -ne $ContentBody) { + $json = ConvertTo-Json $ContentBody -Depth 100 -Compress; + $bytes = [System.Text.Encoding]::UTF8.GetBytes($json); + $HTMLContent = [System.Text.Encoding]::UTF8.GetString($bytes); + if ($bytes.Length -gt 0) { + $ContentLength = [string]::Format( + 'Content-Length: {0}{1}', + $bytes.Length, + (New-IcingaNewLine) + ); + } + } + + if ($BasicAuth) { + $AuthHeader = [string]::Format( + 'WWW-Authenticate: Basic realm="Icinga for Windows"{0}', + (New-IcingaNewLine) + ); + } + + $ResponseMeessage = -Join( + [string]::Format( + 'HTTP/1.1 {0} {1}{2}', + $HTTPResponse, + $IcingaHTTPEnums.HTTPResponseCode[$HTTPResponse], + (New-IcingaNewLine) + ), + [string]::Format( + 'Server: {0}{1}', + (Get-IcingaHostname -LowerCase $TRUE -AutoUseFQDN $TRUE), + (New-IcingaNewLine) + ), + [string]::Format( + 'Content-Type: application/json{0}', + (New-IcingaNewLine) + ), + $AuthHeader, + $ContentLength, + (New-IcingaNewLine), + $HTMLContent + ); + + # Encode our message before sending it + $UTF8Message = [System.Text.Encoding]::UTF8.GetBytes($ResponseMeessage); + + return @{ + 'message' = $UTF8Message; + 'length' = $UTF8Message.Length; + }; +} diff --git a/lib/web/Send-IcingaTCPClientMessage.psm1 b/lib/web/Send-IcingaTCPClientMessage.psm1 new file mode 100644 index 0000000..9ee0613 --- /dev/null +++ b/lib/web/Send-IcingaTCPClientMessage.psm1 @@ -0,0 +1,14 @@ +function Send-IcingaTCPClientMessage() +{ + param( + [Hashtable]$Message = @{}, + [System.Net.Security.SslStream]$Stream = $null + ); + + if ($null -eq $Message -Or $Message.Count -eq 0 -Or $Message.length -eq 0) { + return; + } + + $Stream.Write($Message.message, 0, $Message.length); + $Stream.Flush(); +}