Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(119444)

Side by Side Diff: Lib/httplib.py

Issue 16037: httplib: header parsing is not delimited
Patch Set: Created 6 years, 2 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | Lib/test/test_httplib.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 r"""HTTP/1.1 client library 1 r"""HTTP/1.1 client library
2 2
3 <intro stuff goes here> 3 <intro stuff goes here>
4 <other stuff, too> 4 <other stuff, too>
5 5
6 HTTPConnection goes through a number of "states", which define when a client 6 HTTPConnection goes through a number of "states", which define when a client
7 may legally make another request or fetch the response for a particular 7 may legally make another request or fetch the response for a particular
8 request. This diagram details these state transitions: 8 request. This diagram details these state transitions:
9 9
10 (null) 10 (null)
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 503: 'Service Unavailable', 207 503: 'Service Unavailable',
208 504: 'Gateway Timeout', 208 504: 'Gateway Timeout',
209 505: 'HTTP Version Not Supported', 209 505: 'HTTP Version Not Supported',
210 } 210 }
211 211
212 # maximal amount of data to read at one time in _safe_read 212 # maximal amount of data to read at one time in _safe_read
213 MAXAMOUNT = 1048576 213 MAXAMOUNT = 1048576
214 214
215 # maximal line length when calling readline(). 215 # maximal line length when calling readline().
216 _MAXLINE = 65536 216 _MAXLINE = 65536
217 _MAXHEADERS = 100
218
217 219
218 class HTTPMessage(mimetools.Message): 220 class HTTPMessage(mimetools.Message):
219 221
220 def addheader(self, key, value): 222 def addheader(self, key, value):
221 """Add header for field key handling repeats.""" 223 """Add header for field key handling repeats."""
222 prev = self.dict.get(key) 224 prev = self.dict.get(key)
223 if prev is None: 225 if prev is None:
224 self.dict[key] = value 226 self.dict[key] = value
225 else: 227 else:
226 combined = ", ".join((prev, value)) 228 combined = ", ".join((prev, value))
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 self.headers = hlist = [] 266 self.headers = hlist = []
265 self.status = '' 267 self.status = ''
266 headerseen = "" 268 headerseen = ""
267 firstline = 1 269 firstline = 1
268 startofline = unread = tell = None 270 startofline = unread = tell = None
269 if hasattr(self.fp, 'unread'): 271 if hasattr(self.fp, 'unread'):
270 unread = self.fp.unread 272 unread = self.fp.unread
271 elif self.seekable: 273 elif self.seekable:
272 tell = self.fp.tell 274 tell = self.fp.tell
273 while True: 275 while True:
276 if len(hlist) > _MAXHEADERS:
277 raise TooManyHeaders()
274 if tell: 278 if tell:
275 try: 279 try:
276 startofline = tell() 280 startofline = tell()
277 except IOError: 281 except IOError:
278 startofline = tell = None 282 startofline = tell = None
279 self.seekable = 0 283 self.seekable = 0
280 line = self.fp.readline(_MAXLINE + 1) 284 line = self.fp.readline(_MAXLINE + 1)
281 if len(line) > _MAXLINE: 285 if len(line) > _MAXLINE:
282 raise LineTooLong("header line") 286 raise LineTooLong("header line")
283 if not line: 287 if not line:
(...skipping 978 matching lines...) Expand 10 before | Expand all | Expand 10 after
1262 def __init__(self, line): 1266 def __init__(self, line):
1263 if not line: 1267 if not line:
1264 line = repr(line) 1268 line = repr(line)
1265 self.args = line, 1269 self.args = line,
1266 self.line = line 1270 self.line = line
1267 1271
1268 class LineTooLong(HTTPException): 1272 class LineTooLong(HTTPException):
1269 def __init__(self, line_type): 1273 def __init__(self, line_type):
1270 HTTPException.__init__(self, "got more than %d bytes when reading %s" 1274 HTTPException.__init__(self, "got more than %d bytes when reading %s"
1271 % (_MAXLINE, line_type)) 1275 % (_MAXLINE, line_type))
1276
1277
1278 class TooManyHeaders(HTTPException):
1279 def __init__(self):
1280 HTTPException.__init__(self, "got more than %d headers" % _MAXHEADERS)
1272 1281
1273 # for backwards compatibility 1282 # for backwards compatibility
1274 error = HTTPException 1283 error = HTTPException
1275 1284
1276 class LineAndFileWrapper: 1285 class LineAndFileWrapper:
1277 """A limited file-like object for HTTP/0.9 responses.""" 1286 """A limited file-like object for HTTP/0.9 responses."""
1278 1287
1279 # The status-line parsing code calls readline(), which normally 1288 # The status-line parsing code calls readline(), which normally
1280 # get the HTTP status line. For a 0.9 response, however, this is 1289 # get the HTTP status line. For a 0.9 response, however, this is
1281 # actually the first line of the body! Clients need to get a 1290 # actually the first line of the body! Clients need to get a
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
1333 def readlines(self, size=None): 1342 def readlines(self, size=None):
1334 if self._line_consumed: 1343 if self._line_consumed:
1335 return self._file.readlines(size) 1344 return self._file.readlines(size)
1336 assert self._line_left 1345 assert self._line_left
1337 L = [self._line[self._line_offset:]] 1346 L = [self._line[self._line_offset:]]
1338 self._done() 1347 self._done()
1339 if size is None: 1348 if size is None:
1340 return L + self._file.readlines() 1349 return L + self._file.readlines()
1341 else: 1350 else:
1342 return L + self._file.readlines(size) 1351 return L + self._file.readlines(size)
OLDNEW
« no previous file with comments | « no previous file | Lib/test/test_httplib.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+