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
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319 | ------------------------------------------------------------------------------
-- Ada Web Server --
-- --
-- Copyright (C) 2000-2021, AdaCore --
-- --
-- This library 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 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. --
-- --
-- As a special exception under Section 7 of GPL version 3, you are --
-- granted additional permissions described in the GCC Runtime Library --
-- Exception, version 3.1, as published by the Free Software Foundation. --
-- --
-- You should have received a copy of the GNU General Public License and --
-- a copy of the GCC Runtime Library Exception along with this program; --
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
-- <http://www.gnu.org/licenses/>. --
-- --
-- As a special exception, if other files instantiate generics from this --
-- unit, or you link this unit with other files to produce an executable, --
-- this unit does not by itself cause the resulting executable to be --
-- covered by the GNU General Public License. This exception does not --
-- however invalidate any other reasons why the executable file might be --
-- covered by the GNU Public License. --
------------------------------------------------------------------------------
pragma Ada_2012;
with Ada.Strings.Fixed;
with Ada.Text_IO;
with AWS.Client.HTTP_Utils;
with AWS.Messages;
with AWS.MIME;
with AWS.Net.Buffered;
with AWS.Net.SSL;
with AWS.Translator;
with AWS.URL.Set;
package body AWS.Client is
use AWS.Client.HTTP_Utils;
------------------------
-- Cipher_Description --
------------------------
function Cipher_Description (Connection : HTTP_Connection) return String is
use type AWS.Net.Socket_Access;
begin
if Connection.Socket = null then
return "";
end if;
return Connection.Socket.Cipher_Description;
end Cipher_Description;
-----------------------
-- Clear_SSL_Session --
-----------------------
procedure Clear_SSL_Session (Connection : in out HTTP_Connection) is
begin
Net.SSL.Free (Connection.SSL_Session);
end Clear_SSL_Session;
-----------
-- Close --
-----------
procedure Close (Connection : in out HTTP_Connection) is
begin
Disconnect (Connection);
if Connection.Default_SSL_Config then
Net.SSL.Release (Connection.SSL_Config);
end if;
if ZLib.Is_Open (Connection.Decode_Filter) then
ZLib.Close (Connection.Decode_Filter, Ignore_Error => True);
end if;
Utils.Unchecked_Free (Connection.Decode_Buffer);
Net.SSL.Free (Connection.SSL_Session);
end Close;
---------------------
-- Connect_Timeout --
---------------------
function Connect_Timeout (T : Timeouts_Values) return Duration is
begin
return T.Connect;
end Connect_Timeout;
-----------------
-- Copy_Cookie --
-----------------
procedure Copy_Cookie
(Source : HTTP_Connection;
Destination : in out HTTP_Connection) is
begin
Destination.Cookie := Source.Cookie;
end Copy_Cookie;
------------
-- Create --
------------
function Create
(Host : String;
User : String := No_Data;
Pwd : String := No_Data;
Proxy : String := No_Data;
Proxy_User : String := No_Data;
Proxy_Pwd : String := No_Data;
Retry : Natural := Retry_Default;
Persistent : Boolean := True;
Timeouts : Timeouts_Values := No_Timeout;
Server_Push : Boolean := False;
Certificate : String := Default.Client_Certificate;
User_Agent : String := Default.User_Agent;
HTTP_Version : HTTP_Protocol := HTTP_Default)
return HTTP_Connection is
begin
return Connection : HTTP_Connection do
Create (Connection, Host,
User, Pwd,
Proxy, Proxy_User, Proxy_Pwd,
Retry, Persistent, Timeouts,
Server_Push,
Net.SSL.Null_Config, Certificate, User_Agent, HTTP_Version);
end return;
end Create;
procedure Create
(Connection : in out HTTP_Connection;
Host : String;
User : String := No_Data;
Pwd : String := No_Data;
Proxy : String := No_Data;
Proxy_User : String := No_Data;
Proxy_Pwd : String := No_Data;
Retry : Natural := Retry_Default;
Persistent : Boolean := True;
Timeouts : Timeouts_Values := No_Timeout;
Server_Push : Boolean := False;
SSL_Config : Net.SSL.Config := Net.SSL.Null_Config;
Certificate : String := Default.Client_Certificate;
User_Agent : String := Default.User_Agent;
HTTP_Version : HTTP_Protocol := HTTP_Default)
is
use type Net.SSL.Config;
Host_URL : constant AWS.URL.Object := AWS.URL.Parse (Host);
Proxy_URL : constant AWS.URL.Object := AWS.URL.Parse (Proxy);
Connect_URL : AWS.URL.Object;
begin
-- If there is a proxy, the host to connect to is the proxy otherwise
-- we connect to the Web server.
if URL.Host (Host_URL) = "" then
raise URL.URL_Error with "wrong host specified or missing protocol.";
end if;
if Proxy = No_Data then
Connect_URL := Host_URL;
else
Connect_URL := Proxy_URL;
end if;
Connection.Host := To_Unbounded_String (Host);
Connection.Host_URL := Host_URL;
Connection.Connect_URL := Connect_URL;
Connection.Auth (WWW).User := Value (User);
Connection.Auth (WWW).Pwd := Value (Pwd);
Connection.Proxy := Value (Proxy);
Connection.Proxy_URL := Proxy_URL;
Connection.Auth (Client.Proxy).User := Value (Proxy_User);
Connection.Auth (Client.Proxy).Pwd := Value (Proxy_Pwd);
Connection.Retry := Create.Retry;
Connection.Persistent := Persistent;
Connection.Streaming := Server_Push;
Connection.Certificate := To_Unbounded_String (Certificate);
Connection.Timeouts := Timeouts;
Connection.Config := AWS.Config.Get_Current;
Connection.H2_Preface_Sent := False;
Connection.H2_Stream_Id := 1;
-- Server push is not supported in HTTPv2
Connection.HTTP_Version := (if Server_Push then HTTPv1
else HTTP_Version);
Connection.User_Agent := To_Unbounded_String (User_Agent);
-- If we have set the proxy or standard authentication we must set the
-- authentication mode to Basic.
if Proxy_User /= No_Data then
Connection.Auth (Client.Proxy).Work_Mode := Basic;
end if;
if User /= No_Data then
Connection.Auth (WWW).Work_Mode := Basic;
end if;
if URL.Security (Host_URL) then
-- This is a secure connection, initialize the SSL layer
Connection.Default_SSL_Config := SSL_Config = Net.SSL.Null_Config;
if Connection.Default_SSL_Config then
Net.SSL.Initialize
(Connection.SSL_Config, Certificate, Net.SSL.TLS_Client);
else
Connection.SSL_Config := SSL_Config;
end if;
if Connection.HTTP_Version = HTTPv2 then
Net.SSL.ALPN_Include (Connection.SSL_Config, Messages.H2_Token);
end if;
end if;
if Persistent and then Connection.Retry = 0 then
-- In this case the connection termination can be initiated by the
-- server or the client after a period. So the connection could be
-- closed while trying to get some data from the server. To be nicer
-- from user's point of view just make sure we retry at least one
-- time before reporting an error.
Connection.Retry := 1;
end if;
if Connection.HTTP_Version = HTTPv2 then
Connection.F_Headers.Names_Lowercased (True);
end if;
end Create;
---------------------
-- Debug_Exception --
---------------------
procedure Debug_Exception (E : Ada.Exceptions.Exception_Occurrence) is
begin
Debug_Message ("! ", Ada.Exceptions.Exception_Message (E));
end Debug_Exception;
-------------------
-- Debug_Message --
-------------------
procedure Debug_Message (Prefix, Message : String) is
begin
if Debug_On then
Text_IO.Put_Line (Prefix & Message);
end if;
end Debug_Message;
------------
-- Delete --
------------
function Delete
(URL : String;
Data : String;
User : String := No_Data;
Pwd : String := No_Data;
Proxy : String := No_Data;
Proxy_User : String := No_Data;
Proxy_Pwd : String := No_Data;
Timeouts : Timeouts_Values := No_Timeout;
Headers : Header_List := Empty_Header_List;
User_Agent : String := Default.User_Agent;
HTTP_Version : HTTP_Protocol := HTTP_Default) return Response.Data
is
begin
return Delete
(URL, Translator.To_Stream_Element_Array (Data),
User, Pwd, Proxy, Proxy_User, Proxy_Pwd, Timeouts,
Headers, User_Agent, HTTP_Version);
end Delete;
function Delete
(URL : String;
Data : Stream_Element_Array;
User : String := No_Data;
Pwd : String := No_Data;
Proxy : String := No_Data;
Proxy_User : String := No_Data;
Proxy_Pwd : String := No_Data;
Timeouts : Timeouts_Values := No_Timeout;
Headers : Header_List := Empty_Header_List;
User_Agent : String := Default.User_Agent;
HTTP_Version : HTTP_Protocol := HTTP_Default) return Response.Data
is
Connection : HTTP_Connection;
Result : Response.Data;
begin
Create (Connection,
URL, User, Pwd, Proxy, Proxy_User, Proxy_Pwd,
Persistent => False,
Timeouts => Timeouts,
User_Agent => User_Agent,
HTTP_Version => HTTP_Version);
Delete (Connection, Result, Data, Headers => Headers);
Close (Connection);
return Result;
exception
when others =>
Close (Connection);
raise;
end Delete;
procedure Delete
(Connection : in out HTTP_Connection;
Result : out Response.Data;
Data : Stream_Element_Array;
URI : String := No_Data;
Headers : Header_List := Empty_Header_List) is
begin
Connection.F_Headers.Reset;
Send_Request
(Connection, HTTP_Utils.DELETE, Result, URI, Data, Headers);
end Delete;
procedure Delete
(Connection : in out HTTP_Connection;
Result : out Response.Data;
Data : String;
URI : String := No_Data;
Headers : Header_List := Empty_Header_List) is
begin
Connection.F_Headers.Reset;
Delete
(Connection => Connection,
Result => Result,
Data => Translator.To_Stream_Element_Array (Data),
URI => URI,
Headers => Headers);
end Delete;
----------------------
-- Error_Processing --
----------------------
procedure Error_Processing
(Connection : in out HTTP_Connection;
Try_Count : in out Natural;
Result : out Response.Data;
Context : String;
E : Ada.Exceptions.Exception_Occurrence;
Stamp : Ada.Real_Time.Time)
is
use Real_Time;
Message : constant String := Exception_Information (E);
begin
Debug_Exception (E);
if Real_Time.Clock - Stamp >= Connection.Timeouts.Response
or else Net.Is_Timeout (Connection.Socket.all, E)
then
Result := Response.Build
(MIME.Text_Plain, Context & " Timeout", Messages.S408);
elsif Try_Count = 0 then
Result := Response.Build
(MIME.Text_Plain,
Context & " request error. " & Message,
Messages.S400);
else
Result := Response.Empty;
Try_Count := Try_Count - 1;
end if;
Disconnect (Connection);
end Error_Processing;
--------------
-- Finalize --
--------------
overriding procedure Finalize (Connection : in out HTTP_Connection) is
begin
Close (Connection);
end Finalize;
---------
-- Get --
---------
function Get
(URL : String;
User : String := No_Data;
Pwd : String := No_Data;
Proxy : String := No_Data;
Proxy_User : String := No_Data;
Proxy_Pwd : String := No_Data;
Timeouts : Timeouts_Values := No_Timeout;
Data_Range : Content_Range := No_Range;
Follow_Redirection : Boolean := False;
Certificate : String := Default.Client_Certificate;
Headers : Header_List := Empty_Header_List;
User_Agent : String := Default.User_Agent;
HTTP_Version : HTTP_Protocol := HTTP_Default)
return Response.Data
is
use type Messages.Status_Code;
Result : Response.Data;
Host_URL : AWS.URL.Object;
begin
declare
Connection : HTTP_Connection;
begin
Create (Connection,
URL, User, Pwd, Proxy, Proxy_User, Proxy_Pwd,
Persistent => False,
Certificate => Certificate,
Timeouts => Timeouts,
User_Agent => User_Agent,
HTTP_Version => HTTP_Version);
Host_URL := Connection.Host_URL; -- For the redirection case
Get (Connection, Result,
Data_Range => Data_Range, Headers => Headers);
Close (Connection);
exception
when others =>
Close (Connection);
raise;
end;
declare
SC : constant Messages.Status_Code := Response.Status_Code (Result);
begin
if Follow_Redirection and then SC = Messages.S305 then
-- This is "Use Proxy" message, Location point to the proxy to
-- use. We do not have the login/password for the proxy.
return Get
(URL, User, Pwd, Response.Location (Result),
Timeouts => Timeouts,
Follow_Redirection => Follow_Redirection,
Certificate => Certificate,
Headers => Headers,
User_Agent => User_Agent);
elsif Follow_Redirection
and then SC in Messages.Redirection
and then SC /= Messages.S300 -- multiple choices
and then SC /= Messages.S304 -- not modified, no redirection
then
declare
use AWS.URL;
Location : AWS.URL.Object :=
AWS.URL.Parse (Response.Location (Result));
begin
if Host (Location) = "" then
Set.Connection_Data
(Location,
Host (Host_URL),
Port (Host_URL),
Security (Host_URL));
end if;
return Get
(AWS.URL.URL (Location), User, Pwd,
Proxy, Proxy_User, Proxy_Pwd, Timeouts,
Data_Range, Follow_Redirection,
Certificate => Certificate,
Headers => Headers,
User_Agent => User_Agent);
end;
else
return Result;
end if;
end;
end Get;
---------
-- Get --
---------
procedure Get
(Connection : in out HTTP_Connection;
Result : out Response.Data;
URI : String := No_Data;
Data_Range : Content_Range := No_Range;
Headers : Header_List := Empty_Header_List) is
begin
Connection.Data_Range := Data_Range;
Connection.F_Headers.Reset;
Send_Request
(Connection, HTTP_Utils.GET, Result, URI, HTTP_Utils.No_Data, Headers);
end Get;
---------------------
-- Get_Certificate --
---------------------
function Get_Certificate
(Connection : HTTP_Connection) return Net.SSL.Certificate.Object is
begin
if not Connection.Opened then
-- SSL socket have to be created to get certificate
Connect (Connection.Self.all);
end if;
if Connection.Socket.Is_Secure then
return Net.SSL.Certificate.Get
(Net.SSL.Socket_Type (Connection.Socket.all));
else
return Net.SSL.Certificate.Undefined;
end if;
end Get_Certificate;
----------------
-- Get_Cookie --
----------------
function Get_Cookie (Connection : HTTP_Connection) return String is
begin
return To_String (Connection.Cookie);
end Get_Cookie;
----------
-- Head --
----------
function Head
(URL : String;
User : String := No_Data;
Pwd : String := No_Data;
Proxy : String := No_Data;
Proxy_User : String := No_Data;
Proxy_Pwd : String := No_Data;
Timeouts : Timeouts_Values := No_Timeout;
Headers : Header_List := Empty_Header_List;
User_Agent : String := Default.User_Agent;
HTTP_Version : HTTP_Protocol := HTTP_Default)
return Response.Data
is
Connection : HTTP_Connection;
Result : Response.Data;
begin
Create (Connection,
URL, User, Pwd, Proxy, Proxy_User, Proxy_Pwd,
Persistent => False,
Timeouts => Timeouts,
User_Agent => User_Agent,
HTTP_Version => HTTP_Version);
Head (Connection, Result, Headers => Headers);
Close (Connection);
return Result;
exception
when others =>
Close (Connection);
raise;
end Head;
----------
-- Head --
----------
procedure Head
(Connection : in out HTTP_Connection;
Result : out Response.Data;
URI : String := No_Data;
Headers : Header_List := Empty_Header_List) is
begin
Connection.F_Headers.Reset;
Send_Request
(Connection, HTTP_Utils.HEAD,
Result, URI, HTTP_Utils.No_Data, Headers);
end Head;
----------
-- Host --
----------
function Host (Connection : HTTP_Connection) return String is
begin
return To_String (Connection.Host);
end Host;
----------
-- Post --
----------
function Post
(URL : String;
Data : String;
Content_Type : String := No_Data;
User : String := No_Data;
Pwd : String := No_Data;
Proxy : String := No_Data;
Proxy_User : String := No_Data;
Proxy_Pwd : String := No_Data;
Timeouts : Timeouts_Values := No_Timeout;
Attachments : Attachment_List := Empty_Attachment_List;
Headers : Header_List := Empty_Header_List;
User_Agent : String := Default.User_Agent;
HTTP_Version : HTTP_Protocol := HTTP_Default)
return Response.Data
is
Connection : HTTP_Connection;
Result : Response.Data;
begin
Create (Connection,
URL, User, Pwd, Proxy, Proxy_User, Proxy_Pwd,
Persistent => False,
Timeouts => Timeouts,
User_Agent => User_Agent,
HTTP_Version => HTTP_Version);
Post (Connection, Result, Data, Content_Type,
Attachments => Attachments,
Headers => Headers);
Close (Connection);
return Result;
exception
when others =>
Close (Connection);
raise;
end Post;
function Post
(URL : String;
Data : Stream_Element_Array;
Content_Type : String := No_Data;
User : String := No_Data;
Pwd : String := No_Data;
Proxy : String := No_Data;
Proxy_User : String := No_Data;
Proxy_Pwd : String := No_Data;
Timeouts : Timeouts_Values := No_Timeout;
Attachments : Attachment_List := Empty_Attachment_List;
Headers : Header_List := Empty_Header_List;
User_Agent : String := Default.User_Agent;
HTTP_Version : HTTP_Protocol := HTTP_Default)
return Response.Data
is
Connection : HTTP_Connection;
Result : Response.Data;
begin
Create (Connection,
URL, User, Pwd, Proxy, Proxy_User, Proxy_Pwd,
Persistent => False,
Timeouts => Timeouts,
User_Agent => User_Agent,
HTTP_Version => HTTP_Version);
Post (Connection, Result, Data, Content_Type,
Attachments => Attachments,
Headers => Headers);
Close (Connection);
return Result;
exception
when others =>
Close (Connection);
raise;
end Post;
procedure Post
(Connection : in out HTTP_Connection;
Result : out Response.Data;
Data : Stream_Element_Array;
Content_Type : String := No_Data;
URI : String := No_Data;
Attachments : Attachment_List := Empty_Attachment_List;
Headers : Header_List := Empty_Header_List) is
begin
Connection.F_Headers.Reset;
Internal_Post
(Connection,
Result,
Data,
URI,
SOAPAction => No_Data,
Content_Type => (if Content_Type = No_Data
then MIME.Application_Octet_Stream
else Content_Type),
Attachments => Attachments,
Headers => Headers);
end Post;
procedure Post
(Connection : in out HTTP_Connection;
Result : out Response.Data;
Data : String;
Content_Type : String := No_Data;
URI : String := No_Data;
Attachments : Attachment_List := Empty_Attachment_List;
Headers : Header_List := Empty_Header_List) is
begin
Connection.F_Headers.Reset;
-- For message larger than 20kb the conversion to Stream_Element_Array
-- may create a stack overflow.
if Data'Length > 20 * 1024 then
declare
SEA : Utils.Stream_Element_Array_Access :=
Translator.To_Stream_Element_Array (Data);
begin
Internal_Post
(Connection,
Result,
SEA.all,
URI,
SOAPAction => No_Data,
Content_Type => (if Content_Type = No_Data
then MIME.Application_Form_Data
else Content_Type),
Attachments => Attachments,
Headers => Headers);
Utils.Unchecked_Free (SEA);
exception
when others =>
Utils.Unchecked_Free (SEA);
raise;
end;
else
Internal_Post
(Connection,
Result,
Translator.To_Stream_Element_Array (Data),
URI,
SOAPAction => No_Data,
Content_Type => (if Content_Type = No_Data
then MIME.Application_Form_Data
else Content_Type),
Attachments => Attachments,
Headers => Headers);
end if;
end Post;
---------
-- Put --
---------
function Put
(URL : String;
Data : String;
User : String := No_Data;
Pwd : String := No_Data;
Proxy : String := No_Data;
Proxy_User : String := No_Data;
Proxy_Pwd : String := No_Data;
Timeouts : Timeouts_Values := No_Timeout;
Headers : Header_List := Empty_Header_List;
User_Agent : String := Default.User_Agent;
HTTP_Version : HTTP_Protocol := HTTP_Default) return Response.Data
is
Connection : HTTP_Connection;
Result : Response.Data;
begin
Create (Connection,
URL, User, Pwd, Proxy, Proxy_User, Proxy_Pwd,
Persistent => False,
Timeouts => Timeouts,
User_Agent => User_Agent,
HTTP_Version => HTTP_Version);
Put (Connection, Result, Data, Headers => Headers);
Close (Connection);
return Result;
exception
when others =>
Close (Connection);
raise;
end Put;
---------
-- Put --
---------
procedure Put
(Connection : in out HTTP_Connection;
Result : out Response.Data;
Data : Stream_Element_Array;
URI : String := No_Data;
Headers : Header_List := Empty_Header_List) is
begin
Connection.F_Headers.Reset;
Send_Request (Connection, HTTP_Utils.PUT, Result, URI, Data, Headers);
end Put;
procedure Put
(Connection : in out HTTP_Connection;
Result : out Response.Data;
Data : String;
URI : String := No_Data;
Headers : Header_List := Empty_Header_List) is
begin
Put
(Connection => Connection,
Result => Result,
Data => Translator.To_Stream_Element_Array (Data),
URI => URI,
Headers => Headers);
end Put;
----------
-- Read --
----------
procedure Read
(Connection : in out HTTP_Connection;
Data : out Stream_Element_Array;
Last : out Stream_Element_Offset)
is
First : Stream_Element_Offset := Data'First;
begin
loop
Read_Some (Connection, Data (First .. Data'Last), Last);
exit when Last = Data'Last or else Last < First;
First := Last + 1;
end loop;
end Read;
---------------
-- Read_Some --
---------------
procedure Read_Some
(Connection : in out HTTP_Connection;
Data : out Stream_Element_Array;
Last : out Stream_Element_Offset)
is
procedure Read_Internal
(Data : out Stream_Element_Array;
Last : out Stream_Element_Offset);
-- Read the encoded data as is from HTTP connection
-------------------
-- Read_Internal --
-------------------
procedure Read_Internal
(Data : out Stream_Element_Array;
Last : out Stream_Element_Offset)
is
Sock : Net.Socket_Type'Class renames Connection.Socket.all;
procedure Skip_Line;
-- Skip a line in the sock stream
procedure Read_Limited;
-- Read Connection.Length characters if it can be contained in Data
-- buffer otherwise just fill the remaining space in Data.
------------------
-- Read_Limited --
------------------
procedure Read_Limited is
Limit : constant Stream_Element_Offset :=
Stream_Element_Offset'Min
(Data'Last,
Data'First
+ Stream_Element_Offset (Connection.Length) - 1);
begin
Net.Buffered.Read (Sock, Data (Data'First .. Limit), Last);
Connection.Length := Connection.Length - (Last - Data'First + 1);
end Read_Limited;
---------------
-- Skip_Line --
---------------
procedure Skip_Line is
D : constant String := Net.Buffered.Get_Line (Sock)
with Warnings => Off;
begin
null;
end Skip_Line;
begin
case Connection.Transfer is
when End_Response =>
Last := Data'First - 1;
when Until_Close =>
begin
Net.Buffered.Read (Sock, Data, Last);
exception
when E : Net.Socket_Error =>
Debug_Exception (E);
Connection.Transfer := End_Response;
Last := Data'First - 1;
end;
when Content_Length =>
if Connection.Length = 0 then
Connection.Transfer := End_Response;
Last := Data'First - 1;
return;
end if;
Read_Limited;
when Chunked =>
if Connection.Length = 0 then
Connection.Length :=
Response.Content_Length_Type
(Utils.Hex_Value
(Strings.Fixed.Trim
(Net.Buffered.Get_Line (Sock), Strings.Both)));
if Connection.Length = 0 then
Skip_Line;
Connection.Transfer := End_Response;
Last := Data'First - 1;
return;
end if;
end if;
Read_Limited;
if Connection.Length = 0 then
Skip_Line;
end if;
when None =>
raise Constraint_Error;
end case;
end Read_Internal;
begin
if ZLib.Is_Open (Connection.Decode_Filter) then
declare
procedure Read is new ZLib.Read
(Read_Internal, Connection.Decode_Buffer.all,
Connection.Decode_First, Connection.Decode_Last,
Allow_Read_Some => True);
-- Decompress gzip or deflate encoded data
begin
Read (Connection.Decode_Filter, Data, Last);
end;
if Last < Data'First and then Connection.Transfer = Chunked then
-- When the 4 byte check sum is in the last chunk
-- external routines could think that data is over,
-- and would not call Read_Some any more. We have to
-- read the last chunk of a chunked stream.
Read_Internal (Data, Last);
if Data'First <= Last
or else Connection.Transfer /= End_Response
then
raise Protocol_Error;
end if;
end if;
else
Read_Internal (Data, Last);
end if;
end Read_Some;
----------------
-- Read_Until --
----------------
function Read_Until
(Connection : HTTP_Connection;
Delimiter : String;
Wait : Boolean := True) return String is
begin
Net.Set_Timeout (Connection.Socket.all, Connection.Timeouts.Receive);
return Translator.To_String
(Net.Buffered.Read_Until
(Connection.Socket.all,
Translator.To_Stream_Element_Array (Delimiter),
Wait));
end Read_Until;
procedure Read_Until
(Connection : in out HTTP_Connection;
Delimiter : String;
Result : in out Ada.Strings.Unbounded.Unbounded_String;
Wait : Boolean := True) is
begin
Result := Ada.Strings.Unbounded.To_Unbounded_String
(Read_Until (Connection, Delimiter, Wait));
end Read_Until;
---------------------
-- Receive_Timeout --
---------------------
function Receive_Timeout (T : Timeouts_Values) return Duration is
begin
return T.Receive;
end Receive_Timeout;
----------------------
-- Response_Timeout --
----------------------
function Response_Timeout (T : Timeouts_Values) return Duration is
begin
return Ada.Real_Time.To_Duration (T.Response);
end Response_Timeout;
------------------
-- Send_Timeout --
------------------
function Send_Timeout (T : Timeouts_Values) return Duration is
begin
return T.Send;
end Send_Timeout;
----------------
-- Set_Cookie --
----------------
procedure Set_Cookie
(Connection : in out HTTP_Connection; Cookie : String) is
begin
Connection.Cookie := To_Unbounded_String (Cookie);
end Set_Cookie;
---------------
-- Set_Debug --
---------------
procedure Set_Debug (On : Boolean) is
begin
Debug_On := On;
AWS.Headers.Debug (On);
end Set_Debug;
-----------------
-- Set_Headers --
-----------------
procedure Set_Headers
(Connection : in out HTTP_Connection; Headers : Header_List) is
begin
Connection.C_Headers := Headers;
end Set_Headers;
--------------------
-- Set_Persistent --
--------------------
procedure Set_Persistent
(Connection : in out HTTP_Connection; Value : Boolean) is
begin
Connection.Persistent := Value;
end Set_Persistent;
------------------------------
-- Set_Proxy_Authentication --
------------------------------
procedure Set_Proxy_Authentication
(Connection : in out HTTP_Connection;
User : String;
Pwd : String;
Mode : Authentication_Mode) is
begin
Set_Authentication
(Auth => Connection.Auth (Proxy),
User => User,
Pwd => Pwd,
Mode => Mode);
end Set_Proxy_Authentication;
---------------
-- Set_Retry --
---------------
procedure Set_Retry
(Connection : in out HTTP_Connection; Value : Natural) is
begin
Connection.Retry := Value;
end Set_Retry;
--------------------------
-- Set_Streaming_Output --
--------------------------
procedure Set_Streaming_Output
(Connection : in out HTTP_Connection;
Value : Boolean) is
begin
Connection.Streaming := Value;
end Set_Streaming_Output;
----------------------------
-- Set_WWW_Authentication --
----------------------------
procedure Set_WWW_Authentication
(Connection : in out HTTP_Connection;
User : String;
Pwd : String;
Mode : Authentication_Mode) is
begin
Set_Authentication
(Auth => Connection.Auth (WWW),
User => User,
Pwd => Pwd,
Mode => Mode);
end Set_WWW_Authentication;
---------------
-- SOAP_Post --
---------------
function SOAP_Post
(URL : String;
Data : String;
SOAPAction : String;
User : String := No_Data;
Pwd : String := No_Data;
Proxy : String := No_Data;
Proxy_User : String := No_Data;
Proxy_Pwd : String := No_Data;
Timeouts : Timeouts_Values := No_Timeout;
Attachments : Attachment_List := Empty_Attachment_List;
Headers : Header_List := Empty_Header_List;
User_Agent : String := Default.User_Agent;
HTTP_Version : HTTP_Protocol := HTTP_Default) return Response.Data
is
Connection : HTTP_Connection;
Result : Response.Data;
begin
Create (Connection,
URL, User, Pwd, Proxy, Proxy_User, Proxy_Pwd,
Persistent => False,
Timeouts => Timeouts,
User_Agent => User_Agent,
HTTP_Version => HTTP_Version);
SOAP_Post (Connection, Result, SOAPAction,
Data => Data,
Attachments => Attachments,
Headers => Headers);
Close (Connection);
return Result;
exception
when others =>
Close (Connection);
raise;
end SOAP_Post;
procedure SOAP_Post
(Connection : HTTP_Connection;
Result : out Response.Data;
SOAPAction : String;
Data : String;
Streaming : Boolean := False;
Attachments : Attachment_List := Empty_Attachment_List;
Headers : Header_List := Empty_Header_List)
is
Saved_Streaming : constant Boolean := Connection.Streaming;
begin
Connection.Self.Streaming := Streaming;
-- For large payload (> 100kb) we use a slow version but safe
-- version of To_Stream_Element_Array which uses the heap.
if Data'Length > 100 * 1_024 then
declare
SEA_Data : Utils.Stream_Element_Array_Access :=
AWS.Translator.To_Stream_Element_Array (Data);
begin
Internal_Post
(Connection => Connection.Self.all,
Result => Result,
Data => SEA_Data.all,
URI => No_Data,
SOAPAction => SOAPAction,
Content_Type => MIME.Text_XML,
Attachments => Attachments,
Headers => Headers);
Utils.Unchecked_Free (SEA_Data);
exception
when others =>
Utils.Unchecked_Free (SEA_Data);
end;
else
Internal_Post
(Connection => Connection.Self.all,
Result => Result,
Data => AWS.Translator.To_Stream_Element_Array (Data),
URI => No_Data,
SOAPAction => SOAPAction,
Content_Type => MIME.Text_XML,
Attachments => Attachments,
Headers => Headers);
end if;
Connection.Self.Streaming := Saved_Streaming;
end SOAP_Post;
--------------------
-- SSL_Session_Id --
--------------------
function SSL_Session_Id (Connection : HTTP_Connection) return String is
begin
return Net.SSL.Session_Id_Image (Connection.SSL_Session);
end SSL_Session_Id;
--------------
-- Timeouts --
--------------
function Timeouts
(Connect : Duration := Net.Forever;
Send : Duration := Net.Forever;
Receive : Duration := Net.Forever;
Response : Duration := Net.Forever) return Timeouts_Values is
begin
return (Connect => Connect,
Send => Send,
Receive => Receive,
Response => Ada.Real_Time.To_Time_Span (Response));
end Timeouts;
function Timeouts (Each : Duration) return Timeouts_Values is
begin
return (Response => Ada.Real_Time.To_Time_Span (Each), others => Each);
end Timeouts;
------------
-- Upload --
------------
procedure Upload
(Connection : in out HTTP_Connection;
Result : out Response.Data;
Filename : String;
URI : String := No_Data;
Headers : Header_List := Empty_Header_List;
Progress : access procedure
(Total, Sent : Stream_Element_Offset) := null) is
begin
Internal_Upload (Connection, Result, Filename, URI, Headers, Progress);
end Upload;
function Upload
(URL : String;
Filename : String;
User : String := No_Data;
Pwd : String := No_Data;
Proxy : String := No_Data;
Proxy_User : String := No_Data;
Proxy_Pwd : String := No_Data;
Timeouts : Timeouts_Values := No_Timeout;
Headers : Header_List := Empty_Header_List;
Progress : access procedure
(Total, Sent : Stream_Element_Offset) := null;
User_Agent : String := Default.User_Agent;
HTTP_Version : HTTP_Protocol := HTTP_Default)
return Response.Data
is
Connection : HTTP_Connection;
Result : Response.Data;
begin
Create (Connection,
URL, User, Pwd, Proxy, Proxy_User, Proxy_Pwd,
Persistent => False,
Timeouts => Timeouts,
User_Agent => User_Agent,
HTTP_Version => HTTP_Version);
Upload
(Connection, Result, Filename,
Headers => Headers, Progress => Progress);
Close (Connection);
return Result;
exception
when others =>
Close (Connection);
raise;
end Upload;
end AWS.Client;
|