This repository has been archived by the owner on Jan 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
solid-web-request.adb
267 lines (210 loc) · 9.34 KB
/
solid-web-request.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
with Ada.Calendar;
with GNAT.String_Split;
with Solid.Web.Containers.Tables;
with Solid.Strings;
with Solid.Text_Streams;
use Solid.Strings;
with Ada.Text_IO;
package body Solid.Web.Request is
procedure Validate_Environment (Object : in Data) is
use type Web.Environment.Handle;
begin -- Validate_Environment
if Object.Environment = null then
raise No_Environment;
end if;
end Validate_Environment;
function Method (Object : Data) return Request_Method is
begin -- Method
Validate_Environment (Object => Object);
return Request_Method'Value (Web.Environment.Value (Object.Environment, Name => Web.Environment.Request_Method) );
end Method;
function URI (Object : Data) return String is
begin -- URI
Validate_Environment (Object => Object);
return Web.Environment.Value (Object.Environment, Name => Web.Environment.Request_URI);
end URI;
function Path (Object : Data) return String is
begin -- Path
Validate_Environment (Object => Object);
return Web.Environment.Value (Object.Environment, Name => Web.Environment.Path_Info);
end Path;
function Translated_Path (Object : Data) return String is
begin -- Translated_Path
Validate_Environment (Object => Object);
return Web.Environment.Value (Object.Environment, Name => Web.Environment.Path_Translated);
end Translated_Path;
function Content_Length (Object : Data) return Count is
begin -- Content_Length
Validate_Environment (Object => Object);
return Count'Value (Web.Environment.Value (Object.Environment, Name => Web.Environment.Content_Length) );
exception -- Content_Length
when Constraint_Error =>
return Not_Set;
end Content_Length;
function Content_Type (Object : Data) return String is
begin -- Content_Type
Validate_Environment (Object => Object);
return Web.Environment.Value (Object.Environment, Name => Web.Environment.Content_Type);
end Content_Type;
function Query (Object : Data) return String is
begin -- Query
Validate_Environment (Object => Object);
-- Method?
return Web.Environment.Value (Object.Environment, Name => Web.Environment.Query_String);
end Query;
function Program_Name (Object : Data) return String is
begin -- Program_Name
Validate_Environment (Object => Object);
return Web.Environment.Value (Object.Environment, Name => Web.Environment.Script_Name);
end Program_Name;
function Document_Root (Object : Data) return String is
begin -- Document_Root
Validate_Environment (Object => Object);
return Web.Environment.Value (Object.Environment, Name => Web.Environment.Document_Root);
end Document_Root;
function User_Agent (Object : Data) return String is
begin -- User_Agent
Validate_Environment (Object => Object);
return Web.Environment.Value (Object.Environment, Name => Web.Environment.HTTP_User_Agent);
end User_Agent;
function Host (Object : Data) return String is
begin -- Host
Validate_Environment (Object => Object);
return Web.Environment.Value (Object.Environment, Name => Web.Environment.HTTP_Host);
end Host;
function Server_Name (Object : Data) return String is
begin -- Server_Name
Validate_Environment (Object => Object);
return Web.Environment.Value (Object.Environment, Name => Web.Environment.Server_Name);
end Server_Name;
function Server_Admin (Object : Data) return String is
begin -- Server_Admin
Validate_Environment (Object => Object);
return Web.Environment.Value (Object.Environment, Name => Web.Environment.Server_Admin);
end Server_Admin;
function Server_Software (Object : Data) return String is
begin -- Server_Software
Validate_Environment (Object => Object);
return Web.Environment.Value (Object.Environment, Name => Web.Environment.Server_Software);
end Server_Software;
function Server_Protocol (Object : Data) return String is
begin -- Server_Protocol
Validate_Environment (Object => Object);
return Web.Environment.Value (Object.Environment, Name => Web.Environment.Server_Protocol);
end Server_Protocol;
function Server_Signature (Object : Data) return String is
begin -- Server_Signature
Validate_Environment (Object => Object);
return Web.Environment.Value (Object.Environment, Name => Web.Environment.Server_Signature);
end Server_Signature;
function Server_Address (Object : Data) return String is
begin -- Server_Address
Validate_Environment (Object => Object);
return Web.Environment.Value (Object.Environment, Name => Web.Environment.Server_Addr);
end Server_Address;
function Server_Port (Object : Data) return Network_Port is
begin -- Server_Port
Validate_Environment (Object => Object);
return Network_Port'Value (Web.Environment.Value (Object.Environment, Name => Web.Environment.Server_Port) );
exception -- Server_Port
when Constraint_Error =>
return No_Port;
end Server_Port;
function Remote_Address (Object : Data) return String is
begin -- Remote_Address
Validate_Environment (Object => Object);
return Web.Environment.Value (Object.Environment, Name => Web.Environment.Remote_Addr);
end Remote_Address;
function Remote_Port (Object : Data) return Network_Port is
begin -- Remote_Port
Validate_Environment (Object => Object);
return Network_Port'Value (Web.Environment.Value (Object.Environment, Name => Web.Environment.Server_Port) );
exception -- Remote_Port
when Constraint_Error =>
return No_Port;
end Remote_Port;
function Transaction (Object : Data) return Web.Transaction_ID is
begin -- Transaction
return Object.Transaction;
end Transaction;
function Environment (Object : Data) return Web.Environment.Handle is
begin -- Environment
return Object.Environment;
end Environment;
function Headers (Object : Data) return Web.Headers.List is
begin -- Headers
return Object.Headers;
end Headers;
function Cookies (Object : Data) return Web.Cookies.List is
begin -- Cookies
return Object.Cookies;
end Cookies;
function Parameters (Object : Data) return Web.Parameters.List is
begin -- Parameters
return Object.Parameters;
end Parameters;
function Payload (Object : Data) return Ada.Streams.Stream_Element_Array is
begin -- Payload
return Text_Streams.To_Stream (+Object.Payload);
end Payload;
function Session (Object : Data) return Boolean is
Cookies : constant Web.Cookies.List := Request.Cookies (Object);
use type Web.Session.Storage.Context_Handle;
begin -- Session
if Object.Session_Context = Web.Session.Storage.No_Context then
raise Web.Session.Invalid_Context with "Session: No session context.";
end if;
declare
Session_Name : constant String := Object.Session_Context.Name;
begin
return Cookies.Exists (Session_Name);
end;
end Session;
function Session (Object : Data) return Web.Session.Handle is
begin -- Session
if not Session (Object) then
Ada.Text_IO.Put_Line (Ada.Text_IO.Current_Error, "not Session (Object)");
return Web.Session.No_Session;
-------------------------------------------
-- Once FSF GNAT supports extended return statements, Web.Session.Data can be directly returned.
--~ return No_Session : Web.Session.Data do
--~ null; -- An invalid session.
--~ end return;
end if;
Read_Session : declare
Session_Name : constant String := Object.Session_Context.Name;
Cookies : constant Web.Cookies.List := Request.Cookies (Object);
begin
return Web.Session.Read (From => Object.Session_Context, Identity => Cookies.Get (Name => Session_Name) );
exception -- Read_Session
when Web.Session.Not_Found =>
Ada.Text_IO.Put_Line (Ada.Text_IO.Current_Error, "Not_Found");
-- In the case where a session cookie is found, but no data is found.
-- If this were a procedure, we could remove the cookie from the data,
-- so an appropriate check could be made in New_Session.
return Web.Session.No_Session;
-------------------------------------------
-- Once FSF GNAT supports extended return statements, Web.Session.Data can be directly returned.
--~ return No_Session : Web.Session.Data do
--~ null; -- An invalid session.
--~ end return;
end Read_Session;
end Session;
procedure New_Session (Object : in Data; Session : out Web.Session.Data; Headers : in out Web.Headers.List) is
begin -- New_Session
-- This would be an appropriate check mentioned above.
--~ if Request.Session (Object) then
--~ raise Web.Session.Invalid_Context with "New_Session: Session already exists.";
--~ end if;
declare
Session_Name : constant String := Object.Session_Context.Name;
begin
Web.Session.Create (Settings => Object.Session_Context, Session => Session);
Web.Cookies.Set (Headers, Name => Session_Name, Value => Web.Session.Identity (Session) );
end;
end New_Session;
procedure Initialize (Object : in out Data) is
begin -- Initialize
Object.Created := Ada.Calendar.Clock;
end Initialize;
end Solid.Web.Request;