/usr/share/ada/adainclude/aws/aws-attachments.adb is in libaws2.10.2-dev 2.10.2-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 | ------------------------------------------------------------------------------
-- Ada Web Server --
-- --
-- Copyright (C) 2004-2010, AdaCore --
-- --
-- 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. --
-- --
------------------------------------------------------------------------------
with Ada.Directories;
with Ada.Streams.Stream_IO;
with Ada.IO_Exceptions;
with AWS.Headers.Set;
with AWS.Headers.Values;
with AWS.Messages;
with AWS.Net.Buffered;
with AWS.Translator;
with AWS.Utils;
package body AWS.Attachments is
use Ada.Streams;
function "+"
(V : String)
return Unbounded_String renames To_Unbounded_String;
UID : Utils.Counter (0);
-- Unique Id used for generating the MIME boundaries
---------
-- Add --
---------
procedure Add
(Attachments : in out List;
Filename : String;
Content_Id : String;
Headers : AWS.Headers.List := AWS.Headers.Empty_List;
Name : String := "";
Encode : Encoding := None)
is
use type Attachment_Table.Vector;
Data : constant Content := File
(Filename, Encode, Content_Id, MIME.Content_Type (Filename));
begin
if Name = "" then
Add (Attachments, Filename, Data, Headers);
else
Add (Attachments, Name, Data, Headers);
end if;
end Add;
procedure Add
(Attachments : in out List;
Filename : String;
Headers : AWS.Headers.List;
Name : String := "";
Encode : Encoding := None)
is
Data : constant Content := File
(Filename, Encode, "", MIME.Content_Type (Filename));
begin
if Name = "" then
Add (Attachments, Filename, Data, Headers);
else
Add (Attachments, Name, Data, Headers);
end if;
end Add;
procedure Add
(Attachments : in out List;
Name : String;
Data : Content;
Headers : AWS.Headers.List := AWS.Headers.Empty_List)
is
Has_Content_Type : constant Boolean :=
Headers.Exist (AWS.Messages.Content_Type_Token);
Has_Content_TE : constant Boolean :=
Headers.Exist
(AWS.Messages.Content_Transfer_Encoding_Token);
A : Element :=
(AWS.Attachments.Data, Headers,
Data.Length + AWS.Headers.Length (Headers), Data);
begin
if Data.Filename = Null_Unbounded_String then
if Data.Content_Type /= Null_Unbounded_String then
AWS.Headers.Set.Update
(Headers => A.Headers,
Name => AWS.Messages.Content_Type_Token,
Value => To_String (Data.Content_Type));
end if;
else
AWS.Headers.Set.Add
(Headers => A.Headers,
Name => AWS.Messages.Content_Disposition_Token,
Value => "attachment; filename=""" & Name & '"');
if Data.Content_Type = Null_Unbounded_String then
if not Has_Content_Type then
-- Content_Type is not in A.Headers nor defined with Data,
-- create one based on the file name.
AWS.Headers.Set.Add
(Headers => A.Headers,
Name => AWS.Messages.Content_Type_Token,
Value => MIME.Content_Type (Name) & "; name="""
& Name & '"');
end if;
else
AWS.Headers.Set.Update
(Headers => A.Headers,
Name => AWS.Messages.Content_Type_Token,
Value => To_String (Data.Content_Type) & "; name="""
& Name & '"');
end if;
if Data.Encode = None and then not Has_Content_TE then
if MIME.Is_Text (MIME.Content_Type (Name)) then
AWS.Headers.Set.Add
(Headers => A.Headers,
Name => AWS.Messages.Content_Transfer_Encoding_Token,
Value => "8bit");
else
AWS.Headers.Set.Add
(Headers => A.Headers,
Name => AWS.Messages.Content_Transfer_Encoding_Token,
Value => "binary");
end if;
end if;
end if;
if Data.Encode = Base64 then
AWS.Headers.Set.Update
(Headers => A.Headers,
Name => AWS.Messages.Content_Transfer_Encoding_Token,
Value => "base64");
end if;
if Data.Content_Id /= Null_Unbounded_String then
AWS.Headers.Set.Add
(Headers => A.Headers,
Name => AWS.Messages.Content_Id_Token,
Value => '<' & To_String (Data.Content_Id) & '>');
end if;
Attachments.Vector.Append (A);
end Add;
procedure Add
(Parts : in out Alternatives;
Data : Content) is
begin
Parts.Parts.Append (Data);
end Add;
procedure Add
(Attachments : in out List;
Parts : Alternatives) is
begin
Attachments.Vector.Append (Element (Parts));
end Add;
----------------
-- Content_Id --
----------------
function Content_Id (Attachment : Element) return String is
begin
return AWS.Headers.Get (Attachment.Headers, Messages.Content_Id_Token);
end Content_Id;
------------------
-- Content_Type --
------------------
function Content_Type (Attachment : Element) return String is
begin
return AWS.Headers.Values.Get_Unnamed_Value
(AWS.Headers.Get (Attachment.Headers, Messages.Content_Type_Token));
end Content_Type;
-----------
-- Count --
-----------
function Count (Attachments : List) return Natural is
begin
return Natural (Attachment_Table.Length (Attachments.Vector));
end Count;
----------
-- File --
----------
function File
(Filename : String;
Encode : Encoding := None;
Content_Id : String := "";
Content_Type : String := MIME.Text_Plain) return Content is
begin
return Content'(File, Natural (Utils.File_Size (Filename)),
Content_Id => +Content_Id,
Content_Type => +Content_Type,
Filename => +Filename,
Encode => Encode);
end File;
--------------
-- Filename --
--------------
function Filename (Attachment : Element) return String is
Result : Unbounded_String;
begin
if AWS.Headers.Exist
(Attachment.Headers, Messages.Content_Disposition_Token)
then
Result :=
+(AWS.Headers.Values.Search
(AWS.Headers.Get
(Attachment.Headers, Messages.Content_Disposition_Token),
"filename"));
end if;
if Result = Null_Unbounded_String
and then
AWS.Headers.Exist
(Attachment.Headers, Messages.Content_Type_Token)
then
Result :=
+(AWS.Headers.Values.Search
(AWS.Headers.Get
(Attachment.Headers, Messages.Content_Type_Token),
"name"));
end if;
return To_String (Result);
end Filename;
--------------------------
-- For_Every_Attachment --
--------------------------
procedure For_Every_Attachment (Attachments : List) is
Quit : Boolean := False;
begin
for J in 1 .. Count (Attachments) loop
Action (Get (Attachments, J), J, Quit);
exit when Quit;
end loop;
end For_Every_Attachment;
---------
-- Get --
---------
function Get
(Attachments : List;
Index : Positive) return Element is
begin
return Attachment_Table.Element
(Container => Attachments.Vector, Index => Index);
end Get;
---------
-- Get --
---------
function Get
(Attachments : List;
Content_Id : String) return Element
is
function Get_CID (Content_Id : String) return String;
-- Returns Content_Id stripped from possibly surrounding
-- '<' '>' and prefixing "cid:".
-------------
-- Get_CID --
-------------
function Get_CID (Content_Id : String) return String is
begin
if Content_Id (Content_Id'First) = '<'
and then Content_Id (Content_Id'Last) = '>'
then
return Get_CID
(Content_Id (Content_Id'First + 1 .. Content_Id'Last - 1));
elsif Content_Id'Length > 4
and then
Content_Id (Content_Id'First .. Content_Id'First + 3) = "cid:"
then
return Content_Id (Content_Id'First + 4 .. Content_Id'Last);
else
return Content_Id;
end if;
end Get_CID;
CID : constant String := Get_CID (Content_Id);
Attachment : Element;
begin
for J in 1 .. Count (Attachments) loop
Attachment := Get (Attachments, J);
if AWS.Attachments.Content_Id (Attachment) = CID
or else AWS.Attachments.Content_Id (Attachment) = '<' & CID & '>'
then
return Attachment;
end if;
end loop;
-- No Content ID matched the request
raise Constraint_Error;
end Get;
-------------
-- Headers --
-------------
function Headers (Attachment : Element) return AWS.Headers.List is
begin
return Attachment.Headers;
end Headers;
-------------
-- Iterate --
-------------
procedure Iterate
(Attachments : List;
Process : not null access procedure (Attachment : Element))
is
-- Use callbacks to avoid Elements copy on iteration
procedure Action (Position : Attachment_Table.Cursor);
------------
-- Action --
------------
procedure Action (Position : Attachment_Table.Cursor) is
begin
Attachment_Table.Query_Element (Position, Process);
end Action;
begin
Attachments.Vector.Iterate (Action'Access);
end Iterate;
----------
-- Kind --
----------
function Kind (Attachment : Element) return Attachment_Kind is
begin
return Attachment.Kind;
end Kind;
------------
-- Length --
------------
function Length
(Attachments : List;
Boundary : String) return Natural
is
L : Natural;
begin
-- The length of all start boundaries, and the 2 characters longer
-- end boundary.
L := (Boundary'Length + 6) * (Count (Attachments) + 1) + 2;
-- Add all content lengths
for J in 1 .. Count (Attachments) loop
L := L + Element (Element'(Get (Attachments, J))).Total_Length;
end loop;
return L;
end Length;
--------------------
-- Local_Filename --
--------------------
function Local_Filename (Attachment : Element) return String is
begin
return To_String (Attachment.Data.Filename);
end Local_Filename;
-----------
-- Reset --
-----------
procedure Reset
(Attachments : in out List;
Delete_Files : Boolean) is
begin
if Delete_Files then
for J in 1 .. Count (Attachments) loop
begin
Directories.Delete_File
(Name => Local_Filename (Element'(Get (Attachments, J))));
exception
when IO_Exceptions.Name_Error | IO_Exceptions.Use_Error =>
null;
end;
end loop;
end if;
Attachment_Table.Clear (Attachments.Vector);
end Reset;
---------------
-- Root_MIME --
---------------
function Root_MIME (Attachments : List) return Root_MIME_Kind is
begin
if Count (Attachments) = 1
and then Kind (Element'(Get (Attachments, 1))) = Alternative
then
return Multipart_Alternative;
else
return Multipart_Mixed;
end if;
end Root_MIME;
----------
-- Send --
----------
procedure Send
(Socket : AWS.Net.Socket_Type'Class;
Attachments : List;
Boundary : String)
is
procedure Send_Attachment (Attachment : Element);
-- Sends one Attachment, including the start boundary
procedure Send_Content (Attachment : Element);
-- Set an in-memory content
procedure Send_Content (Data : Content);
procedure Send_Alternative (Attachment : Element);
-- Send an alternative part
Pref_Suf : constant String := "--";
-- The MIME boundary prefix and suffix
Simple_Alternative : constant Boolean :=
Root_MIME (Attachments) = Multipart_Alternative;
----------------------
-- Send_Alternative --
----------------------
procedure Send_Alternative (Attachment : Element) is
procedure Send_Alternative (Position : Alternative_Table.Cursor);
-- Output the pointed part
A_Boundary : Unbounded_String;
----------------------
-- Send_Alternative --
----------------------
procedure Send_Alternative (Position : Alternative_Table.Cursor) is
Part : constant Content :=
Alternative_Table.Element (Position);
begin
Net.Buffered.Put_Line (Socket, Pref_Suf & To_String (A_Boundary));
Net.Buffered.Put_Line
(Socket, Messages.Content_Type (To_String (Part.Content_Type)));
Net.Buffered.New_Line (Socket);
Send_Content (Part);
end Send_Alternative;
begin
if not Simple_Alternative then
-- This is not the first element, we issue an embedded MIME
-- content.
Net.Buffered.Put_Line (Socket, Pref_Suf & Boundary);
Send_MIME_Header
(Socket, Attachments,
Alternative => True,
Boundary => A_Boundary);
else
A_Boundary := To_Unbounded_String (Boundary);
end if;
Attachment.Parts.Iterate (Send_Alternative'Access);
if not Simple_Alternative then
-- Ends the alternative part
Net.Buffered.New_Line (Socket);
Net.Buffered.Put_Line
(Socket, Pref_Suf & To_String (A_Boundary) & Pref_Suf);
end if;
end Send_Alternative;
---------------------
-- Send_Attachment --
---------------------
procedure Send_Attachment (Attachment : Element) is
begin
case Attachment.Kind is
when Data => Send_Content (Attachment);
when Alternative => Send_Alternative (Attachment);
end case;
end Send_Attachment;
------------------
-- Send_Content --
------------------
procedure Send_Content (Attachment : Element) is
begin
-- Send multipart message start boundary
Net.Buffered.Put_Line (Socket, Pref_Suf & Boundary);
-- Send header
AWS.Headers.Send_Header (Socket, Attachment.Headers);
Net.Buffered.New_Line (Socket);
Send_Content (Attachment.Data);
end Send_Content;
procedure Send_Content (Data : Content) is
procedure Send_File;
procedure Send_Content;
------------------
-- Send_Content --
------------------
procedure Send_Content is
procedure Send;
-- Send standard content
procedure Send_Base64;
-- Send a base64 content
----------
-- Send --
----------
procedure Send is
begin
Net.Buffered.Put_Line (Socket, To_String (Data.Content));
end Send;
-----------------
-- Send_Base64 --
-----------------
procedure Send_Base64 is
Chunk_Size : constant := 60;
Content_Len : constant Positive := Length (Data.Content);
K : Positive := 1;
begin
while K <= Content_Len loop
if K + Chunk_Size - 1 > Content_Len then
Net.Buffered.Put_Line
(Socket,
Slice (Data.Content, K, Content_Len));
K := Content_Len + 1;
else
Net.Buffered.Put_Line
(Socket,
Slice (Data.Content, K, K + Chunk_Size - 1));
K := K + Chunk_Size;
end if;
end loop;
end Send_Base64;
begin
case Data.Encode is
when None => Send;
when Base64 => Send_Base64;
end case;
end Send_Content;
---------------
-- Send_File --
---------------
procedure Send_File is
procedure Send;
-- Send file as-is
procedure Send_Base64;
-- Send file encoded in Base64
File : Streams.Stream_IO.File_Type;
----------
-- Send --
----------
procedure Send is
Buffer : Streams.Stream_Element_Array (1 .. 4_096);
Last : Streams.Stream_Element_Offset;
begin
-- Send file content
while not Streams.Stream_IO.End_Of_File (File) loop
Streams.Stream_IO.Read (File, Buffer, Last);
Net.Buffered.Write (Socket, Buffer (1 .. Last));
end loop;
Net.Buffered.New_Line (Socket);
exception
when Net.Socket_Error =>
-- Properly close the file if needed
if Streams.Stream_IO.Is_Open (File) then
Streams.Stream_IO.Close (File);
end if;
raise;
end Send;
-----------------
-- Send_Base64 --
-----------------
procedure Send_Base64 is
Buffer_Size : constant := 60;
-- Note that this size must be a multiple of 3, this is
-- important to have proper chunk MIME encoding.
Buffer : Streams.Stream_Element_Array (1 .. Buffer_Size);
Last : Streams.Stream_Element_Offset;
begin
while not Streams.Stream_IO.End_Of_File (File) loop
Streams.Stream_IO.Read (File, Buffer, Last);
Net.Buffered.Put_Line
(Socket,
AWS.Translator.Base64_Encode (Buffer (1 .. Last)));
end loop;
end Send_Base64;
begin
Stream_IO.Open
(File, Streams.Stream_IO.In_File, To_String (Data.Filename));
case Data.Encode is
when None => Send;
when Base64 => Send_Base64;
end case;
Stream_IO.Close (File);
end Send_File;
begin
case Data.Kind is
when File => Send_File;
when AWS.Attachments.Data => Send_Content;
end case;
end Send_Content;
begin
-- Send the attachments
for J in 1 .. Integer (Attachments.Vector.Length) loop
Send_Attachment
(Attachment_Table.Element
(Container => Attachments.Vector,
Index => J));
end loop;
-- Send multipart message end boundary
Net.Buffered.Put_Line (Socket, Pref_Suf & Boundary & Pref_Suf);
end Send;
----------------------
-- Send_MIME_Header --
----------------------
procedure Send_MIME_Header
(Socket : Net.Socket_Type'Class;
Attachments : List;
Boundary : out Unbounded_String;
Alternative : Boolean := False)
is
L_Boundary : constant String :=
"----=_NextPart_" & Utils.Random_String (10) & "."
& Utils.Image (UID.Value);
begin
UID.Increment;
Boundary := To_Unbounded_String (L_Boundary);
if Alternative
or else Root_MIME (Attachments) = Multipart_Alternative
then
Net.Buffered.Put_Line
(Socket,
Messages.Content_Type ("multipart/alternative", L_Boundary));
else
Net.Buffered.Put_Line
(Socket,
Messages.Content_Type ("multipart/mixed", L_Boundary));
end if;
Net.Buffered.New_Line (Socket);
end Send_MIME_Header;
----------
-- Data --
----------
function Value
(Data : String;
Name : String := "";
Encode : Encoding := None;
Content_Id : String := "";
Content_Type : String := MIME.Text_Plain) return Content
is
CD : Unbounded_String;
begin
if Encode = Base64 then
CD := +Translator.Base64_Encode (Data);
else
CD := +Data;
end if;
return Content'(Attachments.Data, Data'Length,
Content_Id => +Content_Id,
Content_Type => +Content_Type,
Filename => +Name,
Encode => Encode,
Content => CD);
end Value;
end AWS.Attachments;
|