From 199368360239f6b667c4176790533ed4d39e97f3 Mon Sep 17 00:00:00 2001 From: Christian Stein Date: Fri, 27 Mar 2020 16:42:46 +0100 Subject: [PATCH] Fixes REST message reader for reading new msg properly and body read fixing --- lib/web/Read-IcingaRESTMessage.psm1 | 35 ++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/lib/web/Read-IcingaRESTMessage.psm1 b/lib/web/Read-IcingaRESTMessage.psm1 index 3a0f0c6..9ab382b 100644 --- a/lib/web/Read-IcingaRESTMessage.psm1 +++ b/lib/web/Read-IcingaRESTMessage.psm1 @@ -1,20 +1,34 @@ function Read-IcingaRESTMessage() { param( - [string]$RestMessage = $null + [string]$RestMessage = $null, + [hashtable]$Connection = $null + ); + + # Just in case we didnt receive anything - no need to + # parse through everything + if ([string]::IsNullOrEmpty($RestMessage)) { + return $null; + } + + Write-IcingaDebugMessage ( + [string]::Format( + 'Receiving client message{0}{0}{1}', + (New-IcingaNewline), + $RestMessage + ) ); [hashtable]$Request = @{}; - $RestMessage -match '(\d+) (.+) (.+) (.+)' | Out-Null; + $RestMessage -match '(.+) (.+) (.+)' | Out-Null; - $Request.Add('MessageLength', $Matches[1]); - $Request.Add('Method', $Matches[2]); - $Request.Add('FullRequest', $Matches[3]); + $Request.Add('Method', $Matches[1]); + $Request.Add('FullRequest', $Matches[2]); $Request.Add('RequestPath', @{}); $Request.Add('RequestArguments', @{}); #Path - $PathMatch = $Matches[3]; + $PathMatch = $Matches[2]; $PathMatch -match '((\/[^\/\?]+)*)\??([^\/]*)' | Out-Null; $Arguments = $Matches[3]; $Request.RequestPath.Add('FullPath', $Matches[1]); @@ -53,5 +67,14 @@ function Read-IcingaRESTMessage() $RestMessage -match '(\{(.*\n)*}|\{.*\})' | Out-Null; $Request.Add('Body', $Matches[1]); + # We received a content length, but couldnt load the body. Some clients will send the body as separate message + # Lets try to read the body content + if ($null -ne $Connection) { + if ($Request.ContainsKey('ContentLength') -And $Request.ContentLength -gt 0 -And ($Request.ContainsKey('Body') -eq $FALSE -Or [string]::IsNullOrEmpty($Request.Body))) { + $Request.Body = Read-IcingaTCPStream -Client $Connection.Client -Stream $Connection.Stream -ReadLength $Request.ContentLength; + Write-IcingaDebugMessage -Message 'Body Content' -Objects $Request; + } + } + return $Request; }