Debian Codegraph @

deps/minirest/src/minirest.adb

raw
1 with AAA.Processes;
2 with Ada.Strings.Unbounded;
3 with Ada.Integer_Text_IO;
4 with GNAT.OS_Lib;
5
6 package body Minirest is
7
8 package OS renames GNAT.OS_Lib;
9
10 ------------------
11 -- Code_To_Kind --
12 ------------------
13
14 function Code_To_Kind (Code : Integer) return Status_Kinds
15 is (case Code is
16 when 100 .. 199 => Informative,
17 when 200 .. 299 => Success,
18 when 300 .. 399 => Redirect,
19 when 400 .. 499 => Client_Error,
20 when 500 .. 599 => Server_Error,
21 when others => raise Constraint_Error);
22
23 --------------
24 -- Encoding --
25 --------------
26
27 function To_Hex (Char : Character) return String is
28 Hex : String (1 .. 6);
29 begin
30 Ada.Integer_Text_IO.Put (Hex, Character'Pos (Char), Base => 16);
31 return Hex (4 .. 5);
32 end To_Hex;
33
34 function Encoding (Char : Character) return String
35 is (case Char is
36 when '!' | '#' | '$' | '%' | '&' | ''' | '(' | ')' | '*' | '+' |
37 ',' | '/' | ':' | ';' | '=' | '?' | '@' | '[' | ']' | ' '
38 => "%" & To_Hex (Char),
39 when others => (1 => Char));
40
41 ------------
42 -- Encode --
43 ------------
44
45 function Encode (S : String) return String is
46 use Ada.Strings.Unbounded;
47 Result : Unbounded_String;
48 begin
49 for Char of S loop
50 Append (Result, Encoding (Char));
51 end loop;
52
53 return To_String (Result);
54 end Encode;
55
56 -----------
57 -- "and" --
58 -----------
59
60 function "and" (L : Parameters; R : Parameters) return Parameters is
61 begin
62 return Result : Parameters := L do
63 for I in R.Data.Iterate loop
64 Result.Data.Insert (AAA.Strings.Maps.Key (I), R.Data (I));
65 end loop;
66 end return;
67 end "and";
68
69 ---------
70 -- "=" --
71 ---------
72
73 function "=" (Key, Value : String) return Parameters is
74 begin
75 return P : Parameters do
76 P.Data.Insert (Key, Value);
77 end return;
78 end "=";
79
80 ---------
81 -- Get --
82 ---------
83
84 Curl : constant OS.String_Access := OS.Locate_Exec_On_Path ("curl");
85
86 function Get (URL : String;
87 Arguments : Parameters := No_Arguments;
88 Headers : Parameters := No_Arguments)
89 return Response
90 is
91
92 function To_URL_Args (Map : AAA.Strings.Map) return String is
93 use AAA.Strings.Maps;
94 Flat : AAA.Strings.Vector;
95 begin
96 for I in Map.Iterate loop
97 Flat.Append (Encode (Key (I)) & "=" & Encode (Map (I)));
98 end loop;
99
100 return Flat.Flatten ('&');
101 end To_URL_Args;
102
103 Curl_Args : AAA.Strings.Vector :=
104 AAA.Strings
105 .To_Vector ("curl")
106 .Append ("-s")
107 .Append ("-i");
108 begin
109 if Curl in null then
110 raise Rest_Error with "Could not find 'curl' tool in path";
111 end if;
112
113 -- Add request headers
114
115 for I in Headers.Data.Iterate loop
116 Curl_Args.Append ("-H");
117 Curl_Args.Append (AAA.Strings.Maps.Key (I) & ": " & Headers.Data (I));
118 end loop;
119
120 declare
121 Raw : constant AAA.Processes.Result :=
122 AAA.Processes.Run
123 (Curl_Args
124 .Append
125 (URL
126 & (if Arguments.Data.Is_Empty
127 then ""
128 elsif (for some C of URL => C = '?')
129 then "&"
130 else "?")
131 & To_URL_Args (Arguments.Data)),
132 Raise_On_Error => False);
133 begin
134 if Raw.Exit_Code /= 0 then
135 raise Rest_Error with
136 "curl exited with non-zero error code:" & Raw.Exit_Code'Image;
137 end if;
138
139 declare
140 Status_Line : constant String := Raw.Output.First_Element;
141 Code : Integer := -1;
142 In_Headers : Boolean := True;
143 Skip : Boolean := False;
144 begin
145
146 -- Identify code
147
148 for I in Status_Line'Range loop
149 if Status_Line (I) = ' ' then
150 Code := Integer'Value (Status_Line (I + 1 .. I + 4));
151 exit;
152 end if;
153 end loop;
154
155 if Code = -1 then
156 raise Rest_Error with "Malformed status line: " & Status_Line;
157 end if;
158
159 -- Fill response
160
161 return R : Response (Code_To_Kind (Code), Status_Line'Length) do
162 R.Status_Line := Status_Line;
163 R.Status_Code := Code;
164
165 for I in Raw.Output.First_Index + 1 ..
166 Raw.Output.Last_Index
167 loop
168 declare
169 Line : constant String := Raw.Output (I);
170 begin
171 if In_Headers and then Line = "" then
172 In_Headers := False;
173 Skip := True;
174 end if;
175
176 if In_Headers then
177 R.Raw_Headers.Append (Line);
178 R.Headers.Insert (AAA.Strings.Head (Line, ':'),
179 AAA.Strings.Trim
180 (AAA.Strings.Tail (Line, ':')));
181 elsif Skip then
182 Skip := False;
183 else
184 R.Content.Append (Line);
185 end if;
186 end;
187 end loop;
188 end return;
189 end;
190 end;
191 end Get;
192
193 end Minirest;