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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198 | -- --
-- package Copyright (c) Dmitry A. Kazakov --
-- GNAT.Sockets.Connection_State_Machine. Luebeck --
-- HTTP_Server.WebSocket_Server Spring, 2017 --
-- Interface --
-- Last revision : 21:11 16 Apr 2017 --
-- --
-- This library is free software; you can redistribute it and/or --
-- modify it under the terms of the GNU General Public License as --
-- published by the Free Software Foundation; either version 2 of --
-- the License, or (at your option) any later version. This library --
-- is distributed in the hope that it will be useful, but WITHOUT --
-- ANY WARRANTY; without even the implied warranty of --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU --
-- General Public License for more details. You should have --
-- received a copy of the GNU General Public License along with --
-- this library; if not, write to the Free Software Foundation, --
-- Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. --
-- --
-- As a special exception, if other files instantiate generics from --
-- this unit, or you link this unit with other files to produce an --
-- executable, this unit does not by itself cause the resulting --
-- executable to be covered by the GNU General Public License. This --
-- exception does not however invalidate any other reasons why the --
-- executable file might be covered by the GNU Public License. --
--____________________________________________________________________--
with GNAT.Sockets.Server.Handles; use GNAT.Sockets.Server.Handles;
package GNAT.Sockets.Connection_State_Machine.HTTP_Server.
WebSocket_Server is
--
-- HTTP_WebSocket_Client -- A server handling connection objects that
-- use WebSocket communication
--
-- Listener - The connection object
-- Request_Length - The maximum length of one request line
-- Input_Size - The input buffer size
-- Output_Size - The output buffer size
-- Factory - The WebSocket connection objects factory
-- Buffer_Size - The WebSocket message size
--
-- The implementations of connection objects can be used both for
-- for normal sockets and WebSockets. In the later case a HTTP client
-- derived from this type handles the connection object istead of an
-- object derived from Connections_Server.
--
type HTTP_WebSocket_Client
( Listener : access Connections_Server'Class;
Request_Length : Positive;
Input_Size : Buffer_Length;
Output_Size : Buffer_Length;
Factory : access Connections_Factory'Class;
Buffer_Size : Buffer_Length
) is new HTTP_Client with private;
--
-- Get_WebSocket_Client -- Get connection object running over WebSocket
--
-- Client - The HTTP connection object
--
-- Returns :
--
-- A handle to the aWebSocket connection object
--
function Get_WebSocket_Client
( Client : HTTP_WebSocket_Client
) return Handle;
--
-- Get_Error_Code -- Get error code returned when connection refused
--
-- Client - The HTTP connection object
--
-- When the factory of WebSocket connection objects refuses connection,
-- the result of this function is used to report the error code. The
-- default implementation returns 400.
--
-- Returns :
--
-- The error code
--
function Get_Error_Code
( Client : HTTP_WebSocket_Client
) return Positive;
--
-- Get_Error_Reason -- Get error reason returned when connection refused
--
-- Client - The HTTP connection object
--
-- When the factory of WebSocket connection objects refuses connection,
-- the result of this function is used to report the reason text. The
-- default implementation returns "Bad request".
--
-- Returns :
--
-- The error code
--
function Get_Error_Reason
( Client : HTTP_WebSocket_Client
) return String;
--
-- Get_Protocols -- Get the protocols string
--
-- Client - The HTTP connection object
--
-- When accepted the client receives a list of supported protocols as
-- returned by this function. When the result is empty the value of
-- Sec-WebSocket-Protocol is used instead. The default implementation
-- returns empty string.
-- Returns :
--
-- The error code
--
function Get_Protocols
( Client : HTTP_WebSocket_Client
) return String;
--
-- Overridden operations
--
procedure WebSocket_Finalize
( Client : in out HTTP_WebSocket_Client
);
function WebSocket_Open
( Client : access HTTP_WebSocket_Client
) return WebSocket_Accept;
procedure WebSocket_Initialize
( Client : in out HTTP_WebSocket_Client
);
procedure WebSocket_Received
( Client : in out HTTP_WebSocket_Client;
Message : Stream_Element_Array
);
procedure WebSocket_Received
( Client : in out HTTP_WebSocket_Client;
Message : String
);
procedure WebSocket_Received_Part
( Client : in out HTTP_WebSocket_Client;
Message : Stream_Element_Array
);
procedure WebSocket_Received_Part
( Client : in out HTTP_WebSocket_Client;
Message : String
);
private
type WebSockets_Server
( HTTP_Client : access HTTP_WebSocket_Client'Class;
Factory : access Connections_Factory'Class
) is new Connections_Server
( Factory => Factory,
Port => 0
) with null record;
procedure Initialize (Listener : in out WebSockets_Server);
procedure Shutdown
( Listener : in out WebSockets_Server;
Client : in out Connection'Class
);
procedure Send_Socket
( Listener : in out WebSockets_Server;
Client : in out Connection'Class;
Data : Stream_Element_Array;
Last : out Stream_Element_Offset
);
procedure Unblock_Send
( Listener : in out WebSockets_Server;
Client : in out Connection'Class
);
procedure Write
( Stream : access Root_Stream_Type'Class;
Item : WebSockets_Server
);
for WebSockets_Server'Write use Write;
type HTTP_WebSocket_Client
( Listener : access Connections_Server'Class;
Request_Length : Positive;
Input_Size : Buffer_Length;
Output_Size : Buffer_Length;
Factory : access Connections_Factory'Class;
Buffer_Size : Buffer_Length
) is new HTTP_Client
( Listener => Listener,
Request_Length => Request_Length,
Input_Size => Input_Size,
Output_Size => Output_Size
) with
record
Textual : Boolean := False;
WebSocket_Client : Handle;
WebSocket_Listener : aliased
WebSockets_Server
( HTTP_Client => HTTP_WebSocket_Client'Unchecked_Access,
Factory => Factory
);
end record;
end GNAT.Sockets.Connection_State_Machine.HTTP_Server.WebSocket_Server;
|