1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . C G I . C O O K I E --
11 -- Copyright (C) 2000-2001 Ada Core Technologies, Inc.
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
33 ------------------------------------------------------------------------------
35 with Ada
.Strings
.Fixed
;
36 with Ada
.Strings
.Maps
;
38 with Ada
.Integer_Text_IO
;
42 package body GNAT
.CGI
.Cookie
is
46 Valid_Environment
: Boolean := False;
47 -- This boolean will be set to True if the initialization was fine.
49 Header_Sent
: Boolean := False;
50 -- Will be set to True when the header will be sent.
52 -- Cookie data that have been added.
54 type String_Access
is access String;
56 type Cookie_Data
is record
58 Value
: String_Access
;
59 Comment
: String_Access
;
60 Domain
: String_Access
;
63 Secure
: Boolean := False;
66 type Key_Value
is record
67 Key
, Value
: String_Access
;
70 package Cookie_Table
is new Table
(Cookie_Data
, Positive, 1, 5, 50);
71 -- This is the table to keep all cookies to be sent back to the server.
73 package Key_Value_Table
is new Table
(Key_Value
, Positive, 1, 1, 50);
74 -- This is the table to keep all cookies received from the server.
76 procedure Check_Environment
;
77 pragma Inline
(Check_Environment
);
78 -- This procedure will raise Data_Error if Valid_Environment is False.
81 -- Initialize CGI package by reading the runtime environment. This
82 -- procedure is called during elaboration. All exceptions raised during
83 -- this procedure are deferred.
85 -----------------------
86 -- Check_Environment --
87 -----------------------
89 procedure Check_Environment
is
91 if not Valid_Environment
then
94 end Check_Environment
;
100 function Count
return Natural is
102 return Key_Value_Table
.Last
;
109 function Exists
(Key
: String) return Boolean is
113 for K
in 1 .. Key_Value_Table
.Last
loop
114 if Key_Value_Table
.Table
(K
).Key
.all = Key
then
122 ----------------------
123 -- For_Every_Cookie --
124 ----------------------
126 procedure For_Every_Cookie
is
132 for K
in 1 .. Key_Value_Table
.Last
loop
135 Action
(Key_Value_Table
.Table
(K
).Key
.all,
136 Key_Value_Table
.Table
(K
).Value
.all,
142 end For_Every_Cookie
;
148 procedure Initialize
is
150 HTTP_COOKIE
: constant String := Metavariable
(CGI
.HTTP_Cookie
);
152 procedure Set_Parameter_Table
(Data
: String);
153 -- Parse Data and insert information in Key_Value_Table.
155 -------------------------
156 -- Set_Parameter_Table --
157 -------------------------
159 procedure Set_Parameter_Table
(Data
: String) is
161 procedure Add_Parameter
(K
: Positive; P
: String);
162 -- Add a single parameter into the table at index K. The parameter
163 -- format is "key=value".
165 Count
: constant Positive
166 := 1 + Strings
.Fixed
.Count
(Data
, Strings
.Maps
.To_Set
(";"));
167 -- Count is the number of parameters in the string. Parameters are
168 -- separated by ampersand character.
170 Index
: Positive := Data
'First;
177 procedure Add_Parameter
(K
: Positive; P
: String) is
178 Equal
: constant Natural := Strings
.Fixed
.Index
(P
, "=");
183 Key_Value_Table
.Table
(K
) :=
184 Key_Value
'(new String'(Decode
(P
(P
'First .. Equal
- 1))),
185 new String'(Decode (P (Equal + 1 .. P'Last))));
190 Key_Value_Table.Set_Last (Count);
192 for K in 1 .. Count - 1 loop
193 Sep := Strings.Fixed.Index (Data (Index .. Data'Last), ";");
195 Add_Parameter (K, Data (Index .. Sep - 1));
200 -- add last parameter
202 Add_Parameter (Count, Data (Index .. Data'Last));
203 end Set_Parameter_Table;
206 if HTTP_COOKIE /= "" then
207 Set_Parameter_Table (HTTP_COOKIE);
210 Valid_Environment := True;
214 Valid_Environment := False;
221 function Key (Position : Positive) return String is
225 if Position <= Key_Value_Table.Last then
226 return Key_Value_Table.Table (Position).Key.all;
228 raise Cookie_Not_Found;
236 function Ok return Boolean is
238 return Valid_Environment;
246 (Header : String := Default_Header;
247 Force : Boolean := False)
250 procedure Output_Cookies;
251 -- Iterate through the list of cookies to be sent to the server
258 procedure Output_Cookies is
260 procedure Output_One_Cookie
268 -- Output one cookie in the CGI header.
270 -----------------------
271 -- Output_One_Cookie --
272 -----------------------
274 procedure Output_One_Cookie
284 Text_IO.Put ("Set-Cookie: ");
285 Text_IO.Put (Key & '=' & Value);
287 if Comment /= "" then
288 Text_IO.Put ("; Comment=" & Comment);
292 Text_IO.Put ("; Domain=" & Domain);
295 if Max_Age /= Natural'Last then
296 Text_IO.Put ("; Max-Age=");
297 Integer_Text_IO.Put (Max_Age, Width => 0);
301 Text_IO.Put ("; Path=" & Path);
305 Text_IO.Put ("; Secure");
309 end Output_One_Cookie;
311 -- Start of processing for Output_Cookies
314 for C in 1 .. Cookie_Table.Last loop
315 Output_One_Cookie (Cookie_Table.Table (C).Key.all,
316 Cookie_Table.Table (C).Value.all,
317 Cookie_Table.Table (C).Comment.all,
318 Cookie_Table.Table (C).Domain.all,
319 Cookie_Table.Table (C).Max_Age,
320 Cookie_Table.Table (C).Path.all,
321 Cookie_Table.Table (C).Secure);
325 -- Start of processing for Put_Header
328 if Header_Sent = False or else Force then
330 Text_IO.Put_Line (Header);
344 Comment : String := "";
345 Domain : String := "";
346 Max_Age : Natural := Natural'Last;
347 Path : String := "/";
348 Secure : Boolean := False) is
350 Cookie_Table.Increment_Last;
352 Cookie_Table.Table (Cookie_Table.Last) :=
353 Cookie_Data'(new String'(Key),
355 new String'(Comment),
368 Required : Boolean := False)
374 for K in 1 .. Key_Value_Table.Last loop
375 if Key_Value_Table.Table (K).Key.all = Key then
376 return Key_Value_Table.Table (K).Value.all;
381 raise Cookie_Not_Found;
387 function Value (Position : Positive) return String is
391 if Position <= Key_Value_Table.Last then
392 return Key_Value_Table.Table (Position).Value.all;
394 raise Cookie_Not_Found;
398 -- Elaboration code for package
401 -- Initialize unit by reading the HTTP_COOKIE metavariable and fill
402 -- Key_Value_Table structure.