@@ -29,6 +29,9 @@ with Spawn.Processes; use Spawn.Processes;
2929
3030package body LSP.Raw_Clients is
3131
32+ Parse_Exception : exception ;
33+ -- Local exception raised when parsing invalid data
34+
3235 New_Line : constant String :=
3336 (Ada.Characters.Latin_1.CR, Ada.Characters.Latin_1.LF);
3437
@@ -297,7 +300,7 @@ package body LSP.Raw_Clients is
297300 Content_Length := Positive'Value (Buffer (From .. Next - 1 ));
298301 end ;
299302 else
300- raise Constraint_Error with " Unexpected header:" & Buffer;
303+ raise Parse_Exception with " Unexpected header:" & Buffer;
301304 end if ;
302305
303306 Next := Next + New_Line'Length;
@@ -325,12 +328,24 @@ package body LSP.Raw_Clients is
325328 Start := Index (Client.Buffer, New_Line & New_Line);
326329
327330 if Start /= 0 then
328- Parse_Headers
329- (Slice (Client.Buffer, 1 , Start + 1 ),
330- Client.To_Read);
331-
332- Delete
333- (Client.Buffer, 1 , Start + 2 * New_Line'Length - 1 );
331+ begin
332+ Parse_Headers
333+ (Slice (Client.Buffer, 1 , Start + 1 ),
334+ Client.To_Read);
335+ Delete
336+ (Client.Buffer, 1 , Start + 2 * New_Line'Length - 1 );
337+ exception
338+ when E : Parse_Exception =>
339+ Client.On_Exception (E);
340+ -- Delete the first line
341+ Start :=
342+ Index (Client.Buffer,
343+ " " & Ada.Characters.Latin_1.LF);
344+ Delete
345+ (Client.Buffer, 1 , Start);
346+ -- Reset start to 0 => we didn't find a header
347+ Start := 0 ;
348+ end ;
334349 end if ;
335350 end if ;
336351
0 commit comments