Skip to content

Commit

Permalink
Updating various DLL related functions to use THandle instead of HMOD…
Browse files Browse the repository at this point in the history
…ULE.

Updating TIdEMailAddressList.SetEMailAddresses() to use IdDisposeAndNil() instead of FreeAndNil() when removing parsed items that have blank text.

Adding TODO comments, and misc tweaks.
  • Loading branch information
RemyLebeau authored and RemyLebeau committed Nov 2, 2018
1 parent c00fe9b commit 787572b
Show file tree
Hide file tree
Showing 14 changed files with 156 additions and 88 deletions.
5 changes: 4 additions & 1 deletion Lib/Core/IdCmdTCPClient.pas
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ TIdCmdClientContext = class(TIdContext)
public
property Client: TIdCmdTCPClient read FClient;
end;

TIdCmdTCPClientListeningThread = class(TIdThread)
protected
FContext: TIdCmdClientContext;
Expand Down Expand Up @@ -189,6 +189,9 @@ TIdCmdClientContextAccess = class(TIdCmdClientContext)

constructor TIdCmdTCPClientListeningThread.Create(AClient: TIdCmdTCPClient);
begin
// TODO: move this into TIdCmdTCPClient itself so the Context is always
// available even if the thread is not running...
//
FClient := AClient;
FContext := TIdCmdClientContext.Create(AClient, nil, nil);
FContext.FClient := AClient;
Expand Down
2 changes: 1 addition & 1 deletion Lib/Core/IdCompilerDefines.inc
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@
{$ENDIF}
{$ENDIF}

//Delphi & CBuilder 10.3 "Name TBD" (Carnival)
//Delphi & CBuilder 10.3 Rio (Carnival)
{$IFDEF VER330}
{$DEFINE VCL_10_3}
{$IFDEF CBUILDER}
Expand Down
2 changes: 1 addition & 1 deletion Lib/FCL/IdCompilerDefines.inc
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@
{$ENDIF}
{$ENDIF}

//Delphi & CBuilder 10.3 "Name TBD" (Carnival)
//Delphi & CBuilder 10.3 Rio (Carnival)
{$IFDEF VER330}
{$DEFINE VCL_10_3}
{$IFDEF CBUILDER}
Expand Down
2 changes: 1 addition & 1 deletion Lib/Protocols/IdCompilerDefines.inc
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@
{$ENDIF}
{$ENDIF}

//Delphi & CBuilder 10.3 "Name TBD" (Carnival)
//Delphi & CBuilder 10.3 Rio (Carnival)
{$IFDEF VER330}
{$DEFINE VCL_10_3}
{$IFDEF CBUILDER}
Expand Down
22 changes: 17 additions & 5 deletions Lib/Protocols/IdCustomHTTPServer.pas
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,8 @@ function TIdCustomHTTPServer.CreateSession(AContext: TIdContext; HTTPResponse: T
if SessionState then begin
LSessionList := FSessionList;
if Assigned(LSessionList) then begin
// TODO: pass the RemoteIP to the OnCreateSession event handler, or even
// better the entire HTTPRequest object...
DoOnCreateSession(AContext, Result);
if not Assigned(Result) then begin
Result := LSessionList.CreateUniqueSession(HTTPRequest.RemoteIP);
Expand Down Expand Up @@ -1284,6 +1286,9 @@ function TIdCustomHTTPServer.DoExecute(AContext:TIdContext): boolean;
if i = 0 then begin
raise EIdHTTPErrorParsingCommand.Create(RSHTTPErrorParsingCommand);
end;
// TODO: don't recreate the Request and Response objects on each loop
// iteration. Just create them once before entering the loop, and then
// reset them as needed on each iteration...
LRequestInfo := TIdHTTPRequestInfo.Create(Self);
try
LResponseInfo := TIdHTTPResponseInfo.Create(Self, LRequestInfo, LConn);
Expand Down Expand Up @@ -2366,11 +2371,18 @@ function TIdHTTPDefaultSessionList.CreateUniqueSession(
SessionID: String;
begin
SessionID := GetRandomString(15);
while GetSession(SessionID, RemoteIP) <> nil do
begin
SessionID := GetRandomString(15);
end; // while
Result := CreateSession(RemoteIP, SessionID);
// TODO: shouldn't this lock the SessionList before entering the
// loop to prevent race conditions across multiple threads?
{SessionList.LockList;
try}
while GetSession(SessionID, RemoteIP) <> nil do
begin
SessionID := GetRandomString(15);
end; // while
Result := CreateSession(RemoteIP, SessionID);
{finally
SessionList.UnlockList;
end;}
end;

destructor TIdHTTPDefaultSessionList.Destroy;
Expand Down
5 changes: 3 additions & 2 deletions Lib/Protocols/IdEMailAddress.pas
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ function TIdEMailAddressItem.GetDomain: string;
i: Integer;
begin
Result := '';
// TODO: use RPos() or LastDelimiter() instead of a manual loop...
for i := Length(FAddress) downto 1 do
begin
if FAddress[i] = '@' then {do not localize}
Expand Down Expand Up @@ -779,7 +780,7 @@ procedure TIdEMailAddressList.SetEMailAddresses(AList: string);
sTemp := Trim(Email.Text);
if (sTemp = '') or (sTemp = '<>') then {do not localize}
begin
FreeAndNil(Email);
IdDisposeAndNil(Email);
end;
sTemp := '';
IdDelete(AList, 1, iStart);
Expand Down Expand Up @@ -811,7 +812,7 @@ procedure TIdEMailAddressList.SetEMailAddresses(AList: string);
sTemp := Trim(Email.Text);
if (sTemp = '') or (sTemp = '<>') then {do not localize}
begin
FreeAndNil(Email);
IdDisposeAndNil(Email);
end;
end;
end;
Expand Down
4 changes: 4 additions & 0 deletions Lib/Protocols/IdHTTPWebBrokerBridge.pas
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,11 @@ procedure TIdHTTPAppResponse.SetContent(const AValue: {$IFDEF WBB_ANSI}AnsiStrin
LValue := string(AValue);
end;
end;

FResponseInfo.ContentText := LValue;
// TODO: use Length(AValue) instead, as the ContentText *should* get re-encoded
// back to the same value as AValue when transmitted. Or, just set ContentLength
// to -1 and let Indy calculate it later...
FResponseInfo.ContentLength := Length(LValue);

{$ELSE}
Expand Down
1 change: 1 addition & 0 deletions Lib/Protocols/IdIRC.pas
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,7 @@ procedure TIdIRC.Raw(const ALine: String);
begin
if Connected then begin
if Assigned(FOnRaw) then begin
// TODO: use FListeningThread.FContext instead of nil...
FOnRaw(nil, False, ALine);
end;
IOHandler.WriteLn(IRCQuote(ALine));
Expand Down
4 changes: 3 additions & 1 deletion Lib/Protocols/IdSSLOpenSSL.pas
Original file line number Diff line number Diff line change
Expand Up @@ -3323,6 +3323,8 @@ procedure TIdSSLContext.InitContext(CtxMode: TIdSSLCtxMode);
if RootCertFile <> '' then begin {Do not Localize}
SSL_CTX_set_client_CA_list(fContext, IndySSL_load_client_CA_file(RootCertFile));
end

// TODO: provide an event so users can apply their own settings as needed...
end;

procedure TIdSSLContext.SetVerifyMode(Mode: TIdSSLVerifyModeSet; CheckRoutine: Boolean);
Expand Down Expand Up @@ -3775,7 +3777,7 @@ function TIdSSLSocket.Recv(var ABuffer: TIdBytes): Integer;
ret, err: Integer;
begin
repeat
ret := SSL_read(fSSL, @ABuffer[0], Length(ABuffer));
ret := SSL_read(fSSL, PByte(ABuffer), Length(ABuffer));
if ret > 0 then begin
Result := ret;
Exit;
Expand Down
54 changes: 30 additions & 24 deletions Lib/Protocols/IdSSLOpenSSLHeaders.pas
Original file line number Diff line number Diff line change
Expand Up @@ -18188,7 +18188,7 @@ function Load: Boolean;
procedure Unload;
{$IFNDEF STATICLOAD_OPENSSL}
function WhichFailedToLoad: String;
function GetCryptLibHandle : HMODULE;
function GetCryptLibHandle : THandle;
procedure IdOpenSSLSetLibPath(const APath: String);
{$IFDEF UNIX}
procedure IdOpenSSLSetLoadSymLinksFirst(ALoadFirst: Boolean);
Expand Down Expand Up @@ -19550,8 +19550,8 @@ class procedure EIdOpenSSLAPISSLError.RaiseExceptionCode(const AErrCode, ARetCod
{$IFDEF STATICLOAD_OPENSSL}
bIsLoaded : Boolean = False;
{$ELSE}
hIdSSL : HMODULE = 0;
hIdCrypto : HMODULE = 0;
hIdSSL : THandle = 0;
hIdCrypto : THandle = 0;
FFailedLoadList : TStringList;
{$ENDIF}

Expand All @@ -19568,7 +19568,7 @@ class procedure EIdOpenSSLAPISSLError.RaiseExceptionCode(const AErrCode, ARetCod
{$ENDIF}

{$IFNDEF STATICLOAD_OPENSSL}
function GetCryptLibHandle : HMODULE;
function GetCryptLibHandle : THandle;
begin
Result := hIdCrypto;
end;
Expand Down Expand Up @@ -22612,7 +22612,7 @@ procedure IdOpenSSLSetLoadSymLinksFirst(ALoadFirst: Boolean);
end;
{$ENDIF}

function LoadSSLCryptoLibrary: HMODULE;
function LoadSSLCryptoLibrary: THandle;
{$IFNDEF WINDOWS}
{$IFDEF USE_BASEUNIX_OR_VCL_POSIX_OR_KYLIXCOMPAT} // TODO: use {$IF DEFINED(UNIX)} instead?
var
Expand All @@ -22630,35 +22630,29 @@ function LoadSSLCryptoLibrary: HMODULE;
// Workaround that is required under Linux (changed RTLD_GLOBAL with RTLD_LAZY Note: also work with LoadLibrary())
Result := 0;
if GIdLoadSymLinksFirst then begin
Result := {$IFNDEF KYLIXCOMPAT}HMODULE({$ENDIF}
HackLoad(GIdOpenSSLPath + SSLCLIB_DLL_name, [])
{$IFNDEF KYLIXCOMPAT}){$ENDIF};
Result := HackLoad(GIdOpenSSLPath + SSLCLIB_DLL_name, []);
end;
if Result = 0 then begin
for i := Low(SSLDLLVers) to High(SSLDLLVers) do begin
for j := Low(SSLDLLVersChar) to High(SSLDLLVersChar) do begin
LLibVersions[j] := SSLDLLVers[i] + SSLDLLVersChar[j];
end;
Result := {$IFNDEF KYLIXCOMPAT}HMODULE({$ENDIF}
HackLoad(GIdOpenSSLPath + SSLCLIB_DLL_name, LLibVersions)
{$IFNDEF KYLIXCOMPAT}){$ENDIF};
Result := HackLoad(GIdOpenSSLPath + SSLCLIB_DLL_name, LLibVersions);
if Result <> 0 then begin
Break;
end;
end;
end;
if (Result = 0) and (not GIdLoadSymLinksFirst) then begin
Result := {$IFNDEF KYLIXCOMPAT}HMODULE({$ENDIF}
HackLoad(GIdOpenSSLPath + SSLCLIB_DLL_name, [])
{$IFNDEF KYLIXCOMPAT}){$ENDIF};
Result := HackLoad(GIdOpenSSLPath + SSLCLIB_DLL_name, []);
end;
{$ELSE}
Result := 0;
{$ENDIF}
{$ENDIF}
end;

function LoadSSLLibrary: HMODULE;
function LoadSSLLibrary: THandle;
{$IFNDEF WINDOWS}
{$IFDEF USE_BASEUNIX_OR_VCL_POSIX_OR_KYLIXCOMPAT} // TODO: use {$IF DEFINED(UNIX)} instead?
var
Expand All @@ -22681,27 +22675,21 @@ function LoadSSLLibrary: HMODULE;
// Workaround that is required under Linux (changed RTLD_GLOBAL with RTLD_LAZY Note: also work with LoadLibrary())
Result := 0;
if GIdLoadSymLinksFirst then begin
Result := {$IFNDEF KYLIXCOMPAT}HMODULE({$ENDIF}
HackLoad(GIdOpenSSLPath + SSL_DLL_name, [])
{$IFNDEF KYLIXCOMPAT}){$ENDIF};
Result := HackLoad(GIdOpenSSLPath + SSL_DLL_name, []);
end;
if Result = 0 then begin
for i := Low(SSLDLLVers) to High(SSLDLLVers) do begin
for j := Low(SSLDLLVersChar) to High(SSLDLLVersChar) do begin
LLibVersions[j] := SSLDLLVers[i] + SSLDLLVersChar[j];
end;
Result := {$IFNDEF KYLIXCOMPAT}HMODULE({$ENDIF}
HackLoad(GIdOpenSSLPath + SSL_DLL_name, LLibVersions)
{$IFNDEF KYLIXCOMPAT}){$ENDIF};
Result := HackLoad(GIdOpenSSLPath + SSL_DLL_name, LLibVersions);
if Result <> 0 then begin
Break;
end;
end;
end;
if (Result = 0) and (not GIdLoadSymLinksFirst) then begin
Result := {$IFNDEF KYLIXCOMPAT}HMODULE({$ENDIF}
HackLoad(GIdOpenSSLPath + SSL_DLL_name, [])
{$IFNDEF KYLIXCOMPAT}){$ENDIF};
Result := HackLoad(GIdOpenSSLPath + SSL_DLL_name, []);
end;
{$ELSE}
Result := 0;
Expand Down Expand Up @@ -23584,6 +23572,15 @@ function Load: Boolean;
@_FIPS_mode := LoadFunctionCLib(fn_FIPS_mode,False);
{$ENDIF}

// TODO: expose a global callback function pointer, or an optional input
// parameter to Load(), so users can choose to load additional OpenSSL
// functions as desired using the DLL handles that we've already loaded...
{
if Assigned(LoadSSLFuncsCallback) then begin
LoadSSLFuncsCallback(hIdSSL, hIdCrypto, FFailedLoadList);
end;
}

Result := (FFailedLoadList.Count = 0);

{$ENDIF}
Expand Down Expand Up @@ -24316,6 +24313,15 @@ procedure InitializeFuncPointers;
@_FIPS_mode_set := nil;
@_FIPS_mode := nil;
{$ENDIF}

// TODO: expose a global callback function pointer, or an optional input
// parameter to InitializeFuncPointers(), so users can reset any additional
// OpenSSL function pointers they loaded manually during Load()...
{
if Assigned(UnloadSSLFuncsCallback) then begin
UnloadSSLFuncsCallback();
end;
}
end;

procedure Unload;
Expand Down
5 changes: 1 addition & 4 deletions Lib/Protocols/IdZLibHeaders.pas
Original file line number Diff line number Diff line change
Expand Up @@ -877,18 +877,15 @@ function inflateMark; external;
function inflateGetHeader; external;
{$ELSE}
var
{$IFDEF UNIX}
hZlib: HModule = nilhandle;
{$ELSE}
hZLib: THandle = 0;
{$ENDIF}

{$IFDEF UNIX}
const
//The extensions will be resolved by IdGlobal.HackLoad
//This is a little messy because symbolic links to libraries may not always be the same
//in various Unix types. Even then, there could possibly be differences.
libzlib = 'libz';
// TODO: setup this array more like the SSLDLLVers arrays in the IdSSLOpenSSLHeaders unit...
libvers : array [0..3] of string = ('.1','','.3','.2');
{$ENDIF}
{$IFDEF NETWARE} {zlib.nlm comes with netware6}
Expand Down
2 changes: 1 addition & 1 deletion Lib/SuperCore/IdCompilerDefines.inc
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@
{$ENDIF}
{$ENDIF}

//Delphi & CBuilder 10.3 "Name TBD" (Carnival)
//Delphi & CBuilder 10.3 Rio (Carnival)
{$IFDEF VER330}
{$DEFINE VCL_10_3}
{$IFDEF CBUILDER}
Expand Down
2 changes: 1 addition & 1 deletion Lib/System/IdCompilerDefines.inc
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@
{$ENDIF}
{$ENDIF}

//Delphi & CBuilder 10.3 "Name TBD" (Carnival)
//Delphi & CBuilder 10.3 Rio (Carnival)
{$IFDEF VER330}
{$DEFINE VCL_10_3}
{$IFDEF CBUILDER}
Expand Down
Loading

0 comments on commit 787572b

Please sign in to comment.