Re: [twsocket] TSslHttpCli and multi Thread application

2015-09-24 Thread Boris JURČAK , Kostra d . o . o .

Thanks Agnus for advices.
I solved the problem, the error was in initialization of SslContex.

Regards
Boris

Signature


Angus Robertson - Magenta Systems Ltd je 9/24/2015 ob 11:01 AM napisal:

I done what you say, but still got exception ...

I would clean up the SSL context stuff, you have blindly copied a lot of stuff 
from
an ICS sample that is designed to test different SSL features, but which are 
totally
unnecessary for most applications.

You have certificate file names that are probably unused and unnecessary and 
which
may raise errors.

You ResetSSL and DeInitContext before you've done anything.

You also have very little error handling,  If PrepareCliTelekom fails you don't 
exit.
You ignore the response from the sync Post request, the IcsLogger is not a
replacement for checking whether things work.

It's also uncertain why you need a thread atall, usually only if you need to 
post
hundreds of requests in parallel, undertake lengthy processing or keep 
repeating the
request until it works, as in the Magenta Mail Queue component I suggested you
study.

Angus





--
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be


Re: [twsocket] TSslHttpCli and multi Thread application

2015-09-23 Thread Boris JURČAK , Kostra d . o . o .

Hi

Thanks for your response. I spend a few days to try to solve this big 
problem for me ...

I done what you say, but still got exception ...

TSMSMobile is record ..

type THttpsThread_Telekom = class(TThread)
  private
Cli: TSslHttpCli;
SslContextCli: TSslContext;
IcsLogger: TIcsLogger;
SslAvlSessionCache: TSslAvlSessionCache;
DB: TFDConnection;
tranDB: TFDTransaction;
...

procedure Execute; override;
procedure HttpsClientCreate;
procedure IcsLoggerIcsLogEvent(Sender: TObject; LogOption: 
TLogOption;  const Msg: string);

procedure HttpsClientDestroy;
procedure DoTelekomPostar(val: TSMSMobile);
function ParseSMSXmlBillingRequest(var val: TSMSMobile): boolean;
procedure DBCreate(dbPath:string; dbServer:string);
procedure DBDestroy;
  public
constructor Create(val:TSMSMobile; dbPath:string; dbServer:string);
destructor Destroy; override;
end;


constructor THttpsThread_Telekom.Create(val: TSMSMobile; dbPath: string; 
dbServer:string);

begin
  inherited Create(True);
  CopyTSMSMobile(FVal,Val);
  FdbPath:=dbPath;
  FdbServer:=dbServer;
  FreeOnTerminate:=True;
end;

destructor THttpsThread_Telekom.Destroy;
begin
  HttpsClientDestroy;
  DBDestroy;
  inherited;
end;

procedure THttpsThread_Telekom.Execute;
var
  bEnd:boolean;
begin
  inherited;
  providerID:=FVal.id_provider;
  DBCreate(FdbPath, FdbServer);
  HttpsClientCreate;

  PostData;

  HttpsClientDestroy;
  DBDestroy;
  Terminate;
end;

procedure THttpsThread_Telekom.HttpsClientDestroy;
begin
  FreeAndNil(Cli);
  FreeAndNil(SslContextCli);
  FreeAndNil(SslAvlSessionCache);
  FreeAndNil(IcsLogger);
end;

procedure THttpsThread_Telekom.DBDestroy;
begin
  FreeAndNil(DB);
  FreeAndNil(tranDB);
end;

procedure THttpsThread_Telekom.DBCreate(dbPath:string; dbServer:string);
begin
  DB := TFDConnection.Create(nil);
  with DB do
  begin
Name := 'DB';
Params.Clear;
Params.Add('Database='+dbPath);
Params.Add('Protocol=TCPIP');
Params.Add('Server='+dbServer);
Params.Add('Port=3050');
Params.Add('CharacterSet=WIN1250');
Params.Add('DriverID=FB');
LoginPrompt := False;
  end;
  tranDB := TFDTransaction.Create(nil);
  with tranDB do
  begin
Name := 'tranDB';
Connection := DB;
Options.Params.Add('read_committed');
Options.Params.Add('rec_version');
Options.Params.Add('nowait');
  end;
  DB.Transaction := tranDB;
end;

procedure THttpsThread_Telekom.HttpsClientCreate;
begin
  IcsLogger := TIcsLogger.Create(nil);
  with IcsLogger do
  begin
LogFileName := 'Debug_Out_HttpsTst.txt';
LogOptions := [loDestEvent, loDestFile, loDestOutDebug, loAddStamp, 
loWsockErr,
   loWsockInfo, loWsockDump, loSslErr, loSslInfo, 
loSslDump, loProtSpecErr,

   loProtSpecInfo, loProtSpecDump];
OnIcsLogEvent := IcsLoggerIcsLogEvent;
  end;

  SslContextCli := TSslContext.Create(nil);
  with SslContextCli do
  begin
IcsLogger := IcsLogger;
SslVersionMethod := sslBestVer_CLIENT;
SslECDHMethod := sslECDH_P256;
  end;

  SslAvlSessionCache := TSslAvlSessionCache.Create(nil);
  with SslAvlSessionCache do
  begin
IcsLogger := IcsLogger;
  end;

  Cli := TSslHttpCli.Create(nil);
  with Cli do
  begin
MultiThreaded := true;
IcsLogger := IcsLogger;
Timeout := 10;
SslContext := SslContextCli;
OnBgException := SslHttpCliBgException;
OnRequestDone := SslHttpCliRequestDone;
CtrlSocket.OnBgException := BackgroundException;
  end;
end;

function THttpsThread_Telekom.PostData:boolean;
var
  s:string;
  i:integer;
begin
  try
try
  s:=  XML string ... ;
  Cli.CtrlSocket.ResetSSL;
  Cli.SslContext.DeInitContext;
  Cli.SendStream:= TStringStream.Create(s);//Prepare send 
stream

  sRecData:=StrAlloc(MAX_LEN_XML);
  Cli.RcvdStream := TStringStream.Create(sRecData);//Prepare 
receive stream

  PrepareCliTelekom(provider, val);

  if not Cli.SslContext.IsCtxInitialized then Exit;  { V8.01 }

  Cli.Post;

except
  on E:Exception do begin
...
Exit;
  end;
end;
  finally
if Assigned(Cli.SendStream) then begin
  Cli.SendStream.Free;
  Cli.SendStream := nil;
end;
if Assigned(Cli.RcvdStream) then begin
  Cli.RcvdStream.Free;
  Cli.RcvdStream := nil;
end;
  end;
end;

procedure THttpsThread_Telekom.PrepareCliTelekom(provider:TProvider; var 
val:TSMSMobile);

const
  SocksLevelValues : array [0..2] of String = ('5', '4A', '4');
  SslVersions : array [0..5] of TSslVersionMethod =
(sslBestVer_CLIENT,sslV2_CLIENT,sslV3_CLIENT,sslTLS_V1_CLIENT,sslTLS_V1_1_CLIENT,sslTLS_V1_2_CLIENT); 
{ V8.01 }

begin
  cli.ContentTypePost:= 'text/xml';
  cli.RequestVer  := '1.0';
  cli.URL:= 'https://';
  cli.SslContext.SslCertFile:= 'Path to Cert.pem';
  cli.SslContext.SslPrivKeyFile:= 'Path to Cert.pem';
  cli.SslContext.SslPassPhrase:= 'PASSWORD';

  cli.SslContext.SslCAFile   := '';