File: agent.adb

package info (click to toggle)
libaws 2.2dfsg-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 7,624 kB
  • ctags: 1,173
  • sloc: ada: 61,829; ansic: 6,483; makefile: 1,282; xml: 196; sh: 119; java: 112; python: 66; sed: 40
file content (428 lines) | stat: -rw-r--r-- 14,697 bytes parent folder | download
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
------------------------------------------------------------------------------
--                              Ada Web Server                              --
--                                                                          --
--                         Copyright (C) 2000-2004                          --
--                                ACT-Europe                                --
--                                                                          --
--  This library is free software; you can redistribute it and/or modify    --
--  it under the terms of the GNU General Public License as published by    --
--  the Free Software Foundation; either version 2 of the License, or (at   --
--  your option) any later version.                                         --
--                                                                          --
--  This library 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       --
--  along with this library; if not, write to the Free Software Foundation, --
--  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.          --
--                                                                          --
------------------------------------------------------------------------------

--  Usage: agent [options] [GET/PUT] <URL>
--         -f                      force display of message body.
--         -o                      output result in file agent.out.
--         -k                      keep-alive connection.
--         -s                      server-push mode.
--         -r                      follow redirection.
--         -n                      non stop for stress test.
--         -d                      debug mode, view HTTP headers.
--         -c                      display server certificate.
--         -t                      wait response timeout.
--         -proxy <proxy_url>
--         -u <user_name>
--         -p <password>
--         -a <www_authentication_mode (Any, Basic or Digest)>
--         -pu <proxy_user_name>
--         -pp <proxy_password>
--         -pa <proxy_authentication_mode (Any, Basic or Digest)>
--
--  for example:
--
--     agent GET http://perso.wanadoo.fr/pascal.obry/contrib.html
--

with Ada.Text_IO;
with Ada.Strings.Unbounded;
with Ada.Command_Line;
with Ada.Characters.Handling;
with Ada.Streams.Stream_IO;
with Ada.Exceptions;

with GNAT.Command_Line;

with AWS.Client;
with AWS.Resources;
with AWS.Response;
with AWS.Messages;
with AWS.MIME;
with AWS.Net.SSL.Certificate;
with AWS.Status;
with AWS.URL;

procedure Agent is

   use AWS;
   use Ada;
   use Ada.Strings.Unbounded;
   use type Status.Request_Method;
   use type Messages.Status_Code;

   Syntax_Error : exception;

   Method             : Status.Request_Method;
   User               : Unbounded_String;
   Pwd                : Unbounded_String;
   WWW_Auth           : Client.Authentication_Mode := Client.Basic;
   URL                : Unbounded_String;
   Proxy              : Unbounded_String;
   Proxy_User         : Unbounded_String;
   Proxy_Pwd          : Unbounded_String;
   Proxy_Auth         : Client.Authentication_Mode := Client.Basic;
   Force              : Boolean := False;
   File               : Boolean := False;
   Keep_Alive         : Boolean := False;
   Server_Push        : Boolean := False;
   Follow_Redirection : Boolean := False;
   Wait_Key           : Boolean := True;
   Show_Cert          : Boolean := False;
   Timeouts           : Client.Timeouts_Values := Client.No_Timeout;
   Connect            : AWS.Client.HTTP_Connection;

   procedure Parse_Command_Line;
   --  parse Agent command line.

   function Get_Auth_Mode (Mode : String) return Client.Authentication_Mode;
   --  Return the authentication value from the string representation.
   --  raises the human readable exception on error.

   procedure Show_Certificate (Cert : in Net.SSL.Certificate.Object);

   -------------------
   -- Get_Auth_Mode --
   -------------------

   function Get_Auth_Mode (Mode : String) return Client.Authentication_Mode is
   begin
      return Client.Authentication_Mode'Value (Mode);
   exception
      when Constraint_Error =>
         Ada.Exceptions.Raise_Exception
           (Constraint_Error'Identity,
            "Authentication mode should be ""Basic"", ""Digest"" or ""Any"".");
   end Get_Auth_Mode;

   ------------------------
   -- Parse_Command_Line --
   ------------------------

   procedure Parse_Command_Line is
   begin
      loop
         case GNAT.Command_Line.Getopt
           ("f o d u: p: a: pu: pp: pa: proxy: k n s r c t:")
         is
            when ASCII.NUL =>
               exit;

            when 'f' =>
               Force := True;

            when 'o' =>
               File := True;

            when 'n' =>
               Wait_Key := False;

            when 'd' =>
               AWS.Client.Set_Debug (On => True);

            when 'k' =>
               Keep_Alive := True;

            when 'r' =>
               Follow_Redirection := True;

            when 's' =>
               Server_Push := True;

            when 'c' =>
               Show_Cert := True;

            when 'u' =>
               User := To_Unbounded_String (GNAT.Command_Line.Parameter);

            when 'a' =>
               WWW_Auth :=
                  Get_Auth_Mode (GNAT.Command_Line.Parameter);

            when 't' =>
               Timeouts.Receive
                 := Duration'Value (GNAT.Command_Line.Parameter);

            when 'p' =>
               if GNAT.Command_Line.Full_Switch = "p" then
                  Pwd := To_Unbounded_String (GNAT.Command_Line.Parameter);

               elsif GNAT.Command_Line.Full_Switch = "pu" then
                  Proxy_User
                    := To_Unbounded_String (GNAT.Command_Line.Parameter);

               elsif GNAT.Command_Line.Full_Switch = "pp" then
                  Proxy_Pwd
                    := To_Unbounded_String (GNAT.Command_Line.Parameter);

               elsif GNAT.Command_Line.Full_Switch = "proxy" then
                  Proxy
                    := To_Unbounded_String (GNAT.Command_Line.Parameter);

               elsif GNAT.Command_Line.Full_Switch = "pa" then
                  Proxy_Auth
                    := Get_Auth_Mode (GNAT.Command_Line.Parameter);
               end if;

            when others =>
               raise Program_Error;         -- cannot occurs!
         end case;
      end loop;

      if Follow_Redirection and then Keep_Alive then
         Exceptions.Raise_Exception
           (Syntax_Error'Identity,
            "Follow redirection and keep-alive mode can't be used together.");
      end if;

      Method := Status.Request_Method'Value (GNAT.Command_Line.Get_Argument);
      URL    := To_Unbounded_String (GNAT.Command_Line.Get_Argument);
   end Parse_Command_Line;

   ----------------------
   -- Show_Certificate --
   ----------------------

   procedure Show_Certificate (Cert : in Net.SSL.Certificate.Object) is
      use Ada.Text_IO;
      use type Net.SSL.Certificate.Object;
   begin
      if Cert = Net.SSL.Certificate.Undefined then
         Put_Line ("No certificate.");
      else
         Put_Line ("Subject : " & Net.SSL.Certificate.Subject (Cert));
         Put_Line ("Issuer  : " & Net.SSL.Certificate.Issuer (Cert));
      end if;
   end Show_Certificate;

   Data       : Response.Data;
   URL_Object : AWS.URL.Object;

begin
   if Ada.Command_Line.Argument_Count = 0 then
      Text_IO.Put_Line ("Usage: agent [options] [GET/PUT] <URL>");
      Text_IO.Put_Line ("       -f           force display of message body.");
      Text_IO.Put_Line ("       -o           output result in file agent.out");
      Text_IO.Put_Line ("       -k           keep-alive connection.");
      Text_IO.Put_Line ("       -s           server-push mode.");
      Text_IO.Put_Line ("       -n           non stop for stress test.");
      Text_IO.Put_Line ("       -r           follow redirection.");
      Text_IO.Put_Line ("       -d           debug mode, view HTTP headers.");
      Text_IO.Put_Line ("       -c           display server certificate.");
      Text_IO.Put_Line ("       -t           wait response timeout.");
      Text_IO.Put_Line ("       -proxy <proxy_url>");
      Text_IO.Put_Line ("       -u <user_name>");
      Text_IO.Put_Line ("       -p <password>");
      Text_IO.Put_Line ("       -a <www_authentication_mode"
                          & " (Any, Basic or Digest)>");
      Text_IO.Put_Line ("       -pu <proxy_user_name>");
      Text_IO.Put_Line ("       -pp <proxy_password>");
      Text_IO.Put_Line ("       -pa <proxy_authentication_mode"
                          & " (Any, Basic or Digest)>");
      return;
   end if;

   Parse_Command_Line;

   URL_Object := AWS.URL.Parse (To_String (URL));

   if not (Method = Status.GET and then Follow_Redirection) then
      Client.Create
        (Connection  => Connect,
         Host        => To_String (URL),
         Proxy       => To_String (Proxy),
         Persistent  => Keep_Alive,
         Server_Push => Server_Push,
         Timeouts    => Timeouts);

      Client.Set_WWW_Authentication
        (Connection => Connect,
         User       => To_String (User),
         Pwd        => To_String (Pwd),
         Mode       => WWW_Auth);

      Client.Set_Proxy_Authentication
        (Connection => Connect,
         User       => To_String (Proxy_User),
         Pwd        => To_String (Proxy_Pwd),
         Mode       => Proxy_Auth);

      if Show_Cert then
         Show_Certificate (Client.Get_Certificate (Connect));
      end if;
   end if;

   loop
      if Method = Status.GET then

         if Keep_Alive then
            Client.Get (Connect, Data);
         else
            Data := Client.Get
              (To_String (URL), To_String (User), To_String (Pwd),
               To_String (Proxy),
               To_String (Proxy_User), To_String (Proxy_Pwd),
               Follow_Redirection => Follow_Redirection);
         end if;

      else
         --  ??? PUT just send a simple piece of Data.
         --  ??? would also be nice to handle POST request
         Client.Put (Connection => Connect,
                     Result     => Data,
                     Data       => "Un essai");
      end if;

      Text_IO.Put_Line
        ("Status Code = "
           & Messages.Image (Response.Status_Code (Data))
           & " - "
           & Messages.Reason_Phrase (Response.Status_Code (Data)));

      if Response.Status_Code (Data) = Messages.S301 then
         Text_IO.Put_Line ("New location : " & Response.Location (Data));
      end if;

      if MIME.Is_Text (Response.Content_Type (Data)) then

         if File then
            declare
               F : Text_IO.File_Type;
            begin
               Text_IO.Create (F, Text_IO.Out_File, "agent.out");
               Text_IO.Put_Line (F, Response.Message_Body (Data));
               Text_IO.Close (F);
            end;
         else
            Text_IO.New_Line;
            Text_IO.Put_Line (Response.Message_Body (Data));
         end if;

      else
         Text_IO.Put_Line
           (Messages.Content_Type (Response.Content_Type (Data)));

         Text_IO.Put_Line
           (Messages.Content_Length (Response.Content_Length (Data)));

         if Force or else File then
            --  This is not a text/html body, but output it anyway

            declare
               use Streams;

               Message_Stream : Resources.File_Type;
               Buffer         : Stream_Element_Array (1 .. 4_096);
               Last           : Stream_Element_Offset;
            begin
               Response.Message_Body (Data, Message_Stream);

               if File then
                  declare
                     F : Streams.Stream_IO.File_Type;
                  begin
                     Stream_IO.Create
                       (F, Stream_IO.Out_File, "agent.out");

                     loop
                        Resources.Read (Message_Stream, Buffer, Last);

                        Stream_IO.Write (F, Buffer (1 .. Last));

                        exit when Last < Buffer'Last;
                     end loop;

                     Stream_IO.Close (F);
                  end;

               else
                  loop
                     Resources.Read (Message_Stream, Buffer, Last);

                     for K in Buffer'First .. Last loop
                        declare
                           C : constant Character
                             := Character'Val (Buffer (K));
                        begin
                           if C = ASCII.CR
                             or else C = ASCII.LF
                             or else not Characters.Handling.Is_Control (C)
                           then
                              Text_IO.Put (C);
                           else
                              Text_IO.Put ('.');
                           end if;
                        end;
                     end loop;

                     exit when Last < Buffer'Last;
                  end loop;
               end if;

               Resources.Close (Message_Stream);
            end;

         end if;
      end if;

      if Server_Push then
         loop
            declare
               Line : constant String
                 := Client.Read_Until (Connect, "" & ASCII.LF);
            begin
               exit when Line = "";
               Text_IO.Put (Line);
            end;
         end loop;
      end if;

      if Keep_Alive then
         --  check that the keep alive connection is kept alive
         if Wait_Key then

            Text_IO.Put_Line
              ("Type 'q' to exit, the connection will be closed.");

            Text_IO.Put_Line ("Any other key to retreive again the same URL");

            declare
               Char : Character;
            begin
               Text_IO.Get_Immediate (Char);
               exit when Char = 'q';
            end;
         end if;

      else
         Client.Close (Connect);
         exit;
      end if;

   end loop;

exception
   when SE : Syntax_Error =>
      Text_IO.Put_Line ("Syntax error: " & Exceptions.Exception_Message (SE));

   when E : others =>
      Text_IO.Put_Line (Exceptions.Exception_Information (E));
end Agent;