THRIFT-5856 Client should validate HTTP status
Client: Delphi
Patch: Jens Geyer
diff --git a/lib/delphi/src/Thrift.Transport.MsxmlHTTP.pas b/lib/delphi/src/Thrift.Transport.MsxmlHTTP.pas
index 2cc3bfe..e3886a5 100644
--- a/lib/delphi/src/Thrift.Transport.MsxmlHTTP.pas
+++ b/lib/delphi/src/Thrift.Transport.MsxmlHTTP.pas
@@ -53,6 +53,8 @@
     FCustomHeaders : IThriftDictionary<string,string>;
 
     function CreateRequest: IXMLHTTPRequest;
+    class procedure EnsureSuccessHttpStatus( const aRequest : IXMLHTTPRequest);
+
   strict protected
     function GetIsOpen: Boolean; override;
     procedure Open(); override;
@@ -255,6 +257,7 @@
     ms.Position := 0;
     xmlhttp.send( IUnknown( TStreamAdapter.Create( ms, soReference )));
     FInputStream := nil;
+    EnsureSuccessHttpStatus(xmlhttp);  // throws if not
     FInputStream := TThriftStreamAdapterCOM.Create( IUnknown( xmlhttp.responseStream) as IStream);
     ResetMessageSizeAndConsumedBytes;
     UpdateKnownMessageSize( FInputStream.Size);
@@ -271,6 +274,20 @@
 end;
 
 
+class procedure TMsxmlHTTPClientImpl.EnsureSuccessHttpStatus( const aRequest : IXMLHTTPRequest);
+var iStatus : Integer;
+    sText : string;
+begin
+  if aRequest = nil
+  then raise TTransportExceptionNotOpen.Create('Invalid HTTP request data');
+
+  iStatus := aRequest.status;
+  sText   := aRequest.statusText;
+
+  if (200 > iStatus) or (iStatus > 299)
+  then raise TTransportExceptionEndOfFile.Create('HTTP '+IntToStr(iStatus)+' '+sText);
+end;
+
 
 end.
 
diff --git a/lib/delphi/src/Thrift.Transport.WinHTTP.pas b/lib/delphi/src/Thrift.Transport.WinHTTP.pas
index 9deda4a..2d18ca1 100644
--- a/lib/delphi/src/Thrift.Transport.WinHTTP.pas
+++ b/lib/delphi/src/Thrift.Transport.WinHTTP.pas
@@ -51,6 +51,7 @@
 
     function CreateRequest: IWinHTTPRequest;
     function SecureProtocolsAsWinHTTPFlags : Cardinal;
+    class procedure EnsureSuccessHttpStatus( const aRequest : IWinHTTPRequest);
 
   strict private
     type
@@ -335,6 +336,7 @@
 
   // we're about to receive a new message, so reset everyting
   ResetMessageSizeAndConsumedBytes(-1);
+  EnsureSuccessHttpStatus(http);  // throws if not
   FInputStream := THTTPResponseStream.Create( http);
   if http.QueryTotalResponseSize( dwSize)  // FALSE indicates "no info available"
   then UpdateKnownMessageSize( dwSize);
@@ -349,6 +351,18 @@
 end;
 
 
+class procedure TWinHTTPClientImpl.EnsureSuccessHttpStatus( const aRequest : IWinHTTPRequest);
+var dwStatus : Cardinal;
+    sText : string;
+begin
+  if (aRequest <> nil)
+  then aRequest.QueryHttpStatus( dwStatus, sText)
+  else raise TTransportExceptionNotOpen.Create('Invalid HTTP request data');
+
+  if (200 > dwStatus) or (dwStatus > 299)
+  then raise TTransportExceptionEndOfFile.Create('HTTP '+UIntToStr(dwStatus)+' '+sText);
+end;
+
 { TWinHTTPClientImpl.THTTPResponseStream }
 
 constructor TWinHTTPClientImpl.THTTPResponseStream.Create( const aRequest : IWinHTTPRequest);