@@ -38,38 +38,53 @@ def __init__(self, _class=None, *, policy=compat32):
3838 self ._class = _class
3939 self .policy = policy
4040
41- def parse (self , fp , headersonly = False ):
41+ def parse (self , fp , headersonly = False , strictheaders = True ):
4242 """Create a message structure from the data in a file.
4343
4444 Reads all the data from the file and returns the root of the message
45- structure. Optional headersonly is a flag specifying whether to stop
46- parsing after reading the headers or not. The default is False,
47- meaning it parses the entire contents of the file.
45+ structure.
46+
47+ Optional headersonly is a flag specifying whether to stop parsing
48+ after reading the headers or not. The default is False, meaning it
49+ parses the entire contents of the file.
50+
51+ Optional strictheaders is a flag specifying whether to require
52+ RFC-compliant header names. The default is True, causing parsing to
53+ abort if non-compliant header names are encountered.
4854 """
49- feedparser = FeedParser (self ._class , policy = self .policy )
55+ feedparser = FeedParser (
56+ self ._class , policy = self .policy , strictheaders = strictheaders )
5057 if headersonly :
5158 feedparser ._set_headersonly ()
5259 while data := fp .read (8192 ):
5360 feedparser .feed (data )
5461 return feedparser .close ()
5562
56- def parsestr (self , text , headersonly = False ):
63+ def parsestr (self , text , headersonly = False , strictheaders = True ):
5764 """Create a message structure from a string.
5865
59- Returns the root of the message structure. Optional headersonly is a
60- flag specifying whether to stop parsing after reading the headers or
61- not. The default is False, meaning it parses the entire contents of
62- the file.
66+ Returns the root of the message structure.
67+
68+ Optional headersonly is a flag specifying whether to stop parsing
69+ after reading the headers or not. The default is False, meaning it
70+ parses the entire contents of the file.
71+
72+ Optional strictheaders is a flag specifying whether to require
73+ RFC-compliant header names. The default is True, causing parsing to
74+ abort if non-compliant header names are encountered.
6375 """
64- return self .parse (StringIO (text ), headersonly = headersonly )
76+ return self .parse (
77+ StringIO (text ),
78+ headersonly = headersonly ,
79+ strictheaders = strictheaders )
6580
6681
6782class HeaderParser (Parser ):
68- def parse (self , fp , headersonly = True ):
69- return Parser .parse (self , fp , True )
83+ def parse (self , fp , headersonly = True , strictheaders = True ):
84+ return Parser .parse (self , fp , True , strictheaders = strictheaders )
7085
71- def parsestr (self , text , headersonly = True ):
72- return Parser .parsestr (self , text , True )
86+ def parsestr (self , text , headersonly = True , strictheaders = True ):
87+ return Parser .parsestr (self , text , True , strictheaders = strictheaders )
7388
7489
7590class BytesParser :
@@ -92,36 +107,49 @@ def __init__(self, *args, **kw):
92107 """
93108 self .parser = Parser (* args , ** kw )
94109
95- def parse (self , fp , headersonly = False ):
110+ def parse (self , fp , headersonly = False , strictheaders = True ):
96111 """Create a message structure from the data in a binary file.
97112
98113 Reads all the data from the file and returns the root of the message
99- structure. Optional headersonly is a flag specifying whether to stop
100- parsing after reading the headers or not. The default is False,
101- meaning it parses the entire contents of the file.
114+ structure.
115+
116+ Optional headersonly is a flag specifying whether to stop parsing
117+ after reading the headers or not. The default is False, meaning it
118+ parses the entire contents of the file.
119+
120+ Optional strictheaders is a flag specifying whether to require
121+ RFC-compliant header names. The default is True, causing parsing to
122+ abort if non-compliant header names are encountered.
102123 """
103124 fp = TextIOWrapper (fp , encoding = 'ascii' , errors = 'surrogateescape' )
104125 try :
105- return self .parser .parse (fp , headersonly )
126+ return self .parser .parse (fp , headersonly , strictheaders )
106127 finally :
107128 fp .detach ()
108129
109130
110- def parsebytes (self , text , headersonly = False ):
131+ def parsebytes (self , text , headersonly = False , strictheaders = True ):
111132 """Create a message structure from a byte string.
112133
113- Returns the root of the message structure. Optional headersonly is a
114- flag specifying whether to stop parsing after reading the headers or
115- not. The default is False, meaning it parses the entire contents of
116- the file.
134+ Returns the root of the message structure.
135+
136+ Optional headersonly is a flag specifying whether to stop parsing
137+ after reading the headers or not. The default is False, meaning it
138+ parses the entire contents of the file.
139+
140+ Optional strictheaders is a flag specifying whether to require
141+ RFC-compliant header names. The default is True, causing parsing to
142+ abort if non-compliant header names are encountered.
117143 """
118144 text = text .decode ('ASCII' , errors = 'surrogateescape' )
119- return self .parser .parsestr (text , headersonly )
145+ return self .parser .parsestr (text , headersonly , strictheaders )
120146
121147
122148class BytesHeaderParser (BytesParser ):
123- def parse (self , fp , headersonly = True ):
124- return BytesParser .parse (self , fp , headersonly = True )
149+ def parse (self , fp , headersonly = True , strictheaders = True ):
150+ return BytesParser .parse (
151+ self , fp , headersonly = True , strictheaders = strictheaders )
125152
126- def parsebytes (self , text , headersonly = True ):
127- return BytesParser .parsebytes (self , text , headersonly = True )
153+ def parsebytes (self , text , headersonly = True , strictheaders = True ):
154+ return BytesParser .parsebytes (
155+ self , text , headersonly = True , strictheaders = strictheaders )
0 commit comments