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 | ------------------------------------------------------------------------------
-- Ada Web Server --
-- --
-- Copyright (C) 2005-2014, 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.Numerics.Discrete_Random;
with Ada.Streams.Stream_IO;
with Ada.Strings.Fixed;
with Ada.Strings.Unbounded;
with Ada.Text_IO;
with GNAT.MD5;
with AWS.Client;
with AWS.Config.Set;
with AWS.Messages;
with AWS.MIME;
with AWS.Resources.Streams.Disk;
with AWS.Response;
with AWS.Server.Status;
with AWS.Services.Dispatchers.Linker;
with AWS.Services.Dispatchers.URI;
with AWS.Services.Download;
with AWS.Status;
with AWS.Translator;
with AWS.Utils;
procedure DM is
use Ada;
use Ada.Streams;
use Ada.Strings;
use Ada.Strings.Unbounded;
use Ada.Text_IO;
use AWS;
use GNAT;
Debug : constant Boolean := False;
Nb_Client : constant := 5;
WS : Server.HTTP;
type Download_Info is record
Size : Positive;
Signature : MD5.Message_Digest;
end record;
function CB (Request : Status.Data) return Response.Data;
task type Client is
entry Start (N : Positive);
entry Stop (Info : out Download_Info);
end Client;
Filename : constant String := "dm_file.data";
procedure Put_Line (Str : String);
-- Output Str if in Debug mode
function Create_Filename return MD5.Message_Digest;
-- Generate file content and return the MD5 signature
function MD5_Signature
(Content : Unbounded_String) return MD5.Message_Digest;
-- Returns the MD5 signature for Content
Some_Waiting : Boolean := False;
Starting : Natural := 0;
Downloads : Natural := 0;
Signature : MD5.Message_Digest;
-----------------
-- Char_Random --
-----------------
package Char_Random is new Numerics.Discrete_Random (Character);
use Char_Random;
Rand_Generator : Char_Random.Generator;
--------
-- CB --
--------
function CB (Request : Status.Data) return Response.Data is
URI : constant String := Status.URI (Request);
Stream : Resources.Streams.Stream_Access;
begin
if URI = "/welcome" then
Text_IO.Put_Line ("/welcome");
return Response.Build (MIME.Text_HTML, "welcome!");
elsif URI = "/download_file" then
Stream := new Resources.Streams.Disk.Stream_Type;
Resources.Streams.Disk.Open
(Resources.Streams.Disk.Stream_Type (Stream.all), Filename);
return Services.Download.Build (Request, Filename, Stream);
else
return Response.Acknowledge (Messages.S404, "Not found");
end if;
end CB;
------------
-- Client --
------------
task body Client is
use type Messages.Status_Code;
URI : Unbounded_String;
R : Response.Data;
Code : Messages.Status_Code;
N : Positive;
HTTP : AWS.Client.HTTP_Connection;
procedure Get_R (URI : String);
-- Get response for the specified URI, store the URI
procedure Reload_R;
-- Reload the previous URI
---------
-- Get --
---------
procedure Get_R (URI : String) is
begin
Client.URI := To_Unbounded_String (URI);
AWS.Client.Get (HTTP, R, URI);
end Get_R;
------------
-- Reload --
------------
procedure Reload_R is
begin
AWS.Client.Get (HTTP, R, To_String (URI));
end Reload_R;
begin
accept Start (N : Positive) do
Client.N := N;
end Start;
AWS.Client.Create (HTTP, Server.Status.Local_URL (WS));
Get_R ("/download_file");
loop
Code := Response.Status_Code (R);
declare
Message : constant String := Response.Message_Body (R);
begin
if Code = Messages.S302 then
Put_Line
("Client " & Utils.Image (N)
& " " & Messages.Status_Code'Image (Code) & Message);
Get_R (Response.Location (R));
elsif Fixed.Index (Message, "Download manager") /= 0 then
if Fixed.Index (Message, "waiting") /= 0 then
Some_Waiting := True;
else
Starting := Starting + 1;
end if;
-- A download page, we need to reload
Put_Line
("Client " & Utils.Image (N)
& " " & Messages.Status_Code'Image (Code) & Message);
delay 1.0;
Reload_R;
elsif Code = Messages.S200 then
Downloads := Downloads + 1;
Put_Line
("Client " & Utils.Image (N)
& " " & Messages.Status_Code'Image (Code));
exit;
else
Text_IO.Put_Line
("Error code " & Messages.Status_Code'Image (Code)
& Message);
exit;
end if;
end;
end loop;
accept Stop (Info : out Download_Info) do
Info.Size := Length (Response.Message_Body (R));
Info.Signature := MD5_Signature (Response.Message_Body (R));
end Stop;
exception
when others =>
Put_Line ("Client " & Utils.Image (N) & " error!");
end Client;
---------------------
-- Create_Filename --
---------------------
function Create_Filename return MD5.Message_Digest is
subtype Buffer is String (1 .. 20);
Ctx : MD5.Context;
File : Stream_IO.File_Type;
Str : Buffer;
begin
Stream_IO.Create (File, Stream_IO.Out_File, Filename);
for K in 1 .. 10_000 loop
for K in Buffer'Range loop
Str (K) := Random (Rand_Generator);
end loop;
MD5.Update (Ctx, Str);
Stream_IO.Write (File, Translator.To_Stream_Element_Array (Str));
end loop;
Stream_IO.Close (File);
return MD5.Digest (Ctx);
end Create_Filename;
-------------------
-- MD5_Signature --
-------------------
function MD5_Signature
(Content : Unbounded_String) return MD5.Message_Digest
is
Chunk_Size : constant := 4 * 1_024;
Len : constant Positive := Length (Content);
Ctx : MD5.Context;
First, Last : Positive;
begin
First := 1;
loop
Last := Positive'Min (First + Chunk_Size - 1, Len);
MD5.Update (Ctx, Slice (Content, First, Last));
exit when Last = Len;
First := Last + 1;
end loop;
return MD5.Digest (Ctx);
end MD5_Signature;
--------------
-- Put_Line --
--------------
procedure Put_Line (Str : String) is
begin
if Debug then
Text_IO.Put_Line (Str);
end if;
end Put_Line;
U : Services.Dispatchers.URI.Handler;
D : Services.Dispatchers.Linker.Handler;
R : Response.Data;
Conf : Config.Object := Config.Get_Current;
Clients : array (1 .. Nb_Client) of Client;
Results : array (1 .. Nb_Client) of Download_Info;
Size : Positive;
Size_Ok : Boolean := True;
Sig_Ok : Boolean := True;
begin
Reset (Rand_Generator);
Signature := Create_Filename;
Config.Set.Server_Host (Conf, "localhost");
Config.Set.Server_Port (Conf, 0);
Services.Dispatchers.URI.Register
(U, "/welcome", CB'Unrestricted_Access);
Services.Dispatchers.URI.Register
(U, "/download_file", CB'Unrestricted_Access);
Text_IO.Put_Line ("Start download server...");
Services.Download.Start (U, D, 1);
Text_IO.Put_Line ("Start main server...");
Server.Start (WS, D, Conf);
R := AWS.Client.Get (Server.Status.Local_URL (WS) & "/welcome");
-- Start clients
Text_IO.Put_Line ("Start clients...");
for K in Clients'Range loop
Clients (K).Start (K);
end loop;
-- Wait for client to stop
for K in Clients'Range loop
Clients (K).Stop (Results (K));
end loop;
Text_IO.Put_Line ("Clients stopped...");
R := AWS.Client.Get (Server.Status.Local_URL (WS) & "/welcome");
-- Get the real size
Size := Positive (Utils.File_Size (Filename));
-- Check the size of each download
for K in Results'Range loop
if Results (K).Size /= Size then
Size_Ok := False;
end if;
end loop;
-- Check the signature
for K in Results'Range loop
if Results (K).Signature /= Signature then
Sig_Ok := False;
end if;
end loop;
if Some_Waiting then
Text_IO.Put_Line ("OK: some have been waiting");
else
Text_IO.Put_Line ("ERROR: nobody in the waiting queue");
end if;
Text_IO.Put_Line ("Started " & Utils.Image (Starting));
Text_IO.Put_Line ("Donwloads " & Utils.Image (Downloads));
if Size_Ok then
Text_IO.Put_Line ("OK: All downloads have the correct size");
else
Text_IO.Put_Line ("ERROR: some download are not correct");
end if;
if Sig_Ok then
Text_IO.Put_Line ("OK: All downloads have the correct signature");
else
Text_IO.Put_Line ("ERROR: some download have not a correct signature");
end if;
Text_IO.Put_Line ("Stop servers...");
Server.Shutdown (WS);
Services.Download.Stop;
end DM;
|