@@ -29,9 +29,6 @@ with Spawn.Processes; use Spawn.Processes;
29
29
30
30
package body LSP.Raw_Clients is
31
31
32
- Parse_Exception : exception ;
33
- -- Local exception raised when parsing invalid data
34
-
35
32
New_Line : constant String :=
36
33
(Ada.Characters.Latin_1.CR, Ada.Characters.Latin_1.LF);
37
34
@@ -267,8 +264,11 @@ package body LSP.Raw_Clients is
267
264
use Ada.Strings.Fixed;
268
265
269
266
procedure Parse_Headers
270
- (Buffer : String; Content_Length : out Positive);
271
- -- Parse headers in Buffer and return Content-Length
267
+ (Buffer : String;
268
+ Content_Length : out Positive;
269
+ Success : out Boolean);
270
+ -- Parse headers in Buffer and return Content-Length.
271
+ -- Return Success = False if encounter an unexpected header.
272
272
273
273
Client : Raw_Clients.Raw_Client'Class renames Self.Client.all ;
274
274
@@ -278,7 +278,8 @@ package body LSP.Raw_Clients is
278
278
279
279
procedure Parse_Headers
280
280
(Buffer : String;
281
- Content_Length : out Positive)
281
+ Content_Length : out Positive;
282
+ Success : out Boolean)
282
283
is
283
284
function Skip (Pattern : String) return Boolean;
284
285
-- Find Pattern in current position of Buffer and skip it
@@ -303,7 +304,9 @@ package body LSP.Raw_Clients is
303
304
end Skip ;
304
305
305
306
begin
306
- while Next < Buffer'Last loop
307
+ Success := True;
308
+
309
+ while Success and Next < Buffer'Last loop
307
310
if Skip (" Content-Type: " ) then
308
311
Next := Index (Buffer, New_Line);
309
312
pragma Assert (Next /= 0 );
@@ -316,7 +319,8 @@ package body LSP.Raw_Clients is
316
319
Content_Length := Positive'Value (Buffer (From .. Next - 1 ));
317
320
end ;
318
321
else
319
- raise Parse_Exception with " Unexpected header:" & Buffer;
322
+ Next := Index (Buffer, New_Line);
323
+ Success := False;
320
324
end if ;
321
325
322
326
Next := Next + New_Line'Length;
@@ -346,24 +350,21 @@ package body LSP.Raw_Clients is
346
350
Start := Index (Client.Buffer, New_Line & New_Line);
347
351
348
352
if Start /= 0 then
349
- begin
350
- Parse_Headers
351
- (Slice (Client.Buffer, 1 , Start + 1 ),
352
- Client.To_Read);
353
- Delete
354
- (Client.Buffer, 1 , Start + 2 * New_Line'Length - 1 );
355
- exception
356
- when E : Parse_Exception =>
357
- Client.On_Exception (E);
358
- -- Delete the first line
359
- Start :=
360
- Index (Client.Buffer,
361
- " " & Ada.Characters.Latin_1.LF);
362
- Delete
363
- (Client.Buffer, 1 , Start);
364
- -- Reset start to 0 => we didn't find a header
365
- Start := 0 ;
366
- end ;
353
+ Parse_Headers
354
+ (Slice (Client.Buffer, 1 , Start + 1 ),
355
+ Client.To_Read,
356
+ Success);
357
+
358
+ if not Success then
359
+ Client.On_Error
360
+ (" Unable to parse:" &
361
+ Slice (Client.Buffer, 1 , Start + 1 ));
362
+ -- Notify upstream and try to recover by dropping the
363
+ -- headers block.
364
+ end if ;
365
+
366
+ Delete
367
+ (Client.Buffer, 1 , Start + 2 * New_Line'Length - 1 );
367
368
end if ;
368
369
end if ;
369
370
0 commit comments