/usr/share/ada/adainclude/aws/aws-net-buffered.adb is in libaws3.3.2.2-dev 17.2.2017-2.
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 | ------------------------------------------------------------------------------
-- Ada Web Server --
-- --
-- Copyright (C) 2002-2015, 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. --
-- --
-- --
-- --
-- --
-- --
-- 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/>. --
-- --
-- --
-- --
-- --
-- --
-- --
-- --
------------------------------------------------------------------------------
pragma Ada_2012;
with AWS.Containers.Memory_Streams;
with AWS.Default;
with AWS.Translator;
package body AWS.Net.Buffered is
CRLF : constant Stream_Element_Array :=
Translator.To_Stream_Element_Array (ASCII.CR & ASCII.LF);
Input_Limit : Positive := AWS.Default.Input_Line_Size_Limit with Atomic;
function Is_Empty
(C : not null access constant Read_Cache) return Boolean
is
(C.First > C.Last) with Inline_Always;
-- Returns true if the read-cache is empty
procedure Read (Socket : Socket_Type'Class)
with Pre => Socket.C.R_Cache /= null;
-- Refill the read-cache
function Get_Read_Cache
(Socket : Socket_Type'Class)
return not null access Read_Cache with Inline;
-- Get the socket read cache, create the cache if it is not created
function Get_Write_Cache
(Socket : Socket_Type'Class)
return not null access Write_Cache with Inline;
-- Get the socket write cache, create the cache if it is not created
-----------
-- Flush --
-----------
procedure Flush (Socket : Socket_Type'Class) is
C : Write_Cache_Access renames Socket.C.W_Cache;
begin
if C = null then
return;
end if;
if C.Last > 0 then
Send (Socket, C.Buffer (1 .. C.Last));
C.Last := 0;
end if;
end Flush;
--------------
-- Get_Char --
--------------
function Get_Char (Socket : Socket_Type'Class) return Character is
C : constant not null access Read_Cache := Get_Read_Cache (Socket);
Char : Character;
begin
if Is_Empty (C) then
Read (Socket);
end if;
Char := Character'Val (C.Buffer (C.First));
C.First := C.First + 1;
return Char;
end Get_Char;
---------------------
-- Get_Input_Limit --
---------------------
function Get_Input_Limit return Stream_Element_Offset is
begin
return Stream_Element_Offset (Input_Limit);
end Get_Input_Limit;
--------------
-- Get_Line --
--------------
function Get_Line (Socket : Socket_Type'Class) return String is
Line : constant String := Read_Until (Socket, "" & ASCII.LF, True);
begin
if Line'Length > 0 and then Line (Line'Last) = ASCII.LF then
if Line'Length > 1 and then Line (Line'Last - 1) = ASCII.CR then
return Line (Line'First .. Line'Last - 2);
else
return Line (Line'First .. Line'Last - 1);
end if;
else
return Line;
end if;
end Get_Line;
--------------------
-- Get_Read_Cache --
--------------------
function Get_Read_Cache
(Socket : Socket_Type'Class) return not null access Read_Cache is
begin
if Socket.C.R_Cache = null then
Socket.C.R_Cache := new Read_Cache (R_Cache_Size);
end if;
return Socket.C.R_Cache;
end Get_Read_Cache;
---------------------
-- Get_Write_Cache --
---------------------
function Get_Write_Cache
(Socket : Socket_Type'Class) return not null access Write_Cache is
begin
if Socket.C.W_Cache = null then
Socket.C.W_Cache := new Write_Cache (W_Cache_Size);
end if;
return Socket.C.W_Cache;
end Get_Write_Cache;
--------------
-- New_Line --
--------------
procedure New_Line (Socket : Socket_Type'Class) is
begin
Write (Socket, CRLF);
end New_Line;
---------------
-- Peek_Char --
---------------
function Peek_Char (Socket : Socket_Type'Class) return Character is
C : constant not null access Read_Cache := Get_Read_Cache (Socket);
begin
if Is_Empty (C) then
Read (Socket);
end if;
return Character'Val (Natural (C.Buffer (C.First)));
end Peek_Char;
---------
-- Put --
---------
procedure Put (Socket : Socket_Type'Class; Item : String) is
begin
Write (Socket, Translator.To_Stream_Element_Array (Item));
end Put;
--------------
-- Put_Line --
--------------
procedure Put_Line (Socket : Socket_Type'Class; Item : String) is
begin
Write (Socket, Translator.To_Stream_Element_Array (Item) & CRLF);
end Put_Line;
----------
-- Read --
----------
procedure Read (Socket : Socket_Type'Class) is
C : constant not null access Read_Cache := Socket.C.R_Cache;
begin
Receive (Socket, C.Buffer, C.Last);
-- Reset C.First only after successful Receive, the buffer would
-- remain empty on timeout this way.
C.First := C.Buffer'First;
end Read;
procedure Read
(Socket : Socket_Type'Class;
Data : out Stream_Element_Array;
Last : out Stream_Element_Offset)
is
C : constant not null access Read_Cache := Get_Read_Cache (Socket);
begin
Flush (Socket);
if Is_Empty (C) then
if Data'Length < C.Buffer'Length then
-- If Data fit in the cache, fill it
Read (Socket);
else
-- Otherwise read the socket directly
Receive (Socket, Data, Last);
return;
end if;
end if;
Read_Buffer (Socket, Data, Last);
-- Data could remain in internal socket buffer, if there is some
-- space on the buffer, read the socket.
if Last < Data'Last and then Pending (Socket) > 0 then
Receive (Socket, Data (Last + 1 .. Data'Last), Last);
end if;
end Read;
function Read
(Socket : Socket_Type'Class;
Max : Stream_Element_Count := 4096) return Stream_Element_Array
is
Buffer : Stream_Element_Array (1 .. Max);
Last : Stream_Element_Offset;
begin
Read (Socket, Buffer, Last);
return Buffer (1 .. Last);
end Read;
procedure Read
(Socket : Socket_Type'Class; Data : out Stream_Element_Array)
is
Last : Stream_Element_Offset;
First : Stream_Element_Offset := Data'First;
begin
loop
Read (Socket, Data (First .. Data'Last), Last);
exit when Last = Data'Last;
First := Last + 1;
end loop;
end Read;
-----------------
-- Read_Buffer --
-----------------
procedure Read_Buffer
(Socket : Socket_Type'Class;
Data : out Stream_Element_Array;
Last : out Stream_Element_Offset)
is
C : Read_Cache_Access renames Socket.C.R_Cache;
begin
if C = null then
Last := Last_Index (Data'First, Count => 0);
else
declare
C_Last : constant Stream_Element_Offset :=
Stream_Element_Offset'Min
(C.Last, C.First + Data'Length - 1);
begin
Last := Data'First + C_Last - C.First;
Data (Data'First .. Last) := C.Buffer (C.First .. C_Last);
C.First := C_Last + 1;
end;
end if;
end Read_Buffer;
----------------
-- Read_Until --
----------------
function Read_Until
(Socket : Socket_Type'Class;
Delimiter : Stream_Element_Array;
Wait : Boolean := True) return Stream_Element_Array
is
use Containers.Memory_Streams;
function Buffered return Stream_Element_Array with Inline;
procedure Finalize;
Finalizer : Utils.Finalizer (Finalize'Access) with Unreferenced;
Buffer : Stream_Type;
--------------
-- Buffered --
--------------
function Buffered return Stream_Element_Array is
Result : Stream_Element_Array (1 .. Size (Buffer));
Last : Stream_Element_Offset;
begin
Read (Buffer, Result, Last);
return Result;
end Buffered;
--------------
-- Finalize --
--------------
procedure Finalize is
begin
Close (Buffer);
end Finalize;
C : constant not null access Read_Cache := Get_Read_Cache (Socket);
J, K : Stream_Element_Offset;
begin
if Wait then
Flush (Socket);
end if;
J := C.First;
K := Delimiter'First;
loop
if J > C.Last then
if Wait then
Append (Buffer, C.Buffer (C.First .. C.Last));
if Size (Buffer) > Stream_Element_Offset (Input_Limit) then
raise Data_Overflow with
"Size" & Stream_Element_Offset'Image (Size (Buffer));
end if;
begin
Read (Socket);
exception
when E : Socket_Error =>
if Size (Buffer) = 0 or else Is_Timeout (Socket, E) then
raise;
else
C.First := 1;
C.Last := 0;
return Buffered;
end if;
end;
J := C.First;
else
return (1 .. 0 => 0);
end if;
end if;
if C.Buffer (J) = Delimiter (K) then
if K = Delimiter'Last then
K := C.First;
C.First := J + 1;
return Buffered & C.Buffer (K .. J);
else
K := K + 1;
end if;
J := J + 1;
elsif K = Delimiter'First then
J := J + 1;
else
K := Delimiter'First;
end if;
end loop;
end Read_Until;
function Read_Until
(Socket : Socket_Type'Class;
Delimiter : String;
Wait : Boolean := True) return String is
begin
return Translator.To_String
(Read_Until
(Socket, Translator.To_Stream_Element_Array (Delimiter), Wait));
end Read_Until;
---------------------
-- Set_Input_Limit --
---------------------
procedure Set_Input_Limit (Limit : Positive) is
begin
Input_Limit := Limit;
end Set_Input_Limit;
--------------
-- Shutdown --
--------------
procedure Shutdown (Socket : Socket_Type'Class) is
begin
begin
Flush (Socket);
exception
when Socket_Error =>
-- Ignore recent cache buffer send error
null;
end;
Net.Shutdown (Socket);
end Shutdown;
-----------
-- Write --
-----------
procedure Write
(Socket : Socket_Type'Class;
Item : Stream_Element_Array)
is
C : constant not null access Write_Cache :=
Get_Write_Cache (Socket);
Next_Last : constant Stream_Element_Offset := C.Last + Item'Length;
begin
if Next_Last > C.Max_Size then
Send (Socket, C.Buffer (1 .. C.Last));
Send (Socket, Item);
C.Last := 0;
else
C.Buffer (C.Last + 1 .. Next_Last) := Item;
C.Last := Next_Last;
end if;
end Write;
end AWS.Net.Buffered;
|