aws_21.0.0_57fddf8f/regtests/0036_attachment/attachment.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
------------------------------------------------------------------------------
--                              Ada Web Server                              --
--                                                                          --
--                     Copyright (C) 2004-2016, 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.Streams.Stream_IO;
with Ada.Strings.Fixed;
with Ada.Strings.Maps;
with Ada.Strings.Unbounded;
with Ada.Text_IO;

with AWS.Attachments;
with AWS.Client;
with AWS.Config.Set;
with AWS.MIME;
with AWS.Net.Log;
with AWS.Response;
with AWS.Server.Status;
with AWS.Status;
with AWS.Translator;
with AWS.Utils;

procedure Attachment is

   use Ada;
   use Ada.Streams;
   use Ada.Strings;
   use AWS;

   Att      : Attachments.List;
   Response : AWS.Response.Data;

   WS       : Server.HTTP;
   CNF      : Config.Object;

   procedure Output (Filename, Local_Filename, Content_Type : String);
   --  Output content of filename. Output is base64 encoded if content type is
   --  not textual data.

   ----------
   -- Dump --
   ----------

   procedure Dump
     (Direction : Net.Log.Data_Direction;
      Socket    : Net.Socket_Type'Class;
      Data      : Stream_Element_Array;
      Last      : Stream_Element_Offset)
   is
      use type Net.Log.Data_Direction;
   begin
      if Direction = Net.Log.Sent then
         Text_IO.Put_Line
           ("********** " & Net.Log.Data_Direction'Image (Direction));
         Text_IO.Put_Line (Translator.To_String (Data (Data'First .. Last)));
         Text_IO.New_Line;
      end if;
   end Dump;

   ------------
   -- Output --
   ------------

   procedure Output (Filename, Local_Filename, Content_Type : String) is
      use Ada.Streams;
      use Ada.Strings.Unbounded;

      L_Filename : Unbounded_String := To_Unbounded_String (Local_Filename);
      C          : constant Natural :=
                     Strings.Unbounded.Index (L_Filename, "-");

      File   : Stream_IO.File_Type;
      Buffer : Stream_Element_Array (1 .. 4_048);
      Last   : Stream_Element_Offset;
   begin
      --  Convert slashes

      Strings.Unbounded.Translate
        (L_Filename, Strings.Maps.To_Mapping ("\", "/"));

      --  Now remove the pid

      Strings.Unbounded.Replace_Slice (L_Filename, 3, C, "XpidX-");

      Text_IO.Put_Line
        ("File : " & To_String (L_Filename)
         & ", " & Filename & ", " & Content_Type);

      Stream_IO.Open (File, Stream_IO.In_File, Local_Filename);
      Stream_IO.Read (File, Buffer, Last);

      if MIME.Is_Text (Content_Type) then
         Text_IO.Put_Line (Translator.To_String (Buffer (1 .. Last)));
      else
         Text_IO.Put_Line (Translator.Base64_Encode (Buffer (1 .. Last)));
      end if;

      Text_IO.New_Line;

      Stream_IO.Close (File);
   end Output;

   -----------
   -- HW_CB --
   -----------

   function HW_CB (Request : AWS.Status.Data) return AWS.Response.Data is
      Att_List : constant AWS.Attachments.List :=
                   AWS.Status.Attachments (Request);
      Atts     : constant Integer := AWS.Attachments.Count (Att_List);
      Att      : AWS.Attachments.Element;
   begin
      for J in 1 .. Atts loop
         Att := AWS.Attachments.Get (Att_List, J);

         Output (AWS.Attachments.Filename (Att),
                 AWS.Attachments.Local_Filename (Att),
                 AWS.Attachments.Content_Type (Att));
      end loop;

      return AWS.Response.Build
        (MIME.Text_Plain, "Got" & Integer'Image (Atts) & " attachments");
   end HW_CB;

begin
   AWS.Attachments.Add
     (Attachments => Att,
      Filename    => "attachment1.txt",
      Content_Id  => "My-Txt-Attachment");

   AWS.Attachments.Add
     (Attachments => Att,
      Filename    => "attachment2.txt",
      Content_Id  => "My-Second-Txt-Attachment");

   AWS.Attachments.Add
     (Attachments => Att,
      Filename    => "aws_logo.png",
      Content_Id  => "My-Png-Attachment");

   Config.Set.Server_Name      (CNF, "Attachment Server");
   Config.Set.Server_Host      (CNF, "localhost");
   Config.Set.Server_Port      (CNF, 0);
   Config.Set.Upload_Directory (CNF, ".");

   Server.Start (WS, HW_CB'Unrestricted_Access, CNF);

   --  AWS.Net.Log.Start (Dump'Unrestricted_Access);

   Response := AWS.Client.Post
     (URL         => AWS.Server.Status.Local_URL (WS) & "/any_URI",
      Data        => "Dummy message",
      Attachments => Att);

   Text_IO.Put_Line ("Response=" & AWS.Response.Message_Body (Response));

   Server.Shutdown (WS);
end Attachment;