This is an automated email from the ASF dual-hosted git repository.

jensg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/thrift.git

commit 589ee5b160f808fdee18ae9657d7c9e2f70014f0
Author: Jens Geyer <[email protected]>
AuthorDate: Mon Mar 29 21:40:55 2021 +0200

    THRIFT-5384 Improved error message for HTTP transports
    Client: Delphi
    Patch: Jens Geyer
---
 lib/delphi/src/Thrift.Protocol.pas  | 17 ++++++++++++-----
 lib/delphi/src/Thrift.Transport.pas |  4 +++-
 lib/delphi/src/Thrift.Utils.pas     | 26 ++++++++++++++++++++++++++
 3 files changed, 41 insertions(+), 6 deletions(-)

diff --git a/lib/delphi/src/Thrift.Protocol.pas 
b/lib/delphi/src/Thrift.Protocol.pas
index d5a7587..aa12ad3 100644
--- a/lib/delphi/src/Thrift.Protocol.pas
+++ b/lib/delphi/src/Thrift.Protocol.pas
@@ -833,6 +833,7 @@ var
 begin
   Reset;
   Init( result);
+
   size := ReadI32;
   if (size < 0) then begin
     version := size and Integer( VERSION_MASK);
@@ -842,14 +843,20 @@ begin
     result.Type_ := TMessageType( size and $000000ff);
     result.Name := ReadString;
     result.SeqID := ReadI32;
-  end
-  else begin
-    if FStrictRead then begin
-      raise TProtocolExceptionBadVersion.Create('Missing version in 
readMessageBegin, old client?' );
-    end;
+    Exit;
+  end;
+
+  try
+    if FStrictRead
+    then raise TProtocolExceptionBadVersion.Create('Missing version in 
readMessageBegin, old client?' );
+
     result.Name := ReadStringBody( size );
     result.Type_ := TMessageType( ReadByte );
     result.SeqID := ReadI32;
+  except
+    if CharUtils.IsHtmlDoctype(size)
+    then raise TProtocolExceptionInvalidData.Create('Remote end sends HTML 
instead of data')
+    else raise; // something else
   end;
 end;
 
diff --git a/lib/delphi/src/Thrift.Transport.pas 
b/lib/delphi/src/Thrift.Transport.pas
index 6a69d93..79fc977 100644
--- a/lib/delphi/src/Thrift.Transport.pas
+++ b/lib/delphi/src/Thrift.Transport.pas
@@ -1437,7 +1437,9 @@ begin
 
   if Int64(size) > Int64(Configuration.MaxFrameSize) then begin
     Close();
-    raise TTransportExceptionCorruptedData.Create('Frame size 
('+IntToStr(size)+') larger than allowed maximum 
('+IntToStr(Configuration.MaxFrameSize)+')');
+    if CharUtils.IsHtmlDoctype(size)
+    then raise TTransportExceptionCorruptedData.Create('Remote end sends HTML 
instead of data')
+    else raise TTransportExceptionCorruptedData.Create('Frame size 
('+IntToStr(size)+') larger than allowed maximum 
('+IntToStr(Configuration.MaxFrameSize)+')');
   end;
 
   UpdateKnownMessageSize(size + SizeOf(size));
diff --git a/lib/delphi/src/Thrift.Utils.pas b/lib/delphi/src/Thrift.Utils.pas
index bfd020e..4a75af8 100644
--- a/lib/delphi/src/Thrift.Utils.pas
+++ b/lib/delphi/src/Thrift.Utils.pas
@@ -80,6 +80,8 @@ type
   public
     class function IsHighSurrogate( const c : Char) : Boolean; static; inline;
     class function IsLowSurrogate( const c : Char) : Boolean; static; inline;
+
+    class function IsHtmlDoctype( const fourBytes : Integer) : Boolean; static;
   end;
 
   EnumUtils<T> = class sealed
@@ -259,6 +261,30 @@ begin
 end;
 
 
+class function CharUtils.IsHtmlDoctype( const fourBytes : Integer) : Boolean;
+var pc : PAnsiChar;
+const HTML_BEGIN : PAnsiChar = 'OD!<';  // first 4 bytes of '<!DOCTYPE ' in LE 
byte order
+begin
+  pc := @fourBytes;
+
+  if UpCase(pc^) <> HTML_BEGIN[0]
+  then Exit(FALSE);
+
+  Inc( pc);
+  if UpCase(pc^) <> HTML_BEGIN[1]
+  then Exit(FALSE);
+
+
+  Inc( pc);
+  if UpCase(pc^) <> HTML_BEGIN[2]
+  then Exit(FALSE);
+
+  Inc( pc);
+  result := (UpCase(pc^) = HTML_BEGIN[3]);
+end;
+
+
+
 {$IFDEF Win64}
 
 function InterlockedCompareExchange64( var Target : Int64; Exchange, Comparand 
: Int64) : Int64;  inline;

Reply via email to