From 7cd0ea4ee25ae0301f235f00be23b676d853fef4 Mon Sep 17 00:00:00 2001 From: Peter Ross Date: Tue, 26 May 2015 11:23:19 +1000 Subject: [PATCH] Add content-length header to frames. If a frame contains a body, then add a content-length header. This allows the body to contain a NULL character and for it not to be interpreted as the end of the frame. --- StompClient.pas | 4 ++++ StompTypes.pas | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/StompClient.pas b/StompClient.pas index e1fafcac..82b9d6bd 100644 --- a/StompClient.pas +++ b/StompClient.pas @@ -405,6 +405,10 @@ begin h := AHeaders.GetAt(i); AFrame.GetHeaders.Add(h.Key, h.Value); end; + + // If the frame has some content, then set the length of that content. + if (AFrame.ContentLength > 0) then + AFrame.GetHeaders.Add('content-length', IntToStr(AFrame.ContentLength)); end; procedure TStompClient.Nack(const MessageID, TransactionIdentifier: string); diff --git a/StompTypes.pas b/StompTypes.pas index abf53f66..13600c73 100644 --- a/StompTypes.pas +++ b/StompTypes.pas @@ -63,6 +63,7 @@ type procedure SetBody(const Value: string); function GetHeaders: IStompHeaders; function MessageID: string; + function ContentLength: Integer; end; IStompClient = interface @@ -130,6 +131,7 @@ type private FCommand: string; FBody: string; + FContentLength: Integer; FHeaders: IStompHeaders; procedure SetHeaders(const Value: IStompHeaders); function GetCommand: string; @@ -147,6 +149,7 @@ type // otherwise, return Value; function Output: string; function MessageID: string; + function ContentLength: Integer; property Headers: IStompHeaders read GetHeaders write SetHeaders; end; @@ -273,6 +276,7 @@ begin FHeaders := TStompHeaders.Create; self.FCommand := ''; self.FBody := ''; + self.FContentLength := 0; end; destructor TStompFrame.Destroy; @@ -305,9 +309,15 @@ begin Result := FCommand + LINE_END + FHeaders.Output + LINE_END + FBody + COMMAND_END; end; +function TStompFrame.ContentLength: Integer; +begin + Result := FContentLength; +end; + procedure TStompFrame.SetBody(const Value: string); begin FBody := Value; + FContentLength := Length(TEncoding.UTF8.GetBytes(FBody)); end; procedure TStompFrame.SetCommand(const Value: string);