From 50133d22f21726892ceb51b3af62945a197e3b38 Mon Sep 17 00:00:00 2001 From: Alexander Stoll <45196568+Crited@users.noreply.github.com> Date: Tue, 24 Mar 2020 18:40:33 +0100 Subject: [PATCH] Create Read-IcingaRESTMessage.psm1 First draft of Read-IcingaRESTMessage.psm1 #57 Regarding parsing --- lib/core/tools/Read-IcingaRESTMessage.psm1 | 58 ++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 lib/core/tools/Read-IcingaRESTMessage.psm1 diff --git a/lib/core/tools/Read-IcingaRESTMessage.psm1 b/lib/core/tools/Read-IcingaRESTMessage.psm1 new file mode 100644 index 0000000..f2acd03 --- /dev/null +++ b/lib/core/tools/Read-IcingaRESTMessage.psm1 @@ -0,0 +1,58 @@ +function Read-IcingaRESTMessage() +{ + param( + [string]$RestMessage = $null + ); + + [hashtable]$Request = @{} + $RestMessage -match '(\d+) (.+) (.+) (.+)' | Out-Null + + $Request.Add('MessageLength', $Matches[1]); + $Request.Add('Method', $Matches[2]); + $Request.Add('FullRequest', $Matches[3]); + $Request.Add('RequestPath', @{}); + $Request.Add('RequestArguments', @{}); + + #Path + $PathMatch = $Matches[3] + $PathMatch -match '(.+)\?(.*)' | Out-Null + $Path = $Matches[1] + $Arguments = $Matches[2] + $Request.RequestPath.Add('FullPath', $Matches[1]); + $Request.RequestPath.Add('PathArray', $Matches[1].TrimStart('/').Split('/')); + + $Matches = $null + + # Arguments + $ArgumentsSplit = $Arguments.Split('&') + $ArgumentsSplit+='\\\\\\\\\\\\=FIN' + foreach ( $Argument in $ArgumentsSplit | sort -descending) { + $Argument -match '(.+)=(.+)' | Out-Null + If (($Matches[1] -ne $Current) -And ($NULL -ne $Current)) { + $Request.RequestArguments.Add( $Current, $ArgumentContent ); + [array]$ArgumentContent = $null + } + $Current = $Matches[1] + [array]$ArgumentContent+=($Matches[2]); + } + + # Header + $Request.Add( 'Header', @{} ); + $SplitString = $RestMessage.split("`r`n") + 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)); + + $Matches = $null + + # Body + $RestMessage -match '(\{(.*\n)*}|\{.*\})' | Out-Null + $Request.Add('Body', $Matches[1]); + + return $Request; +}