ews_1.11.0_17517ec5/src/ews-server.adb

  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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
--  This package is free software; you can redistribute it and/or
--  modify it under terms of the GNU General Public License as
--  published by the Free Software Foundation; either version 3, or
--  (at your option) any later version.  It 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.
--
--  As a special exception under Section 7 of GPL version 3, you are
--  granted additional permissions described in the GCC Runtime
--  Library Exception, version 3.1, as published by the Free Software
--  Foundation.
--
--  You should have received a copy of the GNU General Public License
--  and a copy of the GCC Runtime Library Exception along with this
--  program; see the files COPYING3 and COPYING.RUNTIME respectively.
--  If not, see <http://www.gnu.org/licenses/>.
--
--  Copyright (C) 2003-2022, Simon Wright <simon@pushface.org>

with Ada.Dynamic_Priorities;
with Ada.Exceptions;
with Ada.Text_IO;
with EWS.HTTP;
with Interfaces.C.Strings;

package body EWS.Server is

   task type Server (With_Stack : Positive) is
      pragma Storage_Size (With_Stack);
      entry Start (Using_Port : GNAT.Sockets.Port_Type;
                   At_Priority : System.Priority;
                   Logging_Via : Logger;
                   Tracing : Boolean);
   end Server;
   type Server_P is access Server;

   procedure Respond (To : GNAT.Sockets.Socket_Type;
                      In_Sockets : in out GNAT.Sockets.Socket_Set_Type;
                      Logging_Via : Logger;
                      Tracing : Boolean);

   --  Logging/tracing
   procedure Default_Logger (Message : String; Level : Error_Level);
   procedure Log (Logging_Via : Logger;
                  Message : String;
                  With_Exception : Ada.Exceptions.Exception_Occurrence);
   function Resolve_Exception
     (Occurrence : Ada.Exceptions.Exception_Occurrence) return String;
   procedure Trace (Logging_Via : Logger;
                    Message : String;
                    Skt : GNAT.Sockets.Socket_Type;
                    Tracing : Boolean);
   procedure Trace (Logging_Via : Logger;
                    Message : String;
                    Tracing : Boolean);

   task body Server is
      Port : GNAT.Sockets.Port_Type;
      Priority : System.Priority;
      Logging_Via : Logger;
      Tracing : Boolean;
      Address : GNAT.Sockets.Sock_Addr_Type;
      Server_Socket : GNAT.Sockets.Socket_Type;
      Sockets : GNAT.Sockets.Socket_Set_Type;
      Write_Sockets : GNAT.Sockets.Socket_Set_Type; -- never used
      Selector : GNAT.Sockets.Selector_Type;
   begin
      pragma Warnings (Off, "call to obsolescent procedure *");
      GNAT.Sockets.Initialize;
      pragma Warnings (On, "call to obsolescent procedure *");
      accept Start (Using_Port : GNAT.Sockets.Port_Type;
                    At_Priority : System.Priority;
                    Logging_Via : Logger;
                    Tracing : Boolean) do
         Port := Using_Port;
         Priority := At_Priority;
         if Logging_Via = null then
            Server.Logging_Via := Default_Logger'Access;
         else
            Server.Logging_Via := Logging_Via;
         end if;
         Server.Tracing := Tracing;
      end Start;
      Ada.Dynamic_Priorities.Set_Priority (Priority);
      Address.Addr := GNAT.Sockets.Any_Inet_Addr;
      Address.Port := Port;
      GNAT.Sockets.Create_Socket (Server_Socket);
      GNAT.Sockets.Set_Socket_Option
        (Server_Socket,
         GNAT.Sockets.Socket_Level,
         (GNAT.Sockets.Reuse_Address, True));
      GNAT.Sockets.Bind_Socket (Server_Socket, Address);
      GNAT.Sockets.Listen_Socket (Server_Socket);
      GNAT.Sockets.Set (Sockets, Server_Socket);
      GNAT.Sockets.Create_Selector (Selector);
      loop
         declare
            Read_Sockets : GNAT.Sockets.Socket_Set_Type;
            Status : GNAT.Sockets.Selector_Status;
            use type GNAT.Sockets.Selector_Status;
         begin
            --  Initialize Read_Sockets with the sockets in use
            --  (Write_Sockets remains empty, we don't care if a
            --  socket becomes writable; we'll just block in that
            --  case).
            GNAT.Sockets.Copy (Sockets, Read_Sockets);
            --  Wait until something happens on one of the sockets.
            GNAT.Sockets.Check_Selector
              (Selector, Read_Sockets, Write_Sockets, Status);
            if Status = GNAT.Sockets.Completed then
               --  This was a successful completion. Find out which
               --  socket woke us up and deal with it. If there was
               --  more than one, we'll find out next time round the
               --  loop.
               declare
                  Socket : GNAT.Sockets.Socket_Type;
                  use type GNAT.Sockets.Socket_Type;
               begin
                  --  Which socket?
                  GNAT.Sockets.Get (Read_Sockets, Socket);
                  if Socket = Server_Socket then
                     --  It was the server; a new client has called
                     --  connect(). Accept the connection ...
                     GNAT.Sockets.Accept_Socket
                       (Server_Socket, Socket, Address);
                     Trace (Logging_Via, "connection", Socket, Tracing);
                     --  ... and add the new connected socket to the
                     --  set of sockets in use.
                     GNAT.Sockets.Set (Sockets, Socket);
                  elsif Socket = GNAT.Sockets.No_Socket then
                     --  None of our sockets has data/connection
                     --  available; don't care why.
                     Logging_Via ("server got No_Socket", Error);
                  else
                     --  There's an HTTP request to be read on one of
                     --  our connected clients' sockets; deal with it.
                     Trace (Logging_Via, "request", Socket, Tracing);
                     Respond (Socket, Sockets, Logging_Via, Tracing);
                  end if;
               end;
            else
               --  Unexpected, non-fatal error.
               Logging_Via ("server: Check_Selector returned " & Status'Img,
                            Error);
            end if;
            --  Clean up.
            GNAT.Sockets.Empty (Read_Sockets);
         exception
            when E : others =>
               Log (Logging_Via,
                    "server failed in inner loop",
                    With_Exception => E);
         end;
      end loop;
   exception
      when E : others =>
         Log (Logging_Via,
              "server task failed",
              With_Exception => E);
         GNAT.Sockets.Close_Socket (Server_Socket);
   end Server;


   procedure Serve
     (Using_Port : GNAT.Sockets.Port_Type;
      At_Priority : System.Priority := System.Default_Priority;
      With_Stack : Positive := 20_000;
      Logging_Via : Logger := null;
      Tracing : Boolean := False)
   is
      EWS_Server : constant Server_P := new Server (With_Stack);
   begin
      EWS_Server.Start (Using_Port, At_Priority, Logging_Via, Tracing);
   end Serve;


   procedure Respond (To : GNAT.Sockets.Socket_Type;
                      In_Sockets : in out GNAT.Sockets.Socket_Set_Type;
                      Logging_Via : Logger;
                      Tracing : Boolean)
   is

      procedure Close;
      procedure Close
      is
      begin
         GNAT.Sockets.Clear (In_Sockets, To);
         GNAT.Sockets.Close_Socket (To);
      end Close;

      Request : aliased HTTP.Request;
      Terminated : Boolean;

   begin

      begin
         HTTP.Initialize (Request, From => To, Terminated => Terminated);
      exception
         when E : others =>
            Log (Logging_Via,
                 "failed reading request",
                 With_Exception => E);
            return;
      end;

      if Terminated then
         Trace (Logging_Via, "connection terminated", Tracing);
         Close;
         return;
      end if;

      Trace (Logging_Via,
             "method "
               & HTTP.Get_Method (Request)
               & ", version "
               & HTTP.Get_Version (Request)
               & ", url "
               & HTTP.Get_URL (Request),
             Tracing);

      begin
         HTTP.Respond (HTTP.Find (Request'Unchecked_Access),
                       To => To);
      exception
         when GNAT.Sockets.Socket_Error =>
            --  Going to assume that a socket error occurs because of
            --  some browser behaviour (they've closed the socket
            --  without waiting for the response).
            Close;
            return;
         when E : others =>
            Log (Logging_Via, "failed in respond", With_Exception => E);
            begin
               HTTP.Respond
                 (HTTP.Exception_Response (E, Request'Unchecked_Access),
                  To => To);
            exception
               when others => null;
            end;
            Close;
            return;
      end;

      if not HTTP.Keep_Alive_After_Responding (Request) then
         Close;
      end if;

   end Respond;


   procedure Default_Logger (Message : String; Level : Error_Level)
   is
   begin
      Ada.Text_IO.Put_Line
        (Ada.Text_IO.Standard_Error, "EWS: " & Level'Img & ": " & Message);
   end Default_Logger;


   procedure Log (Logging_Via : Logger;
                  Message : String;
                  With_Exception : Ada.Exceptions.Exception_Occurrence)
   is
      use Ada.Exceptions;
   begin
      if Exception_Identity (With_Exception)
        = GNAT.Sockets.Socket_Error'Identity
      then
         begin
            Logging_Via
              (Message & ", "
                 & Resolve_Exception (With_Exception) & ", "
                 & Exception_Information (With_Exception),
               Error);
         exception
            --  If the special Socket_Error handling fails (XXX why
            --  would it?), revert to the standard case.
            when others =>
               Logging_Via (Message
                              & ", "
                              & Exception_Information (With_Exception),
                            Error);
         end;
      else
         Logging_Via (Message & ", " & Exception_Information (With_Exception),
                      Error);
      end if;
   end Log;


   function Resolve_Exception
     (Occurrence : Ada.Exceptions.Exception_Occurrence) return String
   is
      --  Fragmentarily copied from GNAT.Sockets function of the same
      --  name.
      Error : constant GNAT.Sockets.Error_Type
        := GNAT.Sockets.Resolve_Exception (Occurrence);
   begin
      case Error is
         when GNAT.Sockets.Cannot_Resolve_Error =>
            --  The errno is at the start of the exception message
            --  (inside []).
            declare
               function C_Strerror
                 (Errnum : Interfaces.C.int)
                 return Interfaces.C.Strings.chars_ptr;
               pragma Import (C, C_Strerror, "strerror");
               Msg : String
                 renames Ada.Exceptions.Exception_Message (Occurrence);
               First : Natural;
               Last : Natural;
               Errno : Integer;
               C_Msg : Interfaces.C.Strings.chars_ptr;
               use type Interfaces.C.Strings.chars_ptr;
            begin
               First := Msg'First;
               while First <= Msg'Last
                 and then Msg (First) not in '0' .. '9'
               loop
                  First := First + 1;
               end loop;

               if First > Msg'Last then
                  return "Cannot_Resolve_Error";
               end if;

               Last := First;
               while Last < Msg'Last
                 and then Msg (Last + 1) in '0' .. '9'
               loop
                  Last := Last + 1;
               end loop;

               Errno := Integer'Value (Msg (First .. Last));

               C_Msg := C_Strerror (Interfaces.C.int (Errno));

               if C_Msg = Interfaces.C.Strings.Null_Ptr then
                  return "unknown error " & Errno'Img;
               else
                  return Interfaces.C.Strings.Value (C_Msg);
               end if;
            end;
         when others =>
            return Error'Img;
      end case;
   end Resolve_Exception;


   procedure Trace (Logging_Via : Logger;
                    Message : String;
                    Skt : GNAT.Sockets.Socket_Type;
                    Tracing : Boolean)
   is
   begin
      if Tracing then
         Logging_Via
           (Message
              & ", socket"
              & GNAT.Sockets.Image (Skt)
              & " from "
              & GNAT.Sockets.Image (GNAT.Sockets.Get_Peer_Name (Skt)),
            Trace);
      end if;
   exception
      --  Only seen on Mandrake 10 on browser close. Leave it to
      --  Respond to clear up when it sees the send failure.
      when E : GNAT.Sockets.Socket_Error =>
         Log (Logging_Via,
              "failed in trace (" & Message & ")",
              With_Exception => E);
   end Trace;


   procedure Trace (Logging_Via : Logger;
                    Message : String;
                    Tracing : Boolean)
   is
   begin
      if Tracing then
         Logging_Via (Message, Trace);
      end if;
   end Trace;


end EWS.Server;