File tree Expand file tree Collapse file tree 1 file changed +19
-12
lines changed Expand file tree Collapse file tree 1 file changed +19
-12
lines changed Original file line number Diff line number Diff line change @@ -331,23 +331,30 @@ def _read_header(cls, fileobj):
331
331
f .seek (1 , os .SEEK_CUR ) # Skip \n
332
332
333
333
found_end = False
334
+ key = None
335
+ lines = []
334
336
335
337
# Read all key-value pairs contained in the header, stop at EOF
336
338
for n_line , line in enumerate (f , 1 ):
337
- line = line .decode ('utf-8' ).strip ()
338
-
339
- if not line : # Skip empty lines
340
- continue
341
-
342
- if line == 'END' : # End of the header
339
+ if line .strip () == b'END' :
343
340
found_end = True
344
- break
345
341
346
- if ':' not in line : # Invalid header line
347
- raise HeaderError (f'Invalid header (line { n_line } ): { line } ' )
348
-
349
- key , value = line .split (':' , 1 )
350
- hdr [key .strip ()] = value .strip ()
342
+ # Multiline values mean we don't know we're done until we find the next value
343
+ if found_end or b':' in line :
344
+ if key is not None :
345
+ hdr [key ] = b'' .join (lines ).decode ().strip ()
346
+ if found_end :
347
+ break
348
+
349
+ parts = line .split (b':' , 1 )
350
+ key = parts [0 ].decode ().strip ()
351
+ lines = [parts [1 ]]
352
+ elif key is None :
353
+ # Apparent continuation line before any keys are found
354
+ if line .strip ():
355
+ raise HeaderError (f'Invalid header (line in { n_line } ): { line } ' )
356
+ else :
357
+ lines .append (line )
351
358
352
359
if not found_end :
353
360
raise HeaderError ('Missing END in the header.' )
You can’t perform that action at this time.
0 commit comments