EldoS | Feel safer!

Software components for data protection, secure storage and transfer

TelHttpsClient Post Content Length -0-

Also by EldoS: MsgConnect
Cross-platform protocol-independent communication framework for building peer-to-peer and client-server applications and middleware components.
#11988
Posted: 12/25/2009 10:33:09
by John Santmann (Standard support level)
Joined: 01/12/2009
Posts: 3

I have a WebSnap application that takes posted data and inserts it into a database. When I run an app that I created with Indy using the TIdHTTP & TIdSSLIOHandlerSocketOpenSSL1 components, the data gets posted to the server via my WebSnap app.

When I try to post data using the attached code with the TelHTTPSClient, the data doesn't post, and as you'll see at the end of the attached info, the content length in the request is -0-.

Can you see something wrong with my approach?

******************************************************

SBB Approach:

******************************************************


procedure TdmHTTPS.CreateHTTPS;
begin
FHttps := TelHTTPSClient.Create(Self);
with FHttps do
begin
CipherSuites[SB_SUITE_RSA_RC4_SHA] := False;
CipherSuites[SB_SUITE_DHE_RSA_AES256_SHA] := True;
HTTPProxyPort := 3128;
HTTPVersion := hvHTTP10;
IncomingSpeedLimit := 0;
LocalPort := 0;
OnCertificateValidate := httpsClientCertificateValidate;
OnData := httpsClientData;
OnDocumentBegin := httpsClientDocumentBegin;
OnDocumentEnd := httpsClientDocumentEnd;
OnError := httpsClientError;
OnKeyNeeded := httpsKeyNeeded;
OnPreparedHeaders := httpsClientPreparedHeaders;
OnProgress := httpsClientProgress;
OnReceivingHeaders := httpsClientReceivingHeaders;
OutgoingSpeedLimit := 0;
PreferKeepAlive := True;
RequestParameters.ContentRangeStart := -1;
RequestParameters.ContentRangeEnd := -1;
RequestParameters.UserAgent := 'SecureBlackbox';
SendBufferSize := 65535;
SocksAuthentication := saNoAuthentication;
SocketBinding.Port := 0;
SocketBinding.PortRangeFrom := 0;
SocketBinding.PortRangeTo := 0;
SSLEnabled := False;
UseDigestAuth := False;
UseHTTPProxy := False;
UseNTLMAuth := False;
Versions := [sbSSL3];
WebTunnelPort := 3128;
end;
end;

function TdmHTTPS.EncodeAuthHeader: String;
begin
Result := 'Basic ' + GetBase64(FUserName + ':' + FPassword);
end;

function TdmHTTPS.SendHTTPSMessage(Msg: String; URL: String = ''): Integer;
var
tss: TStringStream;
begin
try
tss := TStringStream.Create(Msg);
tss.Position := 0;
FHTTPS.RequestHeaders.Add('Authorization: ' + EncodeAuthHeader);
Result := FHttps.Post(URL,tss);
finally
FHttps.Close(True);
FreeAndNil(tss);
end;
end;

procedure TdmHTTPS.httpsClientCertificateValidate(Sender: TObject;
X509Certificate: TElX509Certificate; var Validate: Boolean);
begin
Validate := True;
end;

procedure TdmHTTPS.httpsClientData(Sender: TObject; Buffer: Pointer; Size: Integer);
var
Bytes: ByteArray;
S: string;
begin
SetLength(Bytes, Size);
Move(Buffer^, Bytes[0], Size);
S := StringOfBytes(Bytes);
FLogger[FLogger.Messages].PostEntry(s,FHandles[FLogger.Messages]);
end;

procedure TdmHTTPS.httpsClientDocumentBegin(Sender: TObject);
begin
FLogger[FLogger.Response].PostEntry('Receiving Document',FHandles[FLogger.Response]);
end;

procedure TdmHTTPS.httpsClientDocumentEnd(Sender: TObject);
begin
FLogger[FLogger.Response].PostEntry('Finished Receiving Document',FHandles[FLogger.Response]);
end;

procedure TdmHTTPS.httpsClientError(Sender: TObject; ErrorCode: Integer; Fatal, Remote: Boolean);
begin
FLogger[FLogger.Messages].PostEntry(IntToStr(ErrorCode),FHandles[FLogger.Messages]);
end;

procedure TdmHTTPS.httpsClientProgress(Sender: TObject; Total, Current: Int64; var Cancel: Boolean);
begin
FLogger[FLogger.Messages].PostEntry('Current: ' + IntToStr(Current) + ' - Total: ' + IntToStr(Total),FHandles[FLogger.Messages]);
end;

procedure TdmHTTPS.httpsClientPreparedHeaders(Sender: TObject; Headers: TStringList);
begin
FLogger[FLogger.Request].PostEntry(Headers.Text,FHandles[FLogger.Request]);
FLogger[FLogger.Request].PostEntry(FHttps.RequestParameters.ToString,FHandles[FLogger.Request]);
end;

procedure TdmHTTPS.httpsClientReceivingHeaders(Sender: TObject; Headers: TStringList);
begin
FLogger[FLogger.Response].PostEntry(Headers.Text,FHandles[FLogger.Response]);
end;

procedure TdmHTTPS.httpsKeyNeeded(Sender: TObject; const Hint: string; var Identity: string; var Key: ByteArray);
begin
FLogger[FLogger.Messages].PostEntry('Hint: ' + Hint + ' - Identity: ' + Identity,FHandles[FLogger.Messages]);
end;

Output:

MESSAGES:
ValiCert Class 2 Policy Validation Authority
[12/25/2009 11:17:18 AM]: Finished Receiving Document
Apache/2.2.14 (Win32) mod_ssl/2.2.14 OpenSSL/0.9.8k

REQUEST:
ValiCert Class 2 Policy Validation Authority
HTTP/1.1 200 OK
Date: Fri, 25 Dec 2009 16:17:18 GMT
Server: Apache/2.2.14 (Win32) mod_ssl/2.2.14 OpenSSL/0.9.8k
Content-Length: 0
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: TEXT/HTML

RESPONSE:
[12/25/2009 11:17:18 AM]: Finished Receiving Document
ValiCert Class 2 Policy Validation Authority
[12/25/2009 11:17:18 AM]: HTTP/1.1 200 OK
Date: Fri, 25 Dec 2009 16:17:18 GMT
Server: Apache/2.2.14 (Win32) mod_ssl/2.2.14 OpenSSL/0.9.8k
Content-Length: 0
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: TEXT/HTML


******************************************************

Indy approach:

******************************************************

procedure TForm1.CreateIndies(var Http: TMyHTTP; var SSL: TIdSSLIOHandlerSocketOpenSSL);
begin
http := TMyHTTP.Create(Nil);
SSL := TIdSSLIOHandlerSocketOpenSSL.Create(Nil);
with SSL do
begin
MaxLineAction := maException;
Port := 0;
DefaultPort := 0;
SSLOptions.Mode := sslmUnassigned;
SSLOptions.Method := sslvSSLv3;
SSLOptions.VerifyMode := [];
SSLOptions.VerifyDepth := 0;
end;
with http do
begin
OnStatus := http1Status;
IOHandler := SSL;
OnDisconnected := http1Disconnected;
OnWork := http1Work;
OnWorkBegin := http1WorkBegin;
OnWorkEnd := http1WorkEnd;
OnConnected := http1Connected;
AllowCookies := True;
ProxyParams.BasicAuthentication := False;
ProxyParams.ProxyPort := 0;
Request.ContentLength := -1;
Request.ContentType := 'text/xml';
Request.Accept := 'text/html, */*,text/xml';
Request.BasicAuthentication := False;
Request.UserAgent := 'Mozilla/3.0 (compatible; Indy Library)';
HTTPOptions := [hoForceEncodeParams];
end;
end;

TIdHTTP setup:


var
http1: TIdHTTP;

http1 := TIdHTTP.Create(Self);
with http1 do
begin
Name := 'http1';
OnStatus := http1Status;
IOHandler := IdSSLIOHandlerSocketOpenSSL1;
OnDisconnected := http1Disconnected;
OnWork := http1Work;
OnWorkBegin := http1WorkBegin;
OnWorkEnd := http1WorkEnd;
OnConnected := http1Connected;
AllowCookies := True;
HTTPOptions := [hoForceEncodeParams];
end;

TIdSSLIOHandlerSocketOpenSSL setup:
var
IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;

IdSSLIOHandlerSocketOpenSSL1 := TIdSSLIOHandlerSocketOpenSSL.Create(Self);
with IdSSLIOHandlerSocketOpenSSL1 do
begin
Name := 'IdSSLIOHandlerSocketOpenSSL1';
MaxLineAction := maException;
Port := 0;
DefaultPort := 0;
end;

procedure TForm1.PostContent;
var
tsl: TStringList;
tss: TStringStream;
http: TMyHttp;
ssl: TIdSSLIOHandlerSocketOpenSSL;
begin
CreateIndies(http,ssl);
tsl := TStringList.Create;
tsl.LoadFromFile(<XML File>);
tss := TStringStream.Create(tsl.Text);
tss.Position := 0;
try
try
PostMessage(Handle,1029,Integer(PChar(http.Post('https://<URL>',tss))),0);
except
on E:Exception do
begin
PostMessage(Handle,1029,Integer(PChar(E.Message)),0);
FreeAndNil(tsl);
FreeAndNil(tss);
FreeAndNil(http);
FreeAndNil(SSL);
end;
end;
finally
FreeAndNil(tsl);
FreeAndNil(tss);
FreeAndNil(http);
FreeAndNil(SSL);
end;
end;

Thanks for any assistance.
#11991
Posted: 12/25/2009 12:02:41
by Eugene Mayevski (EldoS Corp.)

You don't need to set your own custom header - SecureBlackbox does this for you.

Also, please specify what the request is (you've copied response data to both request and response in your post).


Sincerely yours
Eugene Mayevski
#11992
Posted: 12/25/2009 14:49:49
by John Santmann (Standard support level)
Joined: 01/12/2009
Posts: 3

The Request is an XML Packet. I found another post that demonstrates the use of the OnPreparedHeaders event to modify the header text, that works fine.

Is there a way to view/log the content of the request prior to sending, to make sure that the content looks right?

Thanks.
#11993
Posted: 12/25/2009 15:02:57
by Ken Ivanov (EldoS Corp.)

Yes. Please use the OnSendData event.
#11994
Posted: 12/28/2009 10:48:02
by John Santmann (Standard support level)
Joined: 01/12/2009
Posts: 3

When I try to use the OnSendData event with the TelHTTPSClient, it is not recognized.
#11995
Posted: 12/28/2009 10:53:45
by Ken Ivanov (EldoS Corp.)

This means that you are using an older version of the library. Please upgrade to the latest SBB build (7.2.168), this will give you access to the event.
Also by EldoS: Callback File System
Create virtual file systems and disks, expose and manage remote data as if they were files on the local disk.

Reply

Statistics

Topic viewed 3186 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!