forked from andremussche/DelphiWebsockets
-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Journeyman.WebSocket.Server.Contexts.pas
133 lines (116 loc) · 4.64 KB
/
Journeyman.WebSocket.Server.Contexts.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
unit Journeyman.WebSocket.Server.Contexts;
interface
uses
System.Classes, System.StrUtils, IdContext, IdCustomTCPServer,
IdTCPConnection, // TIdTCPConnection
IdCustomHTTPServer,
Journeyman.WebSocket.IOHandlers,
Journeyman.WebSocket.Types,
Journeyman.WebSocket.Interfaces;
type
TIdServerWSContext = class;
TWebSocketUpgradeEvent = procedure(const AContext: TIdServerWSContext;
const ARequestInfo: TIdHTTPRequestInfo; var Accept: Boolean) of object;
TWebSocketChannelRequest = procedure(const AContext: TIdServerWSContext;
var aType:TWSDataType; const strmRequest, strmResponse: TMemoryStream) of object;
TWebSocketConnected = procedure(const AContext: TIdServerWSContext) of object;
TWebSocketDisconnected = procedure(const AContext: TIdServerWSContext) of object;
TOnBeforeSendHeaders = procedure(const AContext: TIdServerWSContext;
const AResponseInfo: TIdHTTPResponseInfo) of object;
TWebSocketConnectionEvents = record
ConnectedEvent: TWebSocketConnected;
DisconnectedEvent: TWebSocketDisconnected;
OnBeforeSendHeaders: TOnBeforeSendHeaders;
end;
TIdServerWSContext = class(TIdServerContext)
private
FWebSocketKey: string;
FWebSocketVersion: Integer;
FPath: string;
FWebSocketProtocol: string;
FResourceName: string;
FOrigin: string;
FQuery: string;
FHost: string;
FWebSocketExtensions: string;
FCookie: string;
FClientIP: string;
FEncoding: string;
FServerSoftware: string;
// FSocketIOPingSend: Boolean;
FOnWebSocketUpgrade: TWebSocketUpgradeEvent;
FOnCustomChannelExecute: TWebSocketChannelRequest;
// FSocketIO: TIdServerSocketIOHandling;
FOnDestroy: TIdContextEvent;
FSupportsPerMessageDeflate: Boolean;
FClientMaxWindowBits: Integer;
FServerMaxWindowBits: Integer;
function GetClientIP: string;
public
function IOHandler: IIOHandlerWebSocket;
public
function IsSocketIO: Boolean;
// property SocketIO: TIdServerSocketIOHandling read FSocketIO write FSocketIO;
property OnDestroy: TIdContextEvent read FOnDestroy write FOnDestroy;
public
procedure AfterConstruction; override;
destructor Destroy; override;
property Cookie : string read FCookie write FCookie;
property ClientIP : string read GetClientIP write FClientIP;
property Host : string read FHost write FHost;
property Origin : string read FOrigin write FOrigin;
property Path : string read FPath write FPath;
property Query : string read FQuery write FQuery;
property ResourceName: string read FResourceName write FResourceName;
property Encoding : string read FEncoding write FEncoding;
property ServerSoftware: string read FServerSoftware write FServerSoftware;
property ClientMaxWindowBits: Integer read FClientMaxWindowBits write FClientMaxWindowBits;
property ServerMaxWindowBits: Integer read FServerMaxWindowBits write FServerMaxWindowBits;
property SupportsPerMessageDeflate: Boolean read FSupportsPerMessageDeflate write FSupportsPerMessageDeflate;
property WebSocketKey : string read FWebSocketKey write FWebSocketKey;
property WebSocketProtocol : string read FWebSocketProtocol write FWebSocketProtocol;
property WebSocketVersion : Integer read FWebSocketVersion write FWebSocketVersion;
property WebSocketExtensions: string read FWebSocketExtensions write FWebSocketExtensions;
property OnWebSocketUpgrade: TWebSocketUpgradeEvent read FOnWebSocketUpgrade write FOnWebSocketUpgrade;
property OnCustomChannelExecute: TWebSocketChannelRequest read FOnCustomChannelExecute write FOnCustomChannelExecute;
end;
implementation
uses
Journeyman.WebSocket.SSLIOHandlers,
IdIOHandlerStack, Journeyman.WebSocket.Consts;
{ TIdServerWSContext }
procedure TIdServerWSContext.AfterConstruction;
begin
FClientMaxWindowBits := TClientMaxWindowBits.Disabled;
FServerMaxWindowBits := TServerMaxWindowBits.Disabled;
FSupportsPerMessageDeflate := False;
end;
destructor TIdServerWSContext.Destroy;
begin
if Assigned(OnDestroy) then
OnDestroy(Self);
inherited;
end;
function TIdServerWSContext.GetClientIP: string;
var
LHandler: TIdIOHandlerStack;
begin
if FClientIP = '' then
begin
LHandler := IOHandler as TIdIOHandlerStack;
FClientIP := LHandler.Binding.IP;
end;
Result := FClientIP;
end;
function TIdServerWSContext.IOHandler: IIOHandlerWebSocket;
begin
if Connection = nil then
Exit(nil);
Result := Connection.IOHandler as IIOHandlerWebSocket;
end;
function TIdServerWSContext.IsSocketIO: Boolean;
begin
// FDocument = '/socket.io/1/websocket/13412152'
Result := StartsText('/socket.io/1/websocket/', FPath);
end;
end.