added support for openssl - and therewith dependency to openssl units.

Disable ssl support (and additional dependencies) set the compiler swith 
WS_NO_SSL
This commit is contained in:
yvi71 2015-11-13 01:59:22 +01:00
parent 8e7f63aeb6
commit 61cefd77e0
5 changed files with 103 additions and 28 deletions

View file

@ -625,7 +625,12 @@ begin
//ws://host:port/<resourcename>
//about resourcename, see: http://dev.w3.org/html5/websockets/ "Parsing WebSocket URLs"
//sURL := Format('ws://%s:%d/%s', [Host, Port, WSResourceName]);
sURL := Format('https://%s:%d/%s', [Host, Port, WSResourceName]);
{$IFDEF WS_NO_SSL}
//TODO: depend protocol on usessl - param passing in here
sURL := Format('http://%s:%d/%s', [Host, Port, WSResourceName]);
{$ENDIF}
ReadTimeout := Max(5 * 1000, ReadTimeout);
{ voorbeeld:

View file

@ -11,6 +11,9 @@ uses
Classes, SysUtils,
IdIOHandlerStack, IdGlobal, IdException, IdBuffer,
SyncObjs,
{$IFNDEF WS_NO_SSL}
IdSSLOpenSSL,
{$ENDIF}
Generics.Collections;
type
@ -22,11 +25,14 @@ type
TIdIOHandlerWebsocket = class;
EIdWebSocketHandleError = class(EIdSocketHandleError);
{$if CompilerVersion >= 26} //XE5
TIdTextEncoding = IIdTextEncoding;
{$ifend}
{.$if CompilerVersion >= 26} //XE5
//TIdTextEncoding = IIdTextEncoding;
{.$ifend}
{$IFDEF WS_NO_SSL}
TIdIOHandlerWebsocket = class(TIdIOHandlerStack)
{ELSE}
TIdIOHandlerWebsocketSSL = class(TIdSSLIOHandlerSocketOpenSSL)
{$ENDIF}
private
FIsServerSide: Boolean;
FBusyUpgrading: Boolean;
@ -55,12 +61,15 @@ type
function ReadFrame(out aFIN, aRSV1, aRSV2, aRSV3: boolean; out aDataCode: TWSDataCode; out aData: TIdBytes): Integer;
function ReadMessage(var aBuffer: TIdBytes; out aDataCode: TWSDataCode): Integer;
{$if CompilerVersion >= 26} //XE5
function UTF8Encoding: IIdTextEncoding;
{$else}
{.$if CompilerVersion >= 26} //XE5
//function UTF8Encoding: IIdTextEncoding;
{.$else}
function UTF8Encoding: TEncoding;
{$ifend}
{.$ifend}
public
{$IFNDEF WS_NO_SSL}
procedure ClearSSLOptions;
{$ENDIF}
function WriteData(aData: TIdBytes; aType: TWSDataCode;
aFIN: boolean = true; aRSV1: boolean = false; aRSV2: boolean = false; aRSV3: boolean = false): integer;
property BusyUpgrading : Boolean read FBusyUpgrading write FBusyUpgrading;
@ -258,6 +267,14 @@ begin
FPendingWriteCount := 0;
end;
{$IFNDEF WS_NO_SSL}
procedure TIdIOHandlerWebsocketSSL.ClearSSLOptions;
begin
self.fxSSLOptions.Free;
self.fxSSLOptions := nil;
end;
{$ENDIF
procedure TIdIOHandlerWebsocket.Close;
var
iaWriteBuffer: TIdBytes;
@ -556,8 +573,6 @@ end;
function TIdIOHandlerWebsocket.WriteDataToTarget(const ABuffer: TIdBytes;
const AOffset, ALength: Integer): Integer;
var
data: TIdBytes;
begin
if UseSingleWriteThread and IsWebsocket and (GetCurrentThreadId <> TIdWebsocketWriteThread.Instance.ThreadID) then
Assert(False, 'Write done in different thread than TIdWebsocketWriteThread!');
@ -576,19 +591,17 @@ begin
end
else
begin
data := ToBytes(ABuffer, ALength, AOffset);
{$IFDEF DEBUG_WS}
if Debughook > 0 then
OutputDebugString(PChar(Format('Send (ws, TID:%d, P:%d): %s',
[getcurrentthreadid, Self.Binding.PeerPort, BytesToStringRaw(data)])));
[getcurrentthreadid, Self.Binding.PeerPort, BytesToStringRaw(ABuffer)])));
{$ENDIF}
try
if FWriteTextToTarget then
Result := WriteData(data, wdcText, True{send all at once},
Result := WriteData(ABuffer, wdcText, True{send all at once},
webBit1 in ClientExtensionBits, webBit2 in ClientExtensionBits, webBit3 in ClientExtensionBits)
else
Result := WriteData(data, wdcBinary, True{send all at once},
Result := WriteData(ABuffer, wdcBinary, True{send all at once},
webBit1 in ClientExtensionBits, webBit2 in ClientExtensionBits, webBit3 in ClientExtensionBits);
except
FClosedGracefully := True;
@ -831,17 +844,17 @@ begin
FLock.Leave;
end;
{$if CompilerVersion >= 26} //XE5
function TIdIOHandlerWebsocket.UTF8Encoding: IIdTextEncoding;
begin
Result := IndyTextEncoding_UTF8;
end;
{$else}
{.$if CompilerVersion >= 26} //XE5
//function TIdIOHandlerWebsocket.UTF8Encoding: IIdTextEncoding;
//begin
// Result := IndyTextEncoding_UTF8;
//end;
{.$else}
function TIdIOHandlerWebsocket.UTF8Encoding: TEncoding;
begin
Result := TIdTextEncoding.UTF8;
end;
{$ifend}
{.$ifend}
function TIdIOHandlerWebsocket.ReadFrame(out aFIN, aRSV1, aRSV2, aRSV3: boolean;
out aDataCode: TWSDataCode; out aData: TIdBytes): Integer;
@ -1135,11 +1148,11 @@ begin
AppendBytes(bData, aData); //important: send all at once!
ioffset := 0;
iDataLength := Length(bData);
repeat
//Result := Binding.Send(bData, ioffset);
Result := inherited WriteDataToTarget(bdata, iOffset, (Length(bData) - ioffset)); //ssl compatible?
result := inherited WriteDataToTarget(bdata,iOffset, (iDataLength-ioffset));
Inc(ioffset, Result);
until ioffset >= Length(bData);
until ioffset >= iDataLenght;
// if debughook > 0 then
// OutputDebugString(PChar(Format('Written (TID:%d, P:%d): %s',

View file

@ -5,10 +5,18 @@ interface
uses
Classes,
IdServerIOHandlerStack, IdIOHandlerStack, IdGlobal, IdIOHandler, IdYarn, IdThread, IdSocketHandle,
{$IFNDEF WS_NO_SSL}
IdSSLOpenSSL,
sysutils,
{$ENDIF}
IdIOHandlerWebsocket;
type
{$IFNDEF WS_NO_SSL}
TIdServerIOHandlerWebsocket = class(TIdServerIOHandlerStack)
{$ELSE}
TIdServerIOHandlerWebsocket = class(TIdServerIOHandlersslOpenSSL)
{$ENDIF}
protected
procedure InitComponent; override;
public
@ -23,8 +31,40 @@ implementation
function TIdServerIOHandlerWebsocket.Accept(ASocket: TIdSocketHandle;
AListenerThread: TIdThread; AYarn: TIdYarn): TIdIOHandler;
{$IFNDEF WS_NO_SSL}
var
LIO: TIdIOHandlerWebsocketSSL;
{$ENDIF}
begin
{$IFDEF WS_NO_SSL}
Result := inherited Accept(ASocket, AListenerThread, AYarn);
{$ELSE}
Assert(ASocket<>nil);
Assert(fSSLContext<>nil);
LIO := TIdIOHandlerWebsocket.Create(nil);
try
LIO.PassThrough := True;
LIO.Open;
if LIO.Binding.Accept(ASocket.Handle) then
begin
//we need to pass the SSLOptions for the socket from the server
LIO.ClearSSLOptions;
LIO.IsPeer := True;
LIO.SSLOptions := SSLOptions;
LIO.SSLSocket := TIdSSLSocket.Create(Self);
LIO.SSLContext := fSSLContext;
end
else
begin
FreeAndNil(LIO);
end;
except
LIO.Free;
raise;
end;
Result := LIO;
{$ENDIF}
if Result <> nil then
begin
(Result as TIdIOHandlerWebsocket).IsServerSide := True; //server must not mask, only client
@ -35,6 +75,7 @@ end;
procedure TIdServerIOHandlerWebsocket.InitComponent;
begin
inherited InitComponent;
//TODO: Check if this is necessary for SSL
IOHandlerSocketClass := TIdIOHandlerWebsocket;
end;

View file

@ -137,7 +137,9 @@ begin
aSocketIOHandler.WritePing(context);
end
else
begin
context.IOHandler.WriteData(nil, wdcPing);
end;
end;
end;
@ -325,13 +327,22 @@ begin
hash.Free;
end;
AResponseInfo.CustomHeaders.Values['Sec-WebSocket-Accept'] := sValue;
{$IFNDEF WS_NO_SSL}
//keep alive the ssl connection
AResponseInfo.CustomHeaders.Values['Keep-alive'] := 'true';
{$ENDIF}
//send same protocol back?
AResponseInfo.CustomHeaders.Values['Sec-WebSocket-Protocol'] := context.WebSocketProtocol;
//we do not support extensions yet (gzip deflate compression etc)
//AResponseInfo.CustomHeaders.Values['Sec-WebSocket-Extensions'] := context.WebSocketExtensions;
//http://www.lenholgate.com/blog/2011/07/websockets---the-deflate-stream-extension-is-broken-and-badly-designed.html
//but is could be done using idZlib.pas and DecompressGZipStream etc
{$IFNDEF WS_NO_SSL}
//YD: TODO: Check if this is really necessary
AResponseInfo.CustomHeaders.Values['sec-websocket-extensions'] := '';
context.WebSocketExtensions := '';
{$ENDIF}
//send response back
context.IOHandler.InputBuffer.Clear;

View file

@ -4,7 +4,7 @@ interface
uses
IdServerWebsocketHandling, IdServerSocketIOHandling, IdServerWebsocketContext,
IdHTTPServer, IdContext, IdCustomHTTPServer, Classes, IdIOHandlerWebsocket;
IdHTTPServer, IdContext, IdCustomHTTPServer, Classes, IdIOHandlerWebsocket, IdServerIOHandler;
type
TWebsocketMessageText = procedure(const AContext: TIdServerWSContext; const aText: string) of object;
@ -43,7 +43,12 @@ type
implementation
uses
IdServerIOHandlerWebsocket, IdStreamVCL, IdGlobal, Windows, IdWinsock2;
IdServerIOHandlerWebsocket, IdStreamVCL, IdGlobal, Windows,
{$IFNDEF WS_NO_SSL}
idIOHandler,
idssl,
{$ENDIF}
IdWinsock2;
{ TIdWebsocketServer }