@@ -29,9 +29,6 @@ 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-
3532 New_Line : constant String :=
3633 (Ada.Characters.Latin_1.CR, Ada.Characters.Latin_1.LF);
3734
@@ -267,8 +264,11 @@ package body LSP.Raw_Clients is
267264 use Ada.Strings.Fixed;
268265
269266 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.
272272
273273 Client : Raw_Clients.Raw_Client'Class renames Self.Client.all ;
274274
@@ -278,7 +278,8 @@ package body LSP.Raw_Clients is
278278
279279 procedure Parse_Headers
280280 (Buffer : String;
281- Content_Length : out Positive)
281+ Content_Length : out Positive;
282+ Success : out Boolean)
282283 is
283284 function Skip (Pattern : String) return Boolean;
284285 -- Find Pattern in current position of Buffer and skip it
@@ -303,7 +304,9 @@ package body LSP.Raw_Clients is
303304 end Skip ;
304305
305306 begin
306- while Next < Buffer'Last loop
307+ Success := True;
308+
309+ while Success and Next < Buffer'Last loop
307310 if Skip (" Content-Type: " ) then
308311 Next := Index (Buffer, New_Line);
309312 pragma Assert (Next /= 0 );
@@ -316,7 +319,8 @@ package body LSP.Raw_Clients is
316319 Content_Length := Positive'Value (Buffer (From .. Next - 1 ));
317320 end ;
318321 else
319- raise Parse_Exception with " Unexpected header:" & Buffer;
322+ Next := Index (Buffer, New_Line);
323+ Success := False;
320324 end if ;
321325
322326 Next := Next + New_Line'Length;
@@ -346,24 +350,21 @@ package body LSP.Raw_Clients is
346350 Start := Index (Client.Buffer, New_Line & New_Line);
347351
348352 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 );
367368 end if ;
368369 end if ;
369370
0 commit comments