2020-03-24 13:40:33 -04:00
|
|
|
function Read-IcingaRESTMessage()
|
|
|
|
|
{
|
|
|
|
|
param(
|
|
|
|
|
[string]$RestMessage = $null
|
|
|
|
|
);
|
|
|
|
|
|
2020-03-24 13:43:27 -04:00
|
|
|
[hashtable]$Request = @{};
|
|
|
|
|
$RestMessage -match '(\d+) (.+) (.+) (.+)' | Out-Null;
|
2020-03-24 13:40:33 -04:00
|
|
|
|
|
|
|
|
$Request.Add('MessageLength', $Matches[1]);
|
|
|
|
|
$Request.Add('Method', $Matches[2]);
|
|
|
|
|
$Request.Add('FullRequest', $Matches[3]);
|
|
|
|
|
$Request.Add('RequestPath', @{});
|
|
|
|
|
$Request.Add('RequestArguments', @{});
|
|
|
|
|
|
|
|
|
|
#Path
|
2020-03-24 13:43:27 -04:00
|
|
|
$PathMatch = $Matches[3];
|
2020-03-24 13:40:33 -04:00
|
|
|
$PathMatch -match '(.+)\?(.*)' | Out-Null
|
2020-03-24 13:43:27 -04:00
|
|
|
$Path = $Matches[1];
|
|
|
|
|
$Arguments = $Matches[2];
|
2020-03-24 13:40:33 -04:00
|
|
|
$Request.RequestPath.Add('FullPath', $Matches[1]);
|
|
|
|
|
$Request.RequestPath.Add('PathArray', $Matches[1].TrimStart('/').Split('/'));
|
|
|
|
|
|
2020-03-24 13:43:27 -04:00
|
|
|
$Matches = $null;
|
2020-03-24 13:40:33 -04:00
|
|
|
|
|
|
|
|
# Arguments
|
2020-03-24 13:43:27 -04:00
|
|
|
$ArgumentsSplit = $Arguments.Split('&');
|
|
|
|
|
$ArgumentsSplit+='\\\\\\\\\\\\=FIN';
|
2020-03-24 13:40:33 -04:00
|
|
|
foreach ( $Argument in $ArgumentsSplit | sort -descending) {
|
2020-03-24 13:43:27 -04:00
|
|
|
$Argument -match '(.+)=(.+)' | Out-Null;
|
2020-03-24 13:40:33 -04:00
|
|
|
If (($Matches[1] -ne $Current) -And ($NULL -ne $Current)) {
|
|
|
|
|
$Request.RequestArguments.Add( $Current, $ArgumentContent );
|
2020-03-24 13:43:27 -04:00
|
|
|
[array]$ArgumentContent = $null;
|
2020-03-24 13:40:33 -04:00
|
|
|
}
|
2020-03-24 13:43:27 -04:00
|
|
|
$Current = $Matches[1];
|
2020-03-24 13:40:33 -04:00
|
|
|
[array]$ArgumentContent+=($Matches[2]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Header
|
|
|
|
|
$Request.Add( 'Header', @{} );
|
2020-03-24 13:43:27 -04:00
|
|
|
$SplitString = $RestMessage.split("`r`n");
|
2020-03-24 13:40:33 -04:00
|
|
|
foreach ( $SingleString in $SplitString ) {
|
|
|
|
|
if ( ([string]::IsNullOrEmpty($SingleString) -eq $FALSE) -And ($SingleString -match '^{.+' -eq $FALSE) ) {
|
|
|
|
|
$SingleSplitString = $SingleString.Split(':',2);
|
|
|
|
|
$Request.Header.Add( $SingleSplitString[0], $SingleSplitString[1].Trim());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$Request.Add('ContentLength', [int](Get-IcingaRESTHeaderValue -Header 'Content-Length' -Request $Request));
|
|
|
|
|
|
2020-03-24 13:43:27 -04:00
|
|
|
$Matches = $null;
|
2020-03-24 13:40:33 -04:00
|
|
|
|
|
|
|
|
# Body
|
2020-03-24 13:43:27 -04:00
|
|
|
$RestMessage -match '(\{(.*\n)*}|\{.*\})' | Out-Null;
|
2020-03-24 13:40:33 -04:00
|
|
|
$Request.Add('Body', $Matches[1]);
|
|
|
|
|
|
|
|
|
|
return $Request;
|
|
|
|
|
}
|