THRIFT-5123 add possibility to query HTTP status code with WinHTTP
Client: delphi
Patch: Jens Geyer
diff --git a/lib/delphi/src/Thrift.WinHTTP.pas b/lib/delphi/src/Thrift.WinHTTP.pas
index 5c07193..abbb004 100644
--- a/lib/delphi/src/Thrift.WinHTTP.pas
+++ b/lib/delphi/src/Thrift.WinHTTP.pas
@@ -576,7 +576,7 @@
   IWinHTTPConnection = interface;
 
   IWinHTTPRequest = interface
-    ['{7AE6F63F-49B6-436B-8AAB-159E58EB900A}']
+    ['{FADA4B45-D447-4F07-8361-1AD6656E5E8C}']
     function  Handle : HINTERNET;
     function  Connection : IWinHTTPConnection;
     function  AddRequestHeader( const aHeader : string; const addflag : DWORD = WINHTTP_ADDREQ_FLAG_ADD) : Boolean;
@@ -590,6 +590,7 @@
     function  ReadData( const pBuf : Pointer; const dwRead : DWORD) : DWORD;  overload;
     function  QueryDataAvailable : DWORD;
     function  QueryTotalResponseSize( out dwSize : DWORD) : Boolean;
+    function  QueryHttpStatus( out dwStatusCode : DWORD; out sStatusText : string) : Boolean;
   end;
 
   IWinHTTPConnection = interface
@@ -710,6 +711,7 @@
     function  ReadData( const pBuf : Pointer; const dwRead : DWORD) : DWORD;  overload;
     function  QueryDataAvailable : DWORD;
     function  QueryTotalResponseSize( out dwSize : DWORD) : Boolean;
+    function  QueryHttpStatus( out dwStatusCode : DWORD; out sStatusText : string) : Boolean;
 
   public
     constructor Create( const aConnection : IWinHTTPConnection;
@@ -1230,6 +1232,53 @@
 end;
 
 
+function TWinHTTPRequestImpl.QueryHttpStatus( out dwStatusCode : DWORD; out sStatusText : string) : Boolean;
+var dwBytes, dwError, dwIndex : DWORD;
+begin
+  // HTTP status code
+  dwIndex := DWORD( WINHTTP_NO_HEADER_INDEX);
+  dwBytes := SizeOf(dwStatusCode);
+  result  := WinHttpQueryHeaders( FHandle,
+                                  WINHTTP_QUERY_STATUS_CODE or WINHTTP_QUERY_FLAG_NUMBER,
+                                  WINHTTP_HEADER_NAME_BY_INDEX,
+                                  @dwStatusCode, dwBytes,
+                                  dwIndex);
+  if not result then begin
+    dwStatusCode := 0;  // no data
+    dwError      := GetLastError;
+    if dwError <> ERROR_WINHTTP_HEADER_NOT_FOUND then ASSERT(FALSE);  // anything else would be an real error
+  end;
+
+  // HTTP status text
+  dwIndex := DWORD( WINHTTP_NO_HEADER_INDEX);
+  dwBytes := 0;
+  result  := WinHttpQueryHeaders( FHandle,
+                                  WINHTTP_QUERY_STATUS_TEXT,
+                                  WINHTTP_HEADER_NAME_BY_INDEX,
+                                  WINHTTP_NO_OUTPUT_BUFFER,  // we need to determine the size first
+                                  dwBytes,                   // will receive the required buffer size
+                                  dwIndex);
+  if dwBytes > 0 then begin  // allocate buffer and call again to get the value
+    SetLength( sStatusText, dwBytes div SizeOf(Char));
+    dwBytes := Length(sStatusText) * SizeOf(Char);
+    result  := WinHttpQueryHeaders( FHandle,
+                                    WINHTTP_QUERY_STATUS_TEXT,
+                                    WINHTTP_HEADER_NAME_BY_INDEX,
+                                    @sStatusText[1], dwBytes,
+                                    dwIndex);
+  end;
+  if result
+  then SetLength( sStatusText, StrLen(PChar(sStatusText)))  // get rid of any terminating #0 chars
+  else begin
+    sStatusText := '';  // no data
+    dwError     := GetLastError;
+    if dwError <> ERROR_WINHTTP_HEADER_NOT_FOUND then ASSERT(FALSE);  // anything else would be an real error
+  end;
+
+  // do we have at least something?
+  result := (dwStatusCode <> 0) or (sStatusText <> '');
+end;
+
 
 { TWinHTTPUrlImpl }
 
@@ -1385,9 +1434,6 @@
 end;
 
 
-initialization
-  OutputDebugString( PChar( SysErrorMessage( 12002)));
-
 end.