aws_24.0.0_2b75fe6d/regtests/0225_cookie_and_session/cookie_session.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
------------------------------------------------------------------------------
--                              Ada Web Server                              --
--                                                                          --
--                     Copyright (C) 2012-2021, AdaCore                     --
--                                                                          --
--  This 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.  This software 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   --
--  distributed  with  this  software;   see  file COPYING3.  If not, go    --
--  to http://www.gnu.org/licenses for a complete copy of the license.      --
------------------------------------------------------------------------------

with Ada.Containers.Indefinite_Ordered_Sets;
with Ada.Strings.Equal_Case_Insensitive;
with Ada.Strings.Fixed;
with Ada.Text_IO;

with AWS.Client;
with AWS.Cookie;
with AWS.Headers;
with AWS.Messages;
with AWS.Response;
with AWS.Server.Status;
with AWS.Session;
with AWS.Status;

procedure Cookie_Session is

   use Ada;
   use Ada.Strings;
   use AWS;

   package String_Set is new Containers.Indefinite_Ordered_Sets (String);

   S : String_Set.Set;

   function Callback (Request : Status.Data) return Response.Data is
      Res : Response.Data;
      SID : Session.Id := Status.Session (Request);
   begin
      Res := Response.Build
        ("text/plain", "Session " & Session.Image (SID) & ASCII.LF);
      Cookie.Set (Res, "key1", "val1");
      Cookie.Set (Res, "key2", "val2");
      Session.Set (SID, "skey", "sval");
      return Res;
   end Callback;

   WS  : Server.HTTP;
   Res : Response.Data;
   HL  : Headers.List;

begin
   Server.Start
     (WS, "Cookie Session", Callback'Unrestricted_Access, Port => 0,
      Session => True);

   Res := Client.Get (Server.Status.Local_URL (WS));

   HL := Response.Header (Res);

   for K in 1 .. HL.Count loop
      declare
         L : String := Headers.Get_Line (HL, K);
         I : Natural := Strings.Fixed.Index (L, "AWS=SID-");
         O : Positive := 8;
      begin
         if Equal_Case_Insensitive
           (L (L'First .. L'First + Messages.Set_Cookie_Token'Length - 1),
            Messages.Set_Cookie_Token)
         then
            --  Replace header to have casing compatible with HTTP/2
            L (L'First .. L'First + Messages.Set_Cookie_Token'Length - 1) :=
              Messages.Set_Cookie_Token;

            if I = 0 then
               I := Strings.Fixed.Index (L, "AWS_Private=");
               O := 12;
            end if;

            if I /= 0 then
               I := I + O;
               while L (I) /= ';' loop
                  L (I) := 'x';
                  I := I + 1;
               end loop;
            end if;
            S.Insert (L);
         end if;
      end;
   end loop;

   for L of S loop
      Text_IO.Put_Line (L);
   end loop;

   Server.Shutdown (WS);
end Cookie_Session;